Skip to content

Commit 0e54669

Browse files
jrfnlgrogy
authored andcommitted
Tests: add dedicated tests for the getWholeFile[WithLineNumbers]() methods
These tests also test the `private` `getHighlightedLines()`, `splitToLines()` methods and - in part - the `lineNumbers()` method.
1 parent c43139c commit 0e54669

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

tests/GetWholeFileTest.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace PHP_Parallel_Lint\PhpConsoleHighlighter\Test;
4+
5+
use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor;
6+
use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;
7+
8+
/**
9+
* @coversDefaultClass PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter
10+
*/
11+
class GetWholeFileTest extends HighlighterTestCase
12+
{
13+
/** @var Highlighter */
14+
private $highlighter;
15+
16+
/** @var string */
17+
private $input = <<<'EOL'
18+
<?php
19+
20+
namespace FooBar;
21+
22+
class Foo {
23+
/**
24+
* Docblock.
25+
*
26+
* @param type $param Description.
27+
*/
28+
public function bar($param) {
29+
// Do something.
30+
}
31+
}
32+
?>
33+
EOL;
34+
35+
/**
36+
* Set up the class under test.
37+
*
38+
* @before
39+
*/
40+
protected function setUpHighlighter()
41+
{
42+
$this->highlighter = new Highlighter($this->getConsoleColorMock());
43+
}
44+
45+
/**
46+
* Test retrieving the highlighted contents of a complete file.
47+
*
48+
* @covers ::getWholeFile
49+
* @covers ::getHighlightedLines
50+
* @covers ::splitToLines
51+
*
52+
* @dataProvider dataGetWholeFile
53+
*
54+
* @param string $input The input string.
55+
* @param string $expected Expected function output.
56+
*/
57+
public function testGetWholeFile($input, $expected)
58+
{
59+
$output = $this->highlighter->getWholeFile($input);
60+
// Allow unit tests to succeed on non-*nix systems.
61+
$output = str_replace(array("\r\n", "\r"), "\n", $output);
62+
63+
$this->assertSame($expected, $output);
64+
}
65+
66+
/**
67+
* Data provider.
68+
*
69+
* @return array
70+
*/
71+
public function dataGetWholeFile()
72+
{
73+
return array(
74+
'Empty source' => array(
75+
'input' => '',
76+
'expected' => '',
77+
),
78+
'Single line' => array(
79+
'input' => <<<'EOL'
80+
text <?= $var ?> more text
81+
EOL
82+
,
83+
'expected' => <<<'EOL'
84+
<token_html>text </token_html><token_default><?= $var ?></token_default><token_html> more text</token_html>
85+
EOL
86+
),
87+
'Multi-line' => array(
88+
'input' => $this->input,
89+
'expected' => <<<'EOL'
90+
<token_default><?php</token_default>
91+
92+
<token_keyword>namespace </token_keyword><token_default>FooBar</token_default><token_keyword>;</token_keyword>
93+
94+
<token_keyword>class </token_keyword><token_default>Foo </token_default><token_keyword>{</token_keyword>
95+
<token_keyword> </token_keyword><token_comment>/**</token_comment>
96+
<token_comment> * Docblock.</token_comment>
97+
<token_comment> *</token_comment>
98+
<token_comment> * @param type $param Description.</token_comment>
99+
<token_comment> */</token_comment>
100+
<token_comment> </token_comment><token_keyword>public function </token_keyword><token_default>bar</token_default><token_keyword>(</token_keyword><token_default>$param</token_default><token_keyword>) {</token_keyword>
101+
<token_keyword> </token_keyword><token_comment>// Do something.</token_comment>
102+
<token_comment> </token_comment><token_keyword>}</token_keyword>
103+
<token_keyword>}</token_keyword>
104+
<token_default>?></token_default>
105+
EOL
106+
),
107+
);
108+
}
109+
110+
/**
111+
* Test retrieving the highlighted contents of a complete file with line numbers.
112+
*
113+
* @covers ::getWholeFileWithLineNumbers
114+
* @covers ::getHighlightedLines
115+
* @covers ::splitToLines
116+
* @covers ::lineNumbers
117+
*/
118+
public function testGetWholeFileWithLineNumbers()
119+
{
120+
$expected = <<<'EOL'
121+
<line_number> 1| </line_number><token_default><?php</token_default>
122+
<line_number> 2| </line_number>
123+
<line_number> 3| </line_number><token_keyword>namespace </token_keyword><token_default>FooBar</token_default><token_keyword>;</token_keyword>
124+
<line_number> 4| </line_number>
125+
<line_number> 5| </line_number><token_keyword>class </token_keyword><token_default>Foo </token_default><token_keyword>{</token_keyword>
126+
<line_number> 6| </line_number><token_keyword> </token_keyword><token_comment>/**</token_comment>
127+
<line_number> 7| </line_number><token_comment> * Docblock.</token_comment>
128+
<line_number> 8| </line_number><token_comment> *</token_comment>
129+
<line_number> 9| </line_number><token_comment> * @param type $param Description.</token_comment>
130+
<line_number>10| </line_number><token_comment> */</token_comment>
131+
<line_number>11| </line_number><token_comment> </token_comment><token_keyword>public function </token_keyword><token_default>bar</token_default><token_keyword>(</token_keyword><token_default>$param</token_default><token_keyword>) {</token_keyword>
132+
<line_number>12| </line_number><token_keyword> </token_keyword><token_comment>// Do something.</token_comment>
133+
<line_number>13| </line_number><token_comment> </token_comment><token_keyword>}</token_keyword>
134+
<line_number>14| </line_number><token_keyword>}</token_keyword>
135+
<line_number>15| </line_number><token_default>?></token_default>
136+
EOL;
137+
138+
$output = $this->highlighter->getWholeFileWithLineNumbers($this->input);
139+
// Allow unit tests to succeed on non-*nix systems.
140+
$output = str_replace(array("\r\n", "\r"), "\n", $output);
141+
142+
$this->assertSame($expected, $output);
143+
}
144+
}

0 commit comments

Comments
 (0)