2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2011-11-27 21:43:27 +01:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
2017-04-27 18:14:07 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class ErrorTest extends TestCase
|
2011-11-27 21:43:27 +01:00
|
|
|
{
|
|
|
|
public function testConstruct() {
|
2017-08-13 14:35:03 +02:00
|
|
|
$attributes = [
|
2015-04-30 21:58:45 +02:00
|
|
|
'startLine' => 10,
|
|
|
|
'endLine' => 11,
|
2017-08-13 14:35:03 +02:00
|
|
|
];
|
2015-05-01 20:17:39 +02:00
|
|
|
$error = new Error('Some error', $attributes);
|
2011-11-27 21:43:27 +01:00
|
|
|
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('Some error', $error->getRawMessage());
|
2015-05-01 20:17:39 +02:00
|
|
|
$this->assertSame($attributes, $error->getAttributes());
|
2015-04-30 21:58:45 +02:00
|
|
|
$this->assertSame(10, $error->getStartLine());
|
|
|
|
$this->assertSame(11, $error->getEndLine());
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('Some error on line 10', $error->getMessage());
|
2011-11-27 21:43:27 +01:00
|
|
|
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testConstruct
|
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
public function testSetMessageAndLine(Error $error) {
|
2011-11-27 21:43:27 +01:00
|
|
|
$error->setRawMessage('Some other error');
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('Some other error', $error->getRawMessage());
|
2015-04-30 21:58:45 +02:00
|
|
|
|
|
|
|
$error->setStartLine(15);
|
|
|
|
$this->assertSame(15, $error->getStartLine());
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('Some other error on line 15', $error->getMessage());
|
2011-11-27 21:43:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnknownLine() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$error = new Error('Some error');
|
2011-11-27 21:43:27 +01:00
|
|
|
|
2015-04-30 21:58:45 +02:00
|
|
|
$this->assertSame(-1, $error->getStartLine());
|
|
|
|
$this->assertSame(-1, $error->getEndLine());
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('Some error on unknown line', $error->getMessage());
|
2011-11-27 21:43:27 +01:00
|
|
|
}
|
2015-04-14 20:31:06 +02:00
|
|
|
|
2015-04-18 12:51:26 +02:00
|
|
|
/** @dataProvider provideTestColumnInfo */
|
|
|
|
public function testColumnInfo($code, $startPos, $endPos, $startColumn, $endColumn) {
|
2017-08-13 14:35:03 +02:00
|
|
|
$error = new Error('Some error', [
|
2015-04-18 12:51:26 +02:00
|
|
|
'startFilePos' => $startPos,
|
|
|
|
'endFilePos' => $endPos,
|
2017-08-13 14:35:03 +02:00
|
|
|
]);
|
2015-04-14 20:31:06 +02:00
|
|
|
|
2017-12-12 12:02:49 +01:00
|
|
|
$this->assertTrue($error->hasColumnInfo());
|
2015-04-18 12:51:26 +02:00
|
|
|
$this->assertSame($startColumn, $error->getStartColumn($code));
|
|
|
|
$this->assertSame($endColumn, $error->getEndColumn($code));
|
2015-04-14 20:31:06 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:51:26 +02:00
|
|
|
public function provideTestColumnInfo() {
|
2017-08-13 14:35:03 +02:00
|
|
|
return [
|
2015-04-18 12:51:26 +02:00
|
|
|
// Error at "bar"
|
2017-08-13 14:35:03 +02:00
|
|
|
["<?php foo bar baz", 10, 12, 11, 13],
|
|
|
|
["<?php\nfoo bar baz", 10, 12, 5, 7],
|
|
|
|
["<?php foo\nbar baz", 10, 12, 1, 3],
|
|
|
|
["<?php foo bar\nbaz", 10, 12, 11, 13],
|
|
|
|
["<?php\r\nfoo bar baz", 11, 13, 5, 7],
|
2015-04-18 12:51:26 +02:00
|
|
|
// Error at "baz"
|
2017-08-13 14:35:03 +02:00
|
|
|
["<?php foo bar baz", 14, 16, 15, 17],
|
|
|
|
["<?php foo bar\nbaz", 14, 16, 1, 3],
|
2015-04-18 12:51:26 +02:00
|
|
|
// Error at string literal
|
2017-08-13 14:35:03 +02:00
|
|
|
["<?php foo 'bar\nbaz' xyz", 10, 18, 11, 4],
|
|
|
|
["<?php\nfoo 'bar\nbaz' xyz", 10, 18, 5, 4],
|
|
|
|
["<?php foo\n'\nbarbaz\n'\nxyz", 10, 19, 1, 1],
|
2015-04-18 12:51:26 +02:00
|
|
|
// Error over full string
|
2017-08-13 14:35:03 +02:00
|
|
|
["<?php", 0, 4, 1, 5],
|
|
|
|
["<?\nphp", 0, 5, 1, 3],
|
|
|
|
];
|
2015-04-18 12:51:26 +02:00
|
|
|
}
|
2015-04-14 20:31:06 +02:00
|
|
|
|
2015-05-01 20:17:39 +02:00
|
|
|
public function testNoColumnInfo() {
|
2015-04-14 20:31:06 +02:00
|
|
|
$error = new Error('Some error', 3);
|
|
|
|
|
2017-12-12 12:02:49 +01:00
|
|
|
$this->assertFalse($error->hasColumnInfo());
|
2015-04-18 12:51:26 +02:00
|
|
|
try {
|
|
|
|
$error->getStartColumn('');
|
|
|
|
$this->fail('Expected RuntimeException');
|
|
|
|
} catch (\RuntimeException $e) {
|
2015-05-01 20:17:39 +02:00
|
|
|
$this->assertSame('Error does not have column information', $e->getMessage());
|
2015-04-18 12:51:26 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
$error->getEndColumn('');
|
|
|
|
$this->fail('Expected RuntimeException');
|
|
|
|
} catch (\RuntimeException $e) {
|
2015-05-01 20:17:39 +02:00
|
|
|
$this->assertSame('Error does not have column information', $e->getMessage());
|
2015-04-18 12:51:26 +02:00
|
|
|
}
|
2015-04-14 20:31:06 +02:00
|
|
|
}
|
2015-05-01 20:17:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \RuntimeException
|
|
|
|
* @expectedExceptionMessage Invalid position information
|
|
|
|
*/
|
|
|
|
public function testInvalidPosInfo() {
|
2017-08-13 14:35:03 +02:00
|
|
|
$error = new Error('Some error', [
|
2015-05-01 20:17:39 +02:00
|
|
|
'startFilePos' => 10,
|
|
|
|
'endFilePos' => 11,
|
2017-08-13 14:35:03 +02:00
|
|
|
]);
|
2015-05-01 20:17:39 +02:00
|
|
|
$error->getStartColumn('code');
|
|
|
|
}
|
2015-04-14 20:31:06 +02:00
|
|
|
}
|