diff --git a/ChainOfResponsibilities/Handler.php b/ChainOfResponsibilities/Handler.php index 7511221..907e6a9 100644 --- a/ChainOfResponsibilities/Handler.php +++ b/ChainOfResponsibilities/Handler.php @@ -51,6 +51,7 @@ abstract class Handler */ final public function handle(Request $req) { + $req->forDebugOnly = get_called_class(); $processed = $this->processing($req); if (!$processed) { // the request has not been processed by this handler => see the next diff --git a/Tests/ChainOfResponsibilities/ChainTest.php b/Tests/ChainOfResponsibilities/ChainTest.php index 6a9fa99..337559b 100644 --- a/Tests/ChainOfResponsibilities/ChainTest.php +++ b/Tests/ChainOfResponsibilities/ChainTest.php @@ -23,14 +23,56 @@ class ChainTest extends \PHPUnit_Framework_TestCase $this->chain->append(new Responsible\SlowStorage(array('bar' => 'baz', 'foo' => 'bar'))); } - public function testProcess() + public function makeRequest() { $request = new Request(); $request->verb = 'get'; - $request->key = 'bar'; + return array( + array($request) + ); + } + /** + * @dataProvider makeRequest + */ + public function testFastStorage($request) + { + $request->key = 'bar'; $ret = $this->chain->handle($request); - print_r($request); + + $this->assertTrue($ret); + $this->assertObjectHasAttribute('response', $request); + $this->assertEquals('baz', $request->response); + // despite both handle owns the 'bar' key, the FastStorage is responding first + $this->assertEquals('DesignPatterns\ChainOfResponsibilities\Responsible\FastStorage', $request->forDebugOnly); + } + + /** + * @dataProvider makeRequest + */ + public function testSlowStorage($request) + { + $request->key = 'foo'; + $ret = $this->chain->handle($request); + + $this->assertTrue($ret); + $this->assertObjectHasAttribute('response', $request); + $this->assertEquals('bar', $request->response); + // FastStorage has no 'foo' key, the SlowStorage is responding + $this->assertEquals('DesignPatterns\ChainOfResponsibilities\Responsible\SlowStorage', $request->forDebugOnly); + } + + /** + * @dataProvider makeRequest + */ + public function testFailure($request) + { + $request->key = 'kurukuku'; + $ret = $this->chain->handle($request); + + $this->assertFalse($ret); + // the last rsponsible : + $this->assertEquals('DesignPatterns\ChainOfResponsibilities\Responsible\SlowStorage', $request->forDebugOnly); } } \ No newline at end of file