Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DebugTest.php
Go to the documentation of this file.
1 <?php
7 
17 use Symfony\Component\Console\Input\InputInterface;
18 use Symfony\Component\Console\Output\OutputInterface;
19 
37 class DebugTest extends \PHPUnit\Framework\TestCase
38 {
42  private $logger;
43 
47  private $mode;
48 
52  private $inputMock;
53 
57  private $outputMock;
58 
62  private $configSetCommand;
63 
67  private $etcDirectory;
68 
72  private $appConfig;
73 
74  public function setUp()
75  {
77  $filesystem = Bootstrap::getObjectManager()->create(Filesystem::class);
78  $this->etcDirectory = $filesystem->getDirectoryWrite(DirectoryList::CONFIG);
79  $this->etcDirectory->copyFile('env.php', 'env.base.php');
80 
81  $this->inputMock = $this->getMockBuilder(InputInterface::class)
82  ->getMockForAbstractClass();
83  $this->outputMock = $this->getMockBuilder(OutputInterface::class)
84  ->getMockForAbstractClass();
85  $this->logger = Bootstrap::getObjectManager()->get(Monolog::class);
86  $this->mode = Bootstrap::getObjectManager()->create(
87  Mode::class,
88  [
89  'input' => $this->inputMock,
90  'output' => $this->outputMock
91  ]
92  );
93  $this->configSetCommand = Bootstrap::getObjectManager()->create(ConfigSetCommand::class);
94  $this->appConfig = Bootstrap::getObjectManager()->create(Config::class);
95 
96  // Preconditions
97  $this->mode->enableDeveloperMode();
98  $this->enableDebugging();
99  if (file_exists($this->getDebuggerLogPath())) {
100  unlink($this->getDebuggerLogPath());
101  }
102  }
103 
104  public function tearDown()
105  {
106  $this->etcDirectory->delete('env.php');
107  $this->etcDirectory->renameFile('env.base.php', 'env.php');
108  }
109 
110  private function enableDebugging()
111  {
112  $this->inputMock = $this->getMockBuilder(InputInterface::class)
113  ->getMockForAbstractClass();
114  $this->outputMock = $this->getMockBuilder(OutputInterface::class)
115  ->getMockForAbstractClass();
116  $this->inputMock->expects($this->exactly(4))
117  ->method('getOption')
118  ->withConsecutive(
123  )
124  ->willReturnOnConsecutiveCalls(
125  true,
126  false,
128  null
129  );
130  $this->inputMock->expects($this->exactly(2))
131  ->method('getArgument')
133  ->willReturnOnConsecutiveCalls('dev/debug/debug_logging', 1);
134  $this->outputMock->expects($this->once())
135  ->method('writeln')
136  ->with('<info>Value was saved in app/etc/env.php and locked.</info>');
137  $this->assertFalse((bool)$this->configSetCommand->run($this->inputMock, $this->outputMock));
138  }
139 
140  public function testDebugInProductionMode()
141  {
142  $message = 'test message';
143 
144  $this->mode->enableProductionModeMinimal();
145  $this->logger->debug($message);
146  $this->assertFileNotExists($this->getDebuggerLogPath());
147  $this->assertFalse((bool)$this->appConfig->getValue('dev/debug/debug_logging'));
148 
149  $this->enableDebugging();
150  $this->logger->debug($message);
151 
152  $this->assertFileExists($this->getDebuggerLogPath());
153  $this->assertContains($message, file_get_contents($this->getDebuggerLogPath()));
154  }
155 
159  private function getDebuggerLogPath()
160  {
161  foreach ($this->logger->getHandlers() as $handler) {
162  if ($handler instanceof Debug) {
163  return $handler->getUrl();
164  }
165  }
166  return false;
167  }
168 }
$message
$filesystem
catch(\Exception $e) $handler
Definition: index.php:30