Commit e2513de6 by Carsten Brandt

better testing: time()-function-mock

parent c4e9e003
<?php
namespace yii\caching;
/**
* Mock for the time() function for caching classes
* @return int
*/
function time() {
return \yiiunit\framework\caching\CacheTest::$time ?: \time();
}
namespace yiiunit\framework\caching;
use yiiunit\TestCase;
use yii\caching\Cache;
......@@ -9,6 +21,12 @@ use yii\caching\Cache;
abstract class CacheTest extends TestCase
{
/**
* @var int virtual time to be returned by mocked time() function.
* Null means normal time() behavior.
*/
public static $time;
/**
* @return Cache
*/
abstract protected function getCacheInstance();
......@@ -19,6 +37,11 @@ abstract class CacheTest extends TestCase
$this->mockApplication();
}
protected function tearDown()
{
static::$time = null;
}
/**
* @return Cache
*/
......
<?php
namespace yiiunit\framework\caching;
use yii\caching\DbCache;
use yiiunit\TestCase;
......@@ -70,4 +72,15 @@ class DbCacheTest extends CacheTest
}
return $this->_cacheInstance;
}
public function testExpire()
{
$cache = $this->getCacheInstance();
$this->assertTrue($cache->set('expire_test', 'expire_test', 2));
static::$time = time() + 1;
$this->assertEquals('expire_test', $cache->get('expire_test'));
static::$time = time() + 2;
$this->assertFalse($cache->get('expire_test'));
}
}
......@@ -22,4 +22,15 @@ class FileCacheTest extends CacheTest
}
return $this->_cacheInstance;
}
public function testExpire()
{
$cache = $this->getCacheInstance();
$this->assertTrue($cache->set('expire_test', 'expire_test', 2));
static::$time = time() + 1;
$this->assertEquals('expire_test', $cache->get('expire_test'));
static::$time = time() + 2;
$this->assertFalse($cache->get('expire_test'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment