1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73:
<?php
namespace Guzzle\Tests\Plugin\Cache;
use Doctrine\Common\Cache\ArrayCache;
use Guzzle\Cache\DoctrineCacheAdapter;
use Guzzle\Common\Event;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Cache\CachePlugin;
use Guzzle\Plugin\Cache\CallbackCanCacheStrategy;
class CallbackCanCacheStrategyTest extends \Guzzle\Tests\GuzzleTestCase
{
public function testConstructorEnsuresCallbackIsCallable()
{
$p = new CallbackCanCacheStrategy(new \stdClass());
}
public function testUsesCallback()
{
$c = new CallbackCanCacheStrategy(function ($request) { return true; });
$this->assertTrue($c->canCacheRequest(new Request('DELETE', 'http://www.foo.com')));
}
public function testIntegrationWithCachePlugin()
{
$c = new CallbackCanCacheStrategy(
function ($request) { return true; },
function ($response) { return true; }
);
$request = new Request('DELETE', 'http://www.foo.com');
$response = Response::fromMessage(
"HTTP/1.1 200 OK\r\n"
. "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n"
. "Last-Modified: Wed, 09 Jan 2013 08:48:53 GMT\r\n"
. "Content-Length: 2\r\n"
. "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\r\n\r\n"
. "hi"
);
$this->assertTrue($c->canCacheRequest($request));
$this->assertTrue($c->canCacheResponse($response));
$s = $this->getMockBuilder('Guzzle\Plugin\Cache\DefaultCacheStorage')
->setConstructorArgs(array(new DoctrineCacheAdapter(new ArrayCache())))
->setMethods(array('fetch'))
->getMockForAbstractClass();
$s->expects($this->once())
->method('fetch')
->will($this->returnValue($response));
$plugin = new CachePlugin(array('can_cache' => $c, 'storage' => $s));
$plugin->onRequestBeforeSend(new Event(array('request' => $request)));
$this->assertEquals(200, $request->getResponse()->getStatusCode());
$this->assertEquals('hi', $request->getResponse()->getBody(true));
}
}