A codeception module to test your CakePHP 3 powered application. Using Codeception with CakePHP opens up a whole new set of testing capabilities.
#####Front-end testing (i.e. browser-based workflow tests)
- [Functional Tests]
- does the App run the way we expect
- [Acceptance Tests]
- does the App do what the customer/user expects
#####Back-end testing (i.e. direct, internal method tests)
- [API Tests]
- does the App's API work
- [Unit Tests]
- do the App's internal functions do what we expect
From a CakePHP application, run the following from the command-line:
$ composer require --dev cakephp/codeception:dev-master && composer run-script post-install-cmdIf you are developing a plugin, add the post-install script to your composer.json first:
{
"scripts": {
"post-install-cmd": "Cake\\Codeception\\Console\\Installer::customizeCodeceptionBinary"
}
}Once installed, you can now run bootstrap which will create all the codeception required files
in your application:
$ vendor/bin/codecept bootstrapThis creates the following files/folders in your app directory:
├── codeception.yml
├── src
│ └── TestSuite
│ └── Codeception
│ ├── AcceptanceHelper.php
│ ├── FunctionalHelper.php
│ └── UnitHelper.php
└── tests
├── Acceptance.suite.yml
├── Functional.suite.yml
├── Unit.suite.yml
├── Acceptance
│ ├── .gitignore
│ ├── bootstrap.php
│ └── AcceptanceTester.php
├── Fixture
│ └── dump.sql
├── Functional
│ ├── .gitignore
│ ├── bootstrap.php
│ └── FunctionalTester.php
└── Unit
├── .gitignore
├── bootstrap.php
└── UnitTester.php
As you might have noticed, the CakePHP implementation differs in a couple things:
- uses CamelCase suite names (
Functionalvs.functional) - uses
bootstrap.php, no underscore prefix (vs._bootstrap.php) - uses
src/TestSuite/Codeceptionfor custom modules (helpers) (vs.tests/_helpers) - uses
tmp/teststo store logs (vs.tests/_logs) - adds a
.gitignoreto never track auto-generated files - adds custom templates for various generated files using the
codeceptbinary
To better understand how Codeception tests work, please check the official documentation.
<?php
$I = new FunctionalTester($scenario);
$I->wantTo('ensure that adding a bookmark works');
$I->amOnPage('/bookmarks/add');
$I->see('Submit');
$I->submitForm('#add', [
'title' => 'First bookmark',
]);
$I->seeInSession([
'Flash'
]);...
$I->seeInConfig('App.name'); // checks only that the key exists
$I->seeInConfig('App.name', 'CakePHP');
$I->seeInConfig(['App.name' => 'CakePHP']);$I->dontSeeInConfig('App.name'); // checks only that the key does not exist
$I->dontSeeInConfig('App.name', 'CakePHP');
$I->dontSeeInConfig(['App.name' => 'CakePHP']);This is useful when you need a record for just one test (temporary fixture). It does not assert anything and returns the inserted record's ID.
$I->haveRecord('users', ['email' => '[email protected]', 'username' => 'jadb']);This is a wrapper around the Cake\ORM\Table::find('first') method.
$I->grabRecord('users', ['id' => '1']);This checks that the requested record does exist in the database.
$I->seeRecord('users', ['username' => 'jadb']);This checks that the request record does not exist in the database.
$I->dontSeeRecord('users', ['email' => '[email protected]']);...
All the below forms are equivalent:
$I->loadFixtures('app.posts', 'app.tags');
$I->loadFixtures(['app.posts', 'app.tags']);
$I->fixtures = ['app.posts', 'app.tags'];$I->expectedCakePHPVersion('3.0.4');All the below forms are equivalent:
$I->amOnRoute(['controller' => 'Posts', 'action' => 'add']);
$I->amOnRoute('addPost'); // assuming there is a route named `addPost`All the below forms are equivalent:
$I->amOnAction('Posts@add');
$I->amOnAction('Posts.add');
$I->amOnAction('PostsController@add');
$I->amOnAction('PostsController.add');
$I->amOnAction('posts@add');
$I->amOnAction('posts.add');All the below forms are equivalent:
$I->seeCurrentRouteIs(['controller' => 'Posts', 'action' => 'add']);
$I->seeCurrentRouteIs('addPost'); // assuming there is a route named `addPost`All the below forms are equivalent:
$I->seeCurrentActionIs('Posts@add');
$I->seeCurrentActionIs('Posts.add');
$I->seeCurrentActionIs('PostsController@add');
$I->seeCurrentActionIs('PostsController.add');
$I->seeCurrentActionIs('posts@add');
$I->seeCurrentActionIs('posts.add');$I->haveInSession('redirect', Router::url(['_name' => 'dashboard']));
$I->haveInSession(['redirect' => Router::url(['_name' => 'dashboard'])]);$I->seeInSession('redirect'); // only checks the key exists.
$I->seeInSession('redirect', Router::url(['_name' => 'dashboard']));
$I->seeInSession(['redirect', Router::url(['_name' => 'dashboard'])]);$I->dontSeeInSession('redirect'); // only checks the key does not exist.
$I->dontSeeInSession('redirect', Router::url(['_name' => 'dashboard']));
$I->dontSeeInSession(['redirect', Router::url(['_name' => 'dashboard'])]);...
