无论内存缓存服务器是否可用,我都想确保我的代码始终按预期工作。
我的大部分函数看起来像这样:
function foo($parameter,$force = FALSE)
{
$result = Cache::get('foo_'.$parameter);
if (empty($result) || $force)
{
// Do stuff with the DB...
$result = "something";
Cache::put('foo_'.$parameter,$result,$timeout);
}
return $result;
}
现在在一个 TestCase 中我这样做:
class MyClassTest extends PHPUnit_Framework_TestCase {
function testFoo()
{
$result = $myClass->foo($parameter);
$this->assertSomething($result);
}
}
我可以在 PHPUnit 的 setUp()
期间全局禁用缓存,如下所示:
class MyClassTest extends PHPUnit_Framework_TestCase {
protected function setUp()
{
Cache::disable();
}
}
在调用 Cache::disable()
之后,所有对 Cache::get()
的调用都将在该请求期间返回 false
。现在,我想将此类中的所有测试运行两次,一次使用 Cache::disable();
,一次不使用。
关于如何做到这一点有什么想法吗?
最佳答案
一个选择是创建一个 MyClassNoCacheTest
扩展自 MyClassTest
并覆盖(或实现)MyClassNoCacheTest 中的
setUp
方法
关于PHPUnit - 在缓存打开/关闭的情况下运行所有测试两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12597432/