Skip to content

Tests: add dedicated test for the SyntaxError::getLine() method #115

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

Merged
merged 1 commit into from
Apr 8, 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
2 changes: 1 addition & 1 deletion src/Errors/SyntaxError.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
67 changes: 67 additions & 0 deletions tests/Unit/Errors/SyntaxErrorGetLineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace PHP_Parallel_Lint\PhpParallelLint\Tests\Unit\Errors;

use PHP_Parallel_Lint\PhpParallelLint\Errors\SyntaxError;
use PHP_Parallel_Lint\PhpParallelLint\Tests\UnitTestCase;

/**
* @covers \PHP_Parallel_Lint\PhpParallelLint\Errors\SyntaxError::getLine
*/
class SyntaxErrorGetLineTest extends UnitTestCase
{
/**
* Test retrieving the line on which the error occured.
*
* @dataProvider dataGetLine
*
* @param string $message The message input to run the test with.
* @param string $expected The expected method return value.
*
* @return void
*/
public function testGetLine($message, $expected)
{
$error = new SyntaxError('test.php', $message);
$this->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,
),
);
}
}