SymfonyStyleTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Formatter\OutputFormatter;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. use Symfony\Component\Console\Tester\CommandTester;
  19. class SymfonyStyleTest extends TestCase
  20. {
  21. /** @var Command */
  22. protected $command;
  23. /** @var CommandTester */
  24. protected $tester;
  25. private $colSize;
  26. protected function setUp()
  27. {
  28. $this->colSize = getenv('COLUMNS');
  29. putenv('COLUMNS=121');
  30. $this->command = new Command('sfstyle');
  31. $this->tester = new CommandTester($this->command);
  32. }
  33. protected function tearDown()
  34. {
  35. putenv($this->colSize ? 'COLUMNS='.$this->colSize : 'COLUMNS');
  36. $this->command = null;
  37. $this->tester = null;
  38. }
  39. /**
  40. * @dataProvider inputCommandToOutputFilesProvider
  41. */
  42. public function testOutputs($inputCommandFilepath, $outputFilepath)
  43. {
  44. $code = require $inputCommandFilepath;
  45. $this->command->setCode($code);
  46. $this->tester->execute([], ['interactive' => false, 'decorated' => false]);
  47. $this->assertStringEqualsFile($outputFilepath, $this->tester->getDisplay(true));
  48. }
  49. /**
  50. * @dataProvider inputInteractiveCommandToOutputFilesProvider
  51. */
  52. public function testInteractiveOutputs($inputCommandFilepath, $outputFilepath)
  53. {
  54. $code = require $inputCommandFilepath;
  55. $this->command->setCode($code);
  56. $this->tester->execute([], ['interactive' => true, 'decorated' => false]);
  57. $this->assertStringEqualsFile($outputFilepath, $this->tester->getDisplay(true));
  58. }
  59. public function inputInteractiveCommandToOutputFilesProvider()
  60. {
  61. $baseDir = __DIR__.'/../Fixtures/Style/SymfonyStyle';
  62. return array_map(null, glob($baseDir.'/command/interactive_command_*.php'), glob($baseDir.'/output/interactive_output_*.txt'));
  63. }
  64. public function inputCommandToOutputFilesProvider()
  65. {
  66. $baseDir = __DIR__.'/../Fixtures/Style/SymfonyStyle';
  67. return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
  68. }
  69. public function testGetErrorStyle()
  70. {
  71. $input = $this->getMockBuilder(InputInterface::class)->getMock();
  72. $errorOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  73. $errorOutput
  74. ->method('getFormatter')
  75. ->willReturn(new OutputFormatter());
  76. $errorOutput
  77. ->expects($this->once())
  78. ->method('write');
  79. $output = $this->getMockBuilder(ConsoleOutputInterface::class)->getMock();
  80. $output
  81. ->method('getFormatter')
  82. ->willReturn(new OutputFormatter());
  83. $output
  84. ->expects($this->once())
  85. ->method('getErrorOutput')
  86. ->willReturn($errorOutput);
  87. $io = new SymfonyStyle($input, $output);
  88. $io->getErrorStyle()->write('');
  89. }
  90. public function testGetErrorStyleUsesTheCurrentOutputIfNoErrorOutputIsAvailable()
  91. {
  92. $output = $this->getMockBuilder(OutputInterface::class)->getMock();
  93. $output
  94. ->method('getFormatter')
  95. ->willReturn(new OutputFormatter());
  96. $style = new SymfonyStyle($this->getMockBuilder(InputInterface::class)->getMock(), $output);
  97. $this->assertInstanceOf(SymfonyStyle::class, $style->getErrorStyle());
  98. }
  99. }