187beb67bbe235dd59b5b82ec0776f97135e29c2
[yaffs-website] / vendor / jakub-onderka / php-console-highlighter / tests / HigligterTest.php
1 <?php
2 namespace JakubOnderka\PhpConsoleHighlighter;
3
4 class HighlighterTest extends \PHPUnit_Framework_TestCase
5 {
6     /** @var Highlighter */
7     private $uut;
8
9     protected function getConsoleColorMock()
10     {
11         $mock = method_exists($this, 'createMock')
12             ? $this->createMock('\JakubOnderka\PhpConsoleColor\ConsoleColor')
13             : $this->getMock('\JakubOnderka\PhpConsoleColor\ConsoleColor');
14
15         $mock->expects($this->any())
16             ->method('apply')
17             ->will($this->returnCallback(function ($style, $text) {
18                 return "<$style>$text</$style>";
19             }));
20
21         $mock->expects($this->any())
22             ->method('hasTheme')
23             ->will($this->returnValue(true));
24
25         return $mock;
26     }
27
28     protected function setUp()
29     {
30         $this->uut = new Highlighter($this->getConsoleColorMock());
31     }
32
33     protected function compare($original, $expected)
34     {
35         $output = $this->uut->getWholeFile($original);
36         $this->assertEquals($expected, $output);
37     }
38
39     public function testVariable()
40     {
41         $this->compare(
42             <<<EOL
43 <?php
44 echo \$a;
45 EOL
46             ,
47             <<<EOL
48 <token_default><?php</token_default>
49 <token_keyword>echo </token_keyword><token_default>\$a</token_default><token_keyword>;</token_keyword>
50 EOL
51         );
52     }
53
54     public function testInteger()
55     {
56         $this->compare(
57             <<<EOL
58 <?php
59 echo 43;
60 EOL
61             ,
62             <<<EOL
63 <token_default><?php</token_default>
64 <token_keyword>echo </token_keyword><token_default>43</token_default><token_keyword>;</token_keyword>
65 EOL
66         );
67     }
68
69     public function testFloat()
70     {
71         $this->compare(
72             <<<EOL
73 <?php
74 echo 43.3;
75 EOL
76             ,
77             <<<EOL
78 <token_default><?php</token_default>
79 <token_keyword>echo </token_keyword><token_default>43.3</token_default><token_keyword>;</token_keyword>
80 EOL
81         );
82     }
83
84     public function testHex()
85     {
86         $this->compare(
87             <<<EOL
88 <?php
89 echo 0x43;
90 EOL
91             ,
92             <<<EOL
93 <token_default><?php</token_default>
94 <token_keyword>echo </token_keyword><token_default>0x43</token_default><token_keyword>;</token_keyword>
95 EOL
96         );
97     }
98
99     public function testBasicFunction()
100     {
101         $this->compare(
102             <<<EOL
103 <?php
104 function plus(\$a, \$b) {
105     return \$a + \$b;
106 }
107 EOL
108             ,
109             <<<EOL
110 <token_default><?php</token_default>
111 <token_keyword>function </token_keyword><token_default>plus</token_default><token_keyword>(</token_keyword><token_default>\$a</token_default><token_keyword>, </token_keyword><token_default>\$b</token_default><token_keyword>) {</token_keyword>
112 <token_keyword>    return </token_keyword><token_default>\$a </token_default><token_keyword>+ </token_keyword><token_default>\$b</token_default><token_keyword>;</token_keyword>
113 <token_keyword>}</token_keyword>
114 EOL
115         );
116     }
117
118     public function testStringNormal()
119     {
120         $this->compare(
121             <<<EOL
122 <?php
123 echo 'Ahoj světe';
124 EOL
125             ,
126             <<<EOL
127 <token_default><?php</token_default>
128 <token_keyword>echo </token_keyword><token_string>'Ahoj světe'</token_string><token_keyword>;</token_keyword>
129 EOL
130         );
131     }
132
133     public function testStringDouble()
134     {
135         $this->compare(
136             <<<EOL
137 <?php
138 echo "Ahoj světe";
139 EOL
140             ,
141             <<<EOL
142 <token_default><?php</token_default>
143 <token_keyword>echo </token_keyword><token_string>"Ahoj světe"</token_string><token_keyword>;</token_keyword>
144 EOL
145         );
146     }
147
148     public function testInstanceof()
149     {
150         $this->compare(
151             <<<EOL
152 <?php
153 \$a instanceof stdClass;
154 EOL
155             ,
156             <<<EOL
157 <token_default><?php</token_default>
158 <token_default>\$a </token_default><token_keyword>instanceof </token_keyword><token_default>stdClass</token_default><token_keyword>;</token_keyword>
159 EOL
160         );
161     }
162
163     /*
164      * Constants
165      */
166     public function testConstant()
167     {
168         $constants = array(
169             '__FILE__',
170             '__LINE__',
171             '__CLASS__',
172             '__FUNCTION__',
173             '__METHOD__',
174             '__TRAIT__',
175             '__DIR__',
176             '__NAMESPACE__'
177         );
178
179         foreach ($constants as $constant) {
180             $this->compare(
181                 <<<EOL
182 <?php
183 $constant;
184 EOL
185                 ,
186                 <<<EOL
187 <token_default><?php</token_default>
188 <token_default>$constant</token_default><token_keyword>;</token_keyword>
189 EOL
190             );
191         }
192     }
193
194     /*
195      * Comments
196      */
197     public function testComment()
198     {
199         $this->compare(
200             <<<EOL
201 <?php
202 /* Ahoj */
203 EOL
204             ,
205             <<<EOL
206 <token_default><?php</token_default>
207 <token_comment>/* Ahoj */</token_comment>
208 EOL
209         );
210     }
211
212     public function testDocComment()
213     {
214         $this->compare(
215             <<<EOL
216 <?php
217 /** Ahoj */
218 EOL
219             ,
220             <<<EOL
221 <token_default><?php</token_default>
222 <token_comment>/** Ahoj */</token_comment>
223 EOL
224         );
225     }
226
227     public function testInlineComment()
228     {
229         $this->compare(
230             <<<EOL
231 <?php
232 // Ahoj
233 EOL
234             ,
235             <<<EOL
236 <token_default><?php</token_default>
237 <token_comment>// Ahoj</token_comment>
238 EOL
239         );
240     }
241
242     public function testHashComment()
243     {
244         $this->compare(
245             <<<EOL
246 <?php
247 # Ahoj
248 EOL
249             ,
250             <<<EOL
251 <token_default><?php</token_default>
252 <token_comment># Ahoj</token_comment>
253 EOL
254         );
255     }
256
257     public function testEmpty()
258     {
259         $this->compare(
260             ''
261             ,
262             ''
263         );
264     }
265
266     public function testWhitespace()
267     {
268         $this->compare(
269             ' '
270             ,
271             '<token_html> </token_html>'
272         );
273     }
274 }