SymfonyStyleTest.php 3.7 KB

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