$serviceBuilder
$serviceBuilder :
Base testcase class for all Guzzle testcases.
getServer() : \Guzzle\Tests\Http\Server
Get the global server object used throughout the unit tests of Guzzle
setServiceBuilder(\Guzzle\Service\Builder\ServiceBuilderInterface $builder)
Set the service builder to use for tests
\Guzzle\Service\Builder\ServiceBuilderInterface | $builder | Service builder |
getServiceBuilder() : \Guzzle\Service\Builder\ServiceBuilder
Get a service builder object that can be used throughout the service tests
getWildcardObserver(\Guzzle\Common\HasDispatcherInterface $hasDispatcher) : \Guzzle\Tests\Mock\MockObserver
Get a wildcard observer for an event dispatcher
\Guzzle\Common\HasDispatcherInterface | $hasDispatcher |
setMockBasePath(string $path) : \Guzzle\Tests\GuzzleTestCase
Set the mock response base path
string | $path | Path to mock response folder |
addMockedRequest(\Guzzle\Http\Message\RequestInterface $request) : self
Mark a request as being mocked
\Guzzle\Http\Message\RequestInterface | $request |
getMockResponse(string $path) : \Guzzle\Http\Message\Response
Get a mock response for a client by mock file name
string | $path | Relative path to the mock response file |
setMockResponse(\Guzzle\Http\Client $client, string $paths) : \Guzzle\Plugin\Mock\MockPlugin
Set a mock response from a mock file on the next client request.
This method assumes that mock response files are located under the Command/Mock/ directory of the Service being tested (e.g. Unfuddle/Command/Mock/). A mock response is added to the next request sent by the client.
\Guzzle\Http\Client | $client | Client object to modify |
string | $paths | Path to files within the Mock folder of the service |
returns the created mock plugin
compareHeaders(array $filteredHeaders, array $actualHeaders) : array|boolean
Compare HTTP headers and use special markup to filter values A header prefixed with '!' means it must not exist A header prefixed with '_' means it must be ignored A header value of '*' means anything after the * will be ignored
array | $filteredHeaders | Array of special headers |
array | $actualHeaders | Array of headers to check against |
Returns an array of the differences or FALSE if none
hasSubscriber(\Guzzle\Common\HasDispatcherInterface $dispatcher, \Symfony\Component\EventDispatcher\EventSubscriberInterface $subscriber) : boolean
Check if an event dispatcher has a subscriber
\Guzzle\Common\HasDispatcherInterface | $dispatcher | |
\Symfony\Component\EventDispatcher\EventSubscriberInterface | $subscriber |
<?php
namespace Guzzle\Tests\Plugin\Cache;
use Guzzle\Cache\DoctrineCacheAdapter;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\RequestFactory;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Cache\DefaultCacheStorage;
use Doctrine\Common\Cache\ArrayCache;
/**
* @covers Guzzle\Plugin\Cache\DefaultCacheStorage
*/
class DefaultCacheStorageTest extends \Guzzle\Tests\GuzzleTestCase
{
protected function getCache()
{
$a = new ArrayCache();
$c = new DoctrineCacheAdapter($a);
$s = new DefaultCacheStorage($c);
$request = new Request('GET', 'http://foo.com', array('Accept' => 'application/json'));
$response = new Response(200, array(
'Content-Type' => 'application/json',
'Connection' => 'close',
'X-Foo' => 'Bar',
'Vary' => 'Accept'
), 'test');
$s->cache($request, $response);
$data = $this->readAttribute($a, 'data');
return array(
'cache' => $a,
'adapter' => $c,
'storage' => $s,
'request' => $request,
'response' => $response,
'serialized' => end($data)
);
}
public function testReturnsNullForCacheMiss()
{
$cache = $this->getCache();
$this->assertNull($cache['storage']->fetch(new Request('GET', 'http://test.com')));
}
public function testCachesRequests()
{
$cache = $this->getCache();
$foundRequest = $foundBody = $bodyKey = false;
foreach ($this->readAttribute($cache['cache'], 'data') as $key => $v) {
if (strpos($v, 'foo.com')) {
$foundRequest = true;
$data = unserialize($v);
$bodyKey = $data[0][3];
$this->assertInternalType('integer', $data[0][4]);
$this->assertFalse(isset($data[0][0]['connection']));
$this->assertEquals('foo.com', $data[0][0]['host']);
} elseif ($v == 'test') {
$foundBody = $key;
}
}
$this->assertContains($bodyKey, $foundBody);
$this->assertTrue($foundRequest);
}
public function testFetchesResponse()
{
$cache = $this->getCache();
$response = $cache['storage']->fetch($cache['request']);
$this->assertEquals(200, $response->getStatusCode());
$this->assertFalse($response->hasHeader('Connection'));
$this->assertEquals('Bar', (string) $response->getHeader('X-Foo'));
$this->assertEquals('test', (string) $response->getBody());
$this->assertTrue(in_array($cache['serialized'], $this->readAttribute($cache['cache'], 'data')));
}
public function testDeletesRequestItemsAndBody()
{
$cache = $this->getCache();
$cache['storage']->delete($cache['request']);
$this->assertFalse(in_array('test', $this->readAttribute($cache['cache'], 'data')));
$this->assertFalse(in_array($cache['serialized'], $this->readAttribute($cache['cache'], 'data')));
}
public function testCachesMultipleRequestsWithVary()
{
$cache = $this->getCache();
$cache['request']->setHeader('Accept', 'application/xml');
$response = $cache['response']->setHeader('Content-Type', 'application/xml');
$response->setBody('123');
$cache['storage']->cache($cache['request'], $response);
$data = $this->readAttribute($cache['cache'], 'data');
foreach ($data as $v) {
if (strpos($v, 'foo.com')) {
$u = unserialize($v);
$this->assertEquals(2, count($u));
$this->assertEquals($u[0][0]['accept'], 'application/xml');
$this->assertEquals($u[0][1]['content-type'], 'application/xml');
$this->assertEquals($u[1][0]['accept'], 'application/json');
$this->assertEquals($u[1][1]['content-type'], 'application/json');
$this->assertNotSame($u[0][3], $u[1][3]);
break;
}
}
}
public function testPurgeRemovesAllMethodCaches()
{
$cache = $this->getCache();
foreach (array('HEAD', 'POST', 'PUT', 'DELETE') as $method) {
$request = RequestFactory::getInstance()->cloneRequestWithMethod($cache['request'], $method);
$cache['storage']->cache($request, $cache['response']);
}
$cache['storage']->purge('http://foo.com');
$this->assertFalse(in_array('test', $this->readAttribute($cache['cache'], 'data')));
$this->assertFalse(in_array($cache['serialized'], $this->readAttribute($cache['cache'], 'data')));
$this->assertEquals(
array('DoctrineNamespaceCacheKey[]'),
array_keys($this->readAttribute($cache['cache'], 'data'))
);
}
public function testRemovesExpiredResponses()
{
$cache = $this->getCache();
$request = new Request('GET', 'http://xyz.com');
$response = new Response(200, array('Age' => 1000, 'Cache-Control' => 'max-age=-10000'));
$cache['storage']->cache($request, $response);
$this->assertNull($cache['storage']->fetch($request));
$data = $this->readAttribute($cache['cache'], 'data');
$this->assertFalse(in_array('xyz.com', $data));
$this->assertTrue(in_array($cache['serialized'], $data));
}
public function testUsesVaryToDetermineResult()
{
$cache = $this->getCache();
$this->assertInstanceOf('Guzzle\Http\Message\Response', $cache['storage']->fetch($cache['request']));
$request = new Request('GET', 'http://foo.com', array('Accept' => 'application/xml'));
$this->assertNull($cache['storage']->fetch($request));
}
public function testEnsuresResponseIsStillPresent()
{
$cache = $this->getCache();
$data = $this->readAttribute($cache['cache'], 'data');
$key = array_search('test', $data);
$cache['cache']->delete(substr($key, 1, -4));
$this->assertNull($cache['storage']->fetch($cache['request']));
}
public function staleProvider()
{
return array(
array(
new Request('GET', 'http://foo.com', array('Accept' => 'foo')),
new Response(200, array('Cache-Control' => 'stale-if-error=100', 'Vary' => 'Accept'))
),
array(
new Request('GET', 'http://foo.com', array('Accept' => 'foo')),
new Response(200, array('Cache-Control' => 'stale-if-error', 'Vary' => 'Accept'))
)
);
}
/**
* @dataProvider staleProvider
*/
public function testUsesStaleTimeDirectiveForTtd($request, $response)
{
$cache = $this->getCache();
$cache['storage']->cache($request, $response);
$data = $this->readAttribute($cache['cache'], 'data');
foreach ($data as $v) {
if (strpos($v, 'foo.com')) {
$u = unserialize($v);
$this->assertGreaterThan($u[1][4], $u[0][4]);
break;
}
}
}
public function testCanFilterCacheKeys()
{
$cache = $this->getCache();
$cache['request']->getQuery()->set('auth', 'foo');
$this->assertNull($cache['storage']->fetch($cache['request']));
$cache['request']->getParams()->set('cache.key_filter', 'auth');
$this->assertNotNull($cache['storage']->fetch($cache['request']));
}
}