FrameCollectionTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\FrameCollection;
  8. use Whoops\TestCase;
  9. use Mockery as m;
  10. class FrameCollectionTest extends TestCase
  11. {
  12. /**
  13. * Stupid little counter for tagging frames
  14. * with a unique but predictable id
  15. * @var int
  16. */
  17. private $frameIdCounter = 0;
  18. /**
  19. * @return array
  20. */
  21. public function getFrameData()
  22. {
  23. $id = ++$this->frameIdCounter;
  24. return array(
  25. 'file' => __DIR__ . '/../../fixtures/frame.lines-test.php',
  26. 'line' => $id,
  27. 'function' => 'test-' . $id,
  28. 'class' => 'MyClass',
  29. 'args' => array(true, 'hello')
  30. );
  31. }
  32. /**
  33. * @param int $total
  34. * @return array
  35. */
  36. public function getFrameDataList($total)
  37. {
  38. $total = max((int) $total, 1);
  39. $self = $this;
  40. $frames = array_map(function() use($self) {
  41. return $self->getFrameData();
  42. }, range(1, $total));
  43. return $frames;
  44. }
  45. /**
  46. * @param array $frames
  47. * @return Whoops\Exception\FrameCollection
  48. */
  49. private function getFrameCollectionInstance($frames = null)
  50. {
  51. if($frames === null) {
  52. $frames = $this->getFrameDataList(10);
  53. }
  54. return new FrameCollection($frames);
  55. }
  56. /**
  57. * @covers Whoops\Exception\FrameCollection::filter
  58. * @covers Whoops\Exception\FrameCollection::count
  59. */
  60. public function testFilterFrames()
  61. {
  62. $frames = $this->getFrameCollectionInstance();
  63. // Filter out all frames with a line number under 6
  64. $frames->filter(function($frame) {
  65. return $frame->getLine() <= 5;
  66. });
  67. $this->assertCount(5, $frames);
  68. }
  69. /**
  70. * @covers Whoops\Exception\FrameCollection::map
  71. */
  72. public function testMapFrames()
  73. {
  74. $frames = $this->getFrameCollectionInstance();
  75. // Filter out all frames with a line number under 6
  76. $frames->map(function($frame) {
  77. $frame->addComment("This is cool", "test");
  78. return $frame;
  79. });
  80. $this->assertCount(10, $frames);
  81. }
  82. /**
  83. * @covers Whoops\Exception\FrameCollection::map
  84. * @expectedException UnexpectedValueException
  85. */
  86. public function testMapFramesEnforceType()
  87. {
  88. $frames = $this->getFrameCollectionInstance();
  89. // Filter out all frames with a line number under 6
  90. $frames->map(function($frame) {
  91. return "bajango";
  92. });
  93. }
  94. /**
  95. * @covers Whoops\Exception\FrameCollection::getArray
  96. */
  97. public function testGetArray()
  98. {
  99. $frames = $this->getFrameCollectionInstance();
  100. $frames = $frames->getArray();
  101. $this->assertCount(10, $frames);
  102. foreach($frames as $frame) {
  103. $this->assertInstanceOf('Whoops\\Exception\\Frame', $frame);
  104. }
  105. }
  106. /**
  107. * @covers Whoops\Exception\FrameCollection::getIterator
  108. */
  109. public function testCollectionIsIterable()
  110. {
  111. $frames = $this->getFrameCollectionInstance();
  112. foreach($frames as $frame) {
  113. $this->assertInstanceOf('Whoops\\Exception\\Frame', $frame);
  114. }
  115. }
  116. /**
  117. * @covers Whoops\Exception\FrameCollection::serialize
  118. * @covers Whoops\Exception\FrameCollection::unserialize
  119. */
  120. public function testCollectionIsSerializable()
  121. {
  122. $frames = $this->getFrameCollectionInstance();
  123. $serializedFrames = serialize($frames);
  124. $newFrames = unserialize($serializedFrames);
  125. foreach($newFrames as $frame) {
  126. $this->assertInstanceOf('Whoops\\Exception\\Frame', $frame);
  127. }
  128. }
  129. }