CacheTestCase.php 4.62 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
use yii\helpers\StringHelper;
17 18 19 20 21 22
use yiiunit\TestCase;
use yii\caching\Cache;

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

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

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

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

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	/**
	 * @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);
70
		$this->assertNotNull($cache->keyPrefix);
71 72
	}

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

77 78 79
		$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')));
80 81 82 83
	}

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

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

95 96 97 98 99 100 101 102 103 104 105 106
	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'));
	}

107 108 109 110 111
	public function testArrayAccess()
	{
		$cache = $this->getCacheInstance();

		$cache['arrayaccess_test'] = new \stdClass();
112 113 114
		$this->assertInstanceOf('stdClass', $cache['arrayaccess_test']);
	}

115
	public function testGetNonExistent()
116 117
	{
		$cache = $this->getCacheInstance();
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

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

137
		$this->assertEquals(array('string_test' => 'string_test', 'number_test' => 42), $cache->mget(array('string_test', 'number_test')));
138 139 140
		// 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')));
141 142 143 144 145
	}

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

147
		$this->assertTrue($cache->set('expire_test', 'expire_test', 2));
148 149
		sleep(1);
		$this->assertEquals('expire_test', $cache->get('expire_test'));
150
		// wait a bit more than 2 sec to avoid random test failure
151
		if (isset($_ENV['TRAVIS']) && substr(StringHelper::basename(get_class($this)), 0, 8) == 'MemCache') {
152 153 154 155
			sleep(3); // usleep with 2,5 seconds does not work well on travis and memcache
		} else {
			usleep(2500000);
		}
156
		$this->assertFalse($cache->get('expire_test'));
157 158 159 160
	}

	public function testAdd()
	{
161
		$cache = $this->prepare();
162 163

		// should not change existing keys
164
		$this->assertFalse($cache->add('number_test', 13));
165 166
		$this->assertEquals(42, $cache->get('number_test'));

167 168
		// should store data if it's not there yet
		$this->assertFalse($cache->get('add_test'));
169
		$this->assertTrue($cache->add('add_test', 13));
170 171 172 173 174
		$this->assertEquals(13, $cache->get('add_test'));
	}

	public function testDelete()
	{
175
		$cache = $this->prepare();
176

177
		$this->assertNotNull($cache->get('number_test'));
178
		$this->assertTrue($cache->delete('number_test'));
179
		$this->assertFalse($cache->get('number_test'));
180 181 182 183
	}

	public function testFlush()
	{
184
		$cache = $this->prepare();
185
		$this->assertTrue($cache->flush());
186
		$this->assertFalse($cache->get('number_test'));
187 188
	}
}