Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: introduce base HighlighterTestCase, dedicated tests for the getCodeSnippet() + two bug fixes #35

Merged
merged 4 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ public function getCodeSnippet($source, $lineNumber, $linesBefore = 2, $linesAft

$offset = $lineNumber - $linesBefore - 1;
$offset = max($offset, 0);
$length = $linesAfter + $linesBefore + 1;

if ($lineNumber <= $linesBefore) {
$length = $lineNumber + $linesAfter;
} else {
$length = $linesAfter + $linesBefore + 1;
}

$tokenLines = array_slice($tokenLines, $offset, $length, $preserveKeys = true);

$lines = $this->colorLines($tokenLines);
Expand Down Expand Up @@ -309,6 +315,6 @@ private function lineNumbers(array $lines, $markLine = null)
$snippet .= $line . PHP_EOL;
}

return $snippet;
return rtrim($snippet, PHP_EOL);
}
}
138 changes: 138 additions & 0 deletions tests/GetCodeSnippetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace PHP_Parallel_Lint\PhpConsoleHighlighter\Test;

use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;

/**
* @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::getCodeSnippet
* @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::getHighlightedLines
* @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::splitToLines
* @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::lineNumbers
*/
class GetCodeSnippetTest extends HighlighterTestCase
{
/** @var string */
private $input = <<<'EOL'
<?php

namespace FooBar;

class Foo {
/**
* @param type $param Description.
*/
public function bar($param) {
// Do something.
}
}
?>
EOL;

/**
* Test retrieving a code snippet from a larger context.
*
* @dataProvider dataGetCodeSnippet
*
* @param string $expected Expected function output.
* @param int $lineNo Line number to get the code snippet for.
* @param int $linesBeforeAfter Number of lines of code context to retrieve.
*/
public function testGetCodeSnippet($expected, $lineNo, $linesBeforeAfter = 2)
{
$highlighter = new Highlighter($this->getConsoleColorMock());
$output = $highlighter->getCodeSnippet($this->input, $lineNo, $linesBeforeAfter, $linesBeforeAfter);

// Allow unit tests to succeed on non-*nix systems.
$output = str_replace(array("\r\n", "\r"), "\n", $output);

$this->assertSame($expected, $output);
}

/**
* Data provider.
*
* Includes test cases to verify that the line number padding is handled correctly
* depending on the "widest" line number.
*
* @return array
*/
public function dataGetCodeSnippet()
{
return array(
'Snippet at start of code - line 1' => array(
'expected' => <<<'EOL'
<actual_line_mark> > </actual_line_mark><line_number>1| </line_number><token_default><?php</token_default>
<line_number>2| </line_number>
<line_number>3| </line_number><token_keyword>namespace </token_keyword><token_default>FooBar</token_default><token_keyword>;</token_keyword>
EOL
,
'lineNo' => 1,
),
'Snippet at start of code - line 2' => array(
'expected' => <<<'EOL'
<line_number>1| </line_number><token_default><?php</token_default>
<actual_line_mark> > </actual_line_mark><line_number>2| </line_number>
<line_number>3| </line_number><token_keyword>namespace </token_keyword><token_default>FooBar</token_default><token_keyword>;</token_keyword>
<line_number>4| </line_number>
EOL
,
'lineNo' => 2,
),
'Snippet middle of code' => array(
'expected' => <<<'EOL'
<line_number> 7| </line_number><token_comment> * @param type $param Description.</token_comment>
<line_number> 8| </line_number><token_comment> */</token_comment>
<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>
<line_number>10| </line_number><token_keyword> </token_keyword><token_comment>// Do something.</token_comment>
<line_number>11| </line_number><token_comment> </token_comment><token_keyword>}</token_keyword>
EOL
,
'lineNo' => 9,
),
'Snippet at end of code - line before last' => array(
'expected' => <<<'EOL'
<line_number>10| </line_number><token_keyword> </token_keyword><token_comment>// Do something.</token_comment>
<line_number>11| </line_number><token_comment> </token_comment><token_keyword>}</token_keyword>
<actual_line_mark> > </actual_line_mark><line_number>12| </line_number><token_keyword>}</token_keyword>
<line_number>13| </line_number><token_default>?></token_default>
EOL
,
'lineNo' => 12,
),
'Snippet at end of code - last line' => array(
'expected' => <<<'EOL'
<line_number>11| </line_number><token_comment> </token_comment><token_keyword>}</token_keyword>
<line_number>12| </line_number><token_keyword>}</token_keyword>
<actual_line_mark> > </actual_line_mark><line_number>13| </line_number><token_default>?></token_default>
EOL
,
'lineNo' => 13,
),
'Snippet middle of code, 1 line context' => array(
'expected' => <<<'EOL'
<line_number> 8| </line_number><token_comment> */</token_comment>
<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>
<line_number>10| </line_number><token_keyword> </token_keyword><token_comment>// Do something.</token_comment>
EOL
,
'lineNo' => 9,
'linesBeforeAfter' => 1,
),
'Snippet middle of code, 3 line context' => array(
'expected' => <<<'EOL'
<line_number> 6| </line_number><token_keyword> </token_keyword><token_comment>/**</token_comment>
<line_number> 7| </line_number><token_comment> * @param type $param Description.</token_comment>
<line_number> 8| </line_number><token_comment> */</token_comment>
<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>
<line_number>10| </line_number><token_keyword> </token_keyword><token_comment>// Do something.</token_comment>
<line_number>11| </line_number><token_comment> </token_comment><token_keyword>}</token_keyword>
<line_number>12| </line_number><token_keyword>}</token_keyword>
EOL
,
'lineNo' => 9,
'linesBeforeAfter' => 3,
),
);
}
}
39 changes: 39 additions & 0 deletions tests/HighlighterTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace PHP_Parallel_Lint\PhpConsoleHighlighter\Test;

