AbstractDescriptorTest.php 3.7 KB

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