Add FrameCollection::map & tests

This commit is contained in:
filp
2013-05-10 20:26:37 +01:00
parent 63e5049ee1
commit f563bfd939
2 changed files with 99 additions and 13 deletions
+38 -7
View File
@@ -6,7 +6,7 @@
namespace Whoops\Exception;
use Whoops\Exception\Frame;
use InvalidArgumentException;
use UnexpectedValueException;
use IteratorAggregate;
use ArrayIterator;
use Serializable;
@@ -42,16 +42,47 @@ class FrameCollection implements IteratorAggregate, Serializable, Countable
*/
public function filter($callable)
{
if(!is_callable($callable)) {
throw new InvalidArgumentException(
__METHOD__ . " expects a callable, like function($frame) { return bool; }"
);
}
$this->frames = array_filter($this->frames, $callable);
return $this;
}
/**
* Map the collection of frames
*
* @param callable $callable
* @return Whoops\Exception\FrameCollection
*/
public function map($callable)
{
// Contain the map within a higher-order callable
// that enforces type-correctness for the $callable
$this->frames = array_map(function($frame) use($callable) {
$frame = call_user_func($callable, $frame);
if(!$frame instanceof Frame) {
throw new UnexpectedValueException(
"Callable to " . __METHOD__ . " must return a Frame object"
);
}
return $frame;
}, $this->frames);
}
/**
* Returns an array with all frames, does not affect
* the internal array.
*
* @todo If this gets any more complex than this,
* have getIterator use this method.
* @see Whoops\Exception\FrameCollection::getIterator
* @return array
*/
public function getArray()
{
return $this->frames;
}
/**
* @see IteratorAggregate::getIterator
* @return ArrayIterator
+61 -6
View File
@@ -33,6 +33,21 @@ class FrameCollectionTest extends TestCase
);
}
/**
* @param int $total
* @return array
*/
public function getFrameDataList($total)
{
$total = max((int) $total, 1);
$self = $this;
$frames = array_map(function() use($self) {
return $self->getFrameData();
}, range(1, $total));
return $frames;
}
/**
* @param array $frames
* @return Whoops\Exception\FrameCollection
@@ -40,12 +55,7 @@ class FrameCollectionTest extends TestCase
private function getFrameCollectionInstance($frames = null)
{
if($frames === null) {
$self = $this;
// Get 10 frames
$frames = array_map(function() use($self) {
return $self->getFrameData();
}, range(1, 10));
$frames = $this->getFrameDataList(10);
}
return new FrameCollection($frames);
@@ -67,6 +77,51 @@ class FrameCollectionTest extends TestCase
$this->assertCount(5, $frames);
}
/**
* @covers Whoops\Exception\FrameCollection::map
*/
public function testMapFrames()
{
$frames = $this->getFrameCollectionInstance();
// Filter out all frames with a line number under 6
$frames->map(function($frame) {
$frame->addComment("This is cool", "test");
return $frame;
});
$this->assertCount(10, $frames);
}
/**
* @covers Whoops\Exception\FrameCollection::map
* @expectedException UnexpectedValueException
*/
public function testMapFramesEnforceType()
{
$frames = $this->getFrameCollectionInstance();
// Filter out all frames with a line number under 6
$frames->map(function($frame) {
return "bajango";
});
}
/**
* @covers Whoops\Exception\FrameCollection::getArray
*/
public function testGetArray()
{
$frames = $this->getFrameCollectionInstance();
$frames = $frames->getArray();
$this->assertCount(10, $frames);
foreach($frames as $frame) {
$this->assertInstanceOf('Whoops\\Exception\\Frame', $frame);
}
}
/**
* @covers Whoops\Exception\FrameCollection::getIterator
*/