Skip to content
This repository was archived by the owner on Jun 27, 2022. It is now read-only.

Commit 9a16633

Browse files
committed
Adding basic tests. Using constructor injection in CacheStorage for
keyPrefix rather than setter injection.
1 parent 1380041 commit 9a16633

File tree

5 files changed

+109
-16
lines changed

5 files changed

+109
-16
lines changed

phpunit.xml.dist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
colors="true">
4+
<testsuites>
5+
<testsuite>
6+
<directory>tests</directory>
7+
</testsuite>
8+
</testsuites>
9+
<filter>
10+
<whitelist>
11+
<directory suffix=".php">src</directory>
12+
</whitelist>
13+
</filter>
14+
</phpunit>

src/CacheStorage.php

+4-14
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ class CacheStorage implements CacheStorageInterface
2525
private $cache;
2626

2727
/**
28-
* @param Cache $cache Cache backend
28+
* @param Cache $cache Cache backend.
29+
* @param string $keyPrefix Key prefix to add to each key.
2930
*/
30-
public function __construct(Cache $cache)
31+
public function __construct(Cache $cache, $keyPrefix = null)
3132
{
3233
$this->cache = $cache;
34+
$this->keyPrefix = $keyPrefix;
3335
}
3436

3537
public function cache(
@@ -144,18 +146,6 @@ public function fetch(RequestInterface $request)
144146
return $response;
145147
}
146148

147-
/**
148-
* Set the cache key prefix
149-
*
150-
* @param string $name
151-
*
152-
* @return void
153-
*/
154-
public function setPrefix($name)
155-
{
156-
$this->keyPrefix = $name;
157-
}
158-
159149
/**
160150
* Hash a request URL into a string that returns cache metadata
161151
*

src/CacheSubscriber.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace GuzzleHttp\Subscriber\Cache;
33

44
use GuzzleHttp\Event\HasEmitterInterface;
5-
use GuzzleHttp\Message\MessageInterface;
65
use GuzzleHttp\ClientInterface;
76
use GuzzleHttp\Event\BeforeEvent;
87
use GuzzleHttp\Event\CompleteEvent;
@@ -70,6 +69,10 @@ public function __construct(
7069
*
7170
* @param HasEmitterInterface $subject Client or request to attach to,
7271
* @param array $options Options used to control the cache.
72+
*
73+
* @return array Returns an associative array containing a 'subscriber' key
74+
* that holds the created CacheSubscriber, and a 'storage'
75+
* key that contains the cache storage used by the subscriber.
7376
*/
7477
public static function attach(
7578
HasEmitterInterface $subject,
@@ -87,7 +90,8 @@ public static function attach(
8790
}
8891

8992
$emitter = $subject->getEmitter();
90-
$emitter->attach(new self($options['storage'], $options['can_cache']));
93+
$cache = new self($options['storage'], $options['can_cache']);
94+
$emitter->attach($cache);
9195

9296
if (!isset($options['validate']) || $options['validate'] === true) {
9397
$emitter->attach(new ValidationSubscriber(
@@ -99,6 +103,8 @@ public static function attach(
99103
if (!isset($options['purge']) || $options['purge'] === true) {
100104
$emitter->attach(new PurgeSubscriber($options['storage']));
101105
}
106+
107+
return ['subscriber' => $cache, 'storage' => $options['storage']];
102108
}
103109

104110
public function getEvents()

tests/CacheSubscriberTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace GuzzleHttp\Tests\Subscriber\Cache;
3+
4+
use GuzzleHttp\Client;
5+
use GuzzleHttp\Subscriber\Cache\CacheSubscriber;
6+
7+
class CacheSubscriberTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testCreatesAndAttachedDefaultSubscriber()
10+
{
11+
$client = new Client();
12+
$cache = CacheSubscriber::attach($client);
13+
$this->assertArrayHasKey('subscriber', $cache);
14+
$this->assertArrayHasKey('storage', $cache);
15+
$this->assertInstanceOf(
16+
'GuzzleHttp\Subscriber\Cache\CacheStorage',
17+
$cache['storage']
18+
);
19+
$this->assertInstanceOf(
20+
'GuzzleHttp\Subscriber\Cache\CacheSubscriber',
21+
$cache['subscriber']
22+
);
23+
$this->assertTrue($client->getEmitter()->hasListeners('error'));
24+
}
25+
}

tests/IntegrationTest.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
namespace GuzzleHttp\Tests\Subscriber\Cache;
3+
4+
require_once __DIR__ . '/../vendor/guzzlehttp/ringphp/tests/Client/Server.php';
5+
require_once __DIR__ . '/../vendor/guzzlehttp/guzzle/tests/Server.php';
6+
7+
use GuzzleHttp\Client;
8+
use GuzzleHttp\Message\Response;
9+
use GuzzleHttp\Subscriber\Cache\CacheSubscriber;
10+
use GuzzleHttp\Subscriber\History;
11+
use GuzzleHttp\Tests\Server;
12+
13+
class IntegrationTest extends \PHPUnit_Framework_TestCase
14+
{
15+
public static function setupBeforeClass()
16+
{
17+
Server::start();
18+
}
19+
20+
public static function tearDownAfterClass()
21+
{
22+
Server::stop();
23+
}
24+
25+
public function testCachesResponses()
26+
{
27+
Server::enqueue([
28+
new Response(200, [
29+
'Vary' => 'Accept-Encoding,Cookie,X-Use-HHVM',
30+
'Date' => 'Wed, 29 Oct 2014 20:52:15 GMT',
31+
'Cache-Control' => 'private, s-maxage=0, max-age=0, must-revalidate',
32+
'Last-Modified' => 'Wed, 29 Oct 2014 20:30:57 GMT',
33+
'Age' => '1277'
34+
]),
35+
new Response(304, [
36+
'Content-Type' => 'text/html; charset=UTF-8',
37+
'Vary' => 'Accept-Encoding,Cookie,X-Use-HHVM',
38+
'Date' => 'Wed, 29 Oct 2014 20:52:16 GMT',
39+
'Cache-Control' => 'private, s-maxage=0, max-age=0, must-revalidate',
40+
'Last-Modified' => 'Wed, 29 Oct 2014 20:30:57 GMT',
41+
'Age' => '1278'
42+
])
43+
]);
44+
45+
$client = new Client(['base_url' => Server::$url]);
46+
CacheSubscriber::attach($client);
47+
$history = new History();
48+
$client->getEmitter()->attach($history);
49+
$response1 = $client->get('/foo');
50+
$this->assertEquals(200, $response1->getStatusCode());
51+
$response2 = $client->get('/foo');
52+
$this->assertEquals(200, $response2->getStatusCode());
53+
$this->assertCount(2, Server::received());
54+
$last = $history->getLastResponse();
55+
$this->assertEquals('HIT from GuzzleCache', $last->getHeader('X-Cache-Lookup'));
56+
$this->assertEquals('HIT from GuzzleCache', $last->getHeader('X-Cache'));
57+
}
58+
}

0 commit comments

Comments
 (0)