ErrorTest.php 3.5 KB

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