1
votes

I'm using Laravel and Codeception to do acceptance testing. Most of my tests require users to be logged in, so I wanted to extract those behaviours to the _bootstrap.php file inside the acceptance folder. I understand this is the practice for running setup prior to your tests, in Codeception.

I can confirm that my users table exists (I'm viewing it using Navicat), and that a user with an id of 1 exists. Codeception, to the best of my knowledge, is properly configured to use this database. I also have included the Laravel4 and Db modules for the acceptance suite. I receive the following error when I try to run my acceptance suite, or any individual acceptance Cept.

console:

[Illuminate\Database\QueryException]
SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "users" does not exist
LINE 1: select * from "users" where "id" = $1 limit 1
                      ^ (SQL: select * from "users" where "id" = 1 limit 1)

I get this error regardless of the contents of any of the acceptance Cepts. Even a simple $I->amOnPage('/'), or $I->comment('test') still results in the error. I believe the error occurs as soon as it encounters the command in the _bootstrap.php file. Somehow it's able to access my database. I should note that I've also tried various combinations of enabling and disabling the populate and cleanup settings for the Laravel4 and Db modules, to no avail.

Here is my acceptance/_bootstrap.php file:

<?php

Auth::loginUsingId(1);

codeception.yml:

actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    helpers: tests/_support
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
modules:
    config:
        Db:
            dsn: 'pgsql:host=localhost;dbname=myapp'
            user: 'someuser'
            password: 'somepass'
            dump: tests/_data/dump.sql

acceptance.suite.yml:

class_name: AcceptanceTester
modules:
    enabled:
        - AcceptanceHelper
        - Laravel4
        - PhpBrowser
        - Db
    config:
        PhpBrowser:
            url: 'http://localhost'
        Laravel4:
            cleanup: false
        Db:
            populate: true
            cleanup: false
1

1 Answers

4
votes

Firstly, you should not use Laravel4 module for Codeception acceptance tests. It is only for functional and unit tests. See this answer for more info on this issue.

To answer your specific question about automatically logging in your user on your acceptance tests - this is how I do it:

/tests/_support/AcceptanceHelper.php

<?php
namespace Codeception\Module;
use \AcceptanceTester;

class AcceptanceHelper extends \Codeception\Module
{
    // This is my default user I use for all my tests
    public function loginUser(AcceptanceTester $i)
    {
        $this->login($i, '[email protected]', 'tester');
    }  

    // And this function lets me login a specific user if I need someone else
    public function login(AcceptanceTester $i, $email, $password)
    {
        $i->amOnPage('/login');
        $i->fillField('email', $email);
        $i->fillField('password', $password);
        $i->click('submit');
        $i->seeCurrentUrlEquals('/dashboard');
    }

    public function logoutUser(AcceptanceTester $i)
    {
        $i->amOnPage('/logout');
        $i->seeCurrentUrlEquals('/login');
        $i->see('You have been logged out');
    }
}

/tests/acceptance/ExampleCest.php

<?php
use \AcceptanceTester;

class ExampleCest
{
    public function _before(AcceptanceTester $i)
    {
        $i->loginUser($i);
    }


    public function tryToViewDashboard(AcceptanceTester $i)
    {   
        // User is logged in here 
        $i->amOnPage('/dashboard');
    }

    public function tryOtherExample(AcceptanceTester $i)
    {   
        // User is logged in here 
        $i->amOnPage('/dashboard');
    }

    public function tryMoreInfo(AcceptanceTester $i)
    {   
        // User is logged in here 
        $i->amOnPage('/dashboard');
    }
}

Note: I use a smallcase $i instead of $I - I find it easier to type - so you might need to change all the $i to $I in your code