OutputFormatterStyleTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\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[39;49;22;24m", $style->apply('foo'));
  18. $style = new OutputFormatterStyle('red', null, array('blink'));
  19. $this->assertEquals("\033[31;5mfoo\033[39;25m", $style->apply('foo'));
  20. $style = new OutputFormatterStyle(null, 'white');
  21. $this->assertEquals("\033[47mfoo\033[49m", $style->apply('foo'));
  22. }
  23. public function testForeground()
  24. {
  25. $style = new OutputFormatterStyle();
  26. $style->setForeground('black');
  27. $this->assertEquals("\033[30mfoo\033[39m", $style->apply('foo'));
  28. $style->setForeground('blue');
  29. $this->assertEquals("\033[34mfoo\033[39m", $style->apply('foo'));
  30. $style->setForeground('default');
  31. $this->assertEquals("\033[39mfoo\033[39m", $style->apply('foo'));
  32. $this->setExpectedException('InvalidArgumentException');
  33. $style->setForeground('undefined-color');
  34. }
  35. public function testBackground()
  36. {
  37. $style = new OutputFormatterStyle();
  38. $style->setBackground('black');
  39. $this->assertEquals("\033[40mfoo\033[49m", $style->apply('foo'));
  40. $style->setBackground('yellow');
  41. $this->assertEquals("\033[43mfoo\033[49m", $style->apply('foo'));
  42. $style->setBackground('default');
  43. $this->assertEquals("\033[49mfoo\033[49m", $style->apply('foo'));
  44. $this->setExpectedException('InvalidArgumentException');
  45. $style->setBackground('undefined-color');
  46. }
  47. public function testOptions()
  48. {
  49. $style = new OutputFormatterStyle();
  50. $style->setOptions(array('reverse', 'conceal'));
  51. $this->assertEquals("\033[7;8mfoo\033[27;28m", $style->apply('foo'));
  52. $style->setOption('bold');
  53. $this->assertEquals("\033[7;8;1mfoo\033[27;28;22m", $style->apply('foo'));
  54. $style->unsetOption('reverse');
  55. $this->assertEquals("\033[8;1mfoo\033[28;22m", $style->apply('foo'));
  56. $style->setOption('bold');
  57. $this->assertEquals("\033[8;1mfoo\033[28;22m", $style->apply('foo'));
  58. $style->setOptions(array('bold'));
  59. $this->assertEquals("\033[1mfoo\033[22m", $style->apply('foo'));
  60. try {
  61. $style->setOption('foo');
  62. $this->fail('->setOption() throws an \InvalidArgumentException when the option does not exist in the available options');
  63. } catch (\Exception $e) {
  64. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws an \InvalidArgumentException when the option does not exist in the available options');
  65. $this->assertContains('Invalid option specified: "foo"', $e->getMessage(), '->setOption() throws an \InvalidArgumentException when the option does not exist in the available options');
  66. }
  67. try {
  68. $style->unsetOption('foo');
  69. $this->fail('->unsetOption() throws an \InvalidArgumentException when the option does not exist in the available options');
  70. } catch (\Exception $e) {
  71. $this->assertInstanceOf('\InvalidArgumentException', $e, '->unsetOption() throws an \InvalidArgumentException when the option does not exist in the available options');
  72. $this->assertContains('Invalid option specified: "foo"', $e->getMessage(), '->unsetOption() throws an \InvalidArgumentException when the option does not exist in the available options');
  73. }
  74. }
  75. }