1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Colors\Test; |
4
|
|
|
|
5
|
|
|
use Colors\Color; |
6
|
|
|
use Colors\NoStyleFoundException; |
7
|
|
|
use Colors\InvalidStyleNameException; |
8
|
|
|
use Colors\RecursionInUserStylesException; |
9
|
|
|
|
10
|
|
|
function color($string = '') |
11
|
|
|
{ |
12
|
|
|
return new Color($string); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
class ColorsTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
public function testGivenStringShouldApplyStyle() |
18
|
|
|
{ |
19
|
|
|
assertSame("\033[31mfoo\033[0m", (string) color('foo')->red()); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testGivenStringShouldApplyMoreThanOneStyle() |
23
|
|
|
{ |
24
|
|
|
assertSame("\033[1m\033[97mfoo\033[0m\033[0m", (string) color('foo')->white()->bold()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testStyleNameIsNotCaseSensitive() |
28
|
|
|
{ |
29
|
|
|
assertSame("\033[31mfoo\033[0m", (string) color('foo')->RED()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testStateIsInitializedForSuccessiveCalls() |
33
|
|
|
{ |
34
|
|
|
$color = new Color(); |
35
|
|
|
assertSame('foo', (string) $color('foo')); |
36
|
|
|
assertSame('bar', (string) $color('bar')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testGivenStyledStringShouldBeAbleToResetIt() |
40
|
|
|
{ |
41
|
|
|
assertSame('foo', (string) color('foo')->blue()->reset()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testThrowsExceptionForUnknownStyle() |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
|
|
color('foo bar')->foo(); |
48
|
|
|
$this->fail('Must throw an exception'); |
49
|
|
|
} catch (NoStyleFoundException $e) { |
50
|
|
|
assertInstanceOf('InvalidArgumentException', $e); |
51
|
|
|
assertEquals('Invalid style foo', $e->getMessage()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testCanDirectlyCallStyleMethodOnText() |
56
|
|
|
{ |
57
|
|
|
assertsame((string) color('foo')->blue(), color()->blue('foo')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testHasShortcutForForegroundColor() |
61
|
|
|
{ |
62
|
|
|
assertSame((string) color('Hello')->blue(), (string) color('Hello')->fg('blue')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testHasShortcutForBackgroundColor() |
66
|
|
|
{ |
67
|
|
|
assertSame((string) color('Hello')->bg_red(), (string) color('Hello')->bg('red')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testHasHighlightShortcutForBackgroundColor() |
71
|
|
|
{ |
72
|
|
|
assertSame((string) color('Hello')->bg_blue(), (string) color('Hello')->highlight('blue')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testHasPropertyShortcutForStyle() |
76
|
|
|
{ |
77
|
|
|
assertSame((string) color('Hello')->blue(), (string) color('Hello')->blue); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function testShouldSupportUserStyles() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$color = new Color(); |
83
|
|
|
$color->setUserStyles(array('error' => 'red')); |
84
|
|
|
|
85
|
|
|
assertEquals((string) color('Error...')->red(), (string) $color('Error...')->error()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function testUserStylesShouldOverrideDefaultStyles() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$color = new Color(); |
91
|
|
|
$color->setUserStyles(array('white' => 'red')); |
92
|
|
|
|
93
|
|
|
assertEquals((string) color('Warning...')->red, (string) $color('Warning...')->white); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testGivenInvalidUserStyleNameShouldThrowAnException() |
97
|
|
|
{ |
98
|
|
|
$color = new Color(); |
99
|
|
|
try { |
100
|
|
|
$color->setUserStyles(array('foo-bar' => 'red')); |
101
|
|
|
$this->fail('must throw an InvalidArgumentException'); |
102
|
|
|
} catch (InvalidStyleNameException $e) { |
103
|
|
|
assertInstanceOf('InvalidArgumentException', $e); |
104
|
|
|
assertSame('foo-bar is not a valid style name', $e->getMessage()); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGivenStyledStringWhenCleanedShouldStripAllStyles() |
109
|
|
|
{ |
110
|
|
|
assertEquals('some text', (string) color((string) color('some text')->red())->clean()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testHasStripShortcutForClean() |
114
|
|
|
{ |
115
|
|
|
assertEquals('some text', (string) color()->strip(color('some text')->red())); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testGivenThatStylesAreNotSupportedShouldReturnInputString() |
119
|
|
|
{ |
120
|
|
|
$color = $this->getMock('colors\color', array('isSupported')); |
121
|
|
|
$color->expects($this->once()) |
122
|
|
|
->method('isSupported') |
123
|
|
|
->will($this->returnValue(false)); |
124
|
|
|
|
125
|
|
|
assertSame('foo bar', (string) $color('foo bar')->red()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGivenStringWithStyleTagsShouldInterpretThem() |
129
|
|
|
{ |
130
|
|
|
$text = 'before <red>some text</red>'; |
131
|
|
|
assertSame('before ' . color('some text')->red(), (string) color($text)->colorize()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testGivenStringWithNestedStyleTagsShouldInterpretThem() |
135
|
|
|
{ |
136
|
|
|
$actual = (string) color('<cyan>Hello <bold>World!</bold></cyan>')->colorize(); |
137
|
|
|
$expected = (string) color('Hello ' . color('World!')->bold())->cyan(); |
138
|
|
|
assertSame($expected, $actual); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testAppliesStyleDirectlyToText() |
142
|
|
|
{ |
143
|
|
|
assertSame((string) color('foo')->blue(), color()->apply('blue', 'foo')); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testWhenApplyCenterToStringShouldCenterIt() |
147
|
|
|
{ |
148
|
|
|
$width = 80; |
149
|
|
|
$color = new Color(); |
150
|
|
|
|
151
|
|
|
foreach (array('', 'hello', 'hello world!', '✩') as $text) { |
152
|
|
|
$actualWidth = mb_strlen($color($text)->center($width)->__toString(), 'UTF-8'); |
153
|
|
|
assertSame($width, $actualWidth); |
154
|
|
|
$actualWidth = mb_strlen($color($text)->center($width)->bg('blue')->clean()->__toString(), 'UTF-8'); |
155
|
|
|
assertSame($width, $actualWidth); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testWhenApplyCenterToMultilineStringShouldCenterIt() |
160
|
|
|
{ |
161
|
|
|
$width = 80; |
162
|
|
|
$color = new Color(); |
163
|
|
|
$text = 'hello' . PHP_EOL . '✩' . PHP_EOL . 'world'; |
164
|
|
|
|
165
|
|
|
$actual = $color($text)->center($width)->__toString(); |
166
|
|
|
foreach (explode(PHP_EOL, $actual) as $line) { |
167
|
|
|
assertSame($width, mb_strlen($line, 'UTF-8')); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testStylesAreAppliedWhenForced() |
172
|
|
|
{ |
173
|
|
|
$color = $this->getMock('colors\color', array('isSupported')); |
174
|
|
|
$color->expects($this->any()) |
175
|
|
|
->method('isSupported') |
176
|
|
|
->will($this->returnvalue(false)); |
177
|
|
|
|
178
|
|
|
$color->setForceStyle(true); |
179
|
|
|
|
180
|
|
|
assertTrue($color->isStyleForced()); |
181
|
|
|
assertsame((string) color('foo')->blue(), (string) $color('foo')->blue()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testShouldSupport256Colors() |
185
|
|
|
{ |
186
|
|
|
assertSame("\033[38;5;3mfoo\033[0m", color()->apply('color[3]', 'foo')); |
187
|
|
|
assertSame("\033[48;5;3mfoo\033[0m", color()->apply('bg_color[3]', 'foo')); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testGivenInvalidColorNumberShouldThrowException() |
191
|
|
|
{ |
192
|
|
|
try { |
193
|
|
|
color()->apply('color[-1]', 'foo'); |
194
|
|
|
$this->fail('Must throw an exception'); |
195
|
|
|
} catch (NoStyleFoundException $e) { |
196
|
|
|
assertInstanceOf('InvalidArgumentException', $e); |
197
|
|
|
assertEquals('Invalid style color[-1]', $e->getMessage()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
try { |
201
|
|
|
color()->apply('color[256]', 'foo'); |
202
|
|
|
$this->fail('Must throw an exception'); |
203
|
|
|
} catch (NoStyleFoundException $e) { |
204
|
|
|
assertInstanceOf('InvalidArgumentException', $e); |
205
|
|
|
assertEquals('Invalid style color[256]', $e->getMessage()); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Bug #10 |
211
|
|
|
*/ |
212
|
|
|
public function testShouldHandleRecursionInTheme() |
213
|
|
|
{ |
214
|
|
|
try { |
215
|
|
|
$c = color()->setTheme( |
|
|
|
|
216
|
|
|
array( |
217
|
|
|
'green' => array('green'), |
218
|
|
|
) |
219
|
|
|
); |
220
|
|
|
$this->fail('Must throw an exception'); |
221
|
|
|
} catch (RecursionInUserStylesException $e) { |
222
|
|
|
assertInstanceOf('InvalidArgumentException', $e); |
223
|
|
|
assertEquals('User style cannot reference itself.', $e->getMessage()); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.