PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/AbstractDescriptorTest.php

http://github.com/fabpot/symfony
PHP | 324 lines | 242 code | 55 blank | 27 comment | 3 complexity | 43b1d72b975e9d264cd3d1a3f18b9bb9 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\Console\Descriptor;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Input\ArrayInput;
  13. use Symfony\Component\Console\Output\BufferedOutput;
  14. use Symfony\Component\Console\Style\SymfonyStyle;
  15. use Symfony\Component\DependencyInjection\Alias;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Definition;
  18. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  19. use Symfony\Component\EventDispatcher\EventDispatcher;
  20. use Symfony\Component\Routing\Route;
  21. use Symfony\Component\Routing\RouteCollection;
  22. abstract class AbstractDescriptorTest extends TestCase
  23. {
  24. /** @dataProvider getDescribeRouteCollectionTestData */
  25. public function testDescribeRouteCollection(RouteCollection $routes, $expectedDescription)
  26. {
  27. $this->assertDescription($expectedDescription, $routes);
  28. }
  29. public function getDescribeRouteCollectionTestData()
  30. {
  31. return $this->getDescriptionTestData(ObjectsProvider::getRouteCollections());
  32. }
  33. /** @dataProvider getDescribeRouteTestData */
  34. public function testDescribeRoute(Route $route, $expectedDescription)
  35. {
  36. $this->assertDescription($expectedDescription, $route);
  37. }
  38. public function getDescribeRouteTestData()
  39. {
  40. return $this->getDescriptionTestData(ObjectsProvider::getRoutes());
  41. }
  42. /** @dataProvider getDescribeContainerParametersTestData */
  43. public function testDescribeContainerParameters(ParameterBag $parameters, $expectedDescription)
  44. {
  45. $this->assertDescription($expectedDescription, $parameters);
  46. }
  47. public function getDescribeContainerParametersTestData()
  48. {
  49. return $this->getDescriptionTestData(ObjectsProvider::getContainerParameters());
  50. }
  51. /** @dataProvider getDescribeContainerBuilderTestData */
  52. public function testDescribeContainerBuilder(ContainerBuilder $builder, $expectedDescription, array $options)
  53. {
  54. $this->assertDescription($expectedDescription, $builder, $options);
  55. }
  56. public function getDescribeContainerBuilderTestData()
  57. {
  58. return $this->getContainerBuilderDescriptionTestData(ObjectsProvider::getContainerBuilders());
  59. }
  60. /**
  61. * @dataProvider getDescribeContainerExistingClassDefinitionTestData
  62. */
  63. public function testDescribeContainerExistingClassDefinition(Definition $definition, $expectedDescription)
  64. {
  65. $this->assertDescription($expectedDescription, $definition);
  66. }
  67. public function getDescribeContainerExistingClassDefinitionTestData()
  68. {
  69. return $this->getDescriptionTestData(ObjectsProvider::getContainerDefinitionsWithExistingClasses());
  70. }
  71. /** @dataProvider getDescribeContainerDefinitionTestData */
  72. public function testDescribeContainerDefinition(Definition $definition, $expectedDescription)
  73. {
  74. $this->assertDescription($expectedDescription, $definition);
  75. }
  76. public function getDescribeContainerDefinitionTestData()
  77. {
  78. return $this->getDescriptionTestData(ObjectsProvider::getContainerDefinitions());
  79. }
  80. /** @dataProvider getDescribeContainerDefinitionWithArgumentsShownTestData */
  81. public function testDescribeContainerDefinitionWithArgumentsShown(Definition $definition, $expectedDescription)
  82. {
  83. $this->assertDescription($expectedDescription, $definition, ['show_arguments' => true]);
  84. }
  85. public function getDescribeContainerDefinitionWithArgumentsShownTestData()
  86. {
  87. $definitions = ObjectsProvider::getContainerDefinitions();
  88. $definitionsWithArgs = [];
  89. foreach ($definitions as $key => $definition) {
  90. $definitionsWithArgs[str_replace('definition_', 'definition_arguments_', $key)] = $definition;
  91. }
  92. return $this->getDescriptionTestData($definitionsWithArgs);
  93. }
  94. /** @dataProvider getDescribeContainerAliasTestData */
  95. public function testDescribeContainerAlias(Alias $alias, $expectedDescription)
  96. {
  97. $this->assertDescription($expectedDescription, $alias);
  98. }
  99. public function getDescribeContainerAliasTestData()
  100. {
  101. return $this->getDescriptionTestData(ObjectsProvider::getContainerAliases());
  102. }
  103. /** @dataProvider getDescribeContainerDefinitionWhichIsAnAliasTestData */
  104. public function testDescribeContainerDefinitionWhichIsAnAlias(Alias $alias, $expectedDescription, ContainerBuilder $builder, $options = [])
  105. {
  106. $this->assertDescription($expectedDescription, $builder, $options);
  107. }
  108. public function getDescribeContainerDefinitionWhichIsAnAliasTestData()
  109. {
  110. $builder = current(ObjectsProvider::getContainerBuilders());
  111. $builder->setDefinition('service_1', $builder->getDefinition('definition_1'));
  112. $builder->setDefinition('.service_2', $builder->getDefinition('.definition_2'));
  113. $aliases = ObjectsProvider::getContainerAliases();
  114. $aliasesWithDefinitions = [];
  115. foreach ($aliases as $name => $alias) {
  116. $aliasesWithDefinitions[str_replace('alias_', 'alias_with_definition_', $name)] = $alias;
  117. }
  118. $i = 0;
  119. $data = $this->getDescriptionTestData($aliasesWithDefinitions);
  120. foreach ($aliases as $name => $alias) {
  121. $file = array_pop($data[$i]);
  122. $data[$i][] = $builder;
  123. $data[$i][] = ['id' => $name];
  124. $data[$i][] = $file;
  125. ++$i;
  126. }
  127. return $data;
  128. }
  129. /** @dataProvider getDescribeContainerParameterTestData */
  130. public function testDescribeContainerParameter($parameter, $expectedDescription, array $options)
  131. {
  132. $this->assertDescription($expectedDescription, $parameter, $options);
  133. }
  134. public function getDescribeContainerParameterTestData()
  135. {
  136. $data = $this->getDescriptionTestData(ObjectsProvider::getContainerParameter());
  137. $file = array_pop($data[0]);
  138. $data[0][] = ['parameter' => 'database_name'];
  139. $data[0][] = $file;
  140. $file = array_pop($data[1]);
  141. $data[1][] = ['parameter' => 'twig.form.resources'];
  142. $data[1][] = $file;
  143. return $data;
  144. }
  145. /** @dataProvider getDescribeEventDispatcherTestData */
  146. public function testDescribeEventDispatcher(EventDispatcher $eventDispatcher, $expectedDescription, array $options)
  147. {
  148. $this->assertDescription($expectedDescription, $eventDispatcher, $options);
  149. }
  150. public function getDescribeEventDispatcherTestData()
  151. {
  152. return $this->getEventDispatcherDescriptionTestData(ObjectsProvider::getEventDispatchers());
  153. }
  154. /** @dataProvider getDescribeCallableTestData */
  155. public function testDescribeCallable($callable, $expectedDescription)
  156. {
  157. $this->assertDescription($expectedDescription, $callable);
  158. }
  159. public function getDescribeCallableTestData()
  160. {
  161. return $this->getDescriptionTestData(ObjectsProvider::getCallables());
  162. }
  163. /** @dataProvider getClassDescriptionTestData */
  164. public function testGetClassDecription($object, $expectedDescription)
  165. {
  166. $this->assertEquals($expectedDescription, $this->getDescriptor()->getClassDescription($object));
  167. }
  168. public function getClassDescriptionTestData()
  169. {
  170. return [
  171. [ClassWithDocCommentOnMultipleLines::class, 'This is the first line of the description. This is the second line.'],
  172. [ClassWithDocCommentWithoutInitialSpace::class, 'Foo.'],
  173. [ClassWithoutDocComment::class, ''],
  174. [ClassWithDocComment::class, 'This is a class with a doc comment.'],
  175. ];
  176. }
  177. /**
  178. * @dataProvider getDeprecationsTestData
  179. */
  180. public function testGetDeprecations(ContainerBuilder $builder, $expectedDescription)
  181. {
  182. $this->assertDescription($expectedDescription, $builder, ['deprecations' => true]);
  183. }
  184. public function getDeprecationsTestData()
  185. {
  186. return $this->getDescriptionTestData(ObjectsProvider::getContainerDeprecations());
  187. }
  188. abstract protected function getDescriptor();
  189. abstract protected function getFormat();
  190. private function assertDescription($expectedDescription, $describedObject, array $options = [])
  191. {
  192. $options['is_debug'] = false;
  193. $options['raw_output'] = true;
  194. $options['raw_text'] = true;
  195. $output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
  196. if ('txt' === $this->getFormat()) {
  197. $options['output'] = new SymfonyStyle(new ArrayInput([]), $output);
  198. }
  199. $this->getDescriptor()->describe($output, $describedObject, $options);
  200. if ('json' === $this->getFormat()) {
  201. $this->assertEquals(json_encode(json_decode($expectedDescription), JSON_PRETTY_PRINT), json_encode(json_decode($output->fetch()), JSON_PRETTY_PRINT));
  202. } else {
  203. $this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $output->fetch())));
  204. }
  205. }
  206. private function getDescriptionTestData(array $objects)
  207. {
  208. $data = [];
  209. foreach ($objects as $name => $object) {
  210. $file = sprintf('%s.%s', trim($name, '.'), $this->getFormat());
  211. $description = file_get_contents(__DIR__.'/../../Fixtures/Descriptor/'.$file);
  212. $data[] = [$object, $description, $file];
  213. }
  214. return $data;
  215. }
  216. private function getContainerBuilderDescriptionTestData(array $objects)
  217. {
  218. $variations = [
  219. 'services' => ['show_hidden' => true],
  220. 'public' => ['show_hidden' => false],
  221. 'tag1' => ['show_hidden' => true, 'tag' => 'tag1'],
  222. 'tags' => ['group_by' => 'tags', 'show_hidden' => true],
  223. 'arguments' => ['show_hidden' => false, 'show_arguments' => true],
  224. ];
  225. $data = [];
  226. foreach ($objects as $name => $object) {
  227. foreach ($variations as $suffix => $options) {
  228. $file = sprintf('%s_%s.%s', trim($name, '.'), $suffix, $this->getFormat());
  229. $description = file_get_contents(__DIR__.'/../../Fixtures/Descriptor/'.$file);
  230. $data[] = [$object, $description, $options, $file];
  231. }
  232. }
  233. return $data;
  234. }
  235. private function getEventDispatcherDescriptionTestData(array $objects)
  236. {
  237. $variations = [
  238. 'events' => [],
  239. 'event1' => ['event' => 'event1'],
  240. ];
  241. $data = [];
  242. foreach ($objects as $name => $object) {
  243. foreach ($variations as $suffix => $options) {
  244. $file = sprintf('%s_%s.%s', trim($name, '.'), $suffix, $this->getFormat());
  245. $description = file_get_contents(__DIR__.'/../../Fixtures/Descriptor/'.$file);
  246. $data[] = [$object, $description, $options, $file];
  247. }
  248. }
  249. return $data;
  250. }
  251. /** @dataProvider getDescribeContainerBuilderWithPriorityTagsTestData */
  252. public function testDescribeContainerBuilderWithPriorityTags(ContainerBuilder $builder, $expectedDescription, array $options): void
  253. {
  254. $this->assertDescription($expectedDescription, $builder, $options);
  255. }
  256. public function getDescribeContainerBuilderWithPriorityTagsTestData(): array
  257. {
  258. $variations = ['priority_tag' => ['tag' => 'tag1']];
  259. $data = [];
  260. foreach (ObjectsProvider::getContainerBuildersWithPriorityTags() as $name => $object) {
  261. foreach ($variations as $suffix => $options) {
  262. $file = sprintf('%s_%s.%s', trim($name, '.'), $suffix, $this->getFormat());
  263. $description = file_get_contents(__DIR__.'/../../Fixtures/Descriptor/'.$file);
  264. $data[] = [$object, $description, $options, $file];
  265. }
  266. }
  267. return $data;
  268. }
  269. }