\Cake\TestSuiteIntegrationTestTrait

A trait intended to make integration tests of your controllers easier.

This test class provides a number of helper methods and features that make dispatching requests and checking their responses simpler. It favours full integration tests over mock objects as you can test more of your code easily and avoid some of the maintenance pitfalls that mock objects create.

Summary

Methods
Properties
Constants
setupServer()
cleanup()
useHttpServer()
configApplication()
enableSecurityToken()
enableCsrfToken()
enableRetainFlashMessages()
configRequest()
session()
cookie()
cookieEncrypted()
get()
post()
patch()
put()
delete()
head()
options()
controllerSpy()
viewVariable()
assertResponseOk()
assertResponseSuccess()
assertResponseError()
assertResponseFailure()
assertResponseCode()
assertRedirect()
assertRedirectContains()
assertRedirectNotContains()
assertNoRedirect()
assertHeader()
assertHeaderContains()
assertHeaderNotContains()
assertContentType()
assertResponseEquals()
assertResponseNotEquals()
assertResponseContains()
assertResponseNotContains()
assertResponseRegExp()
assertResponseNotRegExp()
assertResponseNotEmpty()
assertResponseEmpty()
assertTemplate()
assertLayout()
assertSession()
assertFlashMessage()
assertFlashMessageAt()
assertFlashElement()
assertFlashElementAt()
assertCookie()
assertCookieNotSet()
disableErrorHandlerMiddleware()
assertCookieEncrypted()
assertFileResponse()
No public properties found
No constants found
_getCookieEncryptionKey()
_sendRequest()
_makeDispatcher()
_handleError()
_buildRequest()
_addTokens()
_castToString()
_url()
_getBodyAsString()
extractVerboseMessage()
extractExceptionMessage()
$_useHttpServer
$_appClass
$_appArgs
$_request
$_response
$_exception
$_session
$_cookie
$_controller
$_viewName
$_layoutName
$_requestSession
$_securityToken
$_csrfToken
$_retainFlashMessages
$_flashMessages
$_cookieEncryptionKey
N/A
No private methods found
No private properties found
N/A

Properties

$_useHttpServer

$_useHttpServer : boolean

Track whether or not tests are run against the PSR7 HTTP stack.

Type

boolean

$_appClass

$_appClass : string|null

The customized application class name.

Type

string|null

$_appArgs

$_appArgs : array|null

The customized application constructor arguments.

Type

array|null

$_request

$_request : array

The data used to build the next request.

Type

array

$_response

$_response : \Cake\Http\Response|null

The response for the most recent request.

Type

\Cake\Http\Response|null

$_exception

$_exception : \Exception|null

The exception being thrown if the case.

Type

\Exception|null

$_session

$_session : array

Session data to use in the next request.

Type

array

$_cookie

$_cookie : array

Cookie data to use in the next request.

Type

array

$_controller

$_controller : \Cake\Controller\Controller|null

The controller used in the last request.

Type

\Cake\Controller\Controller|null

$_viewName

$_viewName : string|null

The last rendered view

Type

string|null

$_layoutName

$_layoutName : string|null

The last rendered layout

Type

string|null

$_requestSession

$_requestSession : \Cake\Http\Session|null

The session instance from the last request

Type

\Cake\Http\Session|null

$_securityToken

$_securityToken : boolean

Boolean flag for whether or not the request should have a SecurityComponent token added.

Type

boolean

$_csrfToken

$_csrfToken : boolean

Boolean flag for whether or not the request should have a CSRF token added.

Type

boolean

$_retainFlashMessages

$_retainFlashMessages : boolean

Boolean flag for whether or not the request should re-store flash messages

Type

boolean

$_flashMessages

$_flashMessages : null|array

Stored flash messages before render

Type

null|array

$_cookieEncryptionKey

$_cookieEncryptionKey : null|string

Type

null|string

Methods

setupServer()

setupServer() : void

Auto-detect if the HTTP middleware stack should be used.

cleanup()

cleanup() : void

Clears the state used for requests.

useHttpServer()

useHttpServer(boolean  $enable) : void

Toggle whether or not you want to use the HTTP Server stack.

Parameters

boolean $enable

Enable/disable the usage of the HTTP Stack.

configApplication()

configApplication(string  $class, array|null  $constructorArgs) : void

Configure the application class to use in integration tests.

Combined with useHttpServer() to customize the class name and constructor arguments of your application class.

Parameters

string $class

The application class name.

array|null $constructorArgs

The constructor arguments for your application class.

