ErrorTest.php 3.5 KB

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