Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JanHenkG's avatar

Laravel 5 codeception module

I created a Codeception module for Laravel 5, you can find it here:

https://github.com/janhenkgerritsen/codeception-laravel5

I modified the existing Laravel 4 module so that it now works with the Laravel 5 development version. I use the module for my own tests without problems. But if you run into any problems just let me know.

0 likes
28 replies
jdubwelch's avatar

You are awesome. I've been hoping for this. I'll try it out today.

JeffreyWay's avatar

Nice! Was hoping that someone would tackle this sooner than later.

Did you tell Davert? He'll probably want to reference it on the Codeception site, once L5 is officially out.

2 likes
ryanw3b3r's avatar

I can't make it work! I installed Codeception phar file on my Homestead box, I added your package via composer, run all (I think) required commands and I'm stuck on "codeception build" which is giving me "Codeception\Exception\Configuration Laravel5 could not be found and loaded".

I checked the sample project on github, I can't find anything that I'm missing. I guess it has to be some kind of latest L5 change that's affecting your package. I hope I'm wrong. Help anyone?!

ryanw3b3r's avatar

Nevermind! It's all working now, but just to share my recent discovery (and it might be a silly one): you have to use codecept file from your project's vendor folder, not the global one.

I have codeception installed globally via phar archive as described on their website, but it didn't work with my Laravel 5.0 project and JanHenkG's package. Everything's working when I go into my project's folder and run

php ./vendor/codeception/codeception/codecept run
3 likes
jacojo's avatar

Hi @JanHenkG, I am just trying to complete the Larabook series but I got stuck with chapter 8 'Users'. Your module worked fine until now, but I don't get it managed to write a record to the database.

My testcase says the form has been submitted (I am checking whether 'Welcome to Larabooks' is displayed) but nothing has been written to the DB. It works fine if I submit the form manually from within the browser!

Any ideas?

I hope you or anybody else can help me out there...

UPDATE: Guess it is related to the XSRF-TOKEN. Found the following discussion: https://laracasts.com/discuss/channels/general-discussion/latest-v5-laravelframework-csrfmiddleware-changes-broke-codeception-functional-tests

hfalucas's avatar

Is it possible to set an sqlite memory database with codeception? If yes how?

1 like
ixudra's avatar

@JanHenkG Hey man, nice work! I've been running my unit tests with great success, but my functional/acceptance tests (basically, all tests which involve the database) are not working correctly. For my project, I have a development database and a separate database for testing. Your package sets the environment to "testing" as you'd expect, but it doesn't trigger the config modifications which have been entered in my phpunit.xml file. Because of this, Codeception runs all tests using my local database for testing purposes and thus overrides all data I have in there.

Any ideas how to work around that?

JanHenkG's avatar

@Elimentz As far as I know Codeception does not use the phpunit.xml file, this is used if you run tests with PHPUnit. So you should make your config changes for testing in the Laravel testing environment configuration.

ixudra's avatar

@JanHenkG See that's the thing: I tried making a new database config file in the config/testing environment, Taylor has removed the overriding config option from Laravel. Because of this, Laravel uses this testing config setup for both testing as well as local environment.

I also tried injecting a new env.testing file into the application when running unit tests but this was not possible since the environment config is loaded while the application is booted in /bootstrap/app.php so no luck there either.

The phpunit.xml file is the only way I got it to work so far and if that doesn't work, I'm afraid we're pretty much stuck

JanHenkG's avatar

I haven't updated the sample app in a while, I plan to do that this week. I will also look into this issue when I do that.

JanHenkG's avatar

I found a solution to the problem. The Laravel Application class has a method loadEnvironmentFrom($file) with which you can specify which environment file to load. It defaults to .env. I updated the module so that you can now specify a specific environment file to load for the test suites:

class_name: FunctionalTester
modules:
    enabled: [Laravel5, FunctionalHelper]
    config:
        Laravel5:
            environment_file: .env.testing

With this configuration for your suite the .env.testing file will be loaded instead of the default .env file.

You can check the sample app at https://github.com/janhenkgerritsen/codeception-laravel5-sample to see how I configured everything to use another database for testing. The most important thing to notice here is that I created another database connection sqlite_testing for the testing database. If you do not do this, you cannot migrate your testing database, because the environment parameter to the php artisan migrate command is ignored. But if you use a connection you can use the --database parameter as follows:

php artisan migrate --database=sqlite_testing

9 likes
orhanM's avatar

Great, thanks, this worked for me also. I am using mysql for testing. The only thing is that I had to hard code some database values in database.php to get mysql testDB to migrate. Do not really understand why the .env file can not be overridden in a simple way like in 4.2.. Not sure when to use "php artisan migrate --env=testing" anymore

pixelpeter's avatar

@JanHenkG When I run the tests with Laravel 5.0 inside homestead both functional tests from HelperCest will fail.

As far as I understand it, if we run Laravel from the console the value url from config/app.php will be used to build the correct url with helpers like route() or url(). I assume this is also the case when we run the functional tests. It looks to me like this is not working and it's always returning localhost no matter what value I set in config/app.php.

@all: Has anybody experienced the same problem and maybe a solution ?

Thanks

JanHenkG's avatar

@pixelpeter There is a fix for the route() and url() functions in the HEAD of the 2.0 branch of Codeception, this fix will be released with Codeception 2.0.15.

Also, please report issues on the issue tracker of the Codeception project on Github. That's the only place where I will check for issues regularly.

pixelpeter's avatar

@JanHenkG Thank you for your reply

I wasn't sure if it's a bug and I wasn't sure if it was Laravel or Codeception related. Otherwise I would have posted it on Github.

Thanks again

progmars's avatar

What about support for Laravel's expectsEvents ?

Alez's avatar

@Elimentz I solve it this way, I create a new environment in example for the functional tests

codecept generate:environment functional

then I create some directory within _envs directory, as example named "files" , and then just add this string to functional.yml file

modules:
    config:
        Laravel5:
            environment_file: tests/_envs/files/.functional

and finally I create tests/_envs/files/.functional file with my environment configuration

Now I can run codeception with desired environment

codecept run --env=functional
mcblum's avatar

@JanHenkG I followed your repo exactly and I can't get Laravel to use any .env at all! Here's my api.suite.yml

class_name: ApiTester
modules:
    enabled:
        - \Helper\Api
        - REST:
            depends: Laravel5
        - Laravel5:
            environment_file: .env.testing

What am I missing?! Been at this like two days :)

Please or to participate in this conversation.