Remove Mock classes, use Mockery

This commit is contained in:
filp
2013-03-12 11:36:52 +00:00
parent 546a75a714
commit 08a860fc2d
2 changed files with 58 additions and 86 deletions
-43
View File
@@ -1,43 +0,0 @@
<?php
/**
* Damnit - php errors for cool kids
* @author Filipe Dobreira <http://github.com/filp>
*/
namespace Damnit\Mock;
use Damnit\Handler\Handler;
class MockHandler extends Handler
{
/**
* @var \Exception[]
*/
public $exceptions = array();
/**
* @var callable
*/
protected $onHandleCallable;
/**
* @param \Exception
* @return int|null
*/
public function handle(\Exception $exception)
{
$this->exceptions[] = $exception;
if($this->onHandleCallable) {
return call_user_func($this->onHandleCallable, $exception);
}
}
/**
* Set a callable to be executed when MockHandler::handle
* is executed. The callable receives the exception passed
* to the method.
*/
public function onHandle($callable)
{
$this->onHandleCallable = $callable;
}
}
+58 -43
View File
@@ -6,11 +6,11 @@
namespace Damnit;
use Damnit\TestCase;
use Damnit\Mock\MockHandler;
use Damnit\Run;
use Damnit\Handler\Handler;
use \RuntimeException;
use \ArrayObject;
use \Mockery as m;
class RunTest extends TestCase
{
@@ -22,6 +22,23 @@ class RunTest extends TestCase
return new Run;
}
/**
* @return Damnit\Handler\Handler
*/
protected function getHandler()
{
return m::mock('Damnit\\Handler\\Handler');
}
/**
* @param string $message
* @return Exception
*/
protected function getException($message = null)
{
return m::mock('Exception', array($message));
}
/**
* @covers Damnit\Run::clearHandlers
*/
@@ -43,8 +60,8 @@ class RunTest extends TestCase
$run = $this->getRunInstance();
$run->clearHandlers();
$handlerOne = new MockHandler;
$handlerTwo = new MockHandler;
$handlerOne = $this->getHandler();
$handlerTwo = $this->getHandler();
$run->pushHandler($handlerOne);
$run->pushHandler($handlerTwo);
@@ -64,9 +81,9 @@ class RunTest extends TestCase
{
$run = $this->getRunInstance();
$handlerOne = new MockHandler;
$handlerTwo = new MockHandler;
$handlerThree = new MockHandler;
$handlerOne = $this->getHandler();
$handlerTwo = $this->getHandler();
$handlerThree = $this->getHandler();
$run->pushHandler($handlerOne);
$run->pushHandler($handlerTwo);
@@ -95,28 +112,28 @@ class RunTest extends TestCase
$run = $this->getRunInstance();
$run->register();
$handler = new MockHandler;
$handler = $this->getHandler();
$run->pushHandler($handler);
throw new RuntimeException('Hi! :)');
throw $this->getException();
$this->assertCount(2, $handler->exceptions);
}
/**
* @covers Damnit\Run::unregister
* @expectedException RuntimeException
* @expectedException Exception
*/
public function testUnregisterHandler()
{
$run = $this->getRunInstance();
$run->register();
$handler = new MockHandler;
$handler = $this->getHandler();
$run->pushHandler($handler);
$run->unregister();
throw new RuntimeException("I'm not supposed to be caught!");
throw $this->getException("I'm not supposed to be caught!");
}
/**
@@ -127,10 +144,10 @@ class RunTest extends TestCase
{
$run = $this->getRunInstance();
$handlerOne = new MockHandler;
$handlerTwo = new MockHandler;
$handlerThree = new MockHandler;
$handlerFour = new MockHandler;
$handlerOne = $this->getHandler();
$handlerTwo = $this->getHandler();
$handlerThree = $this->getHandler();
$handlerFour = $this->getHandler();
$run->pushHandler($handlerOne);
$run->pushHandler($handlerTwo);
@@ -152,36 +169,30 @@ class RunTest extends TestCase
*/
public function testHandlersGonnaHandle()
{
$run = $this->getRunInstance();
$exception = new RuntimeException;
$handlerOrder = new ArrayObject;
$run = $this->getRunInstance();
$exception = $this->getException();
$order = new ArrayObject;
$handlerOne = new MockHandler;
$handlerTwo = new MockHandler;
$handlerThree = new MockHandler;
$handlerFour = new MockHandler;
$handlerOne = $this->getHandler();
$handlerTwo = $this->getHandler();
$handlerThree = $this->getHandler();
$handlerOne ->onHandle(function() use($handlerOrder) {$handlerOrder[] = 1;});
$handlerTwo ->onHandle(function() use($handlerOrder) {$handlerOrder[] = 2;});
$handlerThree ->onHandle(function() use($handlerOrder) {$handlerOrder[] = 3;});
$handlerFour ->onHandle(function() use($handlerOrder) {$handlerOrder[] = 4;});
$handlerOne->shouldReceive('handle')
->andReturnUsing(function() use($order) { $order[] = 1; });
$handlerTwo->shouldReceive('handle')
->andReturnUsing(function() use($order) { $order[] = 2; });
$handlerThree->shouldReceive('handle')
->andReturnUsing(function() use($order) { $order[] = 3; });
$run->pushHandler($handlerOne);
$run->pushHandler($handlerTwo);
$run->pushHandler($handlerThree);
$run->pushHandler($handlerFour);
// Get an exception to be handled, and verify that the handlers
// are given the handler, and in the inverse order they were
// registered.
$run->handleException($exception);
$this->assertContains($exception, $handlerOne->exceptions);
$this->assertContains($exception, $handlerTwo->exceptions);
$this->assertContains($exception, $handlerThree->exceptions);
$this->assertContains($exception, $handlerFour->exceptions);
$this->assertEquals((array) $handlerOrder, array(4, 3, 2, 1));
$this->assertEquals((array) $order, array(3, 2, 1));
}
/**
@@ -191,21 +202,25 @@ class RunTest extends TestCase
{
$run = $this->getRunInstance();
$handlerOne = new MockHandler;
$handlerTwo = new MockHandler;
$handlerOne = $this->getHandler();
$handlerTwo = $this->getHandler();
$run->pushHandler($handlerOne);
$run->pushHandler($handlerTwo);
$test = $this;
$handlerOne->onHandle(function() use($test) {
$test->fail('$handlerOne should not be called to handle an exception');
});
$handlerOne
->shouldReceive('handle')
->andReturnUsing(function () use($test) {
$test->fail('$handlerOne should not be called');
})
;
$handlerTwo->onHandle(function() {
return Handler::LAST_HANDLER;
});
$handlerTwo
->shouldReceive('handle')
->andReturn(Handler::LAST_HANDLER)
;
$run->handleException(new RuntimeException);
$run->handleException($this->getException());
}
}