PHPUnit - 在缓存打开/关闭的情况下运行所有​​测试两次?

标签 php unit-testing phpunit

无论内存缓存服务器是否可用,我都想确保我的代码始终按预期工作。

我的大部分函数看起来像这样:

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/

相关文章:

php - Laravel:根据每个项目的半径选择选择一定距离内的模型

php - 使用 vfsStream 将文件插入特定目录/节点

unit-testing - 用 enzyme react 测试,无法读取未定义的属性 'route'

java - "You need to use a Theme.Appcompat theme..."测试ActionBarActivity时,但我是

php - 工厂助手在 PHPUnit 设置方法中不起作用

php - 如何在 Mac OS X 10.5 上不使用 PEAR 安装 PHPUnit?

php - 将 https 与 elasticsearch-php 客户端一起使用

php - 如何使用准备好的语句在循环中更新多行

PHPUnit 错误 fatal error :调用未定义的方法 Mock_Game_073a8e20::method()

php - 如何将phpunit升级到新版本?