aaa1a224a303cbed27f0b9f76066004896dd31ec
[yaffs-website] / tests / ConsoleColorTest.php
1 <?php
2 use JakubOnderka\PhpConsoleColor\ConsoleColor;
3
4 class ConsoleColorWithForceSupport extends ConsoleColor
5 {
6     private $isSupportedForce = true;
7
8     private $are256ColorsSupportedForce = true;
9
10     public function setIsSupported($isSupported)
11     {
12         $this->isSupportedForce = $isSupported;
13     }
14
15     public function isSupported()
16     {
17         return $this->isSupportedForce;
18     }
19
20     public function setAre256ColorsSupported($are256ColorsSupported)
21     {
22         $this->are256ColorsSupportedForce = $are256ColorsSupported;
23     }
24
25     public function are256ColorsSupported()
26     {
27         return $this->are256ColorsSupportedForce;
28     }
29 }
30
31 class ConsoleColorTest extends \PHPUnit_Framework_TestCase
32 {
33     /** @var ConsoleColorWithForceSupport */
34     private $uut;
35
36     protected function setUp()
37     {
38         $this->uut = new ConsoleColorWithForceSupport();
39     }
40
41     public function testNone()
42     {
43         $output = $this->uut->apply('none', 'text');
44         $this->assertEquals("text", $output);
45     }
46
47     public function testBold()
48     {
49         $output = $this->uut->apply('bold', 'text');
50         $this->assertEquals("\033[1mtext\033[0m", $output);
51     }
52
53     public function testBoldColorsAreNotSupported()
54     {
55         $this->uut->setIsSupported(false);
56
57         $output = $this->uut->apply('bold', 'text');
58         $this->assertEquals("text", $output);
59     }
60
61     public function testBoldColorsAreNotSupportedButAreForced()
62     {
63         $this->uut->setIsSupported(false);
64         $this->uut->setForceStyle(true);
65
66         $output = $this->uut->apply('bold', 'text');
67         $this->assertEquals("\033[1mtext\033[0m", $output);
68     }
69
70     public function testDark()
71     {
72         $output = $this->uut->apply('dark', 'text');
73         $this->assertEquals("\033[2mtext\033[0m", $output);
74     }
75
76     public function testBoldAndDark()
77     {
78         $output = $this->uut->apply(array('bold', 'dark'), 'text');
79         $this->assertEquals("\033[1;2mtext\033[0m", $output);
80     }
81
82     public function test256ColorForeground()
83     {
84         $output = $this->uut->apply('color_255', 'text');
85         $this->assertEquals("\033[38;5;255mtext\033[0m", $output);
86     }
87
88     public function test256ColorWithoutSupport()
89     {
90         $this->uut->setAre256ColorsSupported(false);
91
92         $output = $this->uut->apply('color_255', 'text');
93         $this->assertEquals("text", $output);
94     }
95
96     public function test256ColorBackground()
97     {
98         $output = $this->uut->apply('bg_color_255', 'text');
99         $this->assertEquals("\033[48;5;255mtext\033[0m", $output);
100     }
101
102     public function test256ColorForegroundAndBackground()
103     {
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);
106     }
107
108     public function testSetOwnTheme()
109     {
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);
113     }
114
115     public function testAddOwnTheme()
116     {
117         $this->uut->addTheme('bold_own', 'bold');
118         $output = $this->uut->apply(array('bold_own'), 'text');
119         $this->assertEquals("\033[1mtext\033[0m", $output);
120     }
121
122     public function testAddOwnThemeArray()
123     {
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);
127     }
128
129     public function testOwnWithStyle()
130     {
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);
134     }
135
136     public function testHasAndRemoveTheme()
137     {
138         $this->assertFalse($this->uut->hasTheme('bold_dark'));
139
140         $this->uut->addTheme('bold_dark', array('bold', 'dark'));
141         $this->assertTrue($this->uut->hasTheme('bold_dark'));
142
143         $this->uut->removeTheme('bold_dark');
144         $this->assertFalse($this->uut->hasTheme('bold_dark'));
145     }
146
147     public function testApplyInvalidArgument()
148     {
149         $this->setExpectedException('\InvalidArgumentException');
150         $this->uut->apply(new stdClass(), 'text');
151     }
152
153     public function testApplyInvalidStyleName()
154     {
155         $this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
156         $this->uut->apply('invalid', 'text');
157     }
158
159     public function testApplyInvalid256Color()
160     {
161         $this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
162         $this->uut->apply('color_2134', 'text');
163     }
164
165     public function testThemeInvalidStyle()
166     {
167         $this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
168         $this->uut->addTheme('invalid', array('invalid'));
169     }
170
171     public function testForceStyle()
172     {
173         $this->assertFalse($this->uut->isStyleForced());
174         $this->uut->setForceStyle(true);
175         $this->assertTrue($this->uut->isStyleForced());
176     }
177
178     public function testGetPossibleStyles()
179     {
180         $this->assertInternalType('array', $this->uut->getPossibleStyles());
181         $this->assertNotEmpty($this->uut->getPossibleStyles());
182     }
183 }
184