4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Formatter;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
17 class OutputFormatterStyleTest extends TestCase
19 public function testConstructor()
21 $style = new OutputFormatterStyle('green', 'black', array('bold', 'underscore'));
22 $this->assertEquals("\033[32;40;1;4mfoo\033[39;49;22;24m", $style->apply('foo'));
24 $style = new OutputFormatterStyle('red', null, array('blink'));
25 $this->assertEquals("\033[31;5mfoo\033[39;25m", $style->apply('foo'));
27 $style = new OutputFormatterStyle(null, 'white');
28 $this->assertEquals("\033[47mfoo\033[49m", $style->apply('foo'));
31 public function testForeground()
33 $style = new OutputFormatterStyle();
35 $style->setForeground('black');
36 $this->assertEquals("\033[30mfoo\033[39m", $style->apply('foo'));
38 $style->setForeground('blue');
39 $this->assertEquals("\033[34mfoo\033[39m", $style->apply('foo'));
41 $style->setForeground('default');
42 $this->assertEquals("\033[39mfoo\033[39m", $style->apply('foo'));
44 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
45 $style->setForeground('undefined-color');
48 public function testBackground()
50 $style = new OutputFormatterStyle();
52 $style->setBackground('black');
53 $this->assertEquals("\033[40mfoo\033[49m", $style->apply('foo'));
55 $style->setBackground('yellow');
56 $this->assertEquals("\033[43mfoo\033[49m", $style->apply('foo'));
58 $style->setBackground('default');
59 $this->assertEquals("\033[49mfoo\033[49m", $style->apply('foo'));
61 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
62 $style->setBackground('undefined-color');
65 public function testOptions()
67 $style = new OutputFormatterStyle();
69 $style->setOptions(array('reverse', 'conceal'));
70 $this->assertEquals("\033[7;8mfoo\033[27;28m", $style->apply('foo'));
72 $style->setOption('bold');
73 $this->assertEquals("\033[7;8;1mfoo\033[27;28;22m", $style->apply('foo'));
75 $style->unsetOption('reverse');
76 $this->assertEquals("\033[8;1mfoo\033[28;22m", $style->apply('foo'));
78 $style->setOption('bold');
79 $this->assertEquals("\033[8;1mfoo\033[28;22m", $style->apply('foo'));
81 $style->setOptions(array('bold'));
82 $this->assertEquals("\033[1mfoo\033[22m", $style->apply('foo'));
85 $style->setOption('foo');
86 $this->fail('->setOption() throws an \InvalidArgumentException when the option does not exist in the available options');
87 } catch (\Exception $e) {
88 $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws an \InvalidArgumentException when the option does not exist in the available options');
89 $this->assertContains('Invalid option specified: "foo"', $e->getMessage(), '->setOption() throws an \InvalidArgumentException when the option does not exist in the available options');
93 $style->unsetOption('foo');
94 $this->fail('->unsetOption() throws an \InvalidArgumentException when the option does not exist in the available options');
95 } catch (\Exception $e) {
96 $this->assertInstanceOf('\InvalidArgumentException', $e, '->unsetOption() throws an \InvalidArgumentException when the option does not exist in the available options');
97 $this->assertContains('Invalid option specified: "foo"', $e->getMessage(), '->unsetOption() throws an \InvalidArgumentException when the option does not exist in the available options');