xref: /PHP-Parser/test/PhpParser/ErrorTest.php (revision daaadc3b)
1<?php declare(strict_types=1);
2
3namespace PhpParser;
4
5class ErrorTest extends \PHPUnit\Framework\TestCase {
6    public function testConstruct() {
7        $attributes = [
8            'startLine' => 10,
9            'endLine' => 11,
10        ];
11        $error = new Error('Some error', $attributes);
12
13        $this->assertSame('Some error', $error->getRawMessage());
14        $this->assertSame($attributes, $error->getAttributes());
15        $this->assertSame(10, $error->getStartLine());
16        $this->assertSame(11, $error->getEndLine());
17        $this->assertSame('Some error on line 10', $error->getMessage());
18
19        return $error;
20    }
21
22    /**
23     * @depends testConstruct
24     */
25    public function testSetMessageAndLine(Error $error): void {
26        $error->setRawMessage('Some other error');
27        $this->assertSame('Some other error', $error->getRawMessage());
28
29        $error->setStartLine(15);
30        $this->assertSame(15, $error->getStartLine());
31        $this->assertSame('Some other error on line 15', $error->getMessage());
32    }
33
34    public function testUnknownLine(): void {
35        $error = new Error('Some error');
36
37        $this->assertSame(-1, $error->getStartLine());
38        $this->assertSame(-1, $error->getEndLine());
39        $this->assertSame('Some error on unknown line', $error->getMessage());
40    }
41
42    /** @dataProvider provideTestColumnInfo */
43    public function testColumnInfo($code, $startPos, $endPos, $startColumn, $endColumn): void {
44        $error = new Error('Some error', [
45            'startFilePos' => $startPos,
46            'endFilePos' => $endPos,
47        ]);
48
49        $this->assertTrue($error->hasColumnInfo());
50        $this->assertSame($startColumn, $error->getStartColumn($code));
51        $this->assertSame($endColumn, $error->getEndColumn($code));
52    }
53
54    public static function provideTestColumnInfo() {
55        return [
56            // Error at "bar"
57            ["<?php foo bar baz", 10, 12, 11, 13],
58            ["<?php\nfoo bar baz", 10, 12, 5, 7],
59            ["<?php foo\nbar baz", 10, 12, 1, 3],
60            ["<?php foo bar\nbaz", 10, 12, 11, 13],
61            ["<?php\r\nfoo bar baz", 11, 13, 5, 7],
62            // Error at "baz"
63            ["<?php foo bar baz", 14, 16, 15, 17],
64            ["<?php foo bar\nbaz", 14, 16, 1, 3],
65            // Error at string literal
66            ["<?php foo 'bar\nbaz' xyz", 10, 18, 11, 4],
67            ["<?php\nfoo 'bar\nbaz' xyz", 10, 18, 5, 4],
68            ["<?php foo\n'\nbarbaz\n'\nxyz", 10, 19, 1, 1],
69            // Error over full string
70            ["<?php", 0, 4, 1, 5],
71            ["<?\nphp", 0, 5, 1, 3],
72        ];
73    }
74
75    public function testNoColumnInfo(): void {
76        $error = new Error('Some error', ['startLine' => 3]);
77
78        $this->assertFalse($error->hasColumnInfo());
79        try {
80            $error->getStartColumn('');
81            $this->fail('Expected RuntimeException');
82        } catch (\RuntimeException $e) {
83            $this->assertSame('Error does not have column information', $e->getMessage());
84        }
85        try {
86            $error->getEndColumn('');
87            $this->fail('Expected RuntimeException');
88        } catch (\RuntimeException $e) {
89            $this->assertSame('Error does not have column information', $e->getMessage());
90        }
91    }
92
93    public function testInvalidPosInfo(): void {
94        $this->expectException(\RuntimeException::class);
95        $this->expectExceptionMessage('Invalid position information');
96        $error = new Error('Some error', [
97            'startFilePos' => 10,
98            'endFilePos' => 11,
99        ]);
100        $error->getStartColumn('code');
101    }
102}
103