ErrorTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace PhpParser;
  3. class ErrorTest extends \PHPUnit_Framework_TestCase
  4. {
  5. public function testConstruct() {
  6. $attributes = array(
  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(10, $error->getRawLine());
  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. $error->setRawLine(17);
  29. $this->assertSame(17, $error->getRawLine());
  30. $this->assertSame('Some other error on line 17', $error->getMessage());
  31. }
  32. public function testUnknownLine() {
  33. $error = new Error('Some error');
  34. $this->assertSame(-1, $error->getStartLine());
  35. $this->assertSame(-1, $error->getEndLine());
  36. $this->assertSame(-1, $error->getRawLine());
  37. $this->assertSame('Some error on unknown line', $error->getMessage());
  38. }
  39. /** @dataProvider provideTestColumnInfo */
  40. public function testColumnInfo($code, $startPos, $endPos, $startColumn, $endColumn) {
  41. $error = new Error('Some error', array(
  42. 'startFilePos' => $startPos,
  43. 'endFilePos' => $endPos,
  44. ));
  45. $this->assertSame(true, $error->hasColumnInfo());
  46. $this->assertSame($startColumn, $error->getStartColumn($code));
  47. $this->assertSame($endColumn, $error->getEndColumn($code));
  48. }
  49. public function provideTestColumnInfo() {
  50. return array(
  51. // Error at "bar"
  52. array("<?php foo bar baz", 10, 12, 11, 13),
  53. array("<?php\nfoo bar baz", 10, 12, 5, 7),
  54. array("<?php foo\nbar baz", 10, 12, 1, 3),
  55. array("<?php foo bar\nbaz", 10, 12, 11, 13),
  56. array("<?php\r\nfoo bar baz", 11, 13, 5, 7),
  57. // Error at "baz"
  58. array("<?php foo bar baz", 14, 16, 15, 17),
  59. array("<?php foo bar\nbaz", 14, 16, 1, 3),
  60. // Error at string literal
  61. array("<?php foo 'bar\nbaz' xyz", 10, 18, 11, 4),
  62. array("<?php\nfoo 'bar\nbaz' xyz", 10, 18, 5, 4),
  63. array("<?php foo\n'\nbarbaz\n'\nxyz", 10, 19, 1, 1),
  64. // Error over full string
  65. array("<?php", 0, 4, 1, 5),
  66. array("<?\nphp", 0, 5, 1, 3),
  67. );
  68. }
  69. public function testNoColumnInfo() {
  70. $error = new Error('Some error', 3);
  71. $this->assertSame(false, $error->hasColumnInfo());
  72. try {
  73. $error->getStartColumn('');
  74. $this->fail('Expected RuntimeException');
  75. } catch (\RuntimeException $e) {
  76. $this->assertSame('Error does not have column information', $e->getMessage());
  77. }
  78. try {
  79. $error->getEndColumn('');
  80. $this->fail('Expected RuntimeException');
  81. } catch (\RuntimeException $e) {
  82. $this->assertSame('Error does not have column information', $e->getMessage());
  83. }
  84. }
  85. /**
  86. * @expectedException \RuntimeException
  87. * @expectedExceptionMessage Invalid position information
  88. */
  89. public function testInvalidPosInfo() {
  90. $error = new Error('Some error', array(
  91. 'startFilePos' => 10,
  92. 'endFilePos' => 11,
  93. ));
  94. $error->getStartColumn('code');
  95. }
  96. }