SymfonyStyleTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Symfony\Component\Console\Tests\Style;
  3. use PHPUnit_Framework_TestCase;
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Console\Style\SymfonyStyle;
  8. use Symfony\Component\Console\Tester\CommandTester;
  9. class SymfonyStyleTest extends PHPUnit_Framework_TestCase
  10. {
  11. /** @var Command */
  12. protected $command;
  13. /** @var CommandTester */
  14. protected $tester;
  15. protected function setUp()
  16. {
  17. $this->command = new Command('sfstyle');
  18. $this->tester = new CommandTester($this->command);
  19. }
  20. protected function tearDown()
  21. {
  22. $this->command = null;
  23. $this->tester = null;
  24. }
  25. /**
  26. * @dataProvider inputCommandToOutputFilesProvider
  27. */
  28. public function testOutputs($inputCommandFilepath, $outputFilepath)
  29. {
  30. $code = require $inputCommandFilepath;
  31. $this->command->setCode($code);
  32. $this->tester->execute(array(), array('interactive' => false, 'decorated' => false));
  33. $this->assertStringEqualsFile($outputFilepath, $this->tester->getDisplay(true));
  34. }
  35. public function inputCommandToOutputFilesProvider()
  36. {
  37. $baseDir = __DIR__.'/../Fixtures/Style/SymfonyStyle';
  38. return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
  39. }
  40. public function testLongWordsBlockWrapping()
  41. {
  42. $word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygon';
  43. $wordLength = strlen($word);
  44. $maxLineLength = SymfonyStyle::MAX_LINE_LENGTH - 3;
  45. $this->command->setCode(function (InputInterface $input, OutputInterface $output) use ($word) {
  46. $sfStyle = new SymfonyStyle($input, $output);
  47. $sfStyle->block($word, 'CUSTOM', 'fg=white;bg=blue', ' § ', false);
  48. });
  49. $this->tester->execute(array(), array('interactive' => false, 'decorated' => false));
  50. $expectedCount = (int) ceil($wordLength / ($maxLineLength)) + (int) ($wordLength > $maxLineLength - 5);
  51. $this->assertSame($expectedCount, substr_count($this->tester->getDisplay(true), ' § '));
  52. }
  53. }