AbstractDescriptorTestCase.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 AbstractDescriptorTestCase 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. $this->assertDescription($expectedDescription, $application);
  44. }
  45. public static function getDescribeInputArgumentTestData()
  46. {
  47. return static::getDescriptionTestData(ObjectsProvider::getInputArguments());
  48. }
  49. public static function getDescribeInputOptionTestData()
  50. {
  51. return static::getDescriptionTestData(ObjectsProvider::getInputOptions());
  52. }
  53. public static function getDescribeInputDefinitionTestData()
  54. {
  55. return static::getDescriptionTestData(ObjectsProvider::getInputDefinitions());
  56. }
  57. public static function getDescribeCommandTestData()
  58. {
  59. return static::getDescriptionTestData(ObjectsProvider::getCommands());
  60. }
  61. public static function getDescribeApplicationTestData()
  62. {
  63. return static::getDescriptionTestData(ObjectsProvider::getApplications());
  64. }
  65. abstract protected function getDescriptor();
  66. abstract protected static function getFormat();
  67. protected static function getDescriptionTestData(array $objects)
  68. {
  69. $data = [];
  70. foreach ($objects as $name => $object) {
  71. $description = file_get_contents(sprintf('%s/../Fixtures/%s.%s', __DIR__, $name, static::getFormat()));
  72. $data[] = [$object, $description];
  73. }
  74. return $data;
  75. }
  76. protected function assertDescription($expectedDescription, $describedObject, array $options = [])
  77. {
  78. $output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
  79. $this->getDescriptor()->describe($output, $describedObject, $options + ['raw_output' => true]);
  80. $this->assertEquals($this->normalizeOutput($expectedDescription), $this->normalizeOutput($output->fetch()));
  81. }
  82. protected function normalizeOutput(string $output)
  83. {
  84. $output = str_replace(['%%PHP_SELF%%', '%%PHP_SELF_FULL%%', '%%COMMAND_NAME%%', '%%SHELL%%'], [$_SERVER['PHP_SELF'], realpath($_SERVER['PHP_SELF']), basename($_SERVER['PHP_SELF']), basename($_SERVER['SHELL'] ?? '')], $output);
  85. return trim(str_replace(\PHP_EOL, "\n", $output));
  86. }
  87. }