CacheTest.php 4.08 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12

namespace yii\caching;

/**
 * Mock for the time() function for caching classes
 * @return int
 */
function time() {
	return \yiiunit\framework\caching\CacheTest::$time ?: \time();
}

13
namespace yiiunit\framework\caching;
14

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

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

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

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

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

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	/**
	 * @return Cache
	 */
	public function prepare()
	{
		$cache = $this->getCacheInstance();

		$cache->flush();
		$cache->set('string_test', 'string_test');
		$cache->set('number_test', 42);
		$cache->set('array_test', array('array_test' => 'array_test'));
		$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);
		$this->assertEquals(\Yii::$app->id, $cache->keyPrefix);
	}

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

75 76 77
		$this->assertTrue($cache->set('string_test', 'string_test'));
		$this->assertTrue($cache->set('number_test', 42));
		$this->assertTrue($cache->set('array_test', array('array_test' => 'array_test')));
78 79 80 81
	}

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

84 85 86 87 88 89 90
		$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']);
91
	}
92

93 94 95 96 97
	public function testArrayAccess()
	{
		$cache = $this->getCacheInstance();

		$cache['arrayaccess_test'] = new \stdClass();
98 99 100
		$this->assertInstanceOf('stdClass', $cache['arrayaccess_test']);
	}

101
	public function testGetNonExistent()
102 103
	{
		$cache = $this->getCacheInstance();
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

		$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();

123
		$this->assertEquals(array('string_test' => 'string_test', 'number_test' => 42), $cache->mget(array('string_test', 'number_test')));
124 125 126
		// ensure that order does not matter
		$this->assertEquals(array('number_test' => 42, 'string_test' => 'string_test'), $cache->mget(array('number_test', 'string_test')));
		$this->assertEquals(array('number_test' => 42, 'non_existent_key' => null), $cache->mget(array('number_test', 'non_existent_key')));
127 128 129 130 131
	}

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

133
		$this->assertTrue($cache->set('expire_test', 'expire_test', 2));
134 135
		sleep(1);
		$this->assertEquals('expire_test', $cache->get('expire_test'));
136 137
		// wait a bit more than 2 sec to avoid random test failure
		usleep(2500000);
138
		$this->assertFalse($cache->get('expire_test'));
139 140 141 142
	}

	public function testAdd()
	{
143
		$cache = $this->prepare();
144 145

		// should not change existing keys
146
		$this->assertFalse($cache->add('number_test', 13));
147 148
		$this->assertEquals(42, $cache->get('number_test'));

149 150
		// should store data if it's not there yet
		$this->assertFalse($cache->get('add_test'));
151
		$this->assertTrue($cache->add('add_test', 13));
152 153 154 155 156
		$this->assertEquals(13, $cache->get('add_test'));
	}

	public function testDelete()
	{
157
		$cache = $this->prepare();
158

159
		$this->assertNotNull($cache->get('number_test'));
160
		$this->assertTrue($cache->delete('number_test'));
161
		$this->assertFalse($cache->get('number_test'));
162 163 164 165
	}

	public function testFlush()
	{
166
		$cache = $this->prepare();
167
		$this->assertTrue($cache->flush());
168
		$this->assertFalse($cache->get('number_test'));
169 170
	}
}