Trait IntegrationTestTrait
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.
- Cake\TestSuite\IntegrationTestTrait uses Cake\Utility\CookieCryptTrait , Cake\View\Helper\SecureFieldTokenTrait
Direct Users
Properties summary
-
$_appArgs
protectedarray|null
The customized application constructor arguments. -
$_appClass
protectedstring|null
The customized application class name. -
$_controller
protectedThe controller used in the last request. -
$_cookie
protectedarray
Cookie data to use in the next request. -
$_cookieEncryptionKey
protectednull|string
-
$_csrfToken
protectedboolean
Boolean flag for whether or not the request should have a CSRF token added.
-
$_exception
protectedException|null
The exception being thrown if the case. -
$_flashMessages
protectednull|array
Stored flash messages before render -
$_layoutName
protectedstring|null
The last rendered layout -
$_request
protectedarray
The data used to build the next request. -
$_requestSession
protectedCake\Http\Session
|nullThe session instance from the last request -
$_response
protectedCake\Http\Response
|nullThe response for the most recent request. -
$_retainFlashMessages
protectedboolean
Boolean flag for whether or not the request should re-store flash messages
-
$_securityToken
protectedboolean
Boolean flag for whether or not the request should have a SecurityComponent token added.
-
$_session
protectedarray
Session data to use in the next request. -
$_useHttpServer
protectedboolean
Track whether or not tests are run against the PSR7 HTTP stack.
-
$_viewName
protectedstring|null
The last rendered view
Inherited Properties
Method Summary
-
_addTokens() protected
Add the CSRF and Security Component tokens if necessary. -
_buildRequest() protected
Creates a request object with the configured options and parameters. -
_castToString() protected
Recursively casts all data to string as that is how data would be POSTed in the real world
-
_getBodyAsString() protected
Get the response body as string -
_getCookieEncryptionKey() protected
Returns the encryption key to be used. -
_handleError() protected
Attempts to render an error response for a given exception. -
_makeDispatcher() protected
Get the correct dispatcher instance. -
_sendRequest() protected
Creates and send the request into a Dispatcher instance. -
_url() protected
Creates a valid request url and parameter array more like Request::_url() -
assertContentType() public
Asserts content type -
assertCookie() public
Asserts cookie values -
assertCookieEncrypted() public
Asserts cookie values which are encrypted by the CookieComponent.
-
assertCookieNotSet() public
Asserts a cookie has not been set in the response -
assertFileResponse() public
Asserts that a file with the given name was sent in the response -
assertFlashElement() public
Asserts a flash element was set -
assertFlashElementAt() public
Asserts a flash element was set at a certain index -
assertFlashMessage() public
Asserts a flash message was set -
assertFlashMessageAt() public
Asserts a flash message was set at a certain index -
assertHeader() public
Asserts response headers -
assertHeaderContains() public
Asserts response header contains a string -
assertHeaderNotContains() public
Asserts response header does not contain a string -
assertLayout() public
Asserts that the search string was in the layout name. -
assertNoRedirect() public
Asserts that the Location header is not set. -
assertRedirect() public
Asserts that the Location header is correct. -
assertRedirectContains() public
Asserts that the Location header contains a substring -
assertRedirectNotContains() public
Asserts that the Location header does not contain a substring -
assertResponseCode() public
Asserts a specific response status code. -
assertResponseContains() public
Asserts content exists in the response body. -
assertResponseEmpty() public
Assert response content is empty. -
assertResponseEquals() public
Asserts content in the response body equals. -
assertResponseError() public
Asserts that the response status code is in the 4xx range. -
assertResponseFailure() public
Asserts that the response status code is in the 5xx range. -
assertResponseNotContains() public
Asserts content does not exist in the response body. -
assertResponseNotEmpty() public
Assert response content is not empty. -
assertResponseNotEquals() public
Asserts content in the response body not equals. -
assertResponseNotRegExp() public
Asserts that the response body does not match a given regular expression. -
assertResponseOk() public
Asserts that the response status code is in the 2xx range. -
assertResponseRegExp() public
Asserts that the response body matches a given regular expression. -
assertResponseSuccess() public
Asserts that the response status code is in the 2xx/3xx range. -
assertSession() public
Asserts session contents -
assertTemplate() public
Asserts that the search string was in the template name. -
cleanup() public
Clears the state used for requests. -
configApplication() public
Configure the application class to use in integration tests. -
configRequest() public
Configures the data for the next request. -
controllerSpy() public
Adds additional event spies to the controller/view event manager. -
cookie() public
Sets a request cookie for future requests. -
cookieEncrypted() public
Sets a encrypted request cookie for future requests. -
delete() public
Performs a DELETE request using the current request data. -
disableErrorHandlerMiddleware() public
Disable the error handler middleware. -
enableCsrfToken() public
Calling this method will add a CSRF token to the request. -
enableRetainFlashMessages() public
Calling this method will re-store flash messages into the test session after being removed by the FlashHelper
-
enableSecurityToken() public
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() protected
Extract verbose message for existing exception -
extractVerboseMessage() protected
Inspect controller to extract possible causes of the failed assertion -
get() public
Performs a GET request using the current request data. -
head() public
Performs a HEAD request using the current request data. -
options() public
Performs an OPTIONS request using the current request data. -
patch() public
Performs a PATCH request using the current request data. -
post() public
Performs a POST request using the current request data. -
put() public
Performs a PUT request using the current request data. -
session() public
Sets session data. -
setupServer() public
Auto-detect if the HTTP middleware stack should be used. -
useHttpServer() public
Toggle whether or not you want to use the HTTP Server stack. -
viewVariable() public
Fetches a view variable by name.
Method Detail
_addTokens() protected ¶
_addTokens( string $url , array $data )
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
The request body with tokens added.
_buildRequest() protected ¶
_buildRequest( string|array $url , string $method , string|array|null $data )
Creates a request object with the configured options and parameters.
Parameters
- string|array $url
- The URL
- string $method
- The HTTP method
- string|array|null $data
- The request data.
Returns
The request context
_castToString() protected ¶
_castToString( array $data )
Recursively casts all data to string as that is how data would be POSTed in the real world
Parameters
- array $data
- POST data
Returns
_getBodyAsString() protected ¶
_getBodyAsString( )
Get the response body as string
Returns
The response body.
_getCookieEncryptionKey() protected ¶
_getCookieEncryptionKey( )
Returns the encryption key to be used.
Returns
_handleError() protected ¶
_handleError( Exception $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.
Parameters
- Exception $exception
- Exception to handle.
Throws
_makeDispatcher() protected ¶
_makeDispatcher( )
Get the correct dispatcher instance.
Returns
A dispatcher instance
_sendRequest() protected ¶
_sendRequest( string|array $url , string $method , string|array|null $data [] )
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
- string|array|null $data optional []
- The request data.
Throws
_url() protected ¶
_url( string|array $url )
Creates a valid request url and parameter array more like Request::_url()
Parameters
- string|array $url
- The URL
Returns
Qualified URL and the query parameters
assertContentType() public ¶
assertContentType( string $type , string $message '' )
Asserts content type
Parameters
- string $type
- The content-type to check for.
- string $message optional ''
- The failure message that will be appended to the generated message.
assertCookie() public ¶
assertCookie( string $expected , string $name , string $message '' )
Asserts cookie values
Parameters
- string $expected
- The expected contents.
- string $name
- The cookie name.
- string $message optional ''
- The failure message that will be appended to the generated message.
assertCookieEncrypted() public ¶
assertCookieEncrypted( string $expected , string $name , string|boolean $encrypt 'aes' , string|null $key null , 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.
Parameters
- string $expected
- The expected contents.
- string $name
- The cookie name.
- string|boolean $encrypt optional 'aes'
- Encryption mode to use.
- string|null $key optional null
Encryption key used. Defaults to Security.salt.
- string $message optional ''
- The failure message that will be appended to the generated message.
See
assertCookieNotSet() public ¶
assertCookieNotSet( string $cookie , string $message '' )
Asserts a cookie has not been set in the response
Parameters
- string $cookie
- The cookie name to check
- string $message optional ''
- The failure message that will be appended to the generated message.
assertFileResponse() public ¶
assertFileResponse( string $expected , string $message '' )
Asserts that a file with the given name was sent in the response
Parameters
- 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() public ¶
assertFlashElement( string $expected , string $key 'flash' , string $message '' )
Asserts a flash element was set
Parameters
- string $expected
- Expected element name
- string $key optional 'flash'
- Flash key
- string $message optional ''
- Assertion failure message
assertFlashElementAt() public ¶
assertFlashElementAt( integer $at , string $expected , string $key 'flash' , string $message '' )
Asserts a flash element was set at a certain index
Parameters
- integer $at
- Flash index
- string $expected
- Expected element name
- string $key optional 'flash'
- Flash key
- string $message optional ''
- Assertion failure message
assertFlashMessage() public ¶
assertFlashMessage( string $expected , string $key 'flash' , string $message '' )
Asserts a flash message was set
Parameters
- string $expected
- Expected message
- string $key optional 'flash'
- Flash key
- string $message optional ''
- Assertion failure message
assertFlashMessageAt() public ¶
assertFlashMessageAt( integer $at , string $expected , string $key 'flash' , string $message '' )
Asserts a flash message was set at a certain index
Parameters
- integer $at
- Flash index
- string $expected
- Expected message
- string $key optional 'flash'
- Flash key
- string $message optional ''
- Assertion failure message
assertHeader() public ¶
assertHeader( string $header , string $content , string $message '' )
Asserts response headers
Parameters
- 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() public ¶
assertHeaderContains( string $header , string $content , string $message '' )
Asserts response header contains a string
Parameters
- 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() public ¶
assertHeaderNotContains( string $header , string $content , string $message '' )
Asserts response header does not contain a string
Parameters
- 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() public ¶
assertLayout( string $content , string $message '' )
Asserts that the search string was in the layout name.
Parameters
- string $content
- The content to check for.
- string $message optional ''
- The failure message that will be appended to the generated message.
assertNoRedirect() public ¶
assertNoRedirect( string $message '' )
Asserts that the Location header is not set.
Parameters
- string $message optional ''
- The failure message that will be appended to the generated message.
assertRedirect() public ¶
assertRedirect( string|array|null $url null , string $message '' )
Asserts that the Location header is correct.
Parameters
- string|array|null $url optional null
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() public ¶
assertRedirectContains( string $url , string $message '' )
Asserts that the Location header contains a substring
Parameters
- 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.
assertRedirectNotContains() public ¶
assertRedirectNotContains( string $url , string $message '' )
Asserts that the Location header does not contain a substring
Parameters
- 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() public ¶
assertResponseCode( integer $code , string $message null )
Asserts a specific response status code.
Parameters
- integer $code
- Status code to assert.
- string $message optional null
- Custom message for failure.
assertResponseContains() public ¶
assertResponseContains( string $content , string $message '' , boolean $ignoreCase false )
Asserts content exists in the response body.
Parameters
- string $content
- The content to check for.
- string $message optional ''
- The failure message that will be appended to the generated message.
- boolean $ignoreCase optional false
- A flag to check whether we should ignore case or not.
assertResponseEmpty() public ¶
assertResponseEmpty( string $message '' )
Assert response content is empty.
Parameters
- string $message optional ''
- The failure message that will be appended to the generated message.
assertResponseEquals() public ¶
assertResponseEquals( mixed $content , string $message '' )
Asserts content in the response body equals.
Parameters
- mixed $content
- The content to check for.
- string $message optional ''
- The failure message that will be appended to the generated message.
assertResponseError() public ¶
assertResponseError( string $message null )
Asserts that the response status code is in the 4xx range.
Parameters
- string $message optional null
- Custom message for failure.
assertResponseFailure() public ¶
assertResponseFailure( string $message null )
Asserts that the response status code is in the 5xx range.
Parameters
- string $message optional null
- Custom message for failure.
assertResponseNotContains() public ¶
assertResponseNotContains( string $content , string $message '' , boolean $ignoreCase false )
Asserts content does not exist in the response body.
Parameters
- string $content
- The content to check for.
- string $message optional ''
- The failure message that will be appended to the generated message.
- boolean $ignoreCase optional false
- A flag to check whether we should ignore case or not.
assertResponseNotEmpty() public ¶
assertResponseNotEmpty( string $message '' )
Assert response content is not empty.
Parameters
- string $message optional ''
- The failure message that will be appended to the generated message.
assertResponseNotEquals() public ¶
assertResponseNotEquals( mixed $content , string $message '' )
Asserts content in the response body not equals.
Parameters
- mixed $content
- The content to check for.
- string $message optional ''
- The failure message that will be appended to the generated message.
assertResponseNotRegExp() public ¶
assertResponseNotRegExp( string $pattern , string $message '' )
Asserts that the response body does not match a given regular expression.
Parameters
- string $pattern
- The pattern to compare against.
- string $message optional ''
- The failure message that will be appended to the generated message.
assertResponseOk() public ¶
assertResponseOk( string $message null )
Asserts that the response status code is in the 2xx range.
Parameters
- string $message optional null
- Custom message for failure.
assertResponseRegExp() public ¶
assertResponseRegExp( string $pattern , string $message '' )
Asserts that the response body matches a given regular expression.
Parameters
- string $pattern
- The pattern to compare against.
- string $message optional ''
- The failure message that will be appended to the generated message.
assertResponseSuccess() public ¶
assertResponseSuccess( string $message null )
Asserts that the response status code is in the 2xx/3xx range.
Parameters
- string $message optional null
- Custom message for failure.
assertSession() public ¶
assertSession( string $expected , string $path , string $message '' )
Asserts session contents
Parameters
- string $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.
assertTemplate() public ¶
assertTemplate( string $content , string $message '' )
Asserts that the search string was in the template name.
Parameters
- string $content
- The content to check for.
- string $message optional ''
- The failure message that will be appended to the generated message.
configApplication() public ¶
configApplication( string $class , array|null $constructorArgs )
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.
configRequest() public ¶
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.
Parameters
- array $data
- The request data to use.
controllerSpy() public ¶
controllerSpy( Cake\Event\Event
$event , Cake\Controller\Controller
|null $controller null )
Adds additional event spies to the controller/view event manager.
Parameters
-
Cake\Event\Event
$event - A dispatcher event.
-
Cake\Controller\Controller
|null $controller optional null - Controller instance.
cookie() public ¶
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.
Parameters
- string $name
- The cookie name to use.
- mixed $value
- The value of the cookie.
cookieEncrypted() public ¶
cookieEncrypted( string $name , mixed $value , string|boolean $encrypt 'aes' , string|null $key null )
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 optional 'aes'
- Encryption mode to use.
- string|null $key optional null
Encryption key used. Defaults to Security.salt.
See
delete() public ¶
delete( string|array $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.
Parameters
- string|array $url
- The URL to request.
Throws
disableErrorHandlerMiddleware() public ¶
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() public ¶
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() public ¶
enableRetainFlashMessages( )
Calling this method will re-store flash messages into the test session after being removed by the FlashHelper
enableSecurityToken() public ¶
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() protected ¶
extractExceptionMessage( Exception $exception )
Extract verbose message for existing exception
Parameters
- Exception $exception
- Exception to extract
Returns
extractVerboseMessage() protected ¶
extractVerboseMessage( string $message null )
Inspect controller to extract possible causes of the failed assertion
Parameters
- string $message optional null
- Original message to use as a base
Returns
get() public ¶
get( string|array $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.
Parameters
- string|array $url
- The URL to request.
Throws
head() public ¶
head( string|array $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.
Parameters
- string|array $url
- The URL to request.
Throws
options() public ¶
options( string|array $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.
Parameters
- string|array $url
- The URL to request.
Throws
patch() public ¶
patch( string|array $url , string|array|null $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.
Parameters
- string|array $url
- The URL to request.
- string|array|null $data optional []
- The data for the request.
Throws
post() public ¶
post( string|array $url , string|array|null $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.
Parameters
- string|array $url
- The URL to request.
- string|array|null $data optional []
- The data for the request.
Throws
put() public ¶
put( string|array $url , string|array|null $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.
Parameters
- string|array $url
- The URL to request.
- string|array|null $data optional []
- The data for the request.
Throws
session() public ¶
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.
Parameters
- array $data
- The session data to use.
setupServer() public ¶
setupServer( )
Auto-detect if the HTTP middleware stack should be used.
Before
useHttpServer() public ¶
useHttpServer( boolean $enable )
Toggle whether or not you want to use the HTTP Server stack.
Parameters
- boolean $enable
- Enable/disable the usage of the HTTP Stack.
viewVariable() public ¶
viewVariable( string $name )
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
The view variable if set.
Methods used from Cake\Utility\CookieCryptTrait
_checkCipher() protected ¶
_checkCipher( string $encrypt )
Helper method for validating encryption cipher names.
Parameters
- string $encrypt
- The cipher name.
Throws
When an invalid cipher is provided.
_decode() protected ¶
_decode( string $value , string|false $encrypt , string|null $key )
Decodes and decrypts a single value.
Parameters
- 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.
Returns
Decoded values.
_decrypt() protected ¶
_decrypt( array $values , string|boolean $mode , string|null $key null )
Decrypts $value using public $type method in Security class
Parameters
- array $values
- Values to decrypt
- string|boolean $mode
- Encryption mode
- string|null $key optional null
- Used as the security salt if specified.
Returns
Decrypted values
_encrypt() protected ¶
_encrypt( string $value , string|boolean $encrypt , string|null $key null )
Encrypts $value using public $type method in Security class
Parameters
- string $value
- Value to encrypt
- string|boolean $encrypt
Encryption mode to use. False disabled encryption.
- string|null $key optional null
- Used as the security salt if specified.
Returns
Encoded values
_explode() protected ¶
_explode( string $string )
Explode method to return array from string set in CookieComponent::_implode() Maintains reading backwards compatibility with 1.x CookieComponent::_implode().
Parameters
- string $string
- A string containing JSON encoded data, or a bare string.
Returns
Map of key and values
_implode() protected ¶
_implode( array $array )
Implode method to keep keys are multidimensional arrays
Parameters
- array $array
- Map of key and values
Returns
A json encoded string.
Methods used from Cake\View\Helper\SecureFieldTokenTrait
_buildFieldToken() protected ¶
_buildFieldToken( string $url , array $fields , array $unlockedFields [] )
Generate the token data for the provided inputs.
Parameters
- string $url
- The URL the form is being submitted to.
- array $fields
If set specifies the list of fields to use when generating the hash.
- array $unlockedFields optional []
The list of fields that are excluded from field validation.
Returns
The token data.
Properties detail
$_csrfToken ¶
Boolean flag for whether or not the request should have a CSRF token added.
false
$_retainFlashMessages ¶
Boolean flag for whether or not the request should re-store flash messages
false
$_securityToken ¶
Boolean flag for whether or not the request should have a SecurityComponent token added.
false
$_useHttpServer ¶
Track whether or not tests are run against the PSR7 HTTP stack.
false