enableSecurityToken()

enableSecurityToken() : void

Calling this method will enable a SecurityComponent compatible token to be added to request data. This lets you easily test actions protected by SecurityComponent.

enableCsrfToken()

enableCsrfToken() : void

Calling this method will add a CSRF token to the request.

Both the POST data and cookie will be populated when this option is enabled. The default parameter names will be used.

enableRetainFlashMessages()

enableRetainFlashMessages() : void

Calling this method will re-store flash messages into the test session after being removed by the FlashHelper

configRequest()

configRequest(array  $data) : void

Configures the data for the *next* request.

This data is cleared in the tearDown() method.

You can call this method multiple times to append into the current state.

Parameters

array $data

The request data to use.

session()

session(array  $data) : void

Sets session data.

This method lets you configure the session data you want to be used for requests that follow. The session state is reset in each tearDown().

You can call this method multiple times to append into the current state.

Parameters

array $data

The session data to use.

cookie()

cookie(string  $name, mixed  $value) : void

Sets a request cookie for future requests.

This method lets you configure the session data you want to be used for requests that follow. The session state is reset in each tearDown().

You can call this method multiple times to append into the current state.

Parameters

string $name

The cookie name to use.

mixed $value

The value of the cookie.

cookieEncrypted()

cookieEncrypted(string  $name, mixed  $value, string|boolean  $encrypt = 'aes', string|null  $key = null) : void

Sets a encrypted request cookie for future requests.

The difference from cookie() is this encrypts the cookie value like the CookieComponent.

Parameters

string $name

The cookie name to use.

mixed $value

The value of the cookie.

string|boolean $encrypt

Encryption mode to use.

string|null $key

Encryption key used. Defaults to Security.salt.

get()

get(string|array  $url) : void

Performs a GET request using the current request data.

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters

string|array $url

The URL to request.

Throws

\PHPUnit\Exception

post()

post(string|array  $url, array  $data = array()) : void

Performs a POST request using the current request data.

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters

string|array $url

The URL to request.

array $data

The data for the request.

Throws

\PHPUnit\Exception

patch()

patch(string|array  $url, array  $data = array()) : void

Performs a PATCH request using the current request data.

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters

string|array $url

The URL to request.

array $data

The data for the request.

Throws

\PHPUnit\Exception

put()

put(string|array  $url, array  $data = array()) : void

Performs a PUT request using the current request data.

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters

string|array $url

The URL to request.

array $data

The data for the request.

Throws

\PHPUnit\Exception

delete()

delete(string|array  $url) : void

Performs a DELETE request using the current request data.

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters

string|array $url

The URL to request.

Throws

\PHPUnit\Exception

head()

head(string|array  $url) : void

Performs a HEAD request using the current request data.

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters

string|array $url

The URL to request.

Throws

\PHPUnit\Exception

options()

options(string|array  $url) : void

Performs an OPTIONS request using the current request data.

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters

string|array $url

The URL to request.

Throws

\PHPUnit\Exception

controllerSpy()

controllerSpy(\Cake\Event\Event  $event, \Cake\Controller\Controller|null  $controller = null) : void

Adds additional event spies to the controller/view event manager.

Parameters

\Cake\Event\Event $event

A dispatcher event.

\Cake\Controller\Controller|null $controller

Controller instance.

viewVariable()

viewVariable(string  $name) : mixed

Fetches a view variable by name.

If the view variable does not exist, null will be returned.

Parameters

string $name

The view variable to get.

Returns

mixed —

The view variable if set.

assertResponseOk()

assertResponseOk(string  $message = null) : void

Asserts that the response status code is in the 2xx range.

Parameters

string $message

Custom message for failure.

assertResponseSuccess()

assertResponseSuccess(string  $message = null) : void

Asserts that the response status code is in the 2xx/3xx range.

Parameters

string $message

Custom message for failure.

assertResponseError()

assertResponseError(string  $message = null) : void

Asserts that the response status code is in the 4xx range.

Parameters

string $message

Custom message for failure.

assertResponseFailure()

assertResponseFailure(string  $message = null) : void

Asserts that the response status code is in the 5xx range.

Parameters

string $message

Custom message for failure.

assertResponseCode()

assertResponseCode(integer  $code, string  $message = null) : void

Asserts a specific response status code.

Parameters

integer $code

Status code to assert.

string $message

Custom message for failure.

assertRedirect()

assertRedirect(string|array|null  $url = null, string  $message = '') : void

Asserts that the Location header is correct.

