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.
$_appArgs protected array|null$_appClass protected string|null$_controller protected $_cookie protected array$_cookieEncryptionKey protected null|string$_csrfToken protected booleanBoolean flag for whether or not the request should have a CSRF token added.
$_exception protected Exception|null$_flashMessages protected null|array$_layoutName protected string|null$_request protected array$_requestSession protected Cake\Http\Session|null$_response protected Cake\Http\Response|null$_retainFlashMessages protected booleanBoolean flag for whether or not the request should re-store flash messages
$_securityToken protected booleanBoolean flag for whether or not the request should have a SecurityComponent token added.
$_session protected array$_unlockedFields protected string[]$_useHttpServer protected booleanTrack whether or not tests are run against the PSR7 HTTP stack.
$_viewName protected string|nullRecursively casts all data to string as that is how data would be POSTed in the real world
Asserts cookie values which are encrypted by the CookieComponent.
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.
_addTokens( string $url , array $data )
Add the CSRF and Security Component tokens if necessary.
$url $data _buildRequest( string|array $url , string $method , string|array|null $data )
Creates a request object with the configured options and parameters.
$url $method $data _castToString( array $data )
Recursively casts all data to string as that is how data would be POSTed in the real world
$data _getBodyAsString( )
Get the response body as string
_getCookieEncryptionKey( )
Returns the encryption key to be used.
_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.
$exception _makeDispatcher( )
Get the correct dispatcher instance.
_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.
$url $method $data optional [] _url( string|array $url )
Creates a valid request url and parameter array more like Request::_url()
$url assertContentType( string $type , string $message = '' )
Asserts content type
$type $message optional '' assertCookie( string $expected , string $name , string $message = '' )
Asserts cookie values
$expected $name $message optional '' 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.
$expected $name $encrypt optional 'aes' $key optional null Encryption key used. Defaults to Security.salt.
$message optional '' assertCookieNotSet( string $cookie , string $message = '' )
Asserts a cookie has not been set in the response
$cookie $message optional '' assertFileResponse( string $expected , string $message = '' )
Asserts that a file with the given name was sent in the response
$expected $message optional '' assertFlashElement( string $expected , string $key = 'flash' , string $message = '' )
Asserts a flash element was set
$expected $key optional 'flash' $message optional '' assertFlashElementAt( integer $at , string $expected , string $key = 'flash' , string $message = '' )
Asserts a flash element was set at a certain index
$at $expected $key optional 'flash' $message optional '' assertFlashMessage( string $expected , string $key = 'flash' , string $message = '' )
Asserts a flash message was set
$expected $key optional 'flash' $message optional '' assertFlashMessageAt( integer $at , string $expected , string $key = 'flash' , string $message = '' )
Asserts a flash message was set at a certain index
$at $expected $key optional 'flash' $message optional '' assertHeader( string $header , string $content , string $message = '' )
Asserts response headers
$header $content $message optional '' assertHeaderContains( string $header , string $content , string $message = '' )
Asserts response header contains a string
$header $content $message optional '' assertHeaderNotContains( string $header , string $content , string $message = '' )
Asserts response header does not contain a string
$header $content $message optional '' assertLayout( string $content , string $message = '' )
Asserts that the search string was in the layout name.
$content $message optional '' assertNoRedirect( string $message = '' )
Asserts that the Location header is not set.
$message optional '' assertRedirect( string|array|null $url = null , string $message = '' )
Asserts that the Location header is correct.
$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.
$message optional '' assertRedirectContains( string $url , string $message = '' )
Asserts that the Location header contains a substring
$url $message optional '' assertRedirectNotContains( string $url , string $message = '' )
Asserts that the Location header does not contain a substring
$url $message optional '' assertResponseCode( integer $code , string $message = null )
Asserts a specific response status code.
$code $message optional null assertResponseContains( string $content , string $message = '' , boolean $ignoreCase = false )
Asserts content exists in the response body.
$content $message optional '' $ignoreCase optional false assertResponseEmpty( string $message = '' )
Assert response content is empty.
$message optional '' assertResponseEquals( mixed $content , string $message = '' )
Asserts content in the response body equals.
$content $message optional '' assertResponseError( string $message = null )
Asserts that the response status code is in the 4xx range.
$message optional null assertResponseFailure( string $message = null )
Asserts that the response status code is in the 5xx range.
$message optional null assertResponseNotContains( string $content , string $message = '' , boolean $ignoreCase = false )
Asserts content does not exist in the response body.
$content $message optional '' $ignoreCase optional false assertResponseNotEmpty( string $message = '' )
Assert response content is not empty.
$message optional '' assertResponseNotEquals( mixed $content , string $message = '' )
Asserts content in the response body not equals.
$content $message optional '' assertResponseNotRegExp( string $pattern , string $message = '' )
Asserts that the response body does not match a given regular expression.
$pattern $message optional '' assertResponseOk( string $message = null )
Asserts that the response status code is in the 2xx range.
$message optional null assertResponseRegExp( string $pattern , string $message = '' )
Asserts that the response body matches a given regular expression.
$pattern $message optional '' assertResponseSuccess( string $message = null )
Asserts that the response status code is in the 2xx/3xx range.
$message optional null assertSession( string $expected , string $path , string $message = '' )
Asserts session contents
$expected $path $message optional '' assertTemplate( string $content , string $message = '' )
Asserts that the search string was in the template name.
$content $message optional '' 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.
$class $constructorArgs 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.
$data controllerSpy( Cake\Event\Event $event , Cake\Controller\Controller|null $controller = null )
Adds additional event spies to the controller/view event manager.
Cake\Event\Event $event Cake\Controller\Controller|null $controller optional null 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.
$name $value 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.
$name $value $encrypt optional 'aes' $key optional null Encryption key used. Defaults to Security.salt.
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.
$url 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 extractVerboseMessage( string $message = null )
Inspect controller to extract possible causes of the failed assertion
$message optional null 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.
$url 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.
$url 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.
$url 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.
$url $data optional [] 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.
$url $data optional [] 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.
$url $data optional [] 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.
$data setUnlockedFields( array $unlockedFields = [] )
Set list of fields that are excluded from field validation.
$unlockedFields optional [] setupServer( )
Auto-detect if the HTTP middleware stack should be used.
useHttpServer( boolean $enable )
Toggle whether or not you want to use the HTTP Server stack.
$enable viewVariable( string $name )
Fetches a view variable by name.
If the view variable does not exist, null will be returned.
$name _checkCipher( string $encrypt )
Helper method for validating encryption cipher names.
$encrypt _decode( string $value , string|false $encrypt , string|null $key )
Decodes and decrypts a single value.
$value $encrypt $key _decrypt( array $values , string|boolean $mode , string|null $key = null )
Decrypts $value using public $type method in Security class
$values $mode $key optional null _encrypt( string $value , string|boolean $encrypt , string|null $key = null )
Encrypts $value using public $type method in Security class
$value $encrypt Encryption mode to use. False disabled encryption.
$key optional null _explode( string $string )
Explode method to return array from string set in CookieComponent::_implode() Maintains reading backwards compatibility with 1.x CookieComponent::_implode().
$string _implode( array $array )
Implode method to keep keys are multidimensional arrays
$array _buildFieldToken( string $url , array $fields , array $unlockedFields = [] )
Generate the token data for the provided inputs.
$url $fields If set specifies the list of fields to use when generating the hash.
$unlockedFields optional [] The list of fields that are excluded from field validation.
protected Cake\Controller\Controller|null
The controller used in the last request.
protected null|string
protected boolean
Boolean flag for whether or not the request should have a CSRF token added.
false
protected boolean
Boolean flag for whether or not the request should re-store flash messages
false
protected boolean
Boolean flag for whether or not the request should have a SecurityComponent token added.
false
protected string[]
List of fields that are excluded from field validation.
[]
protected boolean
Track whether or not tests are run against the PSR7 HTTP stack.
false
© 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/3.8/class-Cake.TestSuite.IntegrationTestTrait.html