A test case class 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\TestCase
Cake\TestSuite\IntegrationTestCase uses Cake\Utility\CookieCryptTrait, Cake\View\Helper\SecureFieldTokenTrait _addTokens( string $url , array $data )
Add the CSRF and Security Component tokens if necessary.
$url $data array_assertStatus( integer $min , integer $max , string $message )
Helper method for status assertions.
$min $max $message _buildRequest( string|array $url , string $method , array|null $data )
Creates a request object with the configured options and parameters.
$url $method $data Cake\Network\Request_getCookieEncryptionKey( )
Returns the encryption key to be used.
string_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 Exception_sendRequest( string|array $url , string $method , array|null $data [] )
Creates and send the request into a Dispatcher instance.
Receives and stores the response for future inspection.
$url $method $data optional [] Exception_url( string|array $url )
Creates a valid request url and parameter array more like Request::_url()
$url arrayassertContentType( 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 $message optional '' assertHeader( string $header , string $content , string $message '' )
Asserts response headers
$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 $message optional '' assertRedirectContains( string $url , string $message '' )
Asserts that the Location header contains a substring
$url $message optional '' assertResponseCode( integer $code )
Asserts a specific response status code.
$code assertResponseContains( string $content , string $message '' )
Asserts content exists in the response body.
$content $message optional '' assertResponseEmpty( string $message '' )
Assert response content is empty.
$message optional '' assertResponseEquals( mixed $content , string $message '' )
Asserts content exists in the response body.
$content $message optional '' assertResponseError( )
Asserts that the response status code is in the 4xx range.
assertResponseFailure( )
Asserts that the response status code is in the 5xx range.
assertResponseNotContains( string $content , string $message '' )
Asserts content does not exist in the response body.
$content $message optional '' assertResponseNotEmpty( string $message '' )
Assert response content is not empty.
$message optional '' assertResponseOk( )
Asserts that the response status code is in the 2xx range.
assertResponseSuccess( )
Asserts that the response status code is in the 2xx/3xx range.
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 '' 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 )
Adds additional event spies to the controller/view event manager.
Cake\Event\Event $event 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 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 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.
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.
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 patch( string|array $url , array $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 , array $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 , array $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 tearDown( )
Clears the state used for requests.
Cake\TestSuite\TestCase::tearDown() viewVariable( string $name )
Fetches a view variable by name.
If the view variable does not exist, null will be returned.
$name mixed_assertAttributes( array $assertions , string $string , boolean $fullDebug false , array $regex '' )
Check the attributes as part of an assertTags() check.
$assertions $string $fullDebug optional false $regex optional '' assertHtml
string_normalizePath( string $path )
Normalize a path for comparison.
$path stringassertHtml( array $expected , string $string , boolean $fullDebug false )
Asserts HTML tags.
Takes an array $expected and generates a regex from it to match the provided $string. Samples for $expected:
Checks for an input tag with a name attribute (contains any non-empty value) and an id attribute that contains 'my-input':
['input' => ['name', 'id' => 'my-input']]
Checks for two p elements with some text in them:
[ ['p' => true], 'textA', '/p', ['p' => true], 'textB', '/p' ]
You can also specify a pattern expression as part of the attribute values, or the tag being defined, if you prepend the value with preg: and enclose it with slashes, like so:
[ ['input' => ['name', 'id' => 'preg:/FieldName\d+/']], 'preg:/My\s+field/' ]
Important: This function is very forgiving about whitespace and also accepts any permutation of attribute order. It will also allow whitespace between specified tags.
$expected $string $fullDebug optional false booleanassertNotWithinRange( float $expected , float $result , float $margin , string $message '' )
Compatibility function to test if a value is not between an acceptable range.
$expected $result $margin $message optional '' assertPathEquals( string $expected , string $result , string $message '' )
Compatibility function to test paths.
$expected $result $message optional '' assertTags( string $string , array $expected , boolean $fullDebug false )
Asserts HTML tags.
$string $expected $fullDebug optional false assertTextContains( string $needle , string $haystack , string $message '' , boolean $ignoreCase false )
Assert that a string contains another string, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
$needle $haystack $message optional '' $ignoreCase optional false assertTextEndsNotWith( string $suffix , string $string , string $message '' )
Asserts that a string ends not with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
$suffix $string $message optional '' assertTextEndsWith( string $suffix , string $string , string $message '' )
Asserts that a string ends with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
$suffix $string $message optional '' assertTextEquals( string $expected , string $result , string $message '' )
Assert text equality, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
$expected $result $message optional '' assertTextNotContains( string $needle , string $haystack , string $message '' , boolean $ignoreCase false )
Assert that a text doesn't contain another text, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
$needle $haystack $message optional '' $ignoreCase optional false assertTextNotEquals( string $expected , string $result , string $message '' )
Assert text equality, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
$expected $result $message optional '' assertTextStartsNotWith( string $prefix , string $string , string $message '' )
Asserts that a string starts not with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
$prefix $string $message optional '' assertTextStartsWith( string $prefix , string $string , string $message '' )
Asserts that a string starts with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
$prefix $string $message optional '' assertWithinRange( float $expected , float $result , float $margin , string $message '' )
Compatibility function to test if a value is between an acceptable range.
$expected $result $margin $message optional '' getMockForModel( string $alias , array $methods [] , array $options [] )
Mock a model, maintain fixtures and table association
$alias $methods optional [] $options optional [] Cake\ORM\Table|PHPUnit_Framework_MockObject_MockObjectCake\ORM\Exception\MissingTableClassExceptionloadFixtures( )
Chooses which fixtures to load for a given test
Each parameter is a model name that corresponds to a fixture, i.e. 'Posts', 'Authors', etc.
ExceptionCake\TestSuite\TestCase::$autoFixturessetUp( )
Setup the test case, backup the static object values so they can be restored. Specifically backs up the contents of Configure and paths in App if they have not already been backed up.
skipIf( boolean $shouldSkip , string $message '' )
Overrides SimpleTestCase::skipIf to provide a boolean return value
$shouldSkip $message optional '' booleanskipUnless( boolean $condition , string $message '' )
Compatibility function for skipping.
$condition $message optional '' boolean_checkCipher( string $encrypt )
Helper method for validating encryption cipher names.
$encrypt RuntimeException_decode( string $value , string|false $encrypt )
Decodes and decrypts a single value.
$value $encrypt string_decrypt( array $values , string|boolean $mode )
Decrypts $value using public $type method in Security class
$values $mode string_encrypt( string $value , string|boolean $encrypt , string|null $key null )
Encrypts $value using public $type method in Security class
$value $encrypt $key optional null string_explode( string $string )
Explode method to return array from string set in CookieComponent::_implode() Maintains reading backwards compatibility with 1.x CookieComponent::_implode().
$string array_implode( array $array )
Implode method to keep keys are multidimensional arrays
$array string_buildFieldToken( string $url , array $fields , array $unlockedFields [] )
Generate the token data for the provided inputs.
$url $fields $unlockedFields optional [] arraypublic array
By default, all fixtures attached to this class will be truncated and reloaded after each test. Set this to false to handle manually
true public boolean
Control table create/drops on each test method.
If true, tables will still be dropped at the end of each test runner execution.
false public Cake\TestSuite\Fixture\FixtureManager
The class responsible for managing the creation, loading and removing of fixtures
null
© 2005–2016 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.
http://api.cakephp.org/3.1/class-Cake.TestSuite.IntegrationTestCase.html