1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\PSXTest; |
4
|
|
|
|
5
|
|
|
use Colors\Color; |
6
|
|
|
use PhpParser\ParserFactory; |
7
|
|
|
use PhpSchool\PSX\ColorsAdapter; |
8
|
|
|
use PhpSchool\PSX\Lexer; |
9
|
|
|
use PhpSchool\PSX\SyntaxHighlighter; |
10
|
|
|
use PhpSchool\PSX\SyntaxHighlighterConfig; |
11
|
|
|
use PhpSchool\PSX\SyntaxHighlightPrinter; |
12
|
|
|
use PHPUnit_Framework_TestCase; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class SyntaxHighlighterTest |
17
|
|
|
* @package PhpSchool\PSXTest |
18
|
|
|
* @author Aydin Hassan <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class SyntaxHighlighterTest extends PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
public function testExceptionIsThrownIfCodeIsNotString() |
23
|
|
|
{ |
24
|
|
|
$this->setExpectedException( |
25
|
|
|
InvalidArgumentException::class, |
26
|
|
|
'Argument 1 should be a string of valid PHP code' |
27
|
|
|
); |
28
|
|
|
$highlighter = $this->getHighlighter(); |
29
|
|
|
$highlighter->highlight([]); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testExceptionIsThrownIfInvalidCode() |
33
|
|
|
{ |
34
|
|
|
$this->setExpectedException( |
35
|
|
|
InvalidArgumentException::class, |
36
|
|
|
'PHP could not be parsed' |
37
|
|
|
); |
38
|
|
|
$highlighter = $this->getHighlighter(); |
39
|
|
|
$highlighter->highlight('<?php echo'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testSyntaxHighlighter() |
43
|
|
|
{ |
44
|
|
|
$code = '<?php echo "hello world!";'; |
45
|
|
|
$highlighter = $this->getHighlighter(); |
46
|
|
|
$expected = "[36m<?php[0m\n\n[33mecho[0m [32m\"hello world!\"[0m;"; |
47
|
|
|
$this->assertEquals($expected, $highlighter->highlight($code)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return SyntaxHighlighter |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
private function getHighlighter() |
54
|
|
|
{ |
55
|
|
|
$lexer = new Lexer([ |
56
|
|
|
'usedAttributes' => [ |
57
|
|
|
'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'startTokenPos', 'endTokenPos' |
58
|
|
|
] |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$parserFactory = new ParserFactory; |
62
|
|
|
$color = new Color; |
63
|
|
|
$color->setForceStyle(true); |
64
|
|
|
return new SyntaxHighlighter( |
65
|
|
|
$parserFactory->create(ParserFactory::PREFER_PHP7, $lexer), |
66
|
|
|
new SyntaxHighlightPrinter( |
67
|
|
|
new SyntaxHighlighterConfig, |
68
|
|
|
new ColorsAdapter($color) |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: