AbstractDescriptorTest.php 3.7 KB

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