You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					171 lines
				
				4.1 KiB
			
		
		
			
		
	
	
					171 lines
				
				4.1 KiB
			| 
											13 years ago
										 | <?php
 | ||
| 
											13 years ago
										 | 
 | ||
|  | namespace yii\caching;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * Mock for the time() function for caching classes
 | ||
|  |  * @return int
 | ||
|  |  */
 | ||
|  | function time() {
 | ||
| 
											13 years ago
										 | 	return \yiiunit\framework\caching\CacheTestCase::$time ?: \time();
 | ||
| 
											13 years ago
										 | }
 | ||
|  | 
 | ||
| 
											13 years ago
										 | namespace yiiunit\framework\caching;
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | use yiiunit\TestCase;
 | ||
|  | use yii\caching\Cache;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * Base class for testing cache backends
 | ||
|  |  */
 | ||
| 
											13 years ago
										 | abstract class CacheTestCase extends TestCase
 | ||
| 
											13 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var int virtual time to be returned by mocked time() function.
 | ||
|  | 	 * Null means normal time() behavior.
 | ||
|  | 	 */
 | ||
|  | 	public static $time;
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @return Cache
 | ||
|  | 	 */
 | ||
|  | 	abstract protected function getCacheInstance();
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	protected function setUp()
 | ||
|  | 	{
 | ||
|  | 		parent::setUp();
 | ||
| 
											13 years ago
										 | 		$this->mockApplication();
 | ||
| 
											13 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 	protected function tearDown()
 | ||
|  | 	{
 | ||
|  | 		static::$time = null;
 | ||
|  | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @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);
 | ||
|  | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	public function testSet()
 | ||
|  | 	{
 | ||
|  | 		$cache = $this->getCacheInstance();
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		$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')));
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	public function testGet()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$cache = $this->prepare();
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		$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']);
 | ||
| 
											13 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 	public function testArrayAccess()
 | ||
|  | 	{
 | ||
|  | 		$cache = $this->getCacheInstance();
 | ||
|  | 
 | ||
|  | 		$cache['arrayaccess_test'] = new \stdClass();
 | ||
| 
											13 years ago
										 | 		$this->assertInstanceOf('stdClass', $cache['arrayaccess_test']);
 | ||
|  | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	public function testGetNonExistent()
 | ||
| 
											13 years ago
										 | 	{
 | ||
|  | 		$cache = $this->getCacheInstance();
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 		$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();
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		$this->assertEquals(array('string_test' => 'string_test', 'number_test' => 42), $cache->mget(array('string_test', 'number_test')));
 | ||
| 
											13 years ago
										 | 		// 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')));
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	public function testExpire()
 | ||
|  | 	{
 | ||
|  | 		$cache = $this->getCacheInstance();
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		$this->assertTrue($cache->set('expire_test', 'expire_test', 2));
 | ||
| 
											13 years ago
										 | 		sleep(1);
 | ||
|  | 		$this->assertEquals('expire_test', $cache->get('expire_test'));
 | ||
| 
											13 years ago
										 | 		// wait a bit more than 2 sec to avoid random test failure
 | ||
|  | 		usleep(2500000);
 | ||
| 
											13 years ago
										 | 		$this->assertFalse($cache->get('expire_test'));
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	public function testAdd()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$cache = $this->prepare();
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 		// should not change existing keys
 | ||
| 
											13 years ago
										 | 		$this->assertFalse($cache->add('number_test', 13));
 | ||
| 
											13 years ago
										 | 		$this->assertEquals(42, $cache->get('number_test'));
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		// should store data if it's not there yet
 | ||
|  | 		$this->assertFalse($cache->get('add_test'));
 | ||
| 
											13 years ago
										 | 		$this->assertTrue($cache->add('add_test', 13));
 | ||
| 
											13 years ago
										 | 		$this->assertEquals(13, $cache->get('add_test'));
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	public function testDelete()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$cache = $this->prepare();
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		$this->assertNotNull($cache->get('number_test'));
 | ||
| 
											13 years ago
										 | 		$this->assertTrue($cache->delete('number_test'));
 | ||
| 
											13 years ago
										 | 		$this->assertFalse($cache->get('number_test'));
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	public function testFlush()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$cache = $this->prepare();
 | ||
| 
											13 years ago
										 | 		$this->assertTrue($cache->flush());
 | ||
| 
											13 years ago
										 | 		$this->assertFalse($cache->get('number_test'));
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | }
 |