FrameTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Whoops - php errors for cool kids
  4. * @author Filipe Dobreira <http://github.com/filp>
  5. */
  6. namespace Whoops\Exception;
  7. use Whoops\Exception\Frame;
  8. use Whoops\TestCase;
  9. use Mockery as m;
  10. class FrameTest extends TestCase
  11. {
  12. /**
  13. * @return array
  14. */
  15. private function getFrameData()
  16. {
  17. return array(
  18. 'file' => __DIR__ . '/../../fixtures/frame.lines-test.php',
  19. 'line' => 0,
  20. 'function' => 'test',
  21. 'class' => 'MyClass',
  22. 'args' => array(true, 'hello')
  23. );
  24. }
  25. /**
  26. * @param array $data
  27. * @return Whoops\Exception\Frame
  28. */
  29. private function getFrameInstance($data = null)
  30. {
  31. if($data === null) {
  32. $data = $this->getFrameData();
  33. }
  34. return new Frame($data);
  35. }
  36. /**
  37. * @covers Whoops\Exception\Frame::getFile
  38. */
  39. public function testGetFile()
  40. {
  41. $data = $this->getFrameData();
  42. $frame = $this->getFrameInstance($data);
  43. $this->assertEquals($frame->getFile(), $data['file']);
  44. }
  45. /**
  46. * @covers Whoops\Exception\Frame::getLine
  47. */
  48. public function testGetLine()
  49. {
  50. $data = $this->getFrameData();
  51. $frame = $this->getFrameInstance($data);
  52. $this->assertEquals($frame->getLine(), $data['line']);
  53. }
  54. /**
  55. * @covers Whoops\Exception\Frame::getClass
  56. */
  57. public function testGetClass()
  58. {
  59. $data = $this->getFrameData();
  60. $frame = $this->getFrameInstance($data);
  61. $this->assertEquals($frame->getClass(), $data['class']);
  62. }
  63. /**
  64. * @covers Whoops\Exception\Frame::getFunction
  65. */
  66. public function testGetFunction()
  67. {
  68. $data = $this->getFrameData();
  69. $frame = $this->getFrameInstance($data);
  70. $this->assertEquals($frame->getFunction(), $data['function']);
  71. }
  72. /**
  73. * @covers Whoops\Exception\Frame::getArgs
  74. */
  75. public function testGetArgs()
  76. {
  77. $data = $this->getFrameData();
  78. $frame = $this->getFrameInstance($data);
  79. $this->assertEquals($frame->getArgs(), $data['args']);
  80. }
  81. /**
  82. * @covers Whoops\Exception\Frame::getFileContents
  83. */
  84. public function testGetFileContents()
  85. {
  86. $data = $this->getFrameData();
  87. $frame = $this->getFrameInstance($data);
  88. $this->assertEquals($frame->getFileContents(), file_get_contents($data['file']));
  89. }
  90. /**
  91. * @covers Whoops\Exception\Frame::getFileLines
  92. */
  93. public function testGetFileLines()
  94. {
  95. $data = $this->getFrameData();
  96. $frame = $this->getFrameInstance($data);
  97. $lines = explode("\n", $frame->getFileContents());
  98. $this->assertEquals($frame->getFileLines(), $lines);
  99. }
  100. /**
  101. * @covers Whoops\Exception\Frame::getFileLines
  102. */
  103. public function testGetFileLinesRange()
  104. {
  105. $data = $this->getFrameData();
  106. $frame = $this->getFrameInstance($data);
  107. $lines = $frame->getFileLines(0, 3);
  108. $this->assertEquals($lines[0], '<?php');
  109. $this->assertEquals($lines[1], '// Line 2');
  110. $this->assertEquals($lines[2], '// Line 3');
  111. }
  112. /**
  113. * @covers Whoops\Exception\Frame::addComment
  114. * @covers Whoops\Exception\Frame::getComments
  115. */
  116. public function testGetComments()
  117. {
  118. $frame = $this->getFrameInstance();
  119. $testComments = array(
  120. 'Dang, yo!',
  121. 'Errthangs broken!',
  122. 'Dayumm!'
  123. );
  124. $frame->addComment($testComments[0]);
  125. $frame->addComment($testComments[1]);
  126. $frame->addComment($testComments[2]);
  127. $comments = $frame->getComments();
  128. $this->assertCount(3, $comments);
  129. $this->assertEquals($comments[0]['comment'], $testComments[0]);
  130. $this->assertEquals($comments[1]['comment'], $testComments[1]);
  131. $this->assertEquals($comments[2]['comment'], $testComments[2]);
  132. }
  133. /**
  134. * @covers Whoops\Exception\Frame::addComment
  135. * @covers Whoops\Exception\Frame::getComments
  136. */
  137. public function testGetFilteredComments()
  138. {
  139. $frame = $this->getFrameInstance();
  140. $testComments = array(
  141. array('Dang, yo!', 'test'),
  142. array('Errthangs broken!', 'test'),
  143. 'Dayumm!'
  144. );
  145. $frame->addComment($testComments[0][0], $testComments[0][1]);
  146. $frame->addComment($testComments[1][0], $testComments[1][1]);
  147. $frame->addComment($testComments[2][0], $testComments[2][1]);
  148. $comments = $frame->getComments('test');
  149. $this->assertCount(2, $comments);
  150. $this->assertEquals($comments[0]['comment'], $testComments[0][0]);
  151. $this->assertEquals($comments[1]['comment'], $testComments[1][0]);
  152. }
  153. /**
  154. * @covers Whoops\Exception\Frame::serialize
  155. * @covers Whoops\Exception\Frame::unserialize
  156. */
  157. public function testFrameIsSerializable()
  158. {
  159. $data = $this->getFrameData();
  160. $frame = $this->getFrameInstance();
  161. $commentText = "Gee I hope this works";
  162. $commentContext = "test";
  163. $frame->addComment($commentText, $commentContext);
  164. $serializedFrame = serialize($frame);
  165. $newFrame = unserialize($serializedFrame);
  166. $this->assertInstanceOf('Whoops\\Exception\\Frame', $newFrame);
  167. $this->assertEquals($newFrame->getFile(), $data['file']);
  168. $this->assertEquals($newFrame->getLine(), $data['line']);
  169. $comments = $newFrame->getComments();
  170. $this->assertCount(1, $comments);
  171. $this->assertEquals($comments[0]["comment"], $commentText);
  172. $this->assertEquals($comments[0]["context"], $commentContext);
  173. }
  174. }