use PHPUnit\Framework\TestCase;

class HighlighterTestCase extends TestCase
{
/**
* Helper method mocking the Console Color Class.
*
* @param bool $withTheme Whether or not the mock should act as if themes
* have been registered or not.
*
* @return \PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor
*/
protected function getConsoleColorMock($withTheme = true)
{
$mock = method_exists($this, 'createMock')
? $this->createMock('\PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor')
: $this->getMock('\PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor');

$mock->expects($this->any())
->method('apply')
->will($this->returnCallback(function ($style, $text) use ($withTheme) {
if ($withTheme) {
return "<$style>$text</$style>";
} else {
return $text;
}
}));

$mock->expects($this->any())
->method('hasTheme')
->will($this->returnValue($withTheme));

return $mock;
}
}
27 changes: 1 addition & 26 deletions tests/TokenizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace PHP_Parallel_Lint\PhpConsoleHighlighter\Test;

use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;
use PHPUnit\Framework\TestCase;

/**
* Test support for all token types.
*
* @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::tokenize
* @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::getTokenType
*/
class TokenizeTest extends TestCase
class TokenizeTest extends HighlighterTestCase
{
/** @var Highlighter */
private $uut;
Expand All @@ -24,30 +23,6 @@ protected function setUpHighlighter()
$this->uut = new Highlighter($this->getConsoleColorMock());
}

/**
* Helper method mocking the Console Color Class.
*
* @return \PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor
*/
protected function getConsoleColorMock()
{
$mock = method_exists($this, 'createMock')
? $this->createMock('\PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor')
: $this->getMock('\PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor');

$mock->expects($this->any())
->method('apply')
->will($this->returnCallback(function ($style, $text) {
return "<$style>$text</$style>";
}));

$mock->expects($this->any())
->method('hasTheme')
->will($this->returnValue(true));

return $mock;
}

/**
* Helper method executing the actual tests.
*
Expand Down