PageRenderTime 45ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ContainerDebugCommandTest.php

https://github.com/FabienD/symfony
PHP | 279 lines | 212 code | 52 blank | 15 comment | 0 complexity | dfcd60b4ce8a62d4ee0e9aafcb4d0cb2 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
  11. use Symfony\Bundle\FrameworkBundle\Console\Application;
  12. use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BackslashClass;
  13. use Symfony\Component\Console\Tester\ApplicationTester;
  14. use Symfony\Component\Console\Tester\CommandCompletionTester;
  15. /**
  16. * @group functional
  17. */
  18. class ContainerDebugCommandTest extends AbstractWebTestCase
  19. {
  20. public function testDumpContainerIfNotExists()
  21. {
  22. static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
  23. $application = new Application(static::$kernel);
  24. $application->setAutoExit(false);
  25. @unlink(static::getContainer()->getParameter('debug.container.dump'));
  26. $tester = new ApplicationTester($application);
  27. $tester->run(['command' => 'debug:container']);
  28. $this->assertFileExists(static::getContainer()->getParameter('debug.container.dump'));
  29. }
  30. public function testNoDebug()
  31. {
  32. static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => false]);
  33. $application = new Application(static::$kernel);
  34. $application->setAutoExit(false);
  35. $tester = new ApplicationTester($application);
  36. $tester->run(['command' => 'debug:container']);
  37. $this->assertStringContainsString('public', $tester->getDisplay());
  38. }
  39. public function testPrivateAlias()
  40. {
  41. static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
  42. $application = new Application(static::$kernel);
  43. $application->setAutoExit(false);
  44. $tester = new ApplicationTester($application);
  45. $tester->run(['command' => 'debug:container', '--show-hidden' => true]);
  46. $this->assertStringNotContainsString('public', $tester->getDisplay());
  47. $this->assertStringNotContainsString('private_alias', $tester->getDisplay());
  48. $tester->run(['command' => 'debug:container']);
  49. $this->assertStringContainsString('public', $tester->getDisplay());
  50. $this->assertStringContainsString('private_alias', $tester->getDisplay());
  51. $tester->run(['command' => 'debug:container', 'name' => 'private_alias']);
  52. $this->assertStringContainsString('The "private_alias" service or alias has been removed', $tester->getDisplay());
  53. }
  54. /**
  55. * @dataProvider provideIgnoreBackslashWhenFindingService
  56. */
  57. public function testIgnoreBackslashWhenFindingService(string $validServiceId)
  58. {
  59. static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
  60. $application = new Application(static::$kernel);
  61. $application->setAutoExit(false);
  62. $tester = new ApplicationTester($application);
  63. $tester->run(['command' => 'debug:container', 'name' => $validServiceId]);
  64. $this->assertStringNotContainsString('No services found', $tester->getDisplay());
  65. }
  66. public function testDescribeEnvVars()
  67. {
  68. putenv('REAL=value');
  69. static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
  70. $application = new Application(static::$kernel);
  71. $application->setAutoExit(false);
  72. @unlink(static::getContainer()->getParameter('debug.container.dump'));
  73. $tester = new ApplicationTester($application);
  74. $tester->run(['command' => 'debug:container', '--env-vars' => true], ['decorated' => false]);
  75. $this->assertStringMatchesFormat(<<<'TXT'
  76. Symfony Container Environment Variables
  77. =======================================
  78. --------- ----------------- ------------%w
  79. Name Default value Real value%w
  80. --------- ----------------- ------------%w
  81. JSON "[1, "2.5", 3]" n/a%w
  82. REAL n/a "value"%w
  83. UNKNOWN n/a n/a%w
  84. --------- ----------------- ------------%w
  85. // Note real values might be different between web and CLI.%w
  86. [WARNING] The following variables are missing:%w
  87. * UNKNOWN
  88. TXT
  89. , $tester->getDisplay(true));
  90. putenv('REAL');
  91. }
  92. public function testDescribeEnvVar()
  93. {
  94. static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
  95. $application = new Application(static::$kernel);
  96. $application->setAutoExit(false);
  97. @unlink(static::getContainer()->getParameter('debug.container.dump'));
  98. $tester = new ApplicationTester($application);
  99. $tester->run(['command' => 'debug:container', '--env-var' => 'js'], ['decorated' => false]);
  100. $this->assertStringContainsString(file_get_contents(__DIR__.'/Fixtures/describe_env_vars.txt'), $tester->getDisplay(true));
  101. }
  102. public function testGetDeprecation()
  103. {
  104. static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
  105. $path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.build_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
  106. touch($path);
  107. file_put_contents($path, serialize([[
  108. 'type' => 16384,
  109. 'message' => 'The "Symfony\Bundle\FrameworkBundle\Controller\Controller" class is deprecated since Symfony 4.2, use Symfony\Bundle\FrameworkBundle\Controller\AbstractController instead.',
  110. 'file' => '/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php',
  111. 'line' => 17,
  112. 'trace' => [[
  113. 'file' => '/home/hamza/projet/contrib/sf/src/Controller/DefaultController.php',
  114. 'line' => 9,
  115. 'function' => 'spl_autoload_call',
  116. ]],
  117. 'count' => 1,
  118. ]]));
  119. $application = new Application(static::$kernel);
  120. $application->setAutoExit(false);
  121. @unlink(static::getContainer()->getParameter('debug.container.dump'));
  122. $tester = new ApplicationTester($application);
  123. $tester->run(['command' => 'debug:container', '--deprecations' => true]);
  124. $tester->assertCommandIsSuccessful();
  125. $this->assertStringContainsString('Symfony\Bundle\FrameworkBundle\Controller\Controller', $tester->getDisplay());
  126. $this->assertStringContainsString('/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php', $tester->getDisplay());
  127. }
  128. public function testGetDeprecationNone()
  129. {
  130. static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
  131. $path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.build_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
  132. touch($path);
  133. file_put_contents($path, serialize([]));
  134. $application = new Application(static::$kernel);
  135. $application->setAutoExit(false);
  136. @unlink(static::getContainer()->getParameter('debug.container.dump'));
  137. $tester = new ApplicationTester($application);
  138. $tester->run(['command' => 'debug:container', '--deprecations' => true]);
  139. $tester->assertCommandIsSuccessful();
  140. $this->assertStringContainsString('[OK] There are no deprecations in the logs!', $tester->getDisplay());
  141. }
  142. public function testGetDeprecationNoFile()
  143. {
  144. static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
  145. $path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.build_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
  146. @unlink($path);
  147. $application = new Application(static::$kernel);
  148. $application->setAutoExit(false);
  149. @unlink(static::getContainer()->getParameter('debug.container.dump'));
  150. $tester = new ApplicationTester($application);
  151. $tester->run(['command' => 'debug:container', '--deprecations' => true]);
  152. $tester->assertCommandIsSuccessful();
  153. $this->assertStringContainsString('[WARNING] The deprecation file does not exist', $tester->getDisplay());
  154. }
  155. public function provideIgnoreBackslashWhenFindingService()
  156. {
  157. return [
  158. [BackslashClass::class],
  159. ['FixturesBackslashClass'],
  160. ['\\'.BackslashClass::class],
  161. ];
  162. }
  163. /**
  164. * @dataProvider provideCompletionSuggestions
  165. */
  166. public function testComplete(array $input, array $expectedSuggestions, array $notExpectedSuggestions = [])
  167. {
  168. static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
  169. $application = new Application(static::$kernel);
  170. $tester = new CommandCompletionTester($application->find('debug:container'));
  171. $suggestions = $tester->complete($input);
  172. foreach ($expectedSuggestions as $expectedSuggestion) {
  173. $this->assertContains($expectedSuggestion, $suggestions);
  174. }
  175. foreach ($notExpectedSuggestions as $notExpectedSuggestion) {
  176. $this->assertNotContains($notExpectedSuggestion, $suggestions);
  177. }
  178. }
  179. public function provideCompletionSuggestions()
  180. {
  181. $serviceId = 'console.command.container_debug';
  182. $hiddenServiceId = '.console.command.container_debug.lazy';
  183. $interfaceServiceId = 'Symfony\Component\HttpKernel\HttpKernelInterface';
  184. yield 'name' => [
  185. [''],
  186. [$serviceId, $interfaceServiceId],
  187. [$hiddenServiceId],
  188. ];
  189. yield 'name (with hidden)' => [
  190. ['--show-hidden', ''],
  191. [$serviceId, $interfaceServiceId, $hiddenServiceId],
  192. ];
  193. yield 'name (with current value)' => [
  194. ['--show-hidden', 'console'],
  195. [$serviceId, $hiddenServiceId],
  196. [$interfaceServiceId],
  197. ];
  198. yield 'name (no suggestion with --tags)' => [
  199. ['--tags', ''],
  200. [],
  201. [$serviceId, $interfaceServiceId, $hiddenServiceId],
  202. ];
  203. yield 'option --tag' => [
  204. ['--tag', ''],
  205. ['console.command'],
  206. ];
  207. yield 'option --parameter' => [
  208. ['--parameter', ''],
  209. ['kernel.debug'],
  210. ];
  211. yield 'option --format' => [
  212. ['--format', ''],
  213. ['txt', 'xml', 'json', 'md'],
  214. ];
  215. }
  216. }