Created
September 30, 2015 13:12
-
-
Save AndrewCarterUK/de97ff25ea65e66db5d2 to your computer and use it in GitHub Desktop.
PSR-6 Pool::resolveItem() Testing Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyClass | |
{ | |
private $pool; | |
public function __construct(CacheItemPoolInterface $pool) { $this->pool = $pool; } | |
public function getDataA($param) | |
{ | |
return $this->pool->resolveItem( | |
'item-key', | |
function () use ($param) { return resultOfComputation($param); }, | |
function ($item) { $item->expiresAfter(300); } | |
)->get(); | |
} | |
public function getDataB($param) | |
{ | |
$item = $this->pool->getItem('item-key'); | |
if (!$item->isHit()) { | |
$item->set(resultOfComputation($param)); | |
$item->expiresAfter(300); | |
$this->pool->save($item); | |
} | |
return $item->get(); | |
} | |
} | |
class MyClassTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testGetDataA() | |
{ | |
$param = 'jim'; | |
$expectedResult = 'bob'; | |
$item = $this->getMock('...'); | |
$item->expects($this->once())->method('expiresAfter')->with(300); | |
$item->expects($this->once())->method('get')->willReturn('foobar'); | |
$pool = $this->getMock('...'); | |
$pool | |
->expects($this->once()) | |
->method('resolveItem') | |
->returnCallback(function($key, $valueCallback, $metaCallback) use ($item, $expectedResult) { | |
$this->assertEquals('item-key', $key); | |
$this->assertEquals($expectedResult, call_user_func($valueCallback)); | |
call_user_func($metaCallback, $item); | |
return $item; | |
}); | |
$object = new MyClass($pool); | |
$this->assertEquals('foobar', $object->getDataA($param)); | |
} | |
public function testGetDataB() | |
{ | |
$param = 'jim'; | |
$expectedResult = 'bob'; | |
$hitItem = $this->getMock('...'); | |
$hitItem->expects($this->once())->method('isHit')->willReturn(true); | |
$hitItem->expects($this->once())->method('get')->willReturn('foobarhit'); | |
$hitPool = $this->getMock('...'); | |
$hitPool->expects($this->once())->method('getItem')->with('item-key')->willReturn($hitItem); | |
$hitObject = new MyClass($hitPool); | |
$this->assertEquals('foobarhit', $hitObject->getDataB($param)); | |
$missItem = $this->getMock('...'); | |
$missItem->expects($this->once())->method('isHit')->willReturn(false); | |
$missItem->expects($this->once())->method('set')->with($expectedResult); | |
$missItem->expects($this->once())->method('expiresAfter')->with(300); | |
$missItem->expects($this->once())->method('get')->willReturn('foobarmiss'); | |
$missPool = $this->getMock('...'); | |
$missPool->expects($this->once())->method('getItem')->with('item-key')->willReturn($missItem); | |
$missPool->expects($this->once())->method('save')->with($missItem); | |
$missObject = new MyClass($missPool); | |
$this->assertEquals('foobarmiss', $missObject->getDataB($param)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment