|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SimpleSAML\Test\Store; |
| 4 | + |
| 5 | +use \SimpleSAML_Configuration as Configuration; |
| 6 | +use \SimpleSAML\Store; |
| 7 | + |
| 8 | +/** |
| 9 | + * Tests for the Redis store. |
| 10 | + * |
| 11 | + * For the full copyright and license information, please view the LICENSE file that was distributed with this source |
| 12 | + * code. |
| 13 | + * |
| 14 | + * @package simplesamlphp/simplesamlphp |
| 15 | + */ |
| 16 | +class RedisTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + protected function setUp() |
| 19 | + { |
| 20 | + $this->config = array(); |
| 21 | + |
| 22 | + $this->mocked_redis = $this->getMockBuilder('Predis\Client') |
| 23 | + ->setMethods(array('get', 'set', 'setex', 'del', 'disconnect')) |
| 24 | + ->disableOriginalConstructor() |
| 25 | + ->getMock(); |
| 26 | + |
| 27 | + $this->mocked_redis->method('get') |
| 28 | + ->will($this->returnCallback(array($this, 'getMocked'))); |
| 29 | + |
| 30 | + $this->mocked_redis->method('set') |
| 31 | + ->will($this->returnCallback(array($this, 'setMocked'))); |
| 32 | + |
| 33 | + $this->mocked_redis->method('setex') |
| 34 | + ->will($this->returnCallback(array($this, 'setexMocked'))); |
| 35 | + |
| 36 | + $this->mocked_redis->method('del') |
| 37 | + ->will($this->returnCallback(array($this, 'delMocked'))); |
| 38 | + |
| 39 | + $nop = function () { |
| 40 | + return; |
| 41 | + }; |
| 42 | + |
| 43 | + $this->mocked_redis->method('disconnect') |
| 44 | + ->will($this->returnCallback($nop)); |
| 45 | + |
| 46 | + $this->redis = new Store\Redis($this->mocked_redis); |
| 47 | + } |
| 48 | + |
| 49 | + public function getMocked($key) |
| 50 | + { |
| 51 | + return array_key_exists($key, $this->config) ? $this->config[$key] : false; |
| 52 | + } |
| 53 | + |
| 54 | + public function setMocked($key, $value) |
| 55 | + { |
| 56 | + $this->config[$key] = $value; |
| 57 | + } |
| 58 | + |
| 59 | + public function setexMocked($key, $expire, $value) |
| 60 | + { |
| 61 | + // Testing expiring data is more trouble than it's worth for now |
| 62 | + $this->setMocked($key, $value); |
| 63 | + } |
| 64 | + |
| 65 | + public function delMocked($key) |
| 66 | + { |
| 67 | + unset($this->config[$key]); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @covers \SimpleSAML\Store::getInstance |
| 72 | + * @covers \SimpleSAML\Store\Redis::__construct |
| 73 | + * @test |
| 74 | + */ |
| 75 | + public function testRedisInstance() |
| 76 | + { |
| 77 | + $config = Configuration::loadFromArray(array( |
| 78 | + 'store.type' => 'redis', |
| 79 | + 'store.redis.prefix' => 'phpunit_', |
| 80 | + ), '[ARRAY]', 'simplesaml'); |
| 81 | + |
| 82 | + $store = Store::getInstance(); |
| 83 | + |
| 84 | + $this->assertInstanceOf('SimpleSAML\Store\Redis', $store); |
| 85 | + |
| 86 | + $this->clearInstance($config, '\SimpleSAML_Configuration'); |
| 87 | + $this->clearInstance($store, '\SimpleSAML\Store'); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @covers \SimpleSAML\Store\Redis::get |
| 92 | + * @covers \SimpleSAML\Store\Redis::set |
| 93 | + * @test |
| 94 | + */ |
| 95 | + public function testInsertData() |
| 96 | + { |
| 97 | + $value = 'TEST'; |
| 98 | + |
| 99 | + $this->redis->set('test', 'key', $value); |
| 100 | + $res = $this->redis->get('test', 'key'); |
| 101 | + $expected = $value; |
| 102 | + |
| 103 | + $this->assertEquals($expected, $res); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @covers \SimpleSAML\Store\Redis::get |
| 108 | + * @covers \SimpleSAML\Store\Redis::set |
| 109 | + * @test |
| 110 | + */ |
| 111 | + public function testInsertExpiringData() |
| 112 | + { |
| 113 | + $value = 'TEST'; |
| 114 | + |
| 115 | + $this->redis->set('test', 'key', $value, $expire = 80808080); |
| 116 | + $res = $this->redis->get('test', 'key'); |
| 117 | + $expected = $value; |
| 118 | + |
| 119 | + $this->assertEquals($expected, $res); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @covers \SimpleSAML\Store\Redis::get |
| 124 | + * @test |
| 125 | + */ |
| 126 | + public function testGetEmptyData() |
| 127 | + { |
| 128 | + $res = $this->redis->get('test', 'key'); |
| 129 | + |
| 130 | + $this->assertNull($res); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @covers \SimpleSAML\Store\Redis::get |
| 135 | + * @covers \SimpleSAML\Store\Redis::set |
| 136 | + * @test |
| 137 | + */ |
| 138 | + public function testOverwriteData() |
| 139 | + { |
| 140 | + $value1 = 'TEST1'; |
| 141 | + $value2 = 'TEST2'; |
| 142 | + |
| 143 | + $this->redis->set('test', 'key', $value1); |
| 144 | + $this->redis->set('test', 'key', $value2); |
| 145 | + $res = $this->redis->get('test', 'key'); |
| 146 | + $expected = $value2; |
| 147 | + |
| 148 | + $this->assertEquals($expected, $res); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @covers \SimpleSAML\Store\Redis::get |
| 153 | + * @covers \SimpleSAML\Store\Redis::set |
| 154 | + * @covers \SimpleSAML\Store\Redis::delete |
| 155 | + * @test |
| 156 | + */ |
| 157 | + public function testDeleteData() |
| 158 | + { |
| 159 | + $this->redis->set('test', 'key', 'TEST'); |
| 160 | + $this->redis->delete('test', 'key'); |
| 161 | + $res = $this->redis->get('test', 'key'); |
| 162 | + |
| 163 | + $this->assertNull($res); |
| 164 | + } |
| 165 | + |
| 166 | + protected function clearInstance($service, $className) |
| 167 | + { |
| 168 | + $reflectedClass = new \ReflectionClass($className); |
| 169 | + $reflectedInstance = $reflectedClass->getProperty('instance'); |
| 170 | + $reflectedInstance->setAccessible(true); |
| 171 | + $reflectedInstance->setValue($service, null); |
| 172 | + $reflectedInstance->setAccessible(false); |
| 173 | + } |
| 174 | +} |
0 commit comments