Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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->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!

like image 749
Stephen Mariano Cabrera Avatar asked Sep 03 '25 16:09

Stephen Mariano Cabrera


1 Answers

Artisan::call('cache:clear');

is better approach.

like image 194
Kostiantyn Avatar answered Sep 05 '25 07:09

Kostiantyn