|
| 1 | +<?php namespace CodeIgniter\Cache\Handlers; |
| 2 | + |
| 3 | +/** |
| 4 | + * CodeIgniter |
| 5 | + * |
| 6 | + * An open source application development framework for PHP |
| 7 | + * |
| 8 | + * This content is released under the MIT License (MIT) |
| 9 | + * |
| 10 | + * Copyright (c) 2014-2017 British Columbia Institute of Technology |
| 11 | + * |
| 12 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | + * of this software and associated documentation files (the "Software"), to deal |
| 14 | + * in the Software without restriction, including without limitation the rights |
| 15 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 16 | + * copies of the Software, and to permit persons to whom the Software is |
| 17 | + * furnished to do so, subject to the following conditions: |
| 18 | + * |
| 19 | + * The above copyright notice and this permission notice shall be included in |
| 20 | + * all copies or substantial portions of the Software. |
| 21 | + * |
| 22 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 28 | + * THE SOFTWARE. |
| 29 | + * |
| 30 | + * @package CodeIgniter |
| 31 | + * @author CodeIgniter Dev Team |
| 32 | + * @copyright 2014-2017 British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | + * @license https://opensource.org/licenses/MIT MIT License |
| 34 | + * @link https://codeigniter.com |
| 35 | + * @since Version 3.0.0 |
| 36 | + * @filesource |
| 37 | + */ |
| 38 | +class RedisHandlerTest extends \CIUnitTestCase |
| 39 | +{ |
| 40 | + private $redisHandler; |
| 41 | + private static $key1 = 'key1'; |
| 42 | + private static $key2 = 'key2'; |
| 43 | + private static $key3 = 'key3'; |
| 44 | + private static function getKeyArray() |
| 45 | + { |
| 46 | + return [ |
| 47 | + self::$key1, self::$key2, self::$key3 |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + private static $dummy = 'dymmy'; |
| 52 | + private $config; |
| 53 | + |
| 54 | + public function setUp() |
| 55 | + { |
| 56 | + $this->config = new \Config\Cache(); |
| 57 | + |
| 58 | + $this->redisHandler = new RedisHandler($this->config->redis); |
| 59 | + if (!$this->redisHandler->isSupported()) { |
| 60 | + $this->markTestSkipped('Not support redis'); |
| 61 | + } |
| 62 | + |
| 63 | + $this->redisHandler->initialize(); |
| 64 | + } |
| 65 | + |
| 66 | + public function tearDown() |
| 67 | + { |
| 68 | + foreach (self::getKeyArray() as $key) { |
| 69 | + $this->redisHandler->delete($key); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public function testNew() |
| 74 | + { |
| 75 | + $this->assertInstanceOf(RedisHandler::class, $this->redisHandler); |
| 76 | + } |
| 77 | + |
| 78 | + public function testDestruct() |
| 79 | + { |
| 80 | + $this->redisHandler = new RedisHandler($this->config->redis); |
| 81 | + $this->redisHandler->initialize(); |
| 82 | + |
| 83 | + $this->assertInstanceOf(RedisHandler::class, $this->redisHandler); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public function testGet() |
| 88 | + { |
| 89 | + $this->redisHandler->save(self::$key1, 'value', 1); |
| 90 | + |
| 91 | + $this->assertSame('value', $this->redisHandler->get(self::$key1)); |
| 92 | + $this->assertFalse($this->redisHandler->get(self::$dummy)); |
| 93 | + |
| 94 | + \CodeIgniter\CLI\CLI::wait(2); |
| 95 | + $this->assertFalse($this->redisHandler->get(self::$key1)); |
| 96 | + } |
| 97 | + |
| 98 | + public function testSave() |
| 99 | + { |
| 100 | + $this->assertTrue($this->redisHandler->save(self::$key1, 'value')); |
| 101 | + } |
| 102 | + |
| 103 | + public function testDelete() |
| 104 | + { |
| 105 | + $this->redisHandler->save(self::$key1, 'value'); |
| 106 | + |
| 107 | + $this->assertTrue($this->redisHandler->delete(self::$key1)); |
| 108 | + $this->assertFalse($this->redisHandler->delete(self::$dummy)); |
| 109 | + } |
| 110 | + |
| 111 | + //FIXME: I don't like all Hash logic very much. It's wasting memory. |
| 112 | + //public function testIncrement() |
| 113 | + //{ |
| 114 | + //} |
| 115 | + |
| 116 | + //public function testDecrement() |
| 117 | + //{ |
| 118 | + //} |
| 119 | + |
| 120 | + public function testClean() |
| 121 | + { |
| 122 | + $this->redisHandler->save(self::$key1, 1); |
| 123 | + $this->redisHandler->save(self::$key2, 'value'); |
| 124 | + |
| 125 | + $this->assertTrue($this->redisHandler->clean()); |
| 126 | + } |
| 127 | + |
| 128 | + public function testGetCacheInfo() |
| 129 | + { |
| 130 | + $this->redisHandler->save(self::$key1, 'value'); |
| 131 | + |
| 132 | + $this->assertInternalType('array', $this->redisHandler->getCacheInfo()); |
| 133 | + } |
| 134 | + |
| 135 | + public function testGetMetaData() |
| 136 | + { |
| 137 | + $time = time(); |
| 138 | + $this->redisHandler->save(self::$key1, 'value'); |
| 139 | + |
| 140 | + $this->assertFalse($this->redisHandler->getMetaData(self::$dummy)); |
| 141 | + |
| 142 | + $actual = $this->redisHandler->getMetaData(self::$key1); |
| 143 | + $this->assertLessThanOrEqual(60, $actual['expire'] - $time); |
| 144 | + $this->assertLessThanOrEqual(0, $actual['mtime'] - $time); |
| 145 | + $this->assertSame('value', $actual['data']); |
| 146 | + } |
| 147 | + |
| 148 | + public function testIsSupported() |
| 149 | + { |
| 150 | + $this->assertTrue($this->redisHandler->isSupported()); |
| 151 | + } |
| 152 | +} |
0 commit comments