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