PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/console/Tests/Style/SymfonyStyleTest.php

https://github.com/B00N/pwv-hagenbach
PHP | 116 lines | 81 code | 19 blank | 16 comment | 0 complexity | 0d28e491180d1e086c0428c4c48ddc8d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, MIT, JSON
  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. 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([], ['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([], ['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. }