From c7b10c755b938ee42a11b80d6ff1dc0125d98b0c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 5 Dec 2021 00:34:56 +0100 Subject: [PATCH 01/18] Composer: improve PHPUnit version constraints ... as PHPUnit < 5.7.21 doesn't contain the forward compatibility layer with namespaced classes. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 269855d..fc7fe76 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "php-parallel-lint/php-console-color": "^1.0.1" }, "require-dev": { - "phpunit/phpunit": "^4.8.36 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0", + "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0", "php-parallel-lint/php-parallel-lint": "^1.0", "php-parallel-lint/php-var-dump-check": "0.*", "php-parallel-lint/php-code-style": "^2.0" From 20c7fc4cc2d5de6af42e6a905dd5d4cba9673b8f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 5 Dec 2021 00:39:51 +0100 Subject: [PATCH 02/18] PHPUnit: improve configuration * Add more detailed basic test configuration * Unless `junit` is used somewhere, generating this report for code coverage is unnecessary. * Generating the text report, however, is useful. --- phpunit.xml.dist | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c236588..355e3e8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,9 +1,18 @@ @@ -19,9 +28,9 @@ - + From 08c3d71b86c29daa8fdfb9e0da40ecaafa0a8838 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 26 Dec 2021 19:36:23 +0100 Subject: [PATCH 03/18] Tests: rename the test class/file to `TokenizeTest` Realistically, all tests in the `HighligherTest` file are testing the handling of various token types and only incidentally testing the rest of the logic of the `Highlighter` class. With that in mind, I'm proposing to rename the test file to make it more obvious what is being tested. This will then also allow for adding additional new test classes to test the rest of the functionality of the `Highlighter` class. Includes adding minimal documentation and a minor method order tweak. --- .../{HighlighterTest.php => TokenizeTest.php} | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) rename tests/{HighlighterTest.php => TokenizeTest.php} (95%) diff --git a/tests/HighlighterTest.php b/tests/TokenizeTest.php similarity index 95% rename from tests/HighlighterTest.php rename to tests/TokenizeTest.php index 4c1fb36..3bfff21 100644 --- a/tests/HighlighterTest.php +++ b/tests/TokenizeTest.php @@ -5,11 +5,30 @@ use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter; use PHPUnit\Framework\TestCase; -class HighlighterTest extends 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 { /** @var Highlighter */ private $uut; + /** + * @before + */ + 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') @@ -29,14 +48,6 @@ protected function getConsoleColorMock() return $mock; } - /** - * @before - */ - protected function setUpHighlighter() - { - $this->uut = new Highlighter($this->getConsoleColorMock()); - } - protected function compare($original, $expected) { $output = $this->uut->getWholeFile($original); From 270d11c54a3972b7f65ce2e249c02aba7db0377b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 11 Dec 2021 08:13:44 +0100 Subject: [PATCH 04/18] TokenizeTest: use nowdocs instead of heredocs For most tests, the text within the heredoc should _not_ be interpreted (interpolated). For those tests, it makes more sense to use nowdocs instead of heredocs. Similar to single quoted versus double quoted strings, text within nowdocs is not interpolated (variables are not expanded), while within heredocs, they are. Nowdocs are available since PHP 5.3, so can be safely used within this test suite. Refs: * https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc * https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc --- tests/TokenizeTest.php | 64 +++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 3bfff21..25b2caf 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -60,14 +60,14 @@ protected function compare($original, $expected) public function testVariable() { $this->compare( - << -echo \$a; +echo $a; EOL ); } @@ -75,12 +75,12 @@ public function testVariable() public function testInteger() { $this->compare( - << echo 43; EOL @@ -90,12 +90,12 @@ public function testInteger() public function testFloat() { $this->compare( - << echo 43.3; EOL @@ -105,12 +105,12 @@ public function testFloat() public function testHex() { $this->compare( - << echo 0x43; EOL @@ -120,17 +120,17 @@ public function testHex() public function testBasicFunction() { $this->compare( - << -function plus(\$a, \$b) { - return \$a + \$b; +function plus($a, $b) { + return $a + $b; } EOL ); @@ -139,12 +139,12 @@ function plus(\$a, \$b) { public function testStringNormal() { $this->compare( - << echo 'Ahoj světe'; EOL @@ -154,12 +154,12 @@ public function testStringNormal() public function testStringDouble() { $this->compare( - << echo "Ahoj světe"; EOL @@ -169,14 +169,14 @@ public function testStringDouble() public function testInstanceof() { $this->compare( - << -\$a instanceof stdClass; +$a instanceof stdClass; EOL ); } @@ -218,12 +218,12 @@ public function testConstant() public function testComment() { $this->compare( - << /* Ahoj */ EOL @@ -233,12 +233,12 @@ public function testComment() public function testDocComment() { $this->compare( - << /** Ahoj */ EOL @@ -248,12 +248,12 @@ public function testDocComment() public function testInlineComment() { $this->compare( - << // Ahoj EOL @@ -263,12 +263,12 @@ public function testInlineComment() public function testHashComment() { $this->compare( - << # Ahoj EOL From 4f738620b88a02d0e35644e5fc6391817f73f53c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 26 Dec 2021 22:32:53 +0100 Subject: [PATCH 05/18] TokenizeTest: move "empty" tests to new test setup with data providers This sets up a new structure for this test class using test methods with data providers. All other existing tests will be converted to data sets in data providers in follow on commits. --- tests/TokenizeTest.php | 59 ++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 25b2caf..1fd84b6 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -48,6 +48,17 @@ protected function getConsoleColorMock() return $mock; } + /** + * Helper method executing the actual tests. + * + * {@internal This is a work-around to allow for supporting PHPUnit 4.x. + * In PHPUnit 5 and higher, a test method can have multiple data provider tags and + * will execute the test cases for each. Unfortunately, that wasn't supported in PHPUnit 4.x. + * This effectively means that each data provider needs its own test method for now.} + * + * @param string $original The input string. + * @param string $expected The expected output string. + */ protected function compare($original, $expected) { $output = $this->uut->getWholeFile($original); @@ -57,6 +68,38 @@ protected function compare($original, $expected) $this->assertSame($expected, $output); } + /** + * Test the tokenizer and token specific highlighting of "empty" inputs. + * + * @dataProvider dataEmptyFiles + * + * @param string $original The input string. + * @param string $expected The expected output string. + */ + public function testEmptyFiles($original, $expected) + { + $this->compare($original, $expected); + } + + /** + * Data provider. + * + * @return array + */ + public function dataEmptyFiles() + { + return array( + 'Empty file' => array( + 'original' => '', + 'expected' => '', + ), + 'File only containing whitespace' => array( + 'original' => ' ', + 'expected' => ' ', + ), + ); + } + public function testVariable() { $this->compare( @@ -275,22 +318,6 @@ public function testHashComment() ); } - public function testEmpty() - { - $this->compare( - '', - '' - ); - } - - public function testWhitespace() - { - $this->compare( - ' ', - ' ' - ); - } - public function testFunctionCall() { $this->compare( From 72a1b20faa90c02989fa501b0a3c5ae03955d8ff Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 26 Dec 2021 22:32:53 +0100 Subject: [PATCH 06/18] TokenizeTest: add tests for PHP tags Note that the new test data uses NOWDOC format instead of HEREDOC format. --- tests/TokenizeTest.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 1fd84b6..7c8553e 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -100,6 +100,48 @@ public function dataEmptyFiles() ); } + /** + * Test the tokenizer and token specific highlighting of PHP tag tokens. + * + * @dataProvider dataPhpTags + * + * @param string $original The input string. + * @param string $expected The expected output string. + */ + public function testPhpTags($original, $expected) + { + $this->compare($original, $expected); + } + + /** + * Data provider. + * + * @return array + */ + public function dataPhpTags() + { + return array( + '"Long" open tag with close tag' => array( + 'original' => <<<'EOL' + +EOL + , + 'expected' => <<<'EOL' +echo PHP_EOL; ?> +EOL + ), + 'Short open tag with close tag' => array( + 'original' => <<<'EOL' +text more text +EOL + , + 'expected' => <<<'EOL' +text /* comment */ ?> more text +EOL + ), + ); + } + public function testVariable() { $this->compare( From 8d486f142d26a0ec4889710339d3fa16056f6dbc Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 26 Dec 2021 22:39:55 +0100 Subject: [PATCH 07/18] TokenizeTest: move magic constant test cases to data provider Note: this removes the looping from the test (which was a bad thing). Advantages of using data providers compared to using a loop within the test function, are: 1. When one of the "test cases" fails, the rest of the tests cases will still be run (not so when using a loop). This prevents one test failure "hiding" behind another failure. 2. When there are multiple assertions is a test, it is often hard to determine which of the assertions (test cases) failed. Using a data provider - especially with named cases like used in this commit - will make the error coming from PHPUnit more descriptive and will make debugging which test case failed easier. 3. The test cases will now show (and be counted) as individual tests in progress reports and in testdox output. Ref: https://phpunit.readthedocs.io/en/stable/writing-tests-for-phpunit.html#data-providers --- tests/TokenizeTest.php | 80 ++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 31 deletions(-) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 7c8553e..3a99b4b 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -142,6 +142,55 @@ public function dataPhpTags() ); } + /** + * Test the tokenizer and token specific highlighting of the magic constants. + * + * @dataProvider dataMagicConstants + * + * @param string $original The input string. + * @param string $expected The expected output string. + */ + public function testMagicConstants($original, $expected) + { + $this->compare($original, $expected); + } + + /** + * Data provider. + * + * @return array + */ + public function dataMagicConstants() + { + $magicConstants = array( + '__FILE__', + '__LINE__', + '__CLASS__', + '__FUNCTION__', + '__METHOD__', + '__TRAIT__', + '__DIR__', + '__NAMESPACE__' + ); + + $data = array(); + foreach ($magicConstants as $constant) { + $data['Magic constant: ' . $constant] = array( + 'original' => << << +$constant; +EOL + ); + } + + return $data; + } + public function testVariable() { $this->compare( @@ -266,37 +315,6 @@ public function testInstanceof() ); } - /* - * Constants - */ - public function testConstant() - { - $constants = array( - '__FILE__', - '__LINE__', - '__CLASS__', - '__FUNCTION__', - '__METHOD__', - '__TRAIT__', - '__DIR__', - '__NAMESPACE__' - ); - - foreach ($constants as $constant) { - $this->compare( - << -$constant; -EOL - ); - } - } - /* * Comments */ From 877c417ae3a6dde63c02be05707ccf2c8f885ad8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 26 Dec 2021 23:01:11 +0100 Subject: [PATCH 08/18] TokenizeTest: move miscellaneous token test cases to data provider --- tests/TokenizeTest.php | 80 +++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 3a99b4b..6a34c44 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -191,63 +191,71 @@ public function dataMagicConstants() return $data; } - public function testVariable() + /** + * Test the tokenizer and token specific highlighting of the "miscellaneous" tokens. + * + * @dataProvider dataMiscTokens + * + * @param string $original The input string. + * @param string $expected The expected output string. + */ + public function testMiscTokens($original, $expected) { - $this->compare( - <<<'EOL' + $this->compare($original, $expected); + } + + /** + * Data provider. + * + * @return array + */ + public function dataMiscTokens() + { + return array( + 'Variable' => array( + 'original' => <<<'EOL' <<<'EOL' echo $a; EOL - ); - } - - public function testInteger() - { - $this->compare( - <<<'EOL' + ), + 'Integer: decimal' => array( + 'original' => <<<'EOL' <<<'EOL' echo 43; EOL - ); - } - - public function testFloat() - { - $this->compare( - <<<'EOL' + ), + 'Integer: hexadecimal' => array( + 'original' => <<<'EOL' <<<'EOL' -echo 43.3; +echo 0x43; EOL - ); - } - - public function testHex() - { - $this->compare( - <<<'EOL' + ), + 'Float' => array( + 'original' => <<<'EOL' <<<'EOL' -echo 0x43; +echo 43.3; EOL + ), ); } From a91e27d1e1243672d3ae8c3cff412b1d94e5c337 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 26 Dec 2021 23:14:06 +0100 Subject: [PATCH 09/18] TokenizeTest: add dedicated test for a T_STRING type token --- tests/TokenizeTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 6a34c44..3f75bb9 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -212,6 +212,17 @@ public function testMiscTokens($original, $expected) public function dataMiscTokens() { return array( + 'Constant (T_STRING)' => array( + 'original' => <<<'EOL' + <<<'EOL' + +echo PHP_EOL; +EOL + ), 'Variable' => array( 'original' => <<<'EOL' Date: Sun, 26 Dec 2021 23:19:14 +0100 Subject: [PATCH 10/18] TokenizeTest: move comment token test cases to data provider --- tests/TokenizeTest.php | 123 +++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 59 deletions(-) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 3f75bb9..2980cf3 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -270,129 +270,134 @@ public function dataMiscTokens() ); } - public function testBasicFunction() + /** + * Test the tokenizer and token specific highlighting of comment tokens. + * + * @dataProvider dataComments + * + * @param string $original The input string. + * @param string $expected The expected output string. + */ + public function testComments($original, $expected) { - $this->compare( - <<<'EOL' + $this->compare($original, $expected); + } + + /** + * Data provider. + * + * @return array + */ + public function dataComments() + { + return array( + 'Doc block: single line' => array( + 'original' => <<<'EOL' <<<'EOL' -function plus($a, $b) { - return $a + $b; -} +/** Ahoj */ EOL - ); - } - - public function testStringNormal() - { - $this->compare( - <<<'EOL' + ), + 'Star comment: single line' => array( + 'original' => <<<'EOL' <<<'EOL' -echo 'Ahoj světe'; +/* Ahoj */ EOL - ); - } - - public function testStringDouble() - { - $this->compare( - <<<'EOL' + ), + 'Slash comment' => array( + 'original' => <<<'EOL' <<<'EOL' -echo "Ahoj světe"; +// Ahoj EOL - ); - } - - public function testInstanceof() - { - $this->compare( - <<<'EOL' + ), + 'Hash comment' => array( + 'original' => <<<'EOL' <<<'EOL' -$a instanceof stdClass; +# Ahoj EOL + ), ); } - /* - * Comments - */ - public function testComment() + public function testBasicFunction() { $this->compare( <<<'EOL' -/* Ahoj */ +function plus($a, $b) { + return $a + $b; +} EOL ); } - public function testDocComment() + public function testStringNormal() { $this->compare( <<<'EOL' -/** Ahoj */ +echo 'Ahoj světe'; EOL ); } - public function testInlineComment() + public function testStringDouble() { $this->compare( <<<'EOL' -// Ahoj +echo "Ahoj světe"; EOL ); } - public function testHashComment() + public function testInstanceof() { $this->compare( <<<'EOL' -# Ahoj +$a instanceof stdClass; EOL ); } From 1161418ec17feec6fc657b7ba5527975dbfbedec Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 26 Dec 2021 23:25:26 +0100 Subject: [PATCH 11/18] TokenizeTest: add test cases for multi-line comments --- tests/TokenizeTest.php | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 2980cf3..732f0dd 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -300,6 +300,25 @@ public function dataComments() 'expected' => <<<'EOL' /** Ahoj */ +EOL + ), + 'Doc block: multi line' => array( + 'original' => <<<'EOL' + <<<'EOL' + +/** + * Ahoj + * + * @param type $name Description + */ EOL ), 'Star comment: single line' => array( @@ -311,6 +330,21 @@ public function dataComments() 'expected' => <<<'EOL' /* Ahoj */ +EOL + ), + 'Star comment: multi line' => array( + 'original' => <<<'EOL' + <<<'EOL' + +/* + Ahoj + */ EOL ), 'Slash comment' => array( @@ -322,6 +356,19 @@ public function dataComments() 'expected' => <<<'EOL' // Ahoj +EOL + ), + 'Slash comment, multiple' => array( + 'original' => <<<'EOL' + <<<'EOL' + +// Ahoj +// Ahoj again EOL ), 'Hash comment' => array( From 7f76e609feddb09374d37413a9b5f20925ee66b2 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 26 Dec 2021 23:38:22 +0100 Subject: [PATCH 12/18] TokenizeTest: move text string test cases to data provider --- tests/TokenizeTest.php | 64 ++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 732f0dd..feafe77 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -385,51 +385,67 @@ public function dataComments() ); } - public function testBasicFunction() + /** + * Test the tokenizer and token specific highlighting of text string tokens. + * + * @dataProvider dataTextStrings + * + * @param string $original The input string. + * @param string $expected The expected output string. + */ + public function testTextStrings($original, $expected) { - $this->compare( - <<<'EOL' - -function plus($a, $b) { - return $a + $b; -} -EOL - ); + $this->compare($original, $expected); } - public function testStringNormal() + /** + * Data provider for testing text string tokens. + * + * @return array + */ + public function dataTextStrings() { - $this->compare( - <<<'EOL' + return array( + 'Single quoted text string' => array( + 'original' => <<<'EOL' <<<'EOL' echo 'Ahoj světe'; EOL + ), + 'Double quoted text string' => array( + 'original' => <<<'EOL' + <<<'EOL' + +echo "Ahoj světe"; +EOL + ), ); } - public function testStringDouble() + public function testBasicFunction() { $this->compare( <<<'EOL' -echo "Ahoj světe"; +function plus($a, $b) { + return $a + $b; +} EOL ); } From 98610c425b8c588d1d4b7c2b03221af964bf3b16 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 27 Dec 2021 00:09:05 +0100 Subject: [PATCH 13/18] TokenizeTest: add additional test cases for text string handling This adds extra tests for: * Double quoted strings with interpolated variables. * Nowdoc * Heredocs with and without interpolated variables. Note: for the nowdoc and heredoc tests, the test input is put in single quotes to prevent "nested here/nowdoc", which could skew the tests. These test cases also need to have a new line after the `;` as otherwise PHP does not tokenize these correctly (and the highlighter is not concerned with fixing incorrect tokenization of PHP itself). --- tests/TokenizeTest.php | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index feafe77..286fbe6 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -426,6 +426,81 @@ public function dataTextStrings() 'expected' => <<<'EOL' echo "Ahoj světe"; +EOL + ), + 'Double quoted text string with interpolation [1]' => array( + 'original' => <<<'EOL' + <<<'EOL' + +echo "Ahoj $text and more text"; +EOL + ), + 'Double quoted text string with interpolation [2]' => array( + 'original' => <<<'EOL' + <<<'EOL' + +echo "$text and more text"; +EOL + ), + 'Double quoted text string with interpolation [3]' => array( + 'original' => <<<'EOL' +prop} and more text"; +EOL + , + 'expected' => <<<'EOL' + +echo "Ahoj {$obj->prop} and more text"; +EOL + ), + 'Nowdoc' => array( + 'original' => ' <<<'EOL' + +echo <<<'TXT' +Text +TXT; + +EOL + ), + 'Heredoc' => array( + 'original' => ' <<<'EOL' + +echo << +Text +TXT; + +EOL + ), + 'Heredoc with interpolation' => array( + 'original' => ' <<<'EOL' + +echo << +Text $text +TXT; + EOL ), ); From 09ca6652c7a1849108eac676a166636cf7db4f3f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 27 Dec 2021 00:16:50 +0100 Subject: [PATCH 14/18] TokenizeTest: add test for inline HTML --- tests/TokenizeTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 286fbe6..43b0199 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -501,6 +501,39 @@ public function dataTextStrings() Text $text TXT; +EOL + ), + ); + } + + /** + * Test the tokenizer and token specific highlighting of inline HTML tokens. + * + * @dataProvider dataInlineHtml + * + * @param string $original The input string. + * @param string $expected The expected output string. + */ + public function testInlineHtml($original, $expected) + { + $this->compare($original, $expected); + } + + /** + * Data provider. + * + * @return array + */ + public function dataInlineHtml() + { + return array( + 'Inline HTML' => array( + 'original' => <<<'EOL' +
+EOL + , + 'expected' => <<<'EOL' +
EOL ), ); From 926ba3fb84bedfe87cae46438a1f7a8625f03317 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 27 Dec 2021 01:11:59 +0100 Subject: [PATCH 15/18] TokenizeTest: move function call test cases to data provider --- tests/TokenizeTest.php | 140 ++++++++++++++++++++++------------------- 1 file changed, 75 insertions(+), 65 deletions(-) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 43b0199..169a0ba 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -539,118 +539,128 @@ public function dataInlineHtml() ); } - public function testBasicFunction() - { - $this->compare( - <<<'EOL' - -function plus($a, $b) { - return $a + $b; -} -EOL - ); - } - - public function testInstanceof() + /** + * Test the tokenizer and token specific highlighting of name tokens. + * + * @dataProvider dataNameTokens + * + * @param string $original The input string. + * @param string $expected The expected output string. + */ + public function testNameTokens($original, $expected) { - $this->compare( - <<<'EOL' - -$a instanceof stdClass; -EOL - ); + $this->compare($original, $expected); } - public function testFunctionCall() + /** + * Data provider. + * + * @return array + */ + public function dataNameTokens() { - $this->compare( - <<<'EOL' + $data = array( + 'Unqualified function call' => array( + 'original' => <<<'EOL' <<<'EOL' echo functionName(); EOL + ), ); - } - public function testFQNFunctionCall() - { - $original = <<<'EOL' + $data['Fully qualified function call'] = array( + 'original' => <<<'EOL' echo \My\Package\functionName(); EOL; } else { - $expected = <<<'EOL' + $data['Fully qualified function call']['expected'] = <<<'EOL' echo \My\Package\functionName(); EOL; } - $this->compare($original, $expected); - } - - public function testNamespaceRelativeFunctionCall() - { - $original = <<<'EOL' + $data['Namespace relative function call'] = array( + 'original' => <<<'EOL' echo namespace\functionName(); EOL; } else { - $expected = <<<'EOL' + $data['Namespace relative function call']['expected'] = <<<'EOL' echo namespace\functionName(); EOL; } - $this->compare($original, $expected); - } - - public function testQualifiedFunctionCall() - { - $original = <<<'EOL' + $data['Partially qualified function call'] = array( + 'original' => <<<'EOL' echo Package\functionName(); EOL; } else { - $expected = <<<'EOL' + $data['Partially qualified function call']['expected'] = <<<'EOL' echo Package\functionName(); EOL; } - $this->compare($original, $expected); + return $data; + } + + public function testBasicFunction() + { + $this->compare( + <<<'EOL' + +function plus($a, $b) { + return $a + $b; +} +EOL + ); + } + + public function testInstanceof() + { + $this->compare( + <<<'EOL' + +$a instanceof stdClass; +EOL + ); } } From cbd75803a95898468560e2298b54c0318f25e2d3 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 27 Dec 2021 01:15:18 +0100 Subject: [PATCH 16/18] TokenizeTest: move keyword test case to data provider --- tests/TokenizeTest.php | 56 +++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 169a0ba..8a874c8 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -630,37 +630,53 @@ public function dataNameTokens() return $data; } - public function testBasicFunction() + /** + * Test the tokenizer and token specific highlighting of keyword and operator tokens. + * + * @dataProvider dataKeywordsAndOperators + * + * @param string $original The input string. + * @param string $expected The expected output string. + */ + public function testKeywordsAndOperators($original, $expected) + { + $this->compare($original, $expected); + } + + /** + * Data provider. + * + * @return array + */ + public function dataKeywordsAndOperators() { - $this->compare( - <<<'EOL' + return array( + 'Keywords: instanceof' => array( + 'original' => <<<'EOL' + <<<'EOL' + +$a instanceof stdClass; +EOL + ), + 'Keywords: function, return' => array( + 'original' => <<<'EOL' <<<'EOL' function plus($a, $b) { return $a + $b; } EOL - ); - } - - public function testInstanceof() - { - $this->compare( - <<<'EOL' - -$a instanceof stdClass; -EOL + ), ); } } From 90b3b9ef4d237fd4cac6deffb01374073a64cd16 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 27 Dec 2021 01:44:52 +0100 Subject: [PATCH 17/18] TokenizeTest: add additional tests for keywords and operators --- tests/TokenizeTest.php | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/tests/TokenizeTest.php b/tests/TokenizeTest.php index 8a874c8..0170bc8 100644 --- a/tests/TokenizeTest.php +++ b/tests/TokenizeTest.php @@ -675,6 +675,84 @@ function plus($a, $b) { function plus($a, $b) { return $a + $b; } +EOL + ), + 'Keywords: while, empty, exit' => array( + 'original' => <<<'EOL' + <<<'EOL' + +while(empty($a)) { exit; } +EOL + ), + 'Keywords: type casts' => array( + 'original' => <<<'EOL' + <<<'EOL' + +$a = (int) (bool) $a . (string) $b; +EOL + ), + 'Keywords: new, clone' => array( + 'original' => <<<'EOL' + <<<'EOL' + +$obj = new stdClass; +$clone = clone $obj; +EOL + ), + 'Operators: arithmetic operators' => array( + 'original' => <<<'EOL' + <<<'EOL' + +echo 1 + 2 - 2 * 10 / 5 ** 1; +EOL + ), + 'Operators: assignment operators' => array( + 'original' => <<<'EOL' + <<<'EOL' + +$a = 10; +$a *= 10; +$a ^= 10; +$a ??= $b; +EOL + ), + 'Operators: comparison, boolean and logical operators' => array( + 'original' => <<<'EOL' + ''; +echo '' <=> '' and '' or ! ''; +EOL + , + 'expected' => <<<'EOL' + +echo '' === '' && '' !== ''; +echo true || '' > ''; +echo '' <=> '' and '' or ! ''; EOL ), ); From d32b17d32cd06ee3f1c8ff6638bb7ecb30aa5115 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 30 Dec 2021 17:58:29 +0100 Subject: [PATCH 18/18] GH Actions: set `short_open_tag` ini setting for PHP 5.3 test run The tests for this PR were failing after support for PHP 5.3 had been added back. Reason being that the short PHP open echo tags `