4

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->amOnPage('users/login');
    $I->fillField('email', $username);
    $I->fillField('password', $password);
    $I->click('Login');
    $I->amOnPage('admin/');
    $I->see('Welcome');
  }

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');
    $cache->flush();
  }

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
modules:
    enabled:
        - PhpBrowser
        - AcceptanceHelper
        - Laravel4
    config:
        PhpBrowser:
          url: 'http://104.131.29.69:8000/'
        Laravel4:
          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();
Cache::flush();
}

But I just wind up getting this error instead:

Class 'Codeception\Module\Cache' not found

So I'm stuck. Thanks!

Community
  • 1
  • 1

2 Answers2

2

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
modules:
    enabled:
        - 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.

Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
  • 2
    You can also use module called FileSystem: http://codeception.com/docs/modules/Filesystem Just enable it in your acceptance.suite.yml file: `modules: enabled: - Cli` Then you can write in your test something like: `$I->cleanDir('app/storage/cache');` – Luděk Veselý Aug 03 '15 at 11:00
2
Artisan::call('cache:clear');

is better approach.

Kostiantyn
  • 1,792
  • 2
  • 16
  • 21