From 0b202ec0ed194db74ba4161f7b8206a1f24969ee Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 20 Feb 2022 16:08:13 +0100 Subject: [PATCH] Tests: add dedicated test for the `SyntaxError::getLine()` method This safeguards and documents the current behaviour of the method. Includes removing redundant conditional in the original method. --- src/Errors/SyntaxError.php | 2 +- tests/Unit/Errors/SyntaxErrorGetLineTest.php | 67 ++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Errors/SyntaxErrorGetLineTest.php diff --git a/src/Errors/SyntaxError.php b/src/Errors/SyntaxError.php index 9a4a36e..b3bf663 100644 --- a/src/Errors/SyntaxError.php +++ b/src/Errors/SyntaxError.php @@ -16,7 +16,7 @@ public function getLine() { preg_match('~on line ([0-9]+)$~', $this->message, $matches); - if ($matches && isset($matches[1])) { + if (isset($matches[1])) { return (int) $matches[1]; } diff --git a/tests/Unit/Errors/SyntaxErrorGetLineTest.php b/tests/Unit/Errors/SyntaxErrorGetLineTest.php new file mode 100644 index 0000000..86b824c --- /dev/null +++ b/tests/Unit/Errors/SyntaxErrorGetLineTest.php @@ -0,0 +1,67 @@ +assertSame($expected, $error->getLine()); + } + + /** + * Data provider. + * + * @return array + */ + public function dataGetLine() + { + return array( + 'Message: empty string' => array( + 'message' => '', + 'expected' => null, + ), + 'Message: plain text, no line number' => array( + 'message' => 'plain text', + 'expected' => null, + ), + 'Message: error with line number at end' => array( + 'message' => 'Parse error: syntax error, unexpected token "<", expecting end of file in Source string on line 10', + 'expected' => 10, + ), + 'Message: error with line number at end with trailing whitespace' => array( + 'message' => 'Parse error: syntax error, unexpected token "<", expecting end of file in Source string on line 10 ', + 'expected' => 10, + ), + 'Message: error with line number at end and in the message text [1]' => array( + 'message' => 'Parse error: Unclosed \'{\' on line 2 in test.php on line 5', + 'expected' => 5, + ), + 'Message: error with line number at end and in the message text [2]' => array( + 'message' => 'Parse error: Unterminated comment starting on line 2 in test.php on line 3', + 'expected' => 3, + ), + 'Message: error with line number at end, large number' => array( + 'message' => 'The (real) cast has been removed, use (float) instead in test.php on line 3534384', + 'expected' => 3534384, + ), + ); + } +}