Skip to content

Commit 08be5a8

Browse files
committed
Merge pull request #1 from solilokiam/add-test
Add tests
2 parents 8e13178 + 54eb088 commit 08be5a8

File tree

7 files changed

+231
-7
lines changed

7 files changed

+231
-7
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ Author
152152

153153
TODO
154154
----
155-
- Documentation
156-
- Tests
155+
- Improve Tests
157156
- Symfony 2.4 support
158157

Store/RedisHttpStore.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function lock(Request $request)
183183

184184
$this->client->close();
185185

186-
return $result;
186+
return $result == 1;
187187
}
188188

189189
/**
@@ -203,7 +203,7 @@ public function unlock(Request $request)
203203

204204
$this->client->close();
205205

206-
return $result;
206+
return $result == 1;
207207
}
208208

209209
/**
@@ -243,7 +243,7 @@ public function purge($url)
243243

244244
$this->client->close();
245245

246-
return $result;
246+
return $result == 1;
247247
}
248248

249249
/**

Test/RedisClient/ClientTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: miquel
5+
* Date: 17/03/14
6+
* Time: 11:38
7+
*/
8+
9+
namespace Solilokiam\HttpRedisCache\Test\RedisClient;
10+
11+
12+
use Solilokiam\HttpRedisCache\RedisClient\Client;
13+
14+
class ClientTest extends \PHPUnit_Framework_TestCase
15+
{
16+
protected $redisMock;
17+
18+
protected function setUp()
19+
{
20+
$this->redisMock = $this->getMock('\Redis');
21+
}
22+
23+
public function testSimpleConnect()
24+
{
25+
$client = new Client(array('host' => 'localhost'));
26+
27+
$return = $client->createConnection();
28+
29+
$this->assertTrue($return);
30+
}
31+
32+
public function testReadWriteKey()
33+
{
34+
$client = new Client(array('host' => 'localhost'));
35+
36+
$connection = $client->createConnection();
37+
38+
$this->assertTrue($connection);
39+
40+
$client->set('testkey', '1234');
41+
42+
$result = $client->get('testkey');
43+
44+
$this->assertEquals('1234', $result);
45+
46+
$client->del('testkey');
47+
}
48+
49+
public function testDelKey()
50+
{
51+
$client = new Client(array('host' => 'localhost'));
52+
53+
$connection = $client->createConnection();
54+
55+
$this->assertTrue($connection);
56+
57+
$client->set('testkey', '1234');
58+
$client->del('testkey');
59+
60+
$result = $client->get('testkey');
61+
62+
$this->assertEquals(false, $result);
63+
64+
65+
}
66+
67+
public function testHashGet()
68+
{
69+
$client = new Client(array('host' => 'localhost'));
70+
71+
$connection = $client->createConnection();
72+
73+
$this->assertTrue($connection);
74+
75+
$client->hSetNx('testkey', 'testhash', 1);
76+
77+
$result = $client->hGet('testkey', 'testhash');
78+
79+
$this->assertEquals(1, $result);
80+
81+
$client->del('testkey');
82+
}
83+
}

