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.
array|null
The customized application constructor arguments.
string|null
The customized application class name.
\Cake\Controller\Controller
The controller used in the last request.
array
Cookie data to use in the next request.
string|null
bool
Boolean flag for whether or not the request should have a CSRF token added.
\Throwable|null
The exception being thrown if the case.
array|null
Stored flash messages before render
string
The last rendered layout
array
The data used to build the next request.
\Cake\Http\Session
The session instance from the last request
\Psr\Http\Message\ResponseInterface
The response for the most recent request.
bool
Boolean flag for whether or not the request should re-store flash messages
bool
Boolean flag for whether or not the request should have a SecurityComponent token added.
array
Session data to use in the next request.
string[]
List of fields that are excluded from field validation.
string[]
Valid cipher names for encrypted cookies.
string
The last rendered view
Recursively casts all data to string as that is how data would be POSTed in the real world
Explode method to return array from string set in CookieComponent::_implode() Maintains reading backwards compatibility with 1.x CookieComponent::_implode().
Asserts that the Location header is correct. Comparison is made against a full URL.
Asserts that the Location header is correct. Comparison is made against exactly the URL provided.
Asserts that the response body does not match a given regular expression.
Calling this method will re-store flash messages into the test session after being removed by the FlashHelper
Calling this method will enable a SecurityComponent compatible token to be added to request data. This lets you easily test actions protected by SecurityComponent.
Inspect controller to extract possible causes of the failed assertion
_addTokens(string $url, array $data)
Add the CSRF and Security Component tokens if necessary.
string
$url The URL the form is being submitted on.
array
$data The request body data.
array
The request body with tokens added.
_buildRequest(string $url, mixed $method, mixed $data)
Creates a request object with the configured options and parameters.
string
$url The URL
string
$method The HTTP method
string|array
$data optional The request data.
array
The request context
_castToString(array $data)
Recursively casts all data to string as that is how data would be POSTed in the real world
array
$data POST data
array
_checkCipher(string $encrypt)
Helper method for validating encryption cipher names.
string
$encrypt The cipher name.
RuntimeException
_decode(string $value, mixed $encrypt, ?string $key)
Decodes and decrypts a single value.
string
$value The value to decode & decrypt.
string|false
$encrypt The encryption cipher to use.
string|null
$key Used as the security salt if specified.
string|array
Decoded values.
_decrypt(mixed $values, mixed $mode, ?string $key)
Decrypts $value using public $type method in Security class
string[]|string
$values Values to decrypt
string|false
$mode Encryption mode
string|null
$key optional Used as the security salt if specified.
string|array
Decrypted values
_encrypt(mixed $value, mixed $encrypt, ?string $key)
Encrypts $value using public $type method in Security class
string|array
$value Value to encrypt
string|false
$encrypt Encryption mode to use. False disabled encryption.
string|null
$key optional Used as the security salt if specified.
string
Encoded values
_explode(string $string)
Explode method to return array from string set in CookieComponent::_implode() Maintains reading backwards compatibility with 1.x CookieComponent::_implode().
string
$string A string containing JSON encoded data, or a bare string.
string|array
Map of key and values
_getBodyAsString()
Get the response body as string
string
The response body.
_getCookieEncryptionKey()
Returns the encryption key to be used.
string
_handleError(\Throwable $exception)
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.
\Throwable
$exception Exception to handle.
_implode(array $array)
Implode method to keep keys are multidimensional arrays
array
$array Map of key and values
string
A JSON encoded string.
_makeDispatcher()
Get the correct dispatcher instance.
\Cake\TestSuite\MiddlewareDispatcher
A dispatcher instance
_sendRequest(mixed $url, mixed $method, mixed $data)
Creates and send the request into a Dispatcher instance.
Receives and stores the response for future inspection.
string|array
$url The URL
string
$method The HTTP method
string|array
$data optional The request data.
PHPUnit\Exception|\Throwable
_url(string $url)
Creates a valid request url and parameter array more like Request::_url()
string
$url The URL
array
Qualified URL, the query parameters, and host data
assertContentType(string $type, string $message)
Asserts content type
string
$type The content-type to check for.
string
$message optional The failure message that will be appended to the generated message.
assertCookie(mixed $expected, string $name, string $message)
Asserts cookie values
mixed
$expected The expected contents.
string
$name The cookie name.
string
$message optional The failure message that will be appended to the generated message.
assertCookieEncrypted(mixed $expected, string $name, string $encrypt, ?string $key, string $message)
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.
mixed
$expected The expected contents.
string
$name The cookie name.
string
$encrypt optional Encryption mode to use.
string|null
$key optional Encryption key used. Defaults to Security.salt.
string
$message optional The failure message that will be appended to the generated message.
assertCookieNotSet(string $cookie, string $message)
Asserts a cookie has not been set in the response
string
$cookie The cookie name to check
string
$message optional The failure message that will be appended to the generated message.
assertFileResponse(string $expected, string $message)
Asserts that a file with the given name was sent in the response
string
$expected The absolute file path that should be sent in the response.
string
$message optional The failure message that will be appended to the generated message.
assertFlashElement(string $expected, string $key, string $message)
Asserts a flash element was set
string
$expected Expected element name
string
$key optional Flash key
string
$message optional Assertion failure message
assertFlashElementAt(int $at, string $expected, string $key, string $message)
Asserts a flash element was set at a certain index
int
$at Flash index
string
$expected Expected element name
string
$key optional Flash key
string
$message optional Assertion failure message
assertFlashMessage(string $expected, string $key, string $message)
Asserts a flash message was set
string
$expected Expected message
string
$key optional Flash key
string
$message optional Assertion failure message
assertFlashMessageAt(int $at, string $expected, string $key, string $message)
Asserts a flash message was set at a certain index
int
$at Flash index
string
$expected Expected message
string
$key optional Flash key
string
$message optional Assertion failure message
assertHeader(string $header, string $content, string $message)
Asserts response headers
string
$header The header to check
string
$content The content to check for.
string
$message optional The failure message that will be appended to the generated message.
assertHeaderContains(string $header, string $content, string $message)
Asserts response header contains a string
string
$header The header to check
string
$content The content to check for.
string
$message optional The failure message that will be appended to the generated message.
assertHeaderNotContains(string $header, string $content, string $message)
Asserts response header does not contain a string
string
$header The header to check
string
$content The content to check for.
string
$message optional The failure message that will be appended to the generated message.
assertLayout(string $content, string $message)
Asserts that the search string was in the layout name.
string
$content The content to check for.
string
$message optional The failure message that will be appended to the generated message.
assertNoRedirect(string $message)
Asserts that the Location header is not set.
string
$message optional The failure message that will be appended to the generated message.
assertRedirect(mixed $url, mixed $message)
Asserts that the Location header is correct. Comparison is made against a full URL.
string|array|null
$url optional 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 optional The failure message that will be appended to the generated message.
assertRedirectContains(string $url, string $message)
Asserts that the Location header contains a substring
string
$url The URL you expected the client to go to.
string
$message optional The failure message that will be appended to the generated message.
assertRedirectEquals(mixed $url, mixed $message)
Asserts that the Location header is correct. Comparison is made against exactly the URL provided.
string|array|null
$url optional 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 optional The failure message that will be appended to the generated message.
assertRedirectNotContains(string $url, string $message)
Asserts that the Location header does not contain a substring
string
$url The URL you expected the client to go to.
string
$message optional The failure message that will be appended to the generated message.
assertResponseCode(int $code, string $message)
Asserts a specific response status code.
int
$code Status code to assert.
string
$message optional Custom message for failure.
assertResponseContains(string $content, string $message, bool $ignoreCase)
Asserts content exists in the response body.
string
$content The content to check for.
string
$message optional The failure message that will be appended to the generated message.
bool
$ignoreCase optional A flag to check whether we should ignore case or not.
assertResponseEmpty(string $message)
Assert response content is empty.
string
$message optional The failure message that will be appended to the generated message.
assertResponseEquals(mixed $content, mixed $message)
Asserts content in the response body equals.
mixed
$content The content to check for.
string
$message optional The failure message that will be appended to the generated message.
assertResponseError(string $message)
Asserts that the response status code is in the 4xx range.
string
$message optional Custom message for failure.
assertResponseFailure(string $message)
Asserts that the response status code is in the 5xx range.
string
$message optional Custom message for failure.
assertResponseNotContains(string $content, string $message, bool $ignoreCase)
Asserts content does not exist in the response body.
string
$content The content to check for.
string
$message optional The failure message that will be appended to the generated message.
bool
$ignoreCase optional A flag to check whether we should ignore case or not.
assertResponseNotEmpty(string $message)
Assert response content is not empty.
string
$message optional The failure message that will be appended to the generated message.
assertResponseNotEquals(mixed $content, mixed $message)
Asserts content in the response body not equals.
mixed
$content The content to check for.
string
$message optional The failure message that will be appended to the generated message.
assertResponseNotRegExp(string $pattern, string $message)
Asserts that the response body does not match a given regular expression.
string
$pattern The pattern to compare against.
string
$message optional The failure message that will be appended to the generated message.
assertResponseOk(string $message)
Asserts that the response status code is in the 2xx range.
string
$message optional Custom message for failure.
assertResponseRegExp(string $pattern, string $message)
Asserts that the response body matches a given regular expression.
string
$pattern The pattern to compare against.
string
$message optional The failure message that will be appended to the generated message.
assertResponseSuccess(string $message)
Asserts that the response status code is in the 2xx/3xx range.
string
$message optional Custom message for failure.
assertSession(mixed $expected, string $path, string $message)
Asserts session contents
mixed
$expected The expected contents.
string
$path The session data path. Uses Hash::get() compatible notation
string
$message optional The failure message that will be appended to the generated message.
assertSessionHasKey(string $path, string $message)
Asserts session key exists.
string
$path The session data path. Uses Hash::get() compatible notation.
string
$message optional The failure message that will be appended to the generated message.
assertSessionNotHasKey(string $path, string $message)
Asserts a session key does not exist.
string
$path The session data path. Uses Hash::get() compatible notation.
string
$message optional The failure message that will be appended to the generated message.
assertTemplate(string $content, string $message)
Asserts that the search string was in the template name.
string
$content The content to check for.
string
$message optional The failure message that will be appended to the generated message.
cleanup()
Clears the state used for requests.
configApplication(string $class, ?array $constructorArgs)
Configure the application class to use in integration tests.
string
$class The application class name.
array|null
$constructorArgs The constructor arguments for your application class.
configRequest(array $data)
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.
array
$data The request data to use.
controllerSpy(\Cake\Event\EventInterface $event, ?\Cake\Controller\Controller $controller)
Adds additional event spies to the controller/view event manager.
\Cake\Event\EventInterface
$event A dispatcher event.
\Cake\Controller\Controller|null
$controller optional Controller instance.
cookie(string $name, mixed $value)
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.
string
$name The cookie name to use.
mixed
$value The value of the cookie.
cookieEncrypted(string $name, mixed $value, mixed $encrypt, mixed $key)
Sets a encrypted request cookie for future requests.
The difference from cookie() is this encrypts the cookie value like the CookieComponent.
string
$name The cookie name to use.
mixed
$value The value of the cookie.
string|false
$encrypt optional Encryption mode to use.
string|null
$key optional Encryption key used. Defaults to Security.salt.
delete(mixed $url)
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.
string|array
$url The URL to request.
disableErrorHandlerMiddleware()
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.
enableCsrfToken()
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()
Calling this method will re-store flash messages into the test session after being removed by the FlashHelper
enableSecurityToken()
Calling this method will enable a SecurityComponent compatible token to be added to request data. This lets you easily test actions protected by SecurityComponent.
extractExceptionMessage(\Exception $exception)
Extract verbose message for existing exception
\Exception
$exception Exception to extract
string
extractVerboseMessage(string $message)
Inspect controller to extract possible causes of the failed assertion
string
$message Original message to use as a base
string
get(mixed $url)
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.
string|array
$url The URL to request.
getSession()
\Cake\TestSuite\TestSession
head(mixed $url)
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.
string|array
$url The URL to request.
options(mixed $url)
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.
string|array
$url The URL to request.
patch(mixed $url, mixed $data)
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.
string|array
$url The URL to request.
string|array
$data optional The data for the request.
post(mixed $url, mixed $data)
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.
string|array
$url The URL to request.
string|array
$data optional The data for the request.
put(mixed $url, mixed $data)
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.
string|array
$url The URL to request.
string|array
$data optional The data for the request.
session(array $data)
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.
array
$data The session data to use.
setUnlockedFields(array $unlockedFields)
Set list of fields that are excluded from field validation.
string[]
$unlockedFields optional List of fields that are excluded from field validation.
viewVariable(string $name)
Fetches a view variable by name.
If the view variable does not exist, null will be returned.
string
$name The view variable to get.
mixed
The view variable if set.
The customized application constructor arguments.
array|null
The customized application class name.
string|null
The controller used in the last request.
\Cake\Controller\Controller
Cookie data to use in the next request.
array
string|null
Boolean flag for whether or not the request should have a CSRF token added.
bool
The exception being thrown if the case.
\Throwable|null
Stored flash messages before render
array|null
The last rendered layout
string
The data used to build the next request.
array
The session instance from the last request
\Cake\Http\Session
The response for the most recent request.
\Psr\Http\Message\ResponseInterface
Boolean flag for whether or not the request should re-store flash messages
bool
Boolean flag for whether or not the request should have a SecurityComponent token added.
bool
Session data to use in the next request.
array
List of fields that are excluded from field validation.
string[]
Valid cipher names for encrypted cookies.
string[]
The last rendered view
string
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.1/trait-Cake.TestSuite.IntegrationTestTrait.html