Skip to content

Commit 986f18d

Browse files
jrfnlgrogy
authored andcommitted
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.
1 parent 8f1056e commit 986f18d

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/Errors/SyntaxError.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function getLine()
1616
{
1717
preg_match('~on line ([0-9]+)$~', $this->message, $matches);
1818

19-
if ($matches && isset($matches[1])) {
19+
if (isset($matches[1])) {
2020
return (int) $matches[1];
2121
}
2222

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace PHP_Parallel_Lint\PhpParallelLint\Tests\Unit\Errors;
4+
5+
use PHP_Parallel_Lint\PhpParallelLint\Errors\SyntaxError;
6+
use PHP_Parallel_Lint\PhpParallelLint\Tests\UnitTestCase;
7+
8+
/**
9+
* @covers \PHP_Parallel_Lint\PhpParallelLint\Errors\SyntaxError::getLine
10+
*/
11+
class SyntaxErrorGetLineTest extends UnitTestCase
12+
{
13+
/**
14+
* Test retrieving the line on which the error occured.
15+
*
16+
* @dataProvider dataGetLine
17+
*
18+
* @param string $message The message input to run the test with.
19+
* @param string $expected The expected method return value.
20+
*
21+
* @return void
22+
*/
23+
public function testGetLine($message, $expected)
24+
{
25+
$error = new SyntaxError('test.php', $message);
26+
$this->assertSame($expected, $error->getLine());
27+
}
28+
29+
/**
30+
* Data provider.
31+
*
32+
* @return array
33+
*/
34+
public function dataGetLine()
35+
{
36+
return array(
37+
'Message: empty string' => array(
38+
'message' => '',
39+
'expected' => null,
40+
),
41+
'Message: plain text, no line number' => array(
42+
'message' => 'plain text',
43+
'expected' => null,
44+
),
45+
'Message: error with line number at end' => array(
46+
'message' => 'Parse error: syntax error, unexpected token "<", expecting end of file in Source string on line 10',
47+
'expected' => 10,
48+
),
49+
'Message: error with line number at end with trailing whitespace' => array(
50+
'message' => 'Parse error: syntax error, unexpected token "<", expecting end of file in Source string on line 10 ',
51+
'expected' => 10,
52+
),
53+
'Message: error with line number at end and in the message text [1]' => array(
54+
'message' => 'Parse error: Unclosed \'{\' on line 2 in test.php on line 5',
55+
'expected' => 5,
56+
),
57+
'Message: error with line number at end and in the message text [2]' => array(
58+
'message' => 'Parse error: Unterminated comment starting on line 2 in test.php on line 3',
59+
'expected' => 3,
60+
),
61+
'Message: error with line number at end, large number' => array(
62+
'message' => 'The (real) cast has been removed, use (float) instead in test.php on line 3534384',
63+
'expected' => 3534384,
64+
),
65+
);
66+
}
67+
}

0 commit comments

Comments
 (0)