Test/Store/RedisHttpStoreTest.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: miquel
5+
* Date: 17/03/14
6+
* Time: 12:29
7+
*/
8+
9+
namespace Solilokiam\HttpRedisCache\Test\Store;
10+
11+
12+
use Solilokiam\HttpRedisCache\RedisClient\Client;
13+
use Solilokiam\HttpRedisCache\Store\RedisHttpStore;
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
17+
class RedisHttpStoreTest extends \PHPUnit_Framework_TestCase
18+
{
19+
protected $store;
20+
protected $request;
21+
protected $response;
22+
23+
public function setUp()
24+
{
25+
$this->store = new RedisHttpStore(array('host' => 'localhost'), 'hdr', 'ldr', 'mdr');
26+
27+
$this->request = Request::create('/');
28+
29+
$this->response = new Response('hello miquel', 200, array());
30+
31+
$this->cleanKeys();
32+
}
33+
34+
public function tearDown()
35+
{
36+
$this->store = null;
37+
$this->request = null;
38+
$this->request = null;
39+
40+
$this->cleanKeys();
41+
}
42+
43+
public function testReadsEmptyCacheAtKey()
44+
{
45+
$this->assertEmpty($this->getStoreMetadata('/nothing'));
46+
}
47+
48+
public function testUnlockThatExists()
49+
{
50+
$cacheKey = $this->storeSimpleEntry();
51+
$this->store->lock($this->request);
52+
53+
$this->assertTrue($this->store->unlock($this->request));
54+
}
55+
56+
public function testUnlockThatDoesNotExist()
57+
{
58+
$this->assertFalse($this->store->unlock($this->request));
59+
}
60+
61+
public function testRemoveEntriesForKeyWithPurge()
62+
{
63+
$request = Request::create('/foorequest');
64+
$this->store->write($request, new Response('fooresponse'));
65+
66+
$metadata = $this->getStoreMetadata($request);
67+
$this->assertNotEmpty($metadata);
68+
69+
$this->assertTrue($this->store->purge('/foorequest'));
70+
$this->assertEmpty($this->getStoreMetadata($request));
71+
72+
$content = $this->loadContentData($metadata[0][1]['x-content-digest'][0]);
73+
$this->assertNotEmpty($content);
74+
75+
$this->assertFalse($this->store->purge('/bar'));
76+
}
77+
78+
public function testStoresACacheEntry()
79+
{
80+
$cacheKey = $this->storeSimpleEntry();
81+
82+
$this->assertNotEmpty($this->getStoreMetadata($cacheKey));
83+
}
84+
85+
protected function cleanKeys()
86+
{
87+
$client = new Client(array('host' => 'localhost'));
88+
89+
$client->createConnection();
90+
$client->flushAll();
91+
}
92+
93+
protected function storeSimpleEntry($path = null, $headers = array())
94+
{
95+
if (null === $path) {
96+
$path = '/test';
97+
}
98+
99+
$this->request = Request::create($path, 'get', array(), array(), array(), $headers);
100+
$this->response = new Response('test', 200, array('Cache-Control' => 'max-age=420'));
101+
102+
return $this->store->write($this->request, $this->response);
103+
}
104+
105+
protected function getStoreMetadata($key)
106+
{
107+
$r = new \ReflectionObject($this->store);
108+
$m = $r->getMethod('getMetadata');
109+
$m->setAccessible(true);
110+
111+
if ($key instanceof Request) {
112+
$m1 = $r->getMethod('getMetadataKey');
113+
$m1->setAccessible(true);
114+
$key = $m1->invoke($this->store, $key);
115+
}
116+
117+
return $m->invoke($this->store, $key);
118+
}
119+
120+
protected function loadContentData($key)
121+
{
122+
$r = new \ReflectionObject($this->store);
123+
$m = $r->getMethod('load');
124+
$m->setAccessible(true);
125+
126+
return $m->invoke($this->store, $key);
127+
}
128+
129+
130+
}

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
"description": "An HttpCache extension with redis for Symfony 2",
44
"require": {
55
"php": ">=5.3.3",
6-
"symfony/framework-bundle": "2.3.*"
6+
"predis/predis": "0.8.*"
77
},
88
"require-dev": {
9-
"predis/predis": "0.8.*"
9+
"phpunit/phpunit": "3.7.14",
10+
"symfony/http-foundation": "2.3.*@dev",
11+
"symfony/http-kernel": "2.3.*@dev"
1012
},
1113
"license": "MIT",
1214
"authors": [

phpunit.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true" bootstrap="vendor/autoload.php">
3+
<testsuites>
4+
<testsuite name="Application Test Suite">
5+
<directory>./Test/</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>

0 commit comments

Comments
 (0)