Lumen 5 with Codeception

In this tutorial, I will show you how to replace the default Lumen 5 testing suite with Codeception.



Introduction

In my previous tutorial about Laravel 5 with Codeception I have shown you how to replace the default Laravel 5 testing suite (PHPUnit) with Codeception. In this tutorial, I will show you how to replace the default Lumen 5 testing suite (PHPUnit) with Codeception.

It has been a long time since I have dealt with Lumen. In my tutorial JSON Web Token Authentication for Lumen REBOOT I have made a conclusion about Lumen:

If you need a full blown API you are better of with Laravel. If you need a simple API that does a thing or two, you will be better of with Lumen, but if you get to the point where you need JWT in Lumen you are probably off track and doing too much in it.

I must admit, since then I have not worked with Lumen so I thought that this tutorial will be a nice introduction back to Lumen.

Let's begin.

This is the current state of the repository at this moment 618d292f2651b1557e3161e1e6d1eb22ddbdd99f.

Install Codeception

We will install Codeception as a development dependency using Composer from the command line:

composer require codeception/codeception --dev

Output:

$ composer require codeception/codeception --dev
Using version ^2.3 for codeception/codeception
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing symfony/dom-crawler (v3.3.2)
    Loading from cache

  - Installing symfony/css-selector (v3.3.2)
    Loading from cache

  - Installing symfony/browser-kit (v3.3.2)
    Loading from cache

  - Installing stecman/symfony-console-completion (0.7.0)
    Loading from cache

  - Installing psr/http-message (1.0.1)
    Loading from cache

  - Installing guzzlehttp/psr7 (1.4.2)
    Loading from cache

  - Installing guzzlehttp/promises (v1.3.1)
    Loading from cache

  - Installing guzzlehttp/guzzle (6.2.3)
    Loading from cache

  - Installing facebook/webdriver (1.4.1)
    Loading from cache

  - Installing behat/gherkin (v4.4.5)
    Downloading: 100%

  - Installing codeception/codeception (2.3.3)
    Loading from cache

This is the current state of the repository at this moment 61c7a8cc07f3db39605be8a342884b88082accea.

Default testing suite cleanup

First, we will remove the "phpunit/phpunit": "~5.0" dependency from composer.json. Then run composer update to reflect the change.

Now from the application root delete the file phpunit.xml, and delete the directory called tests.

This is the current state of the repository at this moment 6f79e76d3b8f08eec49ec6ddf8a989d7eab7d428.

Bootstrapping Codeception

To bootstrap (finish installing/integrate) Codeception we need to execute this command:

vendor/bin/codecept bootstrap

Output:

$ vendor/bin/codecept bootstrap
 Bootstrapping Codeception

File codeception.yml created       <- global configuration
> Unit helper has been created in tests/_support\Helper
> UnitTester actor has been created in tests/_support
> Actions have been loaded
tests/unit created                 <- unit tests
tests/unit.suite.yml written       <- unit tests suite configuration
> Functional helper has been created in tests/_support\Helper
> FunctionalTester actor has been created in tests/_support
> Actions have been loaded
tests/functional created           <- functional tests
tests/functional.suite.yml written <- functional tests suite configuration
> Acceptance helper has been created in tests/_support\Helper
> AcceptanceTester actor has been created in tests/_support
> Actions have been loaded
tests/acceptance created           <- acceptance tests
tests/acceptance.suite.yml written <- acceptance tests suite configuration
 ---

 Codeception is installed for acceptance, functional, and unit testing

As you can see Codeception is installed for acceptance, functional, and unit testing. Also, the tests directory is recreated with the Codeception folder structure. You can change the structure if needed, but I find this to be the best option.

This is the current state of the repository at this moment 8819e8d47e851e092bc459237a9ce552d7571a12.

Installing Lumen module

For this tutorial, I will write a functional test that simply checks if the page / returns 200 HTTP code and if there is text on page Lumen.

To better integrate Lumen with Codeception there is a module called Lumen. It enables you to use methods like:

  • amLoggedAs
  • amOnPage
  • amOnRoute
  • click
  • dontSee
  • fillField
  • have
  • see
  • seeLink
  • seePageNotFound
  • seeRecord

And many other useful helper methods.

Other than being able to use helpful methods, it comes with a variety of configuration options that you can tweak:

  • cleanup
  • bootstrap
  • root
  • packages
  • url

This module is to be used for functional testing only. That means that we need to enable it in tests/functional.suite.yml. Make that file look like this:

actor: FunctionalTester
modules:
    enabled:
        - Lumen
        - \Helper\Functional

Now run from the command line:

vendor/bin/codecept build

Awesome! Installation complete.

Optional: If you want to change the configuration options for the module you can do that like so:

actor: FunctionalTester
modules:
    enabled:
        - Lumen:
            cleanup: false
        - \Helper\Functional

This is the current state of the repository at this moment fe222ecae5d97af7d52bec12567cddadf9adf4a7.

Your first functional Codeception test

To create a new functional test using the cept format, run this command:

vendor/bin/codecept generate:cept functional WelcomePage

Output:

$ vendor/bin/codecept generate:cept functional WelcomePage
Test was created in C:\repos\lumen-codeception\tests\functional\WelcomePageCept.php

You can find the test in \tests\functional\WelcomePageCept.php. Now modify the file so that it looks like this:

<?php
$I = new FunctionalTester($scenario);
$I->wantTo('view the welcome page');

$I->amOnPage('/');
$I->seeResponseCodeIs(200);
$I->see('Lumen');

We navigate to /, check if the response code is 200, and verify that we see text Lumen on the page.

Before we run the test there is one more thing to modify. Because our sample application does not use a database we need to modify our database values in the .env file:

DB_CONNECTION=sqlite
DB_DATABASE=:memory:

Remove all other keys starting with DB_.

Now, to run the test execute:

vendor/bin/codecept run functional

Output:

$ vendor/bin/codecept run functional
Codeception PHP Testing Framework v2.3.3
Powered by PHPUnit 6.2.1 by Sebastian Bergmann and contributors.

Functional Tests (1) ---------------------------------------------------------------------------------------------------
+ WelcomePageCept: View the welcome page (0.07s)
------------------------------------------------------------------------------------------------------------------------

Time: 538 ms, Memory: 12.00MB

OK (1 test, 2 assertions)

Success!

This is the current state of the repository at this moment ec84afdab710e6496187e0b601dae33ec193b141.

You are now ready to start writing tests for your Lumen 5 application.

Comments