From e2513de6c6207fa37894a944e10b8cd420ac6804 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sat, 11 May 2013 15:39:19 +0200 Subject: [PATCH] better testing: time()-function-mock as discussed in #203 https://github.com/yiisoft/yii2/issues/203#issuecomment-17759631 --- tests/unit/framework/caching/CacheTest.php | 23 +++++++++++++++++++++++ tests/unit/framework/caching/DbCacheTest.php | 13 +++++++++++++ tests/unit/framework/caching/FileCacheTest.php | 11 +++++++++++ 3 files changed, 47 insertions(+) diff --git a/tests/unit/framework/caching/CacheTest.php b/tests/unit/framework/caching/CacheTest.php index c524956..4b34de0 100644 --- a/tests/unit/framework/caching/CacheTest.php +++ b/tests/unit/framework/caching/CacheTest.php @@ -1,5 +1,17 @@ mockApplication(); } + protected function tearDown() + { + static::$time = null; + } + /** * @return Cache */ diff --git a/tests/unit/framework/caching/DbCacheTest.php b/tests/unit/framework/caching/DbCacheTest.php index 36174b9..a2ff9e3 100644 --- a/tests/unit/framework/caching/DbCacheTest.php +++ b/tests/unit/framework/caching/DbCacheTest.php @@ -1,5 +1,7 @@ _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')); + } } diff --git a/tests/unit/framework/caching/FileCacheTest.php b/tests/unit/framework/caching/FileCacheTest.php index 99e3cbc..4499d9c 100644 --- a/tests/unit/framework/caching/FileCacheTest.php +++ b/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')); + } }