Parameters

string|array|null $url

The URL you expected the client to go to. This can either be a string URL or an array compatible with Router::url(). Use null to simply check for the existence of this header.

string $message

The failure message that will be appended to the generated message.

assertRedirectContains()

assertRedirectContains(string  $url, string  $message = '') : void

Asserts that the Location header contains a substring

Parameters

string $url

The URL you expected the client to go to.

string $message

The failure message that will be appended to the generated message.

assertRedirectNotContains()

assertRedirectNotContains(string  $url, string  $message = '') : void

Asserts that the Location header does not contain a substring

Parameters

string $url

The URL you expected the client to go to.

string $message

The failure message that will be appended to the generated message.

assertNoRedirect()

assertNoRedirect(string  $message = '') : void

Asserts that the Location header is not set.

Parameters

string $message

The failure message that will be appended to the generated message.

assertHeader()

assertHeader(string  $header, string  $content, string  $message = '') : void

Asserts response headers

Parameters

string $header

The header to check

string $content

The content to check for.

string $message

The failure message that will be appended to the generated message.

assertHeaderContains()

assertHeaderContains(string  $header, string  $content, string  $message = '') : void

Asserts response header contains a string

Parameters

string $header

The header to check

string $content

The content to check for.

string $message

The failure message that will be appended to the generated message.

assertHeaderNotContains()

assertHeaderNotContains(string  $header, string  $content, string  $message = '') : void

Asserts response header does not contain a string

Parameters

string $header

The header to check

string $content

The content to check for.

string $message

The failure message that will be appended to the generated message.

assertContentType()

assertContentType(string  $type, string  $message = '') : void

Asserts content type

Parameters

string $type

The content-type to check for.

string $message

The failure message that will be appended to the generated message.

assertResponseEquals()

assertResponseEquals(mixed  $content, string  $message = '') : void

Asserts content in the response body equals.

Parameters

mixed $content

The content to check for.

string $message

The failure message that will be appended to the generated message.

assertResponseNotEquals()

assertResponseNotEquals(mixed  $content, string  $message = '') : void

Asserts content in the response body not equals.

Parameters

mixed $content

The content to check for.

string $message

The failure message that will be appended to the generated message.

assertResponseContains()

assertResponseContains(string  $content, string  $message = '', boolean  $ignoreCase = false) : void

Asserts content exists in the response body.

Parameters

string $content

The content to check for.

string $message

The failure message that will be appended to the generated message.

boolean $ignoreCase

A flag to check whether we should ignore case or not.

assertResponseNotContains()

assertResponseNotContains(string  $content, string  $message = '', boolean  $ignoreCase = false) : void

Asserts content does not exist in the response body.

Parameters

string $content

The content to check for.

string $message

The failure message that will be appended to the generated message.

boolean $ignoreCase

A flag to check whether we should ignore case or not.

assertResponseRegExp()

assertResponseRegExp(string  $pattern, string  $message = '') : void

Asserts that the response body matches a given regular expression.

Parameters

string $pattern

The pattern to compare against.

string $message

The failure message that will be appended to the generated message.

assertResponseNotRegExp()

assertResponseNotRegExp(string  $pattern, string  $message = '') : void

Asserts that the response body does not match a given regular expression.

Parameters

string $pattern

The pattern to compare against.

string $message

The failure message that will be appended to the generated message.

assertResponseNotEmpty()

assertResponseNotEmpty(string  $message = '') : void

Assert response content is not empty.

Parameters

string $message

The failure message that will be appended to the generated message.

assertResponseEmpty()

assertResponseEmpty(string  $message = '') : void

Assert response content is empty.

Parameters

string $message

The failure message that will be appended to the generated message.

assertTemplate()

assertTemplate(string  $content, string  $message = '') : void

Asserts that the search string was in the template name.

Parameters

string $content

The content to check for.

string $message

The failure message that will be appended to the generated message.

assertLayout()

assertLayout(string  $content, string  $message = '') : void

Asserts that the search string was in the layout name.

Parameters

string $content

The content to check for.

string $message

The failure message that will be appended to the generated message.

assertSession()

assertSession(string  $expected, string  $path, string  $message = '') : void

Asserts session contents

Parameters

string $expected

The expected contents.

string $path

The session data path. Uses Hash::get() compatible notation

string $message

The failure message that will be appended to the generated message.

assertFlashMessage()

assertFlashMessage(string  $expected, string  $key = 'flash', string  $message = '') : void

Asserts a flash message was set

Parameters

string $expected

