OutputFormatterStyleTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Tests\Component\Console\Formatter;
  11. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  12. class OutputFormatterStyleTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $style = new OutputFormatterStyle('green', 'black', array('bold', 'underscore'));
  17. $this->assertEquals("\033[32;40;1;4mfoo\033[0m", $style->apply('foo'));
  18. $style = new OutputFormatterStyle('red', null, array('blink'));
  19. $this->assertEquals("\033[31;5mfoo\033[0m", $style->apply('foo'));
  20. $style = new OutputFormatterStyle(null, 'white');
  21. $this->assertEquals("\033[47mfoo\033[0m", $style->apply('foo'));
  22. }
  23. public function testForeground()
  24. {
  25. $style = new OutputFormatterStyle();
  26. $style->setForeground('black');
  27. $this->assertEquals("\033[30mfoo\033[0m", $style->apply('foo'));
  28. $style->setForeground('blue');
  29. $this->assertEquals("\033[34mfoo\033[0m", $style->apply('foo'));
  30. $this->setExpectedException('InvalidArgumentException');
  31. $style->setForeground('undefined-color');
  32. }
  33. public function testBackground()
  34. {
  35. $style = new OutputFormatterStyle();
  36. $style->setBackground('black');
  37. $this->assertEquals("\033[40mfoo\033[0m", $style->apply('foo'));
  38. $style->setBackground('yellow');
  39. $this->assertEquals("\033[43mfoo\033[0m", $style->apply('foo'));
  40. $this->setExpectedException('InvalidArgumentException');
  41. $style->setBackground('undefined-color');
  42. }
  43. public function testOptions()
  44. {
  45. $style = new OutputFormatterStyle();
  46. $style->setOptions(array('reverse', 'conceal'));
  47. $this->assertEquals("\033[7;8mfoo\033[0m", $style->apply('foo'));
  48. $style->setOption('bold');
  49. $this->assertEquals("\033[7;8;1mfoo\033[0m", $style->apply('foo'));
  50. $style->unsetOption('reverse');
  51. $this->assertEquals("\033[8;1mfoo\033[0m", $style->apply('foo'));
  52. $style->setOption('bold');
  53. $this->assertEquals("\033[8;1mfoo\033[0m", $style->apply('foo'));
  54. $style->setOptions(array('bold'));
  55. $this->assertEquals("\033[1mfoo\033[0m", $style->apply('foo'));
  56. }
  57. }