Class IntegrationTestCase
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.
- PHPUnit_Framework_TestCase
- Cake\TestSuite\TestCase
- Cake\TestSuite\IntegrationTestCase uses Cake\Utility\CookieCryptTrait, Cake\View\Helper\SecureFieldTokenTrait
Method Detail
_addTokenssource 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
array
The request body with tokens added.
_assertStatussource protected
_assertStatus( integer $min , integer $max , string $message )
Helper method for status assertions.
Parameters
- integer
$min
- Min status code.
- integer
$max
- Max status code.
- string
$message
- The error message.
_buildRequestsource protected
_buildRequest( string|array $url , string $method , array|null $data )
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
Cake\Network\Request
The built request.
_getCookieEncryptionKeysource protected
_getCookieEncryptionKey( )
Returns the encryption key to be used.
Returns
string
string
_handleErrorsource 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
Exception
\Exception
_sendRequestsource protected
_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.
Parameters
- string|array
$url
- The URL
- string
$method
- The HTTP method
- array|null
$data
optional [] - The request data.
Throws
Exception
\Exception
_urlsource protected
_url( string|array $url )
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
assertContentTypesource 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.
assertCookiesource 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.
assertCookieEncryptedsource 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
CookieCryptTrait::_encryptassertHeadersource 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.
assertLayoutsource 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.
assertNoRedirectsource 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.
assertRedirectsource 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.
assertRedirectContainssource 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.
assertResponseCodesource public
assertResponseCode( integer $code )
Asserts a specific response status code.
Parameters
- integer
$code
- Status code to assert.
assertResponseContainssource public
assertResponseContains( string $content , string $message '' )
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.
assertResponseEmptysource public
assertResponseEmpty( string $message '' )
Assert response content is empty.
Parameters
- string
$message
optional '' - The failure message that will be appended to the generated message.
assertResponseEqualssource public
assertResponseEquals( mixed $content , string $message '' )
Asserts content exists in the response body.
Parameters
- mixed
$content
- The content to check for.
- string
$message
optional '' - The failure message that will be appended to the generated message.
assertResponseErrorsource public
assertResponseError( )
Asserts that the response status code is in the 4xx range.
assertResponseFailuresource public
assertResponseFailure( )
Asserts that the response status code is in the 5xx range.
assertResponseNotContainssource public
assertResponseNotContains( string $content , string $message '' )
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.
assertResponseNotEmptysource 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.
assertResponseOksource public
assertResponseOk( )
Asserts that the response status code is in the 2xx range.
assertResponseSuccesssource public
assertResponseSuccess( )
Asserts that the response status code is in the 2xx/3xx range.
assertSessionsource 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.
assertTemplatesource 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.
configRequestsource 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.
controllerSpysource public
controllerSpy( Cake\Event\Event $event )
Adds additional event spies to the controller/view event manager.
Parameters
-
Cake\Event\Event
$event
- A dispatcher event.
cookiesource 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.
cookieEncryptedsource 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
CookieCryptTrait::_encryptdeletesource 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.
enableCsrfTokensource 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.
enableSecurityTokensource 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.
getsource 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.
patchsource public
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.
Parameters
- string|array
$url
- The URL to request.
- array
$data
optional [] - The data for the request.
postsource public
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.
Parameters
- string|array
$url
- The URL to request.
- array
$data
optional [] - The data for the request.
putsource public
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.
Parameters
- string|array
$url
- The URL to request.
- array
$data
optional [] - The data for the request.
sessionsource 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.
tearDownsource public
tearDown( )
Clears the state used for requests.
Overrides
Cake\TestSuite\TestCase::tearDown()
viewVariablesource 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
mixed
The view variable if set.
Methods inherited from Cake\TestSuite\TestCase
_assertAttributessource protected
_assertAttributes( array $assertions , string $string , boolean $fullDebug false , array $regex '' )
Check the attributes as part of an assertTags() check.
Parameters
- array
$assertions
- Assertions to run.
- string
$string
- The HTML string to check.
- boolean
$fullDebug
optional false - Whether or not more verbose output should be used.
- array
$regex
optional '' - Full regexp from
assertHtml
Returns
string
string
_normalizePathsource protected
_normalizePath( string $path )
Normalize a path for comparison.
Parameters
- string
$path
- Path separated by "/" slash.
Returns
string
Normalized path separated by DS.
assertHtmlsource public
assertHtml( 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.
Parameters
- array
$expected
- An array, see above
- string
$string
- An HTML/XHTML/XML string
- boolean
$fullDebug
optional false - Whether or not more verbose output should be used.
Returns
boolean
bool
assertNotWithinRangesource protected static
assertNotWithinRange( float $expected , float $result , float $margin , string $message '' )
Compatibility function to test if a value is not between an acceptable range.
Parameters
- float
$expected
- float
$result
- float
$margin
- the rage of acceptation
- string
$message
optional '' - the text to display if the assertion is not correct
assertPathEqualssource protected static
assertPathEquals( string $expected , string $result , string $message '' )
Compatibility function to test paths.
Parameters
- string
$expected
- string
$result
- string
$message
optional '' - the text to display if the assertion is not correct
assertTagssource public
assertTags( string $string , array $expected , boolean $fullDebug false )
Asserts HTML tags.
Deprecated
3.0. Use assertHtml() instead.Parameters
- string
$string
- An HTML/XHTML/XML string
- array
$expected
- An array, see above
- boolean
$fullDebug
optional false - Whether or not more verbose output should be used.
assertTextContainssource public
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.
Parameters
- string
$needle
- The string to search for.
- string
$haystack
- The string to search through.
- string
$message
optional '' - The message to display on failure.
- boolean
$ignoreCase
optional false - Whether or not the search should be case-sensitive.
assertTextEndsNotWithsource public
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.
Parameters
- string
$suffix
- The suffix to not find.
- string
$string
- The string to search.
- string
$message
optional '' - The message to use for failure.
assertTextEndsWithsource public
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.
Parameters
- string
$suffix
- The suffix to find.
- string
$string
- The string to search.
- string
$message
optional '' - The message to use for failure.
assertTextEqualssource public
assertTextEquals( string $expected , string $result , string $message '' )
Assert text equality, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
Parameters
- string
$expected
- The expected value.
- string
$result
- The actual value.
- string
$message
optional '' - The message to use for failure.
assertTextNotContainssource public
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.
Parameters
- string
$needle
- The string to search for.
- string
$haystack
- The string to search through.
- string
$message
optional '' - The message to display on failure.
- boolean
$ignoreCase
optional false - Whether or not the search should be case-sensitive.
assertTextNotEqualssource public
assertTextNotEquals( string $expected , string $result , string $message '' )
Assert text equality, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
Parameters
- string
$expected
- The expected value.
- string
$result
- The actual value.
- string
$message
optional '' - The message to use for failure.
assertTextStartsNotWithsource public
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.
Parameters
- string
$prefix
- The prefix to not find.
- string
$string
- The string to search.
- string
$message
optional '' - The message to use for failure.
assertTextStartsWithsource public
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.
Parameters
- string
$prefix
- The prefix to check for.
- string
$string
- The string to search in.
- string
$message
optional '' - The message to use for failure.
assertWithinRangesource protected static
assertWithinRange( float $expected , float $result , float $margin , string $message '' )
Compatibility function to test if a value is between an acceptable range.
Parameters
- float
$expected
- float
$result
- float
$margin
- the rage of acceptation
- string
$message
optional '' - the text to display if the assertion is not correct
getMockForModelsource public
getMockForModel( string $alias , array $methods [] , array $options [] )
Mock a model, maintain fixtures and table association
Parameters
- string
$alias
- The model to get a mock for.
- array
$methods
optional [] - The list of methods to mock
- array
$options
optional [] - The config data for the mock's constructor.
Returns
Cake\ORM\Table|PHPUnit_Framework_MockObject_MockObject
\Cake\ORM\Table|\PHPUnit_Framework_MockObject_MockObject
Throws
Cake\ORM\Exception\MissingTableClassException
\Cake\ORM\Exception\MissingTableClassException
loadFixturessource public
loadFixtures( )
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.
Throws
Exception
when no fixture manager is available.
See
Cake\TestSuite\TestCase::$autoFixtures
setUpsource public
setUp( )
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.
skipIfsource public
skipIf( boolean $shouldSkip , string $message '' )
Overrides SimpleTestCase::skipIf to provide a boolean return value
Parameters
- boolean
$shouldSkip
- Whether or not the test should be skipped.
- string
$message
optional '' - The message to display.
Returns
boolean
bool
skipUnlesssource protected
skipUnless( boolean $condition , string $message '' )
Compatibility function for skipping.
Parameters
- boolean
$condition
- Condition to trigger skipping
- string
$message
optional '' - Message for skip
Returns
boolean
bool
Methods used from Cake\Utility\CookieCryptTrait
_checkCiphersource protected
_checkCipher( string $encrypt )
Helper method for validating encryption cipher names.
Parameters
- string
$encrypt
- The cipher name.
Throws
RuntimeException
When an invalid cipher is provided.
_decodesource protected
_decode( string $value , string|false $encrypt )
Decodes and decrypts a single value.
Parameters
- string
$value
- The value to decode & decrypt.
- string|false
$encrypt
- The encryption cipher to use.
Returns
string
Decoded value.
_decryptsource protected
_decrypt( array $values , string|boolean $mode )
Decrypts $value using public $type method in Security class
Parameters
- array
$values
- Values to decrypt
- string|boolean
$mode
- Encryption mode
Returns
string
decrypted string
_encryptsource 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 only in this time for tests if specified.
Returns
string
Encoded values
_explodesource 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
array
Map of key and values
_implodesource protected
_implode( array $array )
Implode method to keep keys are multidimensional arrays
Parameters
- array
$array
- Map of key and values
Returns
string
A json encoded string.
Methods used from Cake\View\Helper\SecureFieldTokenTrait
_buildFieldTokensource 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
array
The token data.
Properties summary
Properties inherited from Cake\TestSuite\TestCase
$autoFixturessource
public 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
$dropTablessource
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
$fixtureManagersource
public Cake\TestSuite\Fixture\FixtureManager
The class responsible for managing the creation, loading and removing of fixtures
null
Properties used from Cake\Utility\CookieCryptTrait
© 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