DebugTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Test\Unit\Model\Logger\Handler;
  7. use Magento\Developer\Model\Logger\Handler\Debug;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\State;
  10. use Magento\Framework\Filesystem\DriverInterface;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  12. use Magento\Store\Model\ScopeInterface;
  13. use Monolog\Formatter\FormatterInterface;
  14. use Monolog\Logger;
  15. use Magento\Framework\App\DeploymentConfig;
  16. /**
  17. * Class DebugTest
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class DebugTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var Debug
  24. */
  25. private $model;
  26. /**
  27. * @var DriverInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $filesystemMock;
  30. /**
  31. * @var State|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $stateMock;
  34. /**
  35. * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $scopeConfigMock;
  38. /**
  39. * @var FormatterInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $formatterMock;
  42. /**
  43. * @var DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $deploymentConfigMock;
  46. /**
  47. * @inheritdoc
  48. */
  49. protected function setUp()
  50. {
  51. $this->filesystemMock = $this->getMockBuilder(DriverInterface::class)
  52. ->getMockForAbstractClass();
  53. $this->stateMock = $this->getMockBuilder(State::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
  57. ->getMockForAbstractClass();
  58. $this->formatterMock = $this->getMockBuilder(FormatterInterface::class)
  59. ->getMockForAbstractClass();
  60. $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
  61. ->disableOriginalConstructor()
  62. ->disableOriginalClone()
  63. ->getMock();
  64. $this->formatterMock->expects($this->any())
  65. ->method('format')
  66. ->willReturn(null);
  67. $this->model = (new ObjectManager($this))->getObject(Debug::class, [
  68. 'filesystem' => $this->filesystemMock,
  69. 'state' => $this->stateMock,
  70. 'scopeConfig' => $this->scopeConfigMock,
  71. 'deploymentConfig' => $this->deploymentConfigMock
  72. ]);
  73. $this->model->setFormatter($this->formatterMock);
  74. }
  75. /**
  76. * @return void
  77. */
  78. public function testHandleEnabledInDeveloperMode()
  79. {
  80. $this->deploymentConfigMock->expects($this->once())
  81. ->method('isAvailable')
  82. ->willReturn(true);
  83. $this->stateMock
  84. ->expects($this->once())
  85. ->method('getMode')
  86. ->willReturn(State::MODE_DEVELOPER);
  87. $this->scopeConfigMock
  88. ->expects($this->never())
  89. ->method('getValue');
  90. $this->assertTrue($this->model->isHandling(['formatted' => false, 'level' => Logger::DEBUG]));
  91. }
  92. /**
  93. * @return void
  94. */
  95. public function testHandleEnabledInDefaultMode()
  96. {
  97. $this->deploymentConfigMock->expects($this->once())
  98. ->method('isAvailable')
  99. ->willReturn(true);
  100. $this->stateMock
  101. ->expects($this->once())
  102. ->method('getMode')
  103. ->willReturn(State::MODE_DEFAULT);
  104. $this->scopeConfigMock
  105. ->expects($this->never())
  106. ->method('getValue');
  107. $this->assertTrue($this->model->isHandling(['formatted' => false, 'level' => Logger::DEBUG]));
  108. }
  109. /**
  110. * @return void
  111. */
  112. public function testHandleDisabledByProduction()
  113. {
  114. $this->deploymentConfigMock->expects($this->once())
  115. ->method('isAvailable')
  116. ->willReturn(true);
  117. $this->stateMock
  118. ->expects($this->once())
  119. ->method('getMode')
  120. ->willReturn(State::MODE_PRODUCTION);
  121. $this->scopeConfigMock
  122. ->expects($this->never())
  123. ->method('getValue');
  124. $this->assertFalse($this->model->isHandling(['formatted' => false, 'level' => Logger::DEBUG]));
  125. }
  126. /**
  127. * @return void
  128. */
  129. public function testHandleDisabledByLevel()
  130. {
  131. $this->deploymentConfigMock->expects($this->once())
  132. ->method('isAvailable')
  133. ->willReturn(true);
  134. $this->stateMock
  135. ->expects($this->never())
  136. ->method('getMode')
  137. ->willReturn(State::MODE_DEVELOPER);
  138. $this->scopeConfigMock
  139. ->expects($this->never())
  140. ->method('getValue');
  141. $this->assertFalse($this->model->isHandling(['formatted' => false, 'level' => Logger::API]));
  142. }
  143. /**
  144. * @return void
  145. */
  146. public function testDeploymentConfigIsNotAvailable()
  147. {
  148. $this->deploymentConfigMock->expects($this->once())
  149. ->method('isAvailable')
  150. ->willReturn(false);
  151. $this->stateMock
  152. ->expects($this->never())
  153. ->method('getMode');
  154. $this->scopeConfigMock
  155. ->expects($this->never())
  156. ->method('getValue');
  157. $this->assertTrue($this->model->isHandling(['formatted' => false, 'level' => Logger::DEBUG]));
  158. }
  159. }