10 use Doctrine\Common\Cache\ArrayCache;
19 $a =
new ArrayCache();
22 $request =
new Request(
'GET',
'http://foo.com', array(
'Accept' =>
'application/json'));
24 'Content-Type' =>
'application/json',
25 'Connection' =>
'close',
29 $s->cache($request, $response);
30 $data = $this->readAttribute($a,
'data');
36 'request' => $request,
37 'response' => $response,
38 'serialized' => end($data)
45 $this->assertNull($cache[
'storage']->fetch(
new Request(
'GET',
'http://test.com')));
51 $foundRequest = $foundBody = $bodyKey =
false;
52 foreach ($this->readAttribute($cache[
'cache'],
'data') as $key => $v) {
53 if (strpos($v,
'foo.com')) {
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') {
64 $this->assertContains($bodyKey, $foundBody);
65 $this->assertTrue($foundRequest);
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')));
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')));
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')) {
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]);
112 foreach (array(
'HEAD',
'POST',
'PUT',
'DELETE') as $method) {
114 $cache[
'storage']->cache($request, $cache[
'response']);
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')));
120 array(
'DoctrineNamespaceCacheKey[]'),
121 array_keys($this->readAttribute($cache[
'cache'],
'data'))
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));
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));
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']));
158 new Request(
'GET',
'http://foo.com', array(
'Accept' =>
'foo')),
159 new Response(200, array(
'Cache-Control' =>
'stale-if-error=100',
'Vary' =>
'Accept'))
162 new Request(
'GET',
'http://foo.com', array(
'Accept' =>
'foo')),
163 new Response(200, array(
'Cache-Control' =>
'stale-if-error',
'Vary' =>
'Accept'))
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]);
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']));