mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
start a restructure
This commit is contained in:
78
Behavioral/ChainOfResponsibilities/Tests/ChainTest.php
Normal file
78
Behavioral/ChainOfResponsibilities/Tests/ChainTest.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Behavioral\Tests\ChainOfResponsibilities;
|
||||
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Request;
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage;
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage;
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;
|
||||
|
||||
/**
|
||||
* ChainTest tests the CoR
|
||||
*/
|
||||
class ChainTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var FastStorage
|
||||
*/
|
||||
protected $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)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider makeRequest
|
||||
*/
|
||||
public function testFastStorage($request)
|
||||
{
|
||||
$request->key = 'bar';
|
||||
$ret = $this->chain->handle($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\Behavioral\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\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage', $request->forDebugOnly);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider makeRequest
|
||||
*/
|
||||
public function testFailure($request)
|
||||
{
|
||||
$request->key = 'kurukuku';
|
||||
$ret = $this->chain->handle($request);
|
||||
|
||||
$this->assertFalse($ret);
|
||||
// the last responsible :
|
||||
$this->assertEquals('DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage', $request->forDebugOnly);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user