Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit - Running all tests twice with cache on/off?

I'd like to make sure my code always works as intended whether a memcached server is available or not.

Most of my functions look like this:

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;
}

Now in a TestCase I'm doing this:

class MyClassTest extends PHPUnit_Framework_TestCase {
    function testFoo()
    {
        $result = $myClass->foo($parameter);
        $this->assertSomething($result);
    }
}

I can disable caches globally during PHPUnit's setUp() like this:

class MyClassTest extends PHPUnit_Framework_TestCase {

    protected function setUp()
    {
        Cache::disable();
    }
}

After calling Cache::disable(), all calls to Cache::get() will return false during that request. Now, I want to run all tests in this class twice, once with Cache::disable(); and once without.

Any ideas on how to do that?

like image 737
Duru Can Celasun Avatar asked Jan 15 '23 11:01

Duru Can Celasun


1 Answers

One option is to create a MyClassNoCacheTest that extends from MyClassTest and just overwrite (or implement) the setUp method in MyClassNoCacheTest

like image 86
mrzard Avatar answered Jan 22 '23 02:01

mrzard