Skip to content

Commit dfe110a

Browse files
author
Jacob Christiansen
committed
Added tests
1 parent 903a3cd commit dfe110a

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cache.properties
2+
vendor
3+
build/coverage/
4+
build/logs/

lib/Store/Redis.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
*/
1313
class sspmod_redis_Store_Redis extends SimpleSAML_Store
1414
{
15-
protected function __construct()
15+
private $redis;
16+
private $prefix;
17+
private $lifeTime;
18+
19+
public function __construct()
1620
{
1721
$redisConfig = SimpleSAML_Configuration::getConfig('module_redis.php');
18-
$globalConfig = SimpleSAML_Configuration::getConfig();
1922

2023
$this->redis = new Predis\Client($redisConfig->getString('host', 'localhost'));
2124
$this->prefix = $redisConfig->getString('prefix', 'simpleSAMLphp');

test/Store/RedisTest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace Predis {
4+
class Client
5+
{
6+
public static $setKey = null;
7+
public static $setValue = null;
8+
public static $getKey = null;
9+
public static $expireKey = null;
10+
public static $expireValue = null;
11+
public static $deleteKey = null;
12+
13+
public function get($key)
14+
{
15+
self::$getKey = $key;
16+
17+
if ($key == 'simpleSAMLphp.test.key') {
18+
return serialize(['ding' => 'bat']);
19+
}
20+
return null;
21+
}
22+
23+
public function set($key, $value)
24+
{
25+
self::$setKey = $key;
26+
self::$setValue = $value;
27+
}
28+
29+
public function expire($key, $expire)
30+
{
31+
self::$expireKey = $key;
32+
self::$expireValue = $expire;
33+
}
34+
35+
public function del($key)
36+
{
37+
self::$deleteKey = $key;
38+
}
39+
}
40+
}
41+
42+
namespace {
43+
use Nulpunkt\PhpStub\Stub;
44+
45+
class SimpleSAML_Store
46+
{
47+
}
48+
49+
class SimpleSAML_Configuration
50+
{
51+
public static function getConfig()
52+
{
53+
return new SimpleSAML_Configuration;
54+
}
55+
56+
public function getString($value)
57+
{
58+
if ($value == 'host') {
59+
return 'localhost';
60+
}
61+
if ($value == 'prefix') {
62+
return 'simpleSAMLphp';
63+
}
64+
throw new ErrorException('Called with unexpected value');
65+
}
66+
67+
public function getInteger($key)
68+
{
69+
if ($key == 'lifetime') {
70+
return 288000;
71+
}
72+
throw new ErrorException('Called with unexpected value');
73+
}
74+
}
75+
76+
class RedisTest extends \PHPUnit_Framework_TestCase
77+
{
78+
public function setUp()
79+
{
80+
Predis\Client::$setKey = null;
81+
Predis\Client::$setValue = null;
82+
Predis\Client::$getKey = null;
83+
Predis\Client::$expireKey = null;
84+
Predis\Client::$expireValue = null;
85+
Predis\Client::$deleteKey = null;
86+
}
87+
88+
public function testSetKeyInRedis()
89+
{
90+
$store = new sspmod_redis_Store_Redis();
91+
$store->set('test', 'key', ['one', 'two']);
92+
93+
$this->assertEquals('simpleSAMLphp.test.key', Predis\Client::$setKey);
94+
$this->assertEquals(serialize(['one', 'two']), Predis\Client::$setValue);
95+
$this->assertEquals('simpleSAMLphp.test.key', Predis\Client::$expireKey);
96+
$this->assertEquals(288000, Predis\Client::$expireValue);
97+
}
98+
99+
public function testSetKeyWithExpireInRedis()
100+
{
101+
$store = new sspmod_redis_Store_Redis();
102+
$store->set('test', 'key', ['one', 'two'], 11);
103+
104+
$this->assertEquals('simpleSAMLphp.test.key', Predis\Client::$setKey);
105+
$this->assertEquals(serialize(['one', 'two']), Predis\Client::$setValue);
106+
$this->assertEquals('simpleSAMLphp.test.key', Predis\Client::$expireKey);
107+
$this->assertEquals(11, Predis\Client::$expireValue);
108+
}
109+
110+
public function testGetExistingKey()
111+
{
112+
$store = new sspmod_redis_Store_Redis();
113+
$res = $store->get('test', 'key');
114+
115+
$this->assertEquals('simpleSAMLphp.test.key', Predis\Client::$getKey);
116+
$this->assertEquals(['ding' => 'bat'], $res);
117+
}
118+
119+
public function testGetNonExistingKey()
120+
{
121+
$store = new sspmod_redis_Store_Redis();
122+
$res = $store->get('test', 'nokey');
123+
124+
$this->assertEquals('simpleSAMLphp.test.nokey', Predis\Client::$getKey);
125+
$this->assertFalse($res);
126+
}
127+
128+
public function testDeleteKey()
129+
{
130+
$store = new sspmod_redis_Store_Redis();
131+
$res = $store->delete('test', 'nokey');
132+
133+
$this->assertEquals('simpleSAMLphp.test.nokey', Predis\Client::$deleteKey);
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)