diff --git a/src/Codeception/Exception/TestParseException.php b/src/Codeception/Exception/TestParseException.php index 4e2df4cb13..bee55edb09 100644 --- a/src/Codeception/Exception/TestParseException.php +++ b/src/Codeception/Exception/TestParseException.php @@ -8,7 +8,7 @@ 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) { @@ -17,5 +17,8 @@ public function __construct(string $fileName, string $errors = null, int $line = if ($errors) { $this->message .= PHP_EOL . $errors; } + if ($testFile && $fileName !== $testFile) { + $this->message .= PHP_EOL . "(Error occurred while parsing Test '{$testFile}')"; + } } } diff --git a/src/Codeception/Lib/Parser.php b/src/Codeception/Lib/Parser.php index 4c91a59d7f..447baced19 100644 --- a/src/Codeception/Lib/Parser.php +++ b/src/Codeception/Lib/Parser.php @@ -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 } diff --git a/tests/data/InvalidChildClass.php b/tests/data/InvalidChildClass.php new file mode 100755 index 0000000000..6557c271fb --- /dev/null +++ b/tests/data/InvalidChildClass.php @@ -0,0 +1,5 @@ +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':\nFunny 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:\nFunny 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:\nFunny 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:\nFunny error\n(Error occurred while parsing Test '%s')", 'parent.file', 27, 'test.file')); + throw new \Codeception\Exception\TestParseException('parent.file', 'Funny error', 27, 'test.file'); + } + #[Group('core')] public function testModernValidation() { @@ -153,6 +193,33 @@ public function testModernValidation() 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:\n%s", + codecept_data_dir('InvalidClass.php'), + 27, + '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:\n%s\n(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() {