From 08a860fc2d45a5138ff3916970d0968b4e311843 Mon Sep 17 00:00:00 2001 From: filp Date: Tue, 12 Mar 2013 11:36:52 +0000 Subject: [PATCH] Remove Mock classes, use Mockery --- tests/Damnit/Mock/MockHandler.php | 43 ------------- tests/Damnit/RunTest.php | 101 +++++++++++++++++------------- 2 files changed, 58 insertions(+), 86 deletions(-) delete mode 100644 tests/Damnit/Mock/MockHandler.php diff --git a/tests/Damnit/Mock/MockHandler.php b/tests/Damnit/Mock/MockHandler.php deleted file mode 100644 index cdf4b3f..0000000 --- a/tests/Damnit/Mock/MockHandler.php +++ /dev/null @@ -1,43 +0,0 @@ - - */ - -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; - } -} diff --git a/tests/Damnit/RunTest.php b/tests/Damnit/RunTest.php index ed5b109..9107635 100644 --- a/tests/Damnit/RunTest.php +++ b/tests/Damnit/RunTest.php @@ -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()); } }