Skip to content

Commit acd9db4

Browse files
committed
Merge pull request #100 from Bee-Lab/master
add support for Gaufrette cache
2 parents 9f1d286 + 9acae68 commit acd9db4

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
"require-dev": {
2828
"phpunit/phpunit": ">=3.6.0"
2929
},
30+
"suggest": {
31+
"knplabs/gaufrette": "Needed for optional Gaufrette cache"
32+
},
3033
"autoload": {
3134
"psr-0": { "Github\\": "lib/" }
3235
},
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Github\HttpClient\Cache;
4+
5+
use Guzzle\Http\Message\Response;
6+
use Gaufrette\Filesystem;
7+
8+
/**
9+
* Gaufrette Cache
10+
*
11+
* @author Massimiliano Arione <[email protected]>
12+
*/
13+
class GaufretteCache implements CacheInterface
14+
{
15+
/**
16+
* @var Filesystem
17+
*/
18+
protected $filesystem;
19+
20+
/**
21+
* @param Filesystem $filesystem
22+
*/
23+
public function __construct(Filesystem $filesystem)
24+
{
25+
$this->filesystem = $filesystem;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function get($id)
32+
{
33+
$content = $this->filesystem->read($id);
34+
35+
return unserialize($content);
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function set($id, Response $response)
42+
{
43+
$this->filesystem->write($id, serialize($response), true);
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function getModifiedSince($id)
50+
{
51+
if ($this->filesystem->has($id)) {
52+
return $this->filesystem->mtime($id);
53+
}
54+
}
55+
}
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)