|
13 | 13 |
|
14 | 14 | namespace phpDocumentor\Reflection\Php; |
15 | 15 |
|
| 16 | +use phpDocumentor\Reflection\Exception; |
16 | 17 | use phpDocumentor\Reflection\NodeVisitor\ElementNameResolver; |
| 18 | +use PhpParser\Error; |
17 | 19 | use PhpParser\NodeTraverser; |
18 | 20 | use PhpParser\NodeTraverserInterface; |
19 | 21 | use PhpParser\NodeVisitor\NameResolver; |
@@ -57,6 +59,44 @@ public function testThatCodeGetsConvertedIntoNodes(): void |
57 | 59 | $this->assertSame(['traversed code'], $result); |
58 | 60 | } |
59 | 61 |
|
| 62 | + public function testThatParseErrorIncludesFileAndLine(): void |
| 63 | + { |
| 64 | + $factory = NodesFactory::createInstance(); |
| 65 | + |
| 66 | + $this->expectException(Exception::class); |
| 67 | + $this->expectExceptionMessage('Syntax error in /some/file.php on line 1:'); |
| 68 | + |
| 69 | + $factory->create('<?php class { }', '/some/file.php'); |
| 70 | + } |
| 71 | + |
| 72 | + public function testThatParseErrorWithoutFilePathOnlyIncludesLine(): void |
| 73 | + { |
| 74 | + $factory = NodesFactory::createInstance(); |
| 75 | + |
| 76 | + $this->expectException(Exception::class); |
| 77 | + $this->expectExceptionMessage('Syntax error on line 1:'); |
| 78 | + |
| 79 | + $factory->create('<?php class { }'); |
| 80 | + } |
| 81 | + |
| 82 | + public function testThatParseErrorWrapsOriginalException(): void |
| 83 | + { |
| 84 | + $parser = $this->prophesize(Parser::class); |
| 85 | + $parser->parse('bad code')->willThrow(new Error('Unexpected token', ['startLine' => 42])); |
| 86 | + |
| 87 | + $nodeTraverser = $this->prophesize(NodeTraverserInterface::class); |
| 88 | + |
| 89 | + $factory = new NodesFactory($parser->reveal(), $nodeTraverser->reveal()); |
| 90 | + |
| 91 | + try { |
| 92 | + $factory->create('bad code', '/path/to/source.php'); |
| 93 | + $this->fail('Expected Exception was not thrown'); |
| 94 | + } catch (Exception $e) { |
| 95 | + $this->assertSame('Syntax error in /path/to/source.php on line 42: Unexpected token', $e->getMessage()); |
| 96 | + $this->assertInstanceOf(Error::class, $e->getPrevious()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
60 | 100 | private function givenTheExpectedDefaultNodesFactory(): NodesFactory |
61 | 101 | { |
62 | 102 | $parser = (new ParserFactory())->createForNewestSupportedVersion(); |
|
0 commit comments