ErrorTest.php 3.6 KB

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