mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 12:10:10 +02:00
PHP7 Chain Of Responsibilities
This commit is contained in:
@@ -2,80 +2,50 @@
|
||||
|
||||
namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Tests;
|
||||
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Request;
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage;
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage;
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Handler;
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\HttpInMemoryCacheHandler;
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowDatabaseHandler;
|
||||
|
||||
/**
|
||||
* ChainTest tests the CoR.
|
||||
*/
|
||||
class ChainTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var FastStorage
|
||||
* @var Handler
|
||||
*/
|
||||
protected $chain;
|
||||
private $chain;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->chain = new FastStorage(array('bar' => 'baz'));
|
||||
$this->chain->append(new SlowStorage(array('bar' => 'baz', 'foo' => 'bar')));
|
||||
}
|
||||
|
||||
public function makeRequest()
|
||||
{
|
||||
$request = new Request();
|
||||
$request->verb = 'get';
|
||||
|
||||
return array(
|
||||
array($request),
|
||||
$this->chain = new HttpInMemoryCacheHandler(
|
||||
['/foo/bar?index=1' => 'Hello In Memory!'],
|
||||
new SlowDatabaseHandler()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider makeRequest
|
||||
*/
|
||||
public function testFastStorage($request)
|
||||
public function testCanRequestKeyInFastStorage()
|
||||
{
|
||||
$request->key = 'bar';
|
||||
$ret = $this->chain->handle($request);
|
||||
$uri = $this->createMock('Psr\Http\Message\UriInterface');
|
||||
$uri->method('getPath')->willReturn('/foo/bar');
|
||||
$uri->method('getQuery')->willReturn('index=1');
|
||||
|
||||
$this->assertTrue($ret);
|
||||
$this->assertObjectHasAttribute('response', $request);
|
||||
$this->assertEquals('baz', $request->response);
|
||||
// despite both handle owns the 'bar' key, the FastStorage is responding first
|
||||
$className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage';
|
||||
$this->assertEquals($className, $request->forDebugOnly);
|
||||
$request = $this->createMock('Psr\Http\Message\RequestInterface');
|
||||
$request->method('getMethod')
|
||||
->willReturn('GET');
|
||||
$request->method('getUri')->willReturn($uri);
|
||||
|
||||
$this->assertEquals('Hello In Memory!', $this->chain->handle($request));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider makeRequest
|
||||
*/
|
||||
public function testSlowStorage($request)
|
||||
public function testCanRequestKeyInSlowStorage()
|
||||
{
|
||||
$request->key = 'foo';
|
||||
$ret = $this->chain->handle($request);
|
||||
$uri = $this->createMock('Psr\Http\Message\UriInterface');
|
||||
$uri->method('getPath')->willReturn('/foo/baz');
|
||||
$uri->method('getQuery')->willReturn('');
|
||||
|
||||
$this->assertTrue($ret);
|
||||
$this->assertObjectHasAttribute('response', $request);
|
||||
$this->assertEquals('bar', $request->response);
|
||||
// FastStorage has no 'foo' key, the SlowStorage is responding
|
||||
$className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage';
|
||||
$this->assertEquals($className, $request->forDebugOnly);
|
||||
}
|
||||
$request = $this->createMock('Psr\Http\Message\RequestInterface');
|
||||
$request->method('getMethod')
|
||||
->willReturn('GET');
|
||||
$request->method('getUri')->willReturn($uri);
|
||||
|
||||
/**
|
||||
* @dataProvider makeRequest
|
||||
*/
|
||||
public function testFailure($request)
|
||||
{
|
||||
$request->key = 'kurukuku';
|
||||
$ret = $this->chain->handle($request);
|
||||
|
||||
$this->assertFalse($ret);
|
||||
// the last responsible :
|
||||
$className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage';
|
||||
$this->assertEquals($className, $request->forDebugOnly);
|
||||
$this->assertEquals('Hello World!', $this->chain->handle($request));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user