OutputFormatterStyleTest.php 4.0 KB

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