DebugTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Model\Logger\Handler;
  7. use Magento\Config\Setup\ConfigOptionsList;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\App\State;
  11. use Magento\Framework\Filesystem;
  12. use Magento\Framework\Filesystem\Directory\WriteInterface;
  13. use Magento\Framework\Logger\Monolog;
  14. use Magento\Framework\Shell;
  15. use Magento\Setup\Mvc\Bootstrap\InitParamListener;
  16. use Magento\TestFramework\Helper\Bootstrap;
  17. use Magento\TestFramework\ObjectManager;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class DebugTest extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var Monolog
  25. */
  26. private $logger;
  27. /**
  28. * @var WriteInterface
  29. */
  30. private $etcDirectory;
  31. /**
  32. * @var ObjectManager
  33. */
  34. private $objectManager;
  35. /**
  36. * @var Shell
  37. */
  38. private $shell;
  39. /**
  40. * @var DeploymentConfig
  41. */
  42. private $deploymentConfig;
  43. /**
  44. * @var string
  45. */
  46. private $debugLogPath = '';
  47. /**
  48. * @var string
  49. */
  50. private static $backupFile = 'env.base.php';
  51. /**
  52. * @var string
  53. */
  54. private static $configFile = 'env.php';
  55. /**
  56. * @var Debug
  57. */
  58. private $debugHandler;
  59. /**
  60. * @inheritdoc
  61. * @throws \Magento\Framework\Exception\FileSystemException
  62. * @throws \Exception
  63. */
  64. public function setUp()
  65. {
  66. $this->objectManager = Bootstrap::getObjectManager();
  67. $this->shell = $this->objectManager->get(Shell::class);
  68. $this->logger = $this->objectManager->get(Monolog::class);
  69. $this->deploymentConfig = $this->objectManager->get(DeploymentConfig::class);
  70. /** @var Filesystem $filesystem */
  71. $filesystem = $this->objectManager->create(Filesystem::class);
  72. $this->etcDirectory = $filesystem->getDirectoryWrite(DirectoryList::CONFIG);
  73. $this->etcDirectory->copyFile(self::$configFile, self::$backupFile);
  74. }
  75. /**
  76. * @inheritdoc
  77. * @throws \Magento\Framework\Exception\FileSystemException
  78. */
  79. public function tearDown()
  80. {
  81. $this->reinitDeploymentConfig();
  82. $this->etcDirectory->delete(self::$backupFile);
  83. }
  84. /**
  85. * @param bool $flag
  86. * @throws \Magento\Framework\Exception\LocalizedException
  87. */
  88. private function enableDebugging(bool $flag)
  89. {
  90. $this->shell->execute(
  91. PHP_BINARY . ' -f %s setup:config:set -n --%s=%s --%s=%s',
  92. [
  93. BP . '/bin/magento',
  94. ConfigOptionsList::INPUT_KEY_DEBUG_LOGGING,
  95. (int)$flag,
  96. InitParamListener::BOOTSTRAP_PARAM,
  97. urldecode(http_build_query(Bootstrap::getInstance()->getAppInitParams())),
  98. ]
  99. );
  100. $this->deploymentConfig->resetData();
  101. $this->assertSame((int)$flag, $this->deploymentConfig->get(ConfigOptionsList::CONFIG_PATH_DEBUG_LOGGING));
  102. }
  103. /**
  104. * @throws \Magento\Framework\Exception\LocalizedException
  105. */
  106. public function testDebugInProductionMode()
  107. {
  108. $message = 'test message';
  109. $this->reinitDebugHandler(State::MODE_PRODUCTION);
  110. $this->removeDebugLog();
  111. $this->logger->debug($message);
  112. $this->assertFileNotExists($this->getDebuggerLogPath());
  113. $this->assertNull($this->deploymentConfig->get(ConfigOptionsList::CONFIG_PATH_DEBUG_LOGGING));
  114. $this->checkCommonFlow($message);
  115. $this->reinitDeploymentConfig();
  116. }
  117. /**
  118. * @throws \Magento\Framework\Exception\LocalizedException
  119. */
  120. public function testDebugInDeveloperMode()
  121. {
  122. $message = 'test message';
  123. $this->reinitDebugHandler(State::MODE_DEVELOPER);
  124. $this->removeDebugLog();
  125. $this->logger->debug($message);
  126. $this->assertFileExists($this->getDebuggerLogPath());
  127. $this->assertContains($message, file_get_contents($this->getDebuggerLogPath()));
  128. $this->assertNull($this->deploymentConfig->get(ConfigOptionsList::CONFIG_PATH_DEBUG_LOGGING));
  129. $this->checkCommonFlow($message);
  130. $this->reinitDeploymentConfig();
  131. }
  132. /**
  133. * @return string
  134. */
  135. private function getDebuggerLogPath()
  136. {
  137. if (!$this->debugLogPath) {
  138. foreach ($this->logger->getHandlers() as $handler) {
  139. if ($handler instanceof Debug) {
  140. $this->debugLogPath = $handler->getUrl();
  141. }
  142. }
  143. }
  144. return $this->debugLogPath;
  145. }
  146. /**
  147. * @throws \Magento\Framework\Exception\FileSystemException
  148. */
  149. private function reinitDeploymentConfig()
  150. {
  151. $this->etcDirectory->delete(self::$configFile);
  152. $this->etcDirectory->copyFile(self::$backupFile, self::$configFile);
  153. }
  154. /**
  155. * @param string $instanceMode
  156. * @throws \Magento\Framework\Exception\LocalizedException
  157. */
  158. private function reinitDebugHandler(string $instanceMode)
  159. {
  160. $this->debugHandler = $this->objectManager->create(
  161. Debug::class,
  162. [
  163. 'filePath' => Bootstrap::getInstance()->getAppTempDir(),
  164. 'state' => $this->objectManager->create(
  165. State::class,
  166. [
  167. 'mode' => $instanceMode,
  168. ]
  169. ),
  170. ]
  171. );
  172. $this->logger->setHandlers(
  173. [
  174. $this->debugHandler,
  175. ]
  176. );
  177. }
  178. /**
  179. * @return void
  180. */
  181. private function detachLogger()
  182. {
  183. $this->debugHandler->close();
  184. }
  185. /**
  186. * @return void
  187. */
  188. private function removeDebugLog()
  189. {
  190. $this->detachLogger();
  191. if (file_exists($this->getDebuggerLogPath())) {
  192. unlink($this->getDebuggerLogPath());
  193. }
  194. }
  195. /**
  196. * @param string $message
  197. * @throws \Magento\Framework\Exception\LocalizedException
  198. */
  199. private function checkCommonFlow(string $message)
  200. {
  201. $this->enableDebugging(true);
  202. $this->removeDebugLog();
  203. $this->logger->debug($message);
  204. $this->assertFileExists($this->getDebuggerLogPath());
  205. $this->assertContains($message, file_get_contents($this->getDebuggerLogPath()));
  206. $this->enableDebugging(false);
  207. $this->removeDebugLog();
  208. $this->logger->debug($message);
  209. $this->assertFileNotExists($this->getDebuggerLogPath());
  210. }
  211. }