1 <?php declare(strict_types=1);
5 use PHPUnit\Framework\TestCase;
7 class ErrorTest extends TestCase
9 public function testConstruct() {
14 $error = new Error('Some error', $attributes);
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());
26 * @depends testConstruct
28 public function testSetMessageAndLine(Error $error) {
29 $error->setRawMessage('Some other error');
30 $this->assertSame('Some other error', $error->getRawMessage());
32 $error->setStartLine(15);
33 $this->assertSame(15, $error->getStartLine());
34 $this->assertSame('Some other error on line 15', $error->getMessage());
37 public function testUnknownLine() {
38 $error = new Error('Some error');
40 $this->assertSame(-1, $error->getStartLine());
41 $this->assertSame(-1, $error->getEndLine());
42 $this->assertSame('Some error on unknown line', $error->getMessage());
45 /** @dataProvider provideTestColumnInfo */
46 public function testColumnInfo($code, $startPos, $endPos, $startColumn, $endColumn) {
47 $error = new Error('Some error', [
48 'startFilePos' => $startPos,
49 'endFilePos' => $endPos,
52 $this->assertTrue($error->hasColumnInfo());
53 $this->assertSame($startColumn, $error->getStartColumn($code));
54 $this->assertSame($endColumn, $error->getEndColumn($code));
58 public function provideTestColumnInfo() {
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],
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],
79 public function testNoColumnInfo() {
80 $error = new Error('Some error', 3);
82 $this->assertFalse($error->hasColumnInfo());
84 $error->getStartColumn('');
85 $this->fail('Expected RuntimeException');
86 } catch (\RuntimeException $e) {
87 $this->assertSame('Error does not have column information', $e->getMessage());
90 $error->getEndColumn('');
91 $this->fail('Expected RuntimeException');
92 } catch (\RuntimeException $e) {
93 $this->assertSame('Error does not have column information', $e->getMessage());
98 * @expectedException \RuntimeException
99 * @expectedExceptionMessage Invalid position information
101 public function testInvalidPosInfo() {
102 $error = new Error('Some error', [
103 'startFilePos' => 10,
106 $error->getStartColumn('code');