CacheTest.php 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
namespace yiiunit\framework\caching;
use yiiunit\TestCase;
use yii\caching\Cache;

/**
 * Base class for testing cache backends
 */
abstract class CacheTest extends TestCase
{
	/**
	 * @return Cache
	 */
	abstract protected function getCacheInstance();

16 17 18
	protected function setUp()
	{
		parent::setUp();
19
		$this->mockApplication();
20
	}
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

	/**
	 * @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);
	}

48 49 50
	public function testSet()
	{
		$cache = $this->getCacheInstance();
51

52 53 54
		$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')));
55 56 57 58
	}

	public function testGet()
	{
59 60
		$cache = $this->prepare();

61 62 63 64 65 66 67
		$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']);
68
	}
69

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

		$cache['arrayaccess_test'] = new \stdClass();
75 76 77
		$this->assertInstanceOf('stdClass', $cache['arrayaccess_test']);
	}

78
	public function testGetNonExistent()
79 80
	{
		$cache = $this->getCacheInstance();
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

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

100
		$this->assertEquals(array('string_test' => 'string_test', 'number_test' => 42), $cache->mget(array('string_test', 'number_test')));
101 102 103
		// 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')));
104 105 106 107 108
	}

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

110
		$this->assertTrue($cache->set('expire_test', 'expire_test', 2));
111 112
		sleep(1);
		$this->assertEquals('expire_test', $cache->get('expire_test'));
113 114
		// wait a bit more than 2 sec to avoid random test failure
		usleep(2500000);
115
		$this->assertFalse($cache->get('expire_test'));
116 117 118 119
	}

	public function testAdd()
	{
120
		$cache = $this->prepare();
121 122

		// should not change existing keys
123
		$this->assertFalse($cache->add('number_test', 13));
124 125
		$this->assertEquals(42, $cache->get('number_test'));

126 127
		// should store data if it's not there yet
		$this->assertFalse($cache->get('add_test'));
128
		$this->assertTrue($cache->add('add_test', 13));
129 130 131 132 133
		$this->assertEquals(13, $cache->get('add_test'));
	}

	public function testDelete()
	{
134
		$cache = $this->prepare();
135

136
		$this->assertNotNull($cache->get('number_test'));
137
		$this->assertTrue($cache->delete('number_test'));
138
		$this->assertFalse($cache->get('number_test'));
139 140 141 142
	}

	public function testFlush()
	{
143
		$cache = $this->prepare();
144
		$this->assertTrue($cache->flush());
145
		$this->assertFalse($cache->get('number_test'));
146 147
	}
}