How to Integrate Codeception’s c3.php into Laravel4

(Before we start here, if you’d like to see this, and other technical posts like this, done on video in the future, please say so in the comments below. Thanks!)

After banging my head against a wall trying to integrate Codeception’s c3.php into Laravel for code coverage, I realized it wasn’t as hard as I had thought. ┬áRegardless, here’s my post on how I did it.

Prerequisites

You’ll need xdebug installed with remote_enable option turned on.

Here we go

I was doing functional tests, so here’s an example functional.suite.yml with the coverage element added. You’ll want to add that to any/all your test .yml files.

# Codeception Test Suite Configuration

# suite for functional (integration) tests.
# emulate web requests and make application process them.
# Include one of framework modules (Symfony2, Yii2, Laravel4) to use it.

class_name: FunctionalTester
modules:
    enabled: [Filesystem, FunctionalHelper, Laravel4, Db]
    config:
        Laravel4:
            cleanup: true
    coverage:
        remote: true

And then you add the coverage element with attributes as you need to configure into codeception.yml, which should also be in the root of your project.

Here’s mine:

actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    helpers: tests/_support
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
coverage:
    enabled: true
    remote: true
    include:
        - app/*
    exclude:
        - app/config/*
        - app/database/*
        - app/lang/*
        - app/storage/*
        - app/tests/*
modules:
    config:
        Db:
            dsn: 'mysql:host=localhost;dbname=eg_testing'
            user: 'homestead'
            password: 'secret'
            dump: tests/_data/dump.sql

Put the c3.php file in the root of your project (not the app directory). Then, make sure you require it in your /public/index.php like this:

/**
 * Adding c3.php for code coverage during codeception tests
 * ref: https://github.com/Codeception/c3
 */

require __DIR__.'/../c3.php';

If you ran a test right now, it’s supposed to create a directory called c3tmp, but sometimes it doesn’t do it. So, I created my own c3tmp directory inside the root of the project and then did chmod 0777 c3tmp to make 100% certain there weren’t permissions problems writing to it.

Run a test using the --coverage flag. I ran codecept run functional --coverage.

Voila. You should see something like this screenshot.

Codeception c3 code coverage

If you want a pretty HTML report… because I like pretty HTML things… You can instead run codecept run functional --coverage-html.

Then go to your /tests/_output/coverage folder and open index.html in a browser.

Ta da!

Codeception HTML code coverage in Laravel

Time to stop writing this post and start writing more tests for better coverage.

Cheers!