cakephp/codeception

Name: codeception

Owner: CakePHP

Description: CakePHP module for Codeception

Created: 2015-02-09 15:32:47.0

Updated: 2018-04-11 05:51:29.0

Pushed: 2018-03-14 01:37:06.0

Homepage:

Size: 208

Language: PHP

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

CakePHP 3 Codeception Module

Build Status License

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)

Back-end testing

(i.e. direct, internal method tests)

Usage

From a CakePHP application, run the following from the command-line:

mposer require --dev cakephp/codeception:dev-master && composer run-script post-install-cmd

If 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:

ndor/bin/codecept bootstrap

This creates the following files/folders in your app directory:

codeception.yml
src
??? TestSuite
    ??? Codeception
        ??? AcceptanceTester.php
        ??? FunctionalTester.php
        ??? UnitTester.php
        ??? Helper
        ?   ??? Acceptance.php
        ?   ??? Functional.php
        ?   ??? Unit.php
        ??? _generated
            ??? .gitignore
tests
??? Acceptance.suite.yml
??? Functional.suite.yml
??? Unit.suite.yml
??? Acceptance
?   ??? bootstrap.php
??? Fixture
?   ??? dump.sql
??? Functional
?   ??? bootstrap.php
??? Unit
    ??? bootstrap.php

As you might have noticed, the CakePHP implementation differs in a couple things:

To better understand how Codeception tests work, please check the official documentation.

Example Cept
p
 new FunctionalTester($scenario);
wantTo('ensure that adding a bookmark works');
amOnPage('/bookmarks/add');
see('Submit');
submitForm('#add', [
'title' => 'First bookmark',

seeInSession([
'Flash'

Actions
Auth

Config
Assert config key(/value) with seeInConfig($key, $value = null)
seeInConfig('App.name'); // checks only that the key exists
seeInConfig('App.name', 'CakePHP');
seeInConfig(['App.name' => 'CakePHP']);
Assert no config key(/value) with dontSeeInConfig($key, $value = null)
dontSeeInConfig('App.name'); // checks only that the key does not exist
dontSeeInConfig('App.name', 'CakePHP');
dontSeeInConfig(['App.name' => 'CakePHP']);
Db
Insert record with haveRecord($model, $data = [])

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.

haveRecord('users', ['email' => 'jadb@cakephp.org', 'username' => 'jadb']);
Retrieve record with grabRecord($model, $conditions = [])

This is a wrapper around the Cake\ORM\Table::find('first') method.

grabRecord('users', ['id' => '1']);
Assert record exists with seeRecord($model, $conditions = [])

This checks that the requested record does exist in the database.

seeRecord('users', ['username' => 'jadb']);
Assert record does not exist with dontSeeRecord($model, $conditions = [])

This checks that the request record does not exist in the database.

dontSeeRecord('users', ['email' => 'jadb@cakephp.org']);
Dispatcher

Miscellaneous
Load fixtures (Only Cest)

In your Cest test case, write $fixutures property:

s AwesomeCest

public $fixtures = [
    'app.Users',
    'app.Posts',
];

// ...

You can use $autoFixtures, $dropTables property, and loadFixtures() method:

s AwesomeCest

public $autoFixtures = false;
public $dropTables = false;
public $fixtures = [
    'app.Users',
    'app.Posts',
];

public function tryYourSenario($I)
{
    // load fixtures manually
    $I->loadFixtures('Users');
    // ...
}

Assert CakePHP version with expectedCakePHPVersion($ver, $operator = 'ge')
expectedCakePHPVersion('3.0.4');
Router
Open page by route with amOnRoute($route, $params = [])

All the below forms are equivalent:

amOnRoute(['controller' => 'Posts', 'action' => 'add']);
amOnRoute('addPost'); // assuming there is a route named `addPost`
Open page by action with amOnAction($action, $params = [])

All the below forms are equivalent:

amOnAction('Posts@add');
amOnAction('Posts.add');
amOnAction('PostsController@add');
amOnAction('PostsController.add');
amOnAction('posts@add');
amOnAction('posts.add');
Assert URL matches route with seeCurrentRouteIs($route, $params = [])

All the below forms are equivalent:

seeCurrentRouteIs(['controller' => 'Posts', 'action' => 'add']);
seeCurrentRouteIs('addPost'); // assuming there is a route named `addPost`
Assert URL matches action with seeCurrentActionIs($action, $params = [])

All the below forms are equivalent:

seeCurrentActionIs('Posts@add');
seeCurrentActionIs('Posts.add');
seeCurrentActionIs('PostsController@add');
seeCurrentActionIs('PostsController.add');
seeCurrentActionIs('posts@add');
seeCurrentActionIs('posts.add');
Session
Insert key/value(s) in session with haveInSession($key, $value = null)
haveInSession('redirect', Router::url(['_name' => 'dashboard']));
haveInSession(['redirect' => Router::url(['_name' => 'dashboard'])]);
Assert key(/value) in session with seeInSession($key, $value = null)
seeInSession('redirect'); // only checks the key exists.
seeInSession('redirect', Router::url(['_name' => 'dashboard']));
seeInSession(['redirect', Router::url(['_name' => 'dashboard'])]);
Assert key(/value) not in session with dontSeeInSession($key, $value = null)
dontSeeInSession('redirect'); // only checks the key does not exist.
dontSeeInSession('redirect', Router::url(['_name' => 'dashboard']));
dontSeeInSession(['redirect', Router::url(['_name' => 'dashboard'])]);
View


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.