"Fossies" - the Fresh Open Source Software Archive

Member "Symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/AbstractDescriptorTest.php" (30 Mar 2020, 9260 Bytes) of package /linux/www/Symfony_Standard_Vendors_3.4.39.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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