fcbb719b6cd9eee3374b463d0f1d2578c827dd9d
[yaffs-website] / vendor / symfony / console / Tests / Descriptor / AbstractDescriptorTest.php
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\Component\Console\Tests\Descriptor;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Application;
16 use Symfony\Component\Console\Command\Command;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputDefinition;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Output\BufferedOutput;
21
22 abstract class AbstractDescriptorTest extends TestCase
23 {
24     /** @dataProvider getDescribeInputArgumentTestData */
25     public function testDescribeInputArgument(InputArgument $argument, $expectedDescription)
26     {
27         $this->assertDescription($expectedDescription, $argument);
28     }
29
30     /** @dataProvider getDescribeInputOptionTestData */
31     public function testDescribeInputOption(InputOption $option, $expectedDescription)
32     {
33         $this->assertDescription($expectedDescription, $option);
34     }
35
36     /** @dataProvider getDescribeInputDefinitionTestData */
37     public function testDescribeInputDefinition(InputDefinition $definition, $expectedDescription)
38     {
39         $this->assertDescription($expectedDescription, $definition);
40     }
41
42     /** @dataProvider getDescribeCommandTestData */
43     public function testDescribeCommand(Command $command, $expectedDescription)
44     {
45         $this->assertDescription($expectedDescription, $command);
46     }
47
48     /** @dataProvider getDescribeApplicationTestData */
49     public function testDescribeApplication(Application $application, $expectedDescription)
50     {
51         // Replaces the dynamic placeholders of the command help text with a static version.
52         // The placeholder %command.full_name% includes the script path that is not predictable
53         // and can not be tested against.
54         foreach ($application->all() as $command) {
55             $command->setHelp(str_replace('%command.full_name%', 'app/console %command.name%', $command->getHelp()));
56         }
57
58         $this->assertDescription($expectedDescription, $application);
59     }
60
61     public function getDescribeInputArgumentTestData()
62     {
63         return $this->getDescriptionTestData(ObjectsProvider::getInputArguments());
64     }
65
66     public function getDescribeInputOptionTestData()
67     {
68         return $this->getDescriptionTestData(ObjectsProvider::getInputOptions());
69     }
70
71     public function getDescribeInputDefinitionTestData()
72     {
73         return $this->getDescriptionTestData(ObjectsProvider::getInputDefinitions());
74     }
75
76     public function getDescribeCommandTestData()
77     {
78         return $this->getDescriptionTestData(ObjectsProvider::getCommands());
79     }
80
81     public function getDescribeApplicationTestData()
82     {
83         return $this->getDescriptionTestData(ObjectsProvider::getApplications());
84     }
85
86     abstract protected function getDescriptor();
87
88     abstract protected function getFormat();
89
90     protected function getDescriptionTestData(array $objects)
91     {
92         $data = array();
93         foreach ($objects as $name => $object) {
94             $description = file_get_contents(sprintf('%s/../Fixtures/%s.%s', __DIR__, $name, $this->getFormat()));
95             $data[] = array($object, $description);
96         }
97
98         return $data;
99     }
100
101     protected function assertDescription($expectedDescription, $describedObject)
102     {
103         $output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
104         $this->getDescriptor()->describe($output, $describedObject, array('raw_output' => true));
105         $this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $output->fetch())));
106     }
107 }