2 use JakubOnderka\PhpConsoleColor\ConsoleColor;
4 class ConsoleColorWithForceSupport extends ConsoleColor
6 private $isSupportedForce = true;
8 private $are256ColorsSupportedForce = true;
10 public function setIsSupported($isSupported)
12 $this->isSupportedForce = $isSupported;
15 public function isSupported()
17 return $this->isSupportedForce;
20 public function setAre256ColorsSupported($are256ColorsSupported)
22 $this->are256ColorsSupportedForce = $are256ColorsSupported;
25 public function are256ColorsSupported()
27 return $this->are256ColorsSupportedForce;
31 class ConsoleColorTest extends \PHPUnit_Framework_TestCase
33 /** @var ConsoleColorWithForceSupport */
36 protected function setUp()
38 $this->uut = new ConsoleColorWithForceSupport();
41 public function testNone()
43 $output = $this->uut->apply('none', 'text');
44 $this->assertEquals("text", $output);
47 public function testBold()
49 $output = $this->uut->apply('bold', 'text');
50 $this->assertEquals("\033[1mtext\033[0m", $output);
53 public function testBoldColorsAreNotSupported()
55 $this->uut->setIsSupported(false);
57 $output = $this->uut->apply('bold', 'text');
58 $this->assertEquals("text", $output);
61 public function testBoldColorsAreNotSupportedButAreForced()
63 $this->uut->setIsSupported(false);
64 $this->uut->setForceStyle(true);
66 $output = $this->uut->apply('bold', 'text');
67 $this->assertEquals("\033[1mtext\033[0m", $output);
70 public function testDark()
72 $output = $this->uut->apply('dark', 'text');
73 $this->assertEquals("\033[2mtext\033[0m", $output);
76 public function testBoldAndDark()
78 $output = $this->uut->apply(array('bold', 'dark'), 'text');
79 $this->assertEquals("\033[1;2mtext\033[0m", $output);
82 public function test256ColorForeground()
84 $output = $this->uut->apply('color_255', 'text');
85 $this->assertEquals("\033[38;5;255mtext\033[0m", $output);
88 public function test256ColorWithoutSupport()
90 $this->uut->setAre256ColorsSupported(false);
92 $output = $this->uut->apply('color_255', 'text');
93 $this->assertEquals("text", $output);
96 public function test256ColorBackground()
98 $output = $this->uut->apply('bg_color_255', 'text');
99 $this->assertEquals("\033[48;5;255mtext\033[0m", $output);
102 public function test256ColorForegroundAndBackground()
104 $output = $this->uut->apply(array('color_200', 'bg_color_255'), 'text');
105 $this->assertEquals("\033[38;5;200;48;5;255mtext\033[0m", $output);
108 public function testSetOwnTheme()
110 $this->uut->setThemes(array('bold_dark' => array('bold', 'dark')));
111 $output = $this->uut->apply(array('bold_dark'), 'text');
112 $this->assertEquals("\033[1;2mtext\033[0m", $output);
115 public function testAddOwnTheme()
117 $this->uut->addTheme('bold_own', 'bold');
118 $output = $this->uut->apply(array('bold_own'), 'text');
119 $this->assertEquals("\033[1mtext\033[0m", $output);
122 public function testAddOwnThemeArray()
124 $this->uut->addTheme('bold_dark', array('bold', 'dark'));
125 $output = $this->uut->apply(array('bold_dark'), 'text');
126 $this->assertEquals("\033[1;2mtext\033[0m", $output);
129 public function testOwnWithStyle()
131 $this->uut->addTheme('bold_dark', array('bold', 'dark'));
132 $output = $this->uut->apply(array('bold_dark', 'italic'), 'text');
133 $this->assertEquals("\033[1;2;3mtext\033[0m", $output);
136 public function testHasAndRemoveTheme()
138 $this->assertFalse($this->uut->hasTheme('bold_dark'));
140 $this->uut->addTheme('bold_dark', array('bold', 'dark'));
141 $this->assertTrue($this->uut->hasTheme('bold_dark'));
143 $this->uut->removeTheme('bold_dark');
144 $this->assertFalse($this->uut->hasTheme('bold_dark'));
147 public function testApplyInvalidArgument()
149 $this->setExpectedException('\InvalidArgumentException');
150 $this->uut->apply(new stdClass(), 'text');
153 public function testApplyInvalidStyleName()
155 $this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
156 $this->uut->apply('invalid', 'text');
159 public function testApplyInvalid256Color()
161 $this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
162 $this->uut->apply('color_2134', 'text');
165 public function testThemeInvalidStyle()
167 $this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
168 $this->uut->addTheme('invalid', array('invalid'));
171 public function testForceStyle()
173 $this->assertFalse($this->uut->isStyleForced());
174 $this->uut->setForceStyle(true);
175 $this->assertTrue($this->uut->isStyleForced());
178 public function testGetPossibleStyles()
180 $this->assertInternalType('array', $this->uut->getPossibleStyles());
181 $this->assertNotEmpty($this->uut->getPossibleStyles());