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.
Property Summary
-
$_configure protected
arrayConfigure values to restore at end of test.
-
$_controller protected
Cake\Controller\ControllerThe controller used in the last request.
-
$_cookie protected
arrayCookie data to use in the next request.
-
$_exception protected
Cake\Core\Exception\ExceptionThe exception being thrown if the case.
-
$_layoutName protected
stringThe last rendered layout
-
$_pathRestore protected
arrayPath settings to restore at the end of the test.
-
$_request protected
arrayThe data used to build the next request.
-
$_requestSession protected
Cake\Network\SessionThe session instance from the last request
-
$_response protected
Cake\Network\ResponseThe response for the most recent request.
-
$_session protected
arraySession data to use in the next request.
-
$_viewName protected
stringThe last rendered view
-
$autoFixtures public
arrayBy default, all fixtures attached to this class will be truncated and reloaded after each test. Set this to false to handle manually
-
$dropTables public
boolControl table create/drops on each test method.
-
$fixtureManager public
Cake\TestSuite\Fixture\FixtureManagerThe class responsible for managing the creation, loading and removing of fixtures
Method Summary
-
_assertAttributes() protected
Check the attributes as part of an assertTags() check.
-
_assertStatus() protected
Helper method for status assertions.
-
_buildRequest() protected
Creates a request object with the configured options and parameters.
-
_handleError() protected
Attempts to render an error response for a given exception.
-
_normalizePath() protected
Normalize a path for comparison.
-
_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
-
assertHeader() public
Asserts response headers
-
assertHtml() public
Asserts HTML tags.
-
assertLayout() public
Asserts that the search string was in the layout name.
-
assertNoRedirect() public
Asserts that the Location header is not set.
-
assertNotWithinRange() protected static
Compatibility function to test if a value is not between an acceptable range.
-
assertPathEquals() protected static
Compatibility function to test paths.
-
assertRedirect() public
Asserts that the Location header is correct.
-
assertRedirectContains() public
Asserts that the Location header contains 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 exists in the response body.
-
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.
-
assertResponseOk() public
Asserts that the response status code is in the 2xx range.
-
assertResponseSuccess() public
Asserts that the response status code is in the 2xx/3xx range.
-
assertSession() public
Asserts session contents
-
assertTags() public deprecated
Asserts HTML tags.
-
assertTemplate() public
Asserts that the search string was in the template name.
-
assertTextContains() public
Assert that a string contains another string, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
-
assertTextEndsNotWith() public
Asserts that a string ends not with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
-
assertTextEndsWith() public
Asserts that a string ends with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
-
assertTextEquals() public
Assert text equality, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
-
assertTextNotContains() public
Assert that a text doesn't contain another text, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
-
assertTextNotEquals() public
Assert text equality, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
-
assertTextStartsNotWith() public
Asserts that a string starts not with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
-
assertTextStartsWith() public
Asserts that a string starts with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
-
assertWithinRange() protected static
Compatibility function to test if a value is between an acceptable range.
-
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.
-
delete() public
Performs a DELETE request using the current request data.
-
get() public
Performs a GET request using the current request data.
-
getMockForModel() public
Mock a model, maintain fixtures and table association
-
loadFixtures() public
Chooses which fixtures to load for a given test
-
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.
-
setUp() public
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() public
Overrides SimpleTestCase::skipIf to provide a boolean return value
-
skipUnless() protected
Compatibility function for skipping.
-
tearDown() public
Clears the state used for requests.
-
viewVariable() public
Fetches a view variable by name.
Method Detail
_assertAttributes() ¶ protected
_assertAttributes(array $assertions, string $string): string
Check the attributes as part of an assertTags() check.
Parameters
-
array$assertions Assertions to run.
-
string$string The HTML string to check.
Returns
string_assertStatus() ¶ protected
_assertStatus(int $min, int $max, string $message): void
Helper method for status assertions.
Parameters
-
int$min Min status code.
-
int$max Max status code.
-
string$message The error message.
Returns
void_buildRequest() ¶ protected
_buildRequest(string|array $url, string $method, array|null $data): Cake\Network\Request
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\RequestThe built request.
_handleError() ¶ protected
_handleError(Exception $exception): void
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.
Returns
voidThrows
Exception_normalizePath() ¶ protected
_normalizePath(string $path): string
Normalize a path for comparison.
Parameters
-
string$path Path separated by "/" slash.
Returns
stringNormalized path separated by DS.
_sendRequest() ¶ protected
_sendRequest(string|array $url, string $method, array|null $data = []): void
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.
Returns
voidThrows
Exception_url() ¶ protected
_url(string|array $url): array
Creates a valid request url and parameter array more like Request::_url()
Parameters
-
string|array$url The URL
Returns
arrayQualified URL and the query parameters
assertContentType() ¶ public
assertContentType(string $type, string $message = ''): void
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.
Returns
voidassertCookie() ¶ public
assertCookie(string $expected, string $name, string $message = ''): void
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.
Returns
voidassertHeader() ¶ public
assertHeader(string $header, string $content, string $message = ''): void
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.
Returns
voidassertHtml() ¶ public
assertHtml(array $expected, string $string, bool $fullDebug = false): bool
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
-
bool$fullDebug optional Whether or not more verbose output should be used.
Returns
boolassertLayout() ¶ public
assertLayout(string $content, string $message = ''): void
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.
Returns
voidassertNoRedirect() ¶ public
assertNoRedirect(string $message = ''): void
Asserts that the Location header is not set.
Parameters
-
string$message optional The failure message that will be appended to the generated message.
Returns
voidassertNotWithinRange() ¶ protected static
assertNotWithinRange(float $expected, float $result, float $margin, string $message = ''): void
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
Returns
voidassertPathEquals() ¶ protected static
assertPathEquals(string $expected, string $result, string $message = ''): void
Compatibility function to test paths.
Parameters
-
string$expected -
string$result -
string$message optional the text to display if the assertion is not correct
Returns
voidassertRedirect() ¶ public
assertRedirect(string|array|null $url = null, string $message = ''): void
Asserts that the Location header is correct.
Parameters
-
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.
Returns
voidassertRedirectContains() ¶ public
assertRedirectContains(string $url, string $message = ''): void
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.
Returns
voidassertResponseCode() ¶ public
assertResponseCode(int $code): void
Asserts a specific response status code.
Parameters
-
int$code Status code to assert.
Returns
voidassertResponseContains() ¶ public
assertResponseContains(string $content, string $message = ''): void
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.
Returns
voidassertResponseEmpty() ¶ public
assertResponseEmpty(string $message = ''): void
Assert response content is empty.
Parameters
-
string$message optional The failure message that will be appended to the generated message.
Returns
voidassertResponseEquals() ¶ public
assertResponseEquals(mixed $content, string $message = ''): void
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.
Returns
voidassertResponseError() ¶ public
assertResponseError(): void
Asserts that the response status code is in the 4xx range.
Returns
voidassertResponseFailure() ¶ public
assertResponseFailure(): void
Asserts that the response status code is in the 5xx range.
Returns
voidassertResponseNotContains() ¶ public
assertResponseNotContains(string $content, string $message = ''): void
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.
Returns
voidassertResponseNotEmpty() ¶ public
assertResponseNotEmpty(string $message = ''): void
Assert response content is not empty.
Parameters
-
string$message optional The failure message that will be appended to the generated message.
Returns
voidassertResponseOk() ¶ public
assertResponseOk(): void
Asserts that the response status code is in the 2xx range.
Returns
voidassertResponseSuccess() ¶ public
assertResponseSuccess(): void
Asserts that the response status code is in the 2xx/3xx range.
Returns
voidassertSession() ¶ public
assertSession(string $expected, string $path, string $message = ''): void
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.
Returns
voidassertTags() ¶ public
assertTags(string $string, array $expected, bool $fullDebug = false): void
Asserts HTML tags.
Parameters
-
string$string An HTML/XHTML/XML string
-
array$expected An array, see above
-
bool$fullDebug optional Whether or not more verbose output should be used.
Returns
voidassertTemplate() ¶ public
assertTemplate(string $content, string $message = ''): void
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.
Returns
voidassertTextContains() ¶ public
assertTextContains(string $needle, string $haystack, string $message = '', bool $ignoreCase = false): void
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.
-
bool$ignoreCase optional Whether or not the search should be case-sensitive.
Returns
voidassertTextEndsNotWith() ¶ public
assertTextEndsNotWith(string $suffix, string $string, string $message = ''): void
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.
Returns
voidassertTextEndsWith() ¶ public
assertTextEndsWith(string $suffix, string $string, string $message = ''): void
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.
Returns
voidassertTextEquals() ¶ public
assertTextEquals(string $expected, string $result, string $message = ''): void
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.
Returns
voidassertTextNotContains() ¶ public
assertTextNotContains(string $needle, string $haystack, string $message = '', bool $ignoreCase = false): void
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.
-
bool$ignoreCase optional Whether or not the search should be case-sensitive.
Returns
voidassertTextNotEquals() ¶ public
assertTextNotEquals(string $expected, string $result, string $message = ''): void
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.
Returns
voidassertTextStartsNotWith() ¶ public
assertTextStartsNotWith(string $prefix, string $string, string $message = ''): void
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.
Returns
voidassertTextStartsWith() ¶ public
assertTextStartsWith(string $prefix, string $string, string $message = ''): void
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.
Returns
voidassertWithinRange() ¶ protected static
assertWithinRange(float $expected, float $result, float $margin, string $message = ''): void
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
Returns
voidconfigRequest() ¶ public
configRequest(array $data): void
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.
Returns
voidcontrollerSpy() ¶ public
controllerSpy(Cake\Event\Event $event): void
Adds additional event spies to the controller/view event manager.
Parameters
-
Cake\Event\Event$event A dispatcher event.
Returns
voidcookie() ¶ public
cookie(string $name, mixed $value): void
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.
Returns
voiddelete() ¶ public
delete(string|array $url): void
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.
Returns
voidget() ¶ public
get(string|array $url): void
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.
Returns
voidgetMockForModel() ¶ public
getMockForModel(string $alias, mixed $methods = [], array $options = []): Model
Mock a model, maintain fixtures and table association
Parameters
-
string$alias The model to get a mock for.
-
mixed$methods optional The list of methods to mock
-
array$options optional The config data for the mock's constructor.
Returns
ModelThrows
Cake\ORM\Exception\MissingTableClassExceptionloadFixtures() ¶ public
loadFixtures(): void
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.
Returns
voidThrows
Exceptionwhen no fixture manager is available.
See Also
patch() ¶ public
patch(string|array $url, array $data = []): void
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.
Returns
voidpost() ¶ public
post(string|array $url, array $data = []): void
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.
Returns
voidput() ¶ public
put(string|array $url, array $data = []): void
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.
Returns
voidsession() ¶ public
session(array $data): void
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.
Returns
voidsetUp() ¶ public
setUp(): void
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.
Returns
voidskipIf() ¶ public
skipIf(bool $shouldSkip, string $message = ''): bool
Overrides SimpleTestCase::skipIf to provide a boolean return value
Parameters
-
bool$shouldSkip Whether or not the test should be skipped.
-
string$message optional The message to display.
Returns
boolskipUnless() ¶ protected
skipUnless(bool $condition, string $message = ''): bool
Compatibility function for skipping.
Parameters
-
bool$condition Condition to trigger skipping
-
string$message optional Message for skip
Returns
boolviewVariable() ¶ public
viewVariable(string $name): mixed
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
mixedThe view variable if set.
Property Detail
$autoFixtures ¶ public
By default, all fixtures attached to this class will be truncated and reloaded after each test. Set this to false to handle manually
Type
array$dropTables ¶ public
Control table create/drops on each test method.
If true, tables will still be dropped at the end of each test runner execution.
Type
bool$fixtureManager ¶ public
The class responsible for managing the creation, loading and removing of fixtures
Type
Cake\TestSuite\Fixture\FixtureManager