CacheTestCase.php 5.54 KB
Newer Older
1
<?php
2 3 4 5 6 7 8

namespace yii\caching;

/**
 * Mock for the time() function for caching classes
 * @return int
 */
Alexander Makarov committed
9 10
function time()
{
Alexander Makarov committed
11
	return \yiiunit\framework\caching\CacheTestCase::$time ?: \time();
12 13
}

14
namespace yiiunit\framework\caching;
15

16 17 18 19 20 21
use yiiunit\TestCase;
use yii\caching\Cache;

/**
 * Base class for testing cache backends
 */
Alexander Makarov committed
22
abstract class CacheTestCase extends TestCase
23
{
24
	/**
25
	 * @var integer virtual time to be returned by mocked time() function.
26 27 28 29
	 * Null means normal time() behavior.
	 */
	public static $time;

30 31 32 33 34
	/**
	 * @return Cache
	 */
	abstract protected function getCacheInstance();

35 36 37
	protected function setUp()
	{
		parent::setUp();
38
		$this->mockApplication();
39
	}
40

41 42 43 44 45
	protected function tearDown()
	{
		static::$time = null;
	}

46 47 48 49 50 51 52 53 54 55
	/**
	 * @return Cache
	 */
	public function prepare()
	{
		$cache = $this->getCacheInstance();

		$cache->flush();
		$cache->set('string_test', 'string_test');
		$cache->set('number_test', 42);
Alexander Makarov committed
56
		$cache->set('array_test', ['array_test' => 'array_test']);
57 58 59 60 61 62 63 64 65 66 67 68
		$cache['arrayaccess_test'] = new \stdClass();

		return $cache;
	}

	/**
	 * default value of cache prefix is application id
	 */
	public function testKeyPrefix()
	{
		$cache = $this->getCacheInstance();
		$this->assertNotNull(\Yii::$app->id);
69
		$this->assertNotNull($cache->keyPrefix);
70 71
	}

72 73 74
	public function testSet()
	{
		$cache = $this->getCacheInstance();
75

76 77
		$this->assertTrue($cache->set('string_test', 'string_test'));
		$this->assertTrue($cache->set('number_test', 42));
Alexander Makarov committed
78
		$this->assertTrue($cache->set('array_test', ['array_test' => 'array_test']));
79 80 81 82
	}

	public function testGet()
	{
83 84
		$cache = $this->prepare();

85 86 87 88 89 90 91
		$this->assertEquals('string_test', $cache->get('string_test'));

		$this->assertEquals(42, $cache->get('number_test'));

		$array = $cache->get('array_test');
		$this->assertArrayHasKey('array_test', $array);
		$this->assertEquals('array_test', $array['array_test']);
92
	}
93

94 95 96 97 98 99 100 101 102 103 104 105
	/**
	 * @return array testing mset with and without expiry
	 */
	public function msetExpiry()
	{
		return [[0], [2]];
	}

	/**
	 * @dataProvider msetExpiry
	 */
	public function testMset($expiry)
106 107 108 109
	{
		$cache = $this->getCacheInstance();
		$cache->flush();

110 111 112 113
		$cache->mset([
			'string_test' => 'string_test',
			'number_test' => 42,
			'array_test' => ['array_test' => 'array_test'],
114
		], $expiry);
115 116 117 118 119 120 121 122 123 124

		$this->assertEquals('string_test', $cache->get('string_test'));

		$this->assertEquals(42, $cache->get('number_test'));

		$array = $cache->get('array_test');
		$this->assertArrayHasKey('array_test', $array);
		$this->assertEquals('array_test', $array['array_test']);
	}

125 126 127 128 129 130 131 132 133 134 135 136
	public function testExists()
	{
		$cache = $this->prepare();

		$this->assertTrue($cache->exists('string_test'));
		// check whether exists affects the value
		$this->assertEquals('string_test', $cache->get('string_test'));

		$this->assertTrue($cache->exists('number_test'));
		$this->assertFalse($cache->exists('not_exists'));
	}

137 138 139 140 141
	public function testArrayAccess()
	{
		$cache = $this->getCacheInstance();

		$cache['arrayaccess_test'] = new \stdClass();
142 143 144
		$this->assertInstanceOf('stdClass', $cache['arrayaccess_test']);
	}

145
	public function testGetNonExistent()
146 147
	{
		$cache = $this->getCacheInstance();
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

		$this->assertFalse($cache->get('non_existent_key'));
	}

	public function testStoreSpecialValues()
	{
		$cache = $this->getCacheInstance();

		$this->assertTrue($cache->set('null_value', null));
		$this->assertNull($cache->get('null_value'));

		$this->assertTrue($cache->set('bool_value', true));
		$this->assertTrue($cache->get('bool_value'));
	}

	public function testMget()
	{
		$cache = $this->prepare();

Alexander Makarov committed
167
		$this->assertEquals(['string_test' => 'string_test', 'number_test' => 42], $cache->mget(['string_test', 'number_test']));
168
		// ensure that order does not matter
Alexander Makarov committed
169 170
		$this->assertEquals(['number_test' => 42, 'string_test' => 'string_test'], $cache->mget(['number_test', 'string_test']));
		$this->assertEquals(['number_test' => 42, 'non_existent_key' => null], $cache->mget(['number_test', 'non_existent_key']));
171 172 173 174 175
	}

	public function testExpire()
	{
		$cache = $this->getCacheInstance();
176

177
		$this->assertTrue($cache->set('expire_test', 'expire_test', 2));
178
		usleep(500000);
179
		$this->assertEquals('expire_test', $cache->get('expire_test'));
180
		usleep(2500000);
181
		$this->assertFalse($cache->get('expire_test'));
182 183
	}

184 185 186 187 188 189 190 191 192 193 194
	public function testExpireAdd()
	{
		$cache = $this->getCacheInstance();

		$this->assertTrue($cache->add('expire_testa', 'expire_testa', 2));
		usleep(500000);
		$this->assertEquals('expire_testa', $cache->get('expire_testa'));
		usleep(2500000);
		$this->assertFalse($cache->get('expire_testa'));
	}

195 196
	public function testAdd()
	{
197
		$cache = $this->prepare();
198 199

		// should not change existing keys
200
		$this->assertFalse($cache->add('number_test', 13));
201 202
		$this->assertEquals(42, $cache->get('number_test'));

203 204
		// should store data if it's not there yet
		$this->assertFalse($cache->get('add_test'));
205
		$this->assertTrue($cache->add('add_test', 13));
206 207 208
		$this->assertEquals(13, $cache->get('add_test'));
	}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	public function testMadd()
	{
		$cache = $this->prepare();

		$this->assertFalse($cache->get('add_test'));

		$cache->madd([
			'number_test' => 13,
			'add_test' => 13,
		]);

		$this->assertEquals(42, $cache->get('number_test'));
		$this->assertEquals(13, $cache->get('add_test'));
	}

224 225
	public function testDelete()
	{
226
		$cache = $this->prepare();
227

228
		$this->assertNotNull($cache->get('number_test'));
229
		$this->assertTrue($cache->delete('number_test'));
230
		$this->assertFalse($cache->get('number_test'));
231 232 233 234
	}

	public function testFlush()
	{
235
		$cache = $this->prepare();
236
		$this->assertTrue($cache->flush());
237
		$this->assertFalse($cache->get('number_test'));
238 239
	}
}