Open Monograph Press  3.3.0
DefaultCacheStorageTest.php
1 <?php
2 
4 
10 use Doctrine\Common\Cache\ArrayCache;
11 
16 {
17  protected function getCache()
18  {
19  $a = new ArrayCache();
20  $c = new DoctrineCacheAdapter($a);
21  $s = new DefaultCacheStorage($c);
22  $request = new Request('GET', 'http://foo.com', array('Accept' => 'application/json'));
23  $response = new Response(200, array(
24  'Content-Type' => 'application/json',
25  'Connection' => 'close',
26  'X-Foo' => 'Bar',
27  'Vary' => 'Accept'
28  ), 'test');
29  $s->cache($request, $response);
30  $data = $this->readAttribute($a, 'data');
31 
32  return array(
33  'cache' => $a,
34  'adapter' => $c,
35  'storage' => $s,
36  'request' => $request,
37  'response' => $response,
38  'serialized' => end($data)
39  );
40  }
41 
42  public function testReturnsNullForCacheMiss()
43  {
44  $cache = $this->getCache();
45  $this->assertNull($cache['storage']->fetch(new Request('GET', 'http://test.com')));
46  }
47 
48  public function testCachesRequests()
49  {
50  $cache = $this->getCache();
51  $foundRequest = $foundBody = $bodyKey = false;
52  foreach ($this->readAttribute($cache['cache'], 'data') as $key => $v) {
53  if (strpos($v, 'foo.com')) {
54  $foundRequest = true;
55  $data = unserialize($v);
56  $bodyKey = $data[0][3];
57  $this->assertInternalType('integer', $data[0][4]);
58  $this->assertFalse(isset($data[0][0]['connection']));
59  $this->assertEquals('foo.com', $data[0][0]['host']);
60  } elseif ($v == 'test') {
61  $foundBody = $key;
62  }
63  }
64  $this->assertContains($bodyKey, $foundBody);
65  $this->assertTrue($foundRequest);
66  }
67 
68  public function testFetchesResponse()
69  {
70  $cache = $this->getCache();
71  $response = $cache['storage']->fetch($cache['request']);
72  $this->assertEquals(200, $response->getStatusCode());
73  $this->assertFalse($response->hasHeader('Connection'));
74  $this->assertEquals('Bar', (string) $response->getHeader('X-Foo'));
75  $this->assertEquals('test', (string) $response->getBody());
76  $this->assertTrue(in_array($cache['serialized'], $this->readAttribute($cache['cache'], 'data')));
77  }
78 
80  {
81  $cache = $this->getCache();
82  $cache['storage']->delete($cache['request']);
83  $this->assertFalse(in_array('test', $this->readAttribute($cache['cache'], 'data')));
84  $this->assertFalse(in_array($cache['serialized'], $this->readAttribute($cache['cache'], 'data')));
85  }
86 
88  {
89  $cache = $this->getCache();
90  $cache['request']->setHeader('Accept', 'application/xml');
91  $response = $cache['response']->setHeader('Content-Type', 'application/xml');
92  $response->setBody('123');
93  $cache['storage']->cache($cache['request'], $response);
94  $data = $this->readAttribute($cache['cache'], 'data');
95  foreach ($data as $v) {
96  if (strpos($v, 'foo.com')) {
97  $u = unserialize($v);
98  $this->assertEquals(2, count($u));
99  $this->assertEquals($u[0][0]['accept'], 'application/xml');
100  $this->assertEquals($u[0][1]['content-type'], 'application/xml');
101  $this->assertEquals($u[1][0]['accept'], 'application/json');
102  $this->assertEquals($u[1][1]['content-type'], 'application/json');
103  $this->assertNotSame($u[0][3], $u[1][3]);
104  break;
105  }
106  }
107  }
108 
110  {
111  $cache = $this->getCache();
112  foreach (array('HEAD', 'POST', 'PUT', 'DELETE') as $method) {
113  $request = RequestFactory::getInstance()->cloneRequestWithMethod($cache['request'], $method);
114  $cache['storage']->cache($request, $cache['response']);
115  }
116  $cache['storage']->purge('http://foo.com');
117  $this->assertFalse(in_array('test', $this->readAttribute($cache['cache'], 'data')));
118  $this->assertFalse(in_array($cache['serialized'], $this->readAttribute($cache['cache'], 'data')));
119  $this->assertEquals(
120  array('DoctrineNamespaceCacheKey[]'),
121  array_keys($this->readAttribute($cache['cache'], 'data'))
122  );
123  }
124 
125  public function testRemovesExpiredResponses()
126  {
127  $cache = $this->getCache();
128  $request = new Request('GET', 'http://xyz.com');
129  $response = new Response(200, array('Age' => 1000, 'Cache-Control' => 'max-age=-10000'));
130  $cache['storage']->cache($request, $response);
131  $this->assertNull($cache['storage']->fetch($request));
132  $data = $this->readAttribute($cache['cache'], 'data');
133  $this->assertFalse(in_array('xyz.com', $data));
134  $this->assertTrue(in_array($cache['serialized'], $data));
135  }
136 
138  {
139  $cache = $this->getCache();
140  $this->assertInstanceOf('Guzzle\Http\Message\Response', $cache['storage']->fetch($cache['request']));
141  $request = new Request('GET', 'http://foo.com', array('Accept' => 'application/xml'));
142  $this->assertNull($cache['storage']->fetch($request));
143  }
144 
146  {
147  $cache = $this->getCache();
148  $data = $this->readAttribute($cache['cache'], 'data');
149  $key = array_search('test', $data);
150  $cache['cache']->delete(substr($key, 1, -4));
151  $this->assertNull($cache['storage']->fetch($cache['request']));
152  }
153 
154  public function staleProvider()
155  {
156  return array(
157  array(
158  new Request('GET', 'http://foo.com', array('Accept' => 'foo')),
159  new Response(200, array('Cache-Control' => 'stale-if-error=100', 'Vary' => 'Accept'))
160  ),
161  array(
162  new Request('GET', 'http://foo.com', array('Accept' => 'foo')),
163  new Response(200, array('Cache-Control' => 'stale-if-error', 'Vary' => 'Accept'))
164  )
165  );
166  }
167 
171  public function testUsesStaleTimeDirectiveForTtd($request, $response)
172  {
173  $cache = $this->getCache();
174  $cache['storage']->cache($request, $response);
175  $data = $this->readAttribute($cache['cache'], 'data');
176  foreach ($data as $v) {
177  if (strpos($v, 'foo.com')) {
178  $u = unserialize($v);
179  $this->assertGreaterThan($u[1][4], $u[0][4]);
180  break;
181  }
182  }
183  }
184 
185  public function testCanFilterCacheKeys()
186  {
187  $cache = $this->getCache();
188  $cache['request']->getQuery()->set('auth', 'foo');
189  $this->assertNull($cache['storage']->fetch($cache['request']));
190  $cache['request']->getParams()->set('cache.key_filter', 'auth');
191  $this->assertNotNull($cache['storage']->fetch($cache['request']));
192  }
193 }
Guzzle\Tests\Plugin\Cache
Definition: CachePluginTest.php:3
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\getCache
getCache()
Definition: DefaultCacheStorageTest.php:17
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testCachesMultipleRequestsWithVary
testCachesMultipleRequestsWithVary()
Definition: DefaultCacheStorageTest.php:87
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testCachesRequests
testCachesRequests()
Definition: DefaultCacheStorageTest.php:48
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testEnsuresResponseIsStillPresent
testEnsuresResponseIsStillPresent()
Definition: DefaultCacheStorageTest.php:145
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testReturnsNullForCacheMiss
testReturnsNullForCacheMiss()
Definition: DefaultCacheStorageTest.php:42
Guzzle\Http\Message\Response
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testPurgeRemovesAllMethodCaches
testPurgeRemovesAllMethodCaches()
Definition: DefaultCacheStorageTest.php:109
Guzzle\Plugin\Cache\DefaultCacheStorage
Definition: DefaultCacheStorage.php:16
Guzzle\Cache\DoctrineCacheAdapter
Definition: DoctrineCacheAdapter.php:12
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest
Definition: DefaultCacheStorageTest.php:15
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testUsesStaleTimeDirectiveForTtd
testUsesStaleTimeDirectiveForTtd($request, $response)
Definition: DefaultCacheStorageTest.php:171
Guzzle\Http\Message\Request
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Request.php:25
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testFetchesResponse
testFetchesResponse()
Definition: DefaultCacheStorageTest.php:68
Guzzle\Http\Message\RequestFactory
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestFactory.php:14
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testRemovesExpiredResponses
testRemovesExpiredResponses()
Definition: DefaultCacheStorageTest.php:125
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testUsesVaryToDetermineResult
testUsesVaryToDetermineResult()
Definition: DefaultCacheStorageTest.php:137
Guzzle\Http\Message\RequestFactory\getInstance
static getInstance()
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestFactory.php:42
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testCanFilterCacheKeys
testCanFilterCacheKeys()
Definition: DefaultCacheStorageTest.php:185
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\testDeletesRequestItemsAndBody
testDeletesRequestItemsAndBody()
Definition: DefaultCacheStorageTest.php:79
Guzzle\Tests\Plugin\Cache\DefaultCacheStorageTest\staleProvider
staleProvider()
Definition: DefaultCacheStorageTest.php:154