Skip to content

Commit

Permalink
Differentiate parsing errors in a parent class from the ones in the t…
Browse files Browse the repository at this point in the history
…est class
  • Loading branch information
martin-rueegg committed Aug 16, 2023
1 parent 237630e commit 6054f56
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/Codeception/Exception/TestParseException.php
Expand Up @@ -8,14 +8,17 @@

class TestParseException extends Exception
{
public function __construct(string $fileName, string $errors = null, int $line = null)
public function __construct(string $fileName, string $errors = null, int $line = null, string $testFile = null)
{
$this->message = "Couldn't parse test '{$fileName}'";
if ($line !== null) {
$this->message .= " on line {$line}";
}
if ($errors) {
$this->message .= PHP_EOL . $errors;
$this->message .= ":" . PHP_EOL . $errors;
}
if ($testFile && $fileName !== $testFile) {
$this->message .= PHP_EOL . "(Error occurred while parsing Test '{$testFile}')";
}
}
}
2 changes: 1 addition & 1 deletion src/Codeception/Lib/Parser.php
Expand Up @@ -100,7 +100,7 @@ public static function load(string $file): void
try {
self::includeFile($file);
} catch (ParseError $e) {
throw new TestParseException($file, $e->getMessage(), $e->getLine());
throw new TestParseException($e->getFile(), $e->getMessage(), $e->getLine(), $file);
} catch (Exception) {
// file is valid otherwise
}
Expand Down
5 changes: 5 additions & 0 deletions tests/data/InvalidChildClass.php
@@ -0,0 +1,5 @@
<?php

class InvalidChildClass extends InvalidClass
{
}
6 changes: 6 additions & 0 deletions tests/data/InvalidClass.php
@@ -0,0 +1,6 @@
<?php

class InvalidClass
{
foo
}
67 changes: 67 additions & 0 deletions tests/unit/Codeception/Lib/ParserTest.php
Expand Up @@ -146,13 +146,80 @@ public function testParseFileWhichUnsetsFileVariable()
$this->assertSame([], $classes);
}

#[Group('core')]
public function testParseExceptionWithFileNameOnly()
{
$this->expectException(\Codeception\Exception\TestParseException::class);
$this->expectExceptionMessage("Couldn't parse test 'test.file'");
throw new \Codeception\Exception\TestParseException('test.file');
}

#[Group('core')]
public function testParseExceptionWithErrors()
{
$this->expectException(\Codeception\Exception\TestParseException::class);
$this->expectExceptionMessage("Couldn't parse test 'test.file':" . PHP_EOL . "Funny error");
throw new \Codeception\Exception\TestParseException('test.file', 'Funny error');
}

#[Group('core')]
public function testParseExceptionWithLineNumber()
{
$this->expectException(\Codeception\Exception\TestParseException::class);
$this->expectExceptionMessage("Couldn't parse test 'test.file' on line 27:" . PHP_EOL . "Funny error");
throw new \Codeception\Exception\TestParseException('test.file', 'Funny error', 27);
}

#[Group('core')]
public function testParseExceptionWithTestFile()
{
$this->expectException(\Codeception\Exception\TestParseException::class);
$this->expectExceptionMessage("Couldn't parse test 'test.file' on line 27:" . PHP_EOL . "Funny error");
throw new \Codeception\Exception\TestParseException('test.file', 'Funny error', 27, 'test.file');
}

#[Group('core')]
public function testParseExceptionWithDifferentTestFile()
{
$this->expectException(\Codeception\Exception\TestParseException::class);
$this->expectExceptionMessage(sprintf("Couldn't parse test '%s' on line %d:" . PHP_EOL . "%s" . PHP_EOL . "(Error occurred while parsing Test '%s')", 'parent.file', 27, 'Funny error', 'test.file'));
throw new \Codeception\Exception\TestParseException('parent.file', 'Funny error', 27, 'test.file');
}

#[Group('core')]
public function testModernValidation()
{
$this->expectException(\Codeception\Exception\TestParseException::class);
Parser::load(codecept_data_dir('Invalid.php'));
}

#[Group('core')]
public function testModernClassValidation()
{
$this->expectException(\Codeception\Exception\TestParseException::class);
$this->expectExceptionMessage(sprintf(
"Couldn't parse test '%s' on line %d:%s",
codecept_data_dir('InvalidClass.php'),
27,
PHP_EOL . 'syntax error, unexpected identifier "foo", expecting "function" or "const"'
));
Parser::load(codecept_data_dir('InvalidClass.php'));
}

#[Group('core')]
public function testModernChildClassValidation()
{
$this->expectException(\Codeception\Exception\TestParseException::class);
$this->expectExceptionMessage(sprintf(
"Couldn't parse test '%s' on line %d:" . PHP_EOL . "%s" . PHP_EOL . "(Error occurred while parsing Test '%s')",
codecept_data_dir('InvalidClass.php'),
27,
'syntax error, unexpected identifier "foo", expecting "function" or "const"',
codecept_data_dir('InvalidChildClass.php')
));
Parser::load(codecept_data_dir('InvalidChildClass.php'));
}

#[Group('core')]
public function testClassesFromFile()
{
Expand Down

0 comments on commit 6054f56

Please sign in to comment.