Expected message

string $key

Flash key

string $message

Assertion failure message

assertFlashMessageAt()

assertFlashMessageAt(integer  $at, string  $expected, string  $key = 'flash', string  $message = '') : void

Asserts a flash message was set at a certain index

Parameters

integer $at

Flash index

string $expected

Expected message

string $key

Flash key

string $message

Assertion failure message

assertFlashElement()

assertFlashElement(string  $expected, string  $key = 'flash', string  $message = '') : void

Asserts a flash element was set

Parameters

string $expected

Expected element name

string $key

Flash key

string $message

Assertion failure message

assertFlashElementAt()

assertFlashElementAt(integer  $at, string  $expected, string  $key = 'flash', string  $message = '') : void

Asserts a flash element was set at a certain index

Parameters

integer $at

Flash index

string $expected

Expected element name

string $key

Flash key

string $message

Assertion failure message

assertCookie()

assertCookie(string  $expected, string  $name, string  $message = '') : void

Asserts cookie values

Parameters

string $expected

The expected contents.

string $name

The cookie name.

string $message

The failure message that will be appended to the generated message.

assertCookieNotSet()

assertCookieNotSet(string  $cookie, string  $message = '') : void

Asserts a cookie has not been set in the response

Parameters

string $cookie

The cookie name to check

string $message

The failure message that will be appended to the generated message.

disableErrorHandlerMiddleware()

disableErrorHandlerMiddleware() : void

Disable the error handler middleware.

By using this function, exceptions are no longer caught by the ErrorHandlerMiddleware and are instead re-thrown by the TestExceptionRenderer. This can be helpful when trying to diagnose/debug unexpected failures in test cases.

assertCookieEncrypted()

assertCookieEncrypted(string  $expected, string  $name, string|boolean  $encrypt = 'aes', string|null  $key = null, string  $message = '') : void

Asserts cookie values which are encrypted by the CookieComponent.

The difference from assertCookie() is this decrypts the cookie value like the CookieComponent for this assertion.

Parameters

string $expected

The expected contents.

string $name

The cookie name.

string|boolean $encrypt

Encryption mode to use.

string|null $key

Encryption key used. Defaults to Security.salt.

string $message

The failure message that will be appended to the generated message.

assertFileResponse()

assertFileResponse(string  $expected, string  $message = '') : void

Asserts that a file with the given name was sent in the response

Parameters

string $expected

The file name that should be sent in the response

string $message

The failure message that will be appended to the generated message.

_getCookieEncryptionKey()

_getCookieEncryptionKey() : string

Returns the encryption key to be used.

Returns

string

_sendRequest()

_sendRequest(string|array  $url, string  $method, array|null  $data = array()) : void

Creates and send the request into a Dispatcher instance.

Receives and stores the response for future inspection.

Parameters

string|array $url

The URL

string $method

The HTTP method

array|null $data

The request data.

Throws

\PHPUnit\Exception

_handleError()

_handleError(\Exception  $exception) : void

Attempts to render an error response for a given exception.

This method will attempt to use the configured exception renderer. If that class does not exist, the built-in renderer will be used.

Parameters

\Exception $exception

Exception to handle.

Throws

\Exception

_buildRequest()

_buildRequest(string|array  $url, string  $method, array|null  $data) : array

Creates a request object with the configured options and parameters.

Parameters

string|array $url

The URL

string $method

The HTTP method

array|null $data

The request data.

Returns

array —

The request context

_addTokens()

_addTokens(string  $url, array  $data) : array

Add the CSRF and Security Component tokens if necessary.

Parameters

string $url

The URL the form is being submitted on.

array $data

The request body data.

Returns

array —

The request body with tokens added.

_castToString()

_castToString(array  $data) : array

Recursively casts all data to string as that is how data would be POSTed in the real world

Parameters

array $data

POST data

Returns

array

_url()

_url(string|array  $url) : array

Creates a valid request url and parameter array more like Request::_url()

Parameters

string|array $url

The URL

Returns

array —

Qualified URL and the query parameters

_getBodyAsString()

_getBodyAsString() : string

Get the response body as string

Returns

string —

The response body.

extractVerboseMessage()

extractVerboseMessage(string  $message = null) : null|string

Inspect controller to extract possible causes of the failed assertion

Parameters

string $message

Original message to use as a base

Returns

null|string

extractExceptionMessage()

extractExceptionMessage(\Exception  $exception) : string

Extract verbose message for existing exception

Parameters

\Exception $exception

Exception to extract

Returns

string