4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Descriptor;
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;
22 abstract class AbstractDescriptorTest extends TestCase
24 /** @dataProvider getDescribeInputArgumentTestData */
25 public function testDescribeInputArgument(InputArgument $argument, $expectedDescription)
27 $this->assertDescription($expectedDescription, $argument);
30 /** @dataProvider getDescribeInputOptionTestData */
31 public function testDescribeInputOption(InputOption $option, $expectedDescription)
33 $this->assertDescription($expectedDescription, $option);
36 /** @dataProvider getDescribeInputDefinitionTestData */
37 public function testDescribeInputDefinition(InputDefinition $definition, $expectedDescription)
39 $this->assertDescription($expectedDescription, $definition);
42 /** @dataProvider getDescribeCommandTestData */
43 public function testDescribeCommand(Command $command, $expectedDescription)
45 $this->assertDescription($expectedDescription, $command);
48 /** @dataProvider getDescribeApplicationTestData */
49 public function testDescribeApplication(Application $application, $expectedDescription)
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()));
58 $this->assertDescription($expectedDescription, $application);
61 public function getDescribeInputArgumentTestData()
63 return $this->getDescriptionTestData(ObjectsProvider::getInputArguments());
66 public function getDescribeInputOptionTestData()
68 return $this->getDescriptionTestData(ObjectsProvider::getInputOptions());
71 public function getDescribeInputDefinitionTestData()
73 return $this->getDescriptionTestData(ObjectsProvider::getInputDefinitions());
76 public function getDescribeCommandTestData()
78 return $this->getDescriptionTestData(ObjectsProvider::getCommands());
81 public function getDescribeApplicationTestData()
83 return $this->getDescriptionTestData(ObjectsProvider::getApplications());
86 abstract protected function getDescriptor();
88 abstract protected function getFormat();
90 protected function getDescriptionTestData(array $objects)
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);
101 protected function assertDescription($expectedDescription, $describedObject)
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())));