|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHP_Parallel_Lint\PhpConsoleHighlighter\Test; |
| 4 | + |
| 5 | +use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter; |
| 6 | + |
| 7 | +/** |
| 8 | + * @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::getCodeSnippet |
| 9 | + * @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::getHighlightedLines |
| 10 | + * @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::splitToLines |
| 11 | + * @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::lineNumbers |
| 12 | + */ |
| 13 | +class GetCodeSnippetTest extends HighlighterTestCase |
| 14 | +{ |
| 15 | + /** @var string */ |
| 16 | + private $input = <<<'EOL' |
| 17 | +<?php |
| 18 | +
|
| 19 | +namespace FooBar; |
| 20 | +
|
| 21 | +class Foo { |
| 22 | + /** |
| 23 | + * @param type $param Description. |
| 24 | + */ |
| 25 | + public function bar($param) { |
| 26 | + // Do something. |
| 27 | + } |
| 28 | +} |
| 29 | +?> |
| 30 | +EOL; |
| 31 | + |
| 32 | + /** |
| 33 | + * Test retrieving a code snippet from a larger context. |
| 34 | + * |
| 35 | + * @dataProvider dataGetCodeSnippet |
| 36 | + * |
| 37 | + * @param string $expected Expected function output. |
| 38 | + * @param int $lineNo Line number to get the code snippet for. |
| 39 | + * @param int $linesBeforeAfter Number of lines of code context to retrieve. |
| 40 | + */ |
| 41 | + public function testGetCodeSnippet($expected, $lineNo, $linesBeforeAfter = 2) |
| 42 | + { |
| 43 | + $highlighter = new Highlighter($this->getConsoleColorMock()); |
| 44 | + $output = $highlighter->getCodeSnippet($this->input, $lineNo, $linesBeforeAfter, $linesBeforeAfter); |
| 45 | + |
| 46 | + // Allow unit tests to succeed on non-*nix systems. |
| 47 | + $output = str_replace(array("\r\n", "\r"), "\n", $output); |
| 48 | + |
| 49 | + $this->assertSame($expected, $output); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Data provider. |
| 54 | + * |
| 55 | + * Includes test cases to verify that the line number padding is handled correctly |
| 56 | + * depending on the "widest" line number. |
| 57 | + * |
| 58 | + * @return array |
| 59 | + */ |
| 60 | + public function dataGetCodeSnippet() |
| 61 | + { |
| 62 | + return array( |
| 63 | + 'Snippet at start of code - line 1' => array( |
| 64 | + 'expected' => <<<'EOL' |
| 65 | +<actual_line_mark> > </actual_line_mark><line_number>1| </line_number><token_default><?php</token_default> |
| 66 | + <line_number>2| </line_number> |
| 67 | + <line_number>3| </line_number><token_keyword>namespace </token_keyword><token_default>FooBar</token_default><token_keyword>;</token_keyword> |
| 68 | +EOL |
| 69 | + , |
| 70 | + 'lineNo' => 1, |
| 71 | + ), |
| 72 | + 'Snippet at start of code - line 2' => array( |
| 73 | + 'expected' => <<<'EOL' |
| 74 | + <line_number>1| </line_number><token_default><?php</token_default> |
| 75 | +<actual_line_mark> > </actual_line_mark><line_number>2| </line_number> |
| 76 | + <line_number>3| </line_number><token_keyword>namespace </token_keyword><token_default>FooBar</token_default><token_keyword>;</token_keyword> |
| 77 | + <line_number>4| </line_number> |
| 78 | +EOL |
| 79 | + , |
| 80 | + 'lineNo' => 2, |
| 81 | + ), |
| 82 | + 'Snippet middle of code' => array( |
| 83 | + 'expected' => <<<'EOL' |
| 84 | + <line_number> 7| </line_number><token_comment> * @param type $param Description.</token_comment> |
| 85 | + <line_number> 8| </line_number><token_comment> */</token_comment> |
| 86 | +<actual_line_mark> > </actual_line_mark><line_number> 9| </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> |
| 87 | + <line_number>10| </line_number><token_keyword> </token_keyword><token_comment>// Do something.</token_comment> |
| 88 | + <line_number>11| </line_number><token_comment> </token_comment><token_keyword>}</token_keyword> |
| 89 | +EOL |
| 90 | + , |
| 91 | + 'lineNo' => 9, |
| 92 | + ), |
| 93 | + 'Snippet at end of code - line before last' => array( |
| 94 | + 'expected' => <<<'EOL' |
| 95 | + <line_number>10| </line_number><token_keyword> </token_keyword><token_comment>// Do something.</token_comment> |
| 96 | + <line_number>11| </line_number><token_comment> </token_comment><token_keyword>}</token_keyword> |
| 97 | +<actual_line_mark> > </actual_line_mark><line_number>12| </line_number><token_keyword>}</token_keyword> |
| 98 | + <line_number>13| </line_number><token_default>?></token_default> |
| 99 | +EOL |
| 100 | + , |
| 101 | + 'lineNo' => 12, |
| 102 | + ), |
| 103 | + 'Snippet at end of code - last line' => array( |
| 104 | + 'expected' => <<<'EOL' |
| 105 | + <line_number>11| </line_number><token_comment> </token_comment><token_keyword>}</token_keyword> |
| 106 | + <line_number>12| </line_number><token_keyword>}</token_keyword> |
| 107 | +<actual_line_mark> > </actual_line_mark><line_number>13| </line_number><token_default>?></token_default> |
| 108 | +EOL |
| 109 | + , |
| 110 | + 'lineNo' => 13, |
| 111 | + ), |
| 112 | + 'Snippet middle of code, 1 line context' => array( |
| 113 | + 'expected' => <<<'EOL' |
| 114 | + <line_number> 8| </line_number><token_comment> */</token_comment> |
| 115 | +<actual_line_mark> > </actual_line_mark><line_number> 9| </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> |
| 116 | + <line_number>10| </line_number><token_keyword> </token_keyword><token_comment>// Do something.</token_comment> |
| 117 | +EOL |
| 118 | + , |
| 119 | + 'lineNo' => 9, |
| 120 | + 'linesBeforeAfter' => 1, |
| 121 | + ), |
| 122 | + 'Snippet middle of code, 3 line context' => array( |
| 123 | + 'expected' => <<<'EOL' |
| 124 | + <line_number> 6| </line_number><token_keyword> </token_keyword><token_comment>/**</token_comment> |
| 125 | + <line_number> 7| </line_number><token_comment> * @param type $param Description.</token_comment> |
| 126 | + <line_number> 8| </line_number><token_comment> */</token_comment> |
| 127 | +<actual_line_mark> > </actual_line_mark><line_number> 9| </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> |
| 128 | + <line_number>10| </line_number><token_keyword> </token_keyword><token_comment>// Do something.</token_comment> |
| 129 | + <line_number>11| </line_number><token_comment> </token_comment><token_keyword>}</token_keyword> |
| 130 | + <line_number>12| </line_number><token_keyword>}</token_keyword> |
| 131 | +EOL |
| 132 | + , |
| 133 | + 'lineNo' => 9, |
| 134 | + 'linesBeforeAfter' => 3, |
| 135 | + ), |
| 136 | + ); |
| 137 | + } |
| 138 | +} |
0 commit comments