Skip to content

Commit a1f5920

Browse files
committed
add support for Gaufrette cache
1 parent 9f1d286 commit a1f5920

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
"require": {
2020
"php": ">=5.3.2",
2121
"ext-curl": "*",
22-
"guzzle/guzzle": ">=3.7"
22+
"guzzle/guzzle": ">=3.7",
23+
"knplabs/gaufrette": "0.1.*"
2324
},
2425
"require-dev": {
2526
"phpunit/phpunit": ">=3.7"
2627
},
2728
"require-dev": {
2829
"phpunit/phpunit": ">=3.6.0"
2930
},
31+
"suggest": {
32+
"knplabs/gaufrette": "Needed for optional Gaufrette cache"
33+
},
3034
"autoload": {
3135
"psr-0": { "Github\\": "lib/" }
3236
},
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Github\HttpClient\Cache;
4+
5+
use Guzzle\Http\Message\Response;
6+
use Gaufrette\Filesystem;
7+
8+
class GaufretteCache implements CacheInterface
9+
{
10+
/**
11+
* @var Filesystem
12+
*/
13+
protected $filesystem;
14+
15+
/**
16+
* @param Filesystem $filesystem
17+
*/
18+
public function __construct(Filesystem $filesystem)
19+
{
20+
$this->filesystem = $filesystem;
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function get($id)
27+
{
28+
$content = $this->filesystem->read($id);
29+
30+
return unserialize($content);
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function set($id, Response $response)
37+
{
38+
$this->filesystem->write($id, serialize($response));
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getModifiedSince($id)
45+
{
46+
if ($this->filesystem->has($id)) {
47+
return $this->filesystem->mtime($id);
48+
}
49+
50+
return null;
51+
}
52+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Github\Tests\HttpClient\Cache;
4+
5+
use Guzzle\Http\Message\Response;
6+
use Github\HttpClient\Cache\GaufretteCache;
7+
8+
class GaufretteCacheTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function setUp()
11+
{
12+
if (!class_exists('Gaufrette\Filesystem')) {
13+
$this->markTestSkipped('Gaufrette not installed.');
14+
}
15+
}
16+
17+
/**
18+
* @test
19+
*/
20+
public function shouldStoreAResponseForAGivenKey()
21+
{
22+
$response = new Response(200);
23+
$filesystem = $this->getMockBuilder('Gaufrette\Filesystem')->disableOriginalConstructor()->getMock();
24+
$filesystem
25+
->expects($this->once())
26+
->method('write')
27+
->with('test', serialize($response))
28+
;
29+
$filesystem
30+
->expects($this->once())
31+
->method('read')
32+
->with('test')
33+
->will($this->returnValue('a:0:{}'))
34+
;
35+
36+
$cache = new GaufretteCache($filesystem);
37+
$cache->set('test', $response);
38+
$this->assertNotNull($cache->get('test'));
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function shouldGetATimestampForExistingFile()
45+
{
46+
$response = new Response(200);
47+
$filesystem = $this->getMockBuilder('Gaufrette\Filesystem')->disableOriginalConstructor()->getMock();
48+
$filesystem
49+
->expects($this->once())
50+
->method('has')
51+
->with('test')
52+
->will($this->returnValue(true))
53+
;
54+
$filesystem
55+
->expects($this->once())
56+
->method('mtime')
57+
->with('test')
58+
->will($this->returnValue(100))
59+
;
60+
61+
$cache = new GaufretteCache($filesystem);
62+
$cache->set('test', new Response(200));
63+
64+
$this->assertInternalType('int', $cache->getModifiedSince('test'));
65+
}
66+
67+
/**
68+
* @test
69+
*/
70+
public function shouldNotGetATimestampForInexistingFile()
71+
{
72+
$filesystem = $this->getMockBuilder('Gaufrette\Filesystem')->disableOriginalConstructor()->getMock();
73+
$filesystem
74+
->expects($this->once())
75+
->method('has')
76+
->with('test2')
77+
->will($this->returnValue(false))
78+
;
79+
80+
$cache = new GaufretteCache($filesystem);
81+
82+
$this->assertNull($cache->getModifiedSince('test2'));
83+
}
84+
}

0 commit comments

Comments
 (0)