Browse Source

better testing: time()-function-mock

as discussed in #203
https://github.com/yiisoft/yii2/issues/203#issuecomment-17759631
tags/2.0.0-beta
Carsten Brandt 12 years ago
parent
commit
e2513de6c6
  1. 23
      tests/unit/framework/caching/CacheTest.php
  2. 13
      tests/unit/framework/caching/DbCacheTest.php
  3. 11
      tests/unit/framework/caching/FileCacheTest.php

23
tests/unit/framework/caching/CacheTest.php

@ -1,5 +1,17 @@
<?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
*/

13
tests/unit/framework/caching/DbCacheTest.php

@ -1,5 +1,7 @@
<?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'));
}
}

11
tests/unit/framework/caching/FileCacheTest.php

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

Loading…
Cancel
Save