SymfonyStyleTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\Style;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Style\SymfonyStyle;
  16. use Symfony\Component\Console\Tester\CommandTester;
  17. class SymfonyStyleTest extends TestCase
  18. {
  19. /** @var Command */
  20. protected $command;
  21. /** @var CommandTester */
  22. protected $tester;
  23. protected function setUp()
  24. {
  25. $this->command = new Command('sfstyle');
  26. $this->tester = new CommandTester($this->command);
  27. }
  28. protected function tearDown()
  29. {
  30. $this->command = null;
  31. $this->tester = null;
  32. }
  33. /**
  34. * @dataProvider inputCommandToOutputFilesProvider
  35. */
  36. public function testOutputs($inputCommandFilepath, $outputFilepath)
  37. {
  38. $code = require $inputCommandFilepath;
  39. $this->command->setCode($code);
  40. $this->tester->execute(array(), array('interactive' => false, 'decorated' => false));
  41. $this->assertStringEqualsFile($outputFilepath, $this->tester->getDisplay(true));
  42. }
  43. public function inputCommandToOutputFilesProvider()
  44. {
  45. $baseDir = __DIR__.'/../Fixtures/Style/SymfonyStyle';
  46. return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
  47. }
  48. }
  49. /**
  50. * Use this class in tests to force the line length
  51. * and ensure a consistent output for expectations.
  52. */
  53. class SymfonyStyleWithForcedLineLength extends SymfonyStyle
  54. {
  55. public function __construct(InputInterface $input, OutputInterface $output)
  56. {
  57. parent::__construct($input, $output);
  58. $ref = new \ReflectionProperty(get_parent_class($this), 'lineLength');
  59. $ref->setAccessible(true);
  60. $ref->setValue($this, 120);
  61. }
  62. }