comparison vendor/nikic/php-parser/test/PhpParser/ErrorTest.php @ 0:4c8ae668cc8c

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