From 0e546692dd8b836eb09e625c50e3c48f99e1e2da Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 27 Dec 2021 12:35:21 +0100 Subject: [PATCH] Tests: add dedicated tests for the `getWholeFile[WithLineNumbers]()` methods These tests also test the `private` `getHighlightedLines()`, `splitToLines()` methods and - in part - the `lineNumbers()` method. --- tests/GetWholeFileTest.php | 144 +++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 tests/GetWholeFileTest.php diff --git a/tests/GetWholeFileTest.php b/tests/GetWholeFileTest.php new file mode 100644 index 0000000..33bb742 --- /dev/null +++ b/tests/GetWholeFileTest.php @@ -0,0 +1,144 @@ + +EOL; + + /** + * Set up the class under test. + * + * @before + */ + protected function setUpHighlighter() + { + $this->highlighter = new Highlighter($this->getConsoleColorMock()); + } + + /** + * Test retrieving the highlighted contents of a complete file. + * + * @covers ::getWholeFile + * @covers ::getHighlightedLines + * @covers ::splitToLines + * + * @dataProvider dataGetWholeFile + * + * @param string $input The input string. + * @param string $expected Expected function output. + */ + public function testGetWholeFile($input, $expected) + { + $output = $this->highlighter->getWholeFile($input); + // Allow unit tests to succeed on non-*nix systems. + $output = str_replace(array("\r\n", "\r"), "\n", $output); + + $this->assertSame($expected, $output); + } + + /** + * Data provider. + * + * @return array + */ + public function dataGetWholeFile() + { + return array( + 'Empty source' => array( + 'input' => '', + 'expected' => '', + ), + 'Single line' => array( + 'input' => <<<'EOL' +text more text +EOL + , + 'expected' => <<<'EOL' +text more text +EOL + ), + 'Multi-line' => array( + 'input' => $this->input, + 'expected' => <<<'EOL' + + +namespace FooBar; + +class Foo { + /** + * Docblock. + * + * @param type $param Description. + */ + public function bar($param) { + // Do something. + } +} +?> +EOL + ), + ); + } + + /** + * Test retrieving the highlighted contents of a complete file with line numbers. + * + * @covers ::getWholeFileWithLineNumbers + * @covers ::getHighlightedLines + * @covers ::splitToLines + * @covers ::lineNumbers + */ + public function testGetWholeFileWithLineNumbers() + { + $expected = <<<'EOL' + 1| + 2| + 3| namespace FooBar; + 4| + 5| class Foo { + 6| /** + 7| * Docblock. + 8| * + 9| * @param type $param Description. +10| */ +11| public function bar($param) { +12| // Do something. +13| } +14| } +15| ?> +EOL; + + $output = $this->highlighter->getWholeFileWithLineNumbers($this->input); + // Allow unit tests to succeed on non-*nix systems. + $output = str_replace(array("\r\n", "\r"), "\n", $output); + + $this->assertSame($expected, $output); + } +}