Stephen Mariano Cabrera Stephen Mariano Cabrera - 2 years ago 132
PHP Question

Clearing cache for laravel 4 app before codeception tests

I'm using codeception to run acceptance tests for a laravel app. One problem I've been into is is that my login tests start failing when the login page gets cached and, I guess, I'm getting logged in automatically. I think this is the case because my tests start passing again when I clear the cache and they'll generally start failing without my having changed the tests or the application code at all.

Here's the login test in question, now extracted into a helper method

public function login($username, $password, $I) {
$I->fillField('email', $username);
$I->fillField('password', $password);

I've been clearing the cache periodically whenever the tests fail but it's becoming tedious. I'd like to be able to register a helper to clear the cache for me and just call the function in all my tests. I extracted the function into a helper as suggested here with the following function in AcceptanceHelper.php:

public function clearCache($I) {
$cache = $this->getModule('Cache');

This seems to be what was suggested by the documentation here, but I get the error "Module Cache couldn't be connected". It looked like I needed the Laravel4 module so I added that to my acceptance.suite.yml file but no luck there either I received this error:

SQLSTATE[28000] [1045] Access denied for user 'stephen'@'localhost' (using password: NO)

I thought I'd need to authorize mysql in the config file, but that didn't seem to work either. Here's my acceptance.suite.yml file:

class_name: AcceptanceTester
- PhpBrowser
- AcceptanceHelper
- Laravel4
url: ''
user: 'root'
password: 'pAsSwOrD'

Finally I read this answer and it seemed as though I shouldn't actually have included Laravel4 in the config file and that my helper function should look more like this:

public function clearCache($I) {
$L = $I->getLaravel4();

But I just wind up getting this error instead:

Class 'Codeception\Module\Cache' not found

So I'm stuck. Thanks!

Answer Source

Ok so I figured out how to do this. Apparently there's a module called cli which you can include in the acceptance.suite.yml file like this:

class_name: AcceptanceTester
        - PhpBrowser
        - AcceptanceHelper
        - Cli

This module allows you to use shell commands in your script with the runShellCommand() function. Since my cache is stored in files in the app/storage/cache/ directory the shell command I need to execute is:

rm app/storage/cache/*

and voilá cache cleared. Then in the test file it will look like this:

$I->runShellCommand('rm -rf app/storage/cache/*');

I decided to simplify this a bit by including it in the function I used to login, since I knew I'd want to clear the cache before each login I just included that line in a login function inside AcceptanceHelper, which then is accessible in all of my tests.

This is a bit of workaround since it isn't agnostic to the kind of caching I'm using (if I were to use memcached I would need to do something different) but it worked for me so I thought I'd share it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download