Fr3nch13/CakePHP Utilities

AssertIntegrationTestCase extends TestCase
in package
uses IntegrationTestTrait

This class helps in indirectly testing the functionality of IntegrationTestCase

Table of Contents

Properties

$autoFixtures  : bool
By default, all fixtures attached to this class will be truncated and reloaded after each test.
$dropTables  : bool
Control table create/drops on each test method.
$fixtureManager  : FixtureManager|null
The class responsible for managing the creation, loading and removing of fixtures
$_configure  : array<string|int, mixed>
Configure values to restore at end of test.
$_controller  : Controller|null
The controller used in the last request.
$_cookie  : array<string|int, mixed>
Cookie data to use in the next request.
$_cookieEncryptionKey  : string|null
$_csrfKeyName  : string
The name that will be used when retrieving the csrf token.
$_csrfToken  : bool
Boolean flag for whether the request should have a CSRF token added.
$_exception  : Throwable|null
The exception being thrown if the case.
$_flashMessages  : array<string|int, mixed>
Stored flash messages before render
$_layoutName  : string
The last rendered layout
$_request  : array<string|int, mixed>
The data used to build the next request.
$_requestSession  : Session
The session instance from the last request
$_response  : ResponseInterface|null
The response for the most recent request.
$_retainFlashMessages  : bool
Boolean flag for whether the request should re-store flash messages
$_securityToken  : bool
Boolean flag for whether the request should have a SecurityComponent token added.
$_session  : array<string|int, mixed>
Session data to use in the next request.
$_tableLocator  : LocatorInterface|null
Table locator instance
$_unlockedFields  : array<string|int, string>
List of fields that are excluded from field validation.
$_viewName  : string
The last rendered view
$defaultTable  : string|null
This object's default table alias.
$fixtures  : array<string|int, string>
Fixtures used by this test case.
$fixtureStrategy  : FixtureStrategyInterface|null

Methods

assertContentType()  : void
Asserts content type
assertCookie()  : void
Asserts cookie values
assertCookieEncrypted()  : void
Asserts cookie values which are encrypted by the CookieComponent.
assertCookieNotSet()  : void
Asserts a cookie has not been set in the response
assertDirectoryDoesNotExist()  : void
Asserts that a directory does not exist.
assertDoesNotMatchRegularExpression()  : void
Asserts that a string does not match a given regular expression.
assertEqualsSql()  : void
Assert that a string matches SQL with db-specific characters like quotes removed.
assertEventFired()  : void
Asserts that a global event was fired. You must track events in your event manager for this assertion to work
assertEventFiredWith()  : void
Asserts an event was fired with data
assertFileDoesNotExist()  : void
Asserts that a file does not exist.
assertFileResponse()  : void
Asserts that a file with the given name was sent in the response
assertFlashElement()  : void
Asserts a flash element was set
assertFlashElementAt()  : void
Asserts a flash element was set at a certain index
assertFlashMessage()  : void
Asserts a flash message was set
assertFlashMessageAt()  : void
Asserts a flash message was set at a certain index
assertHeader()  : void
Asserts response headers
assertHeaderContains()  : void
Asserts response header contains a string
assertHeaderNotContains()  : void
Asserts response header does not contain a string
assertHtml()  : bool
Asserts HTML tags.
assertLayout()  : void
Asserts that the search string was in the layout name.
assertMatchesRegularExpression()  : void
Asserts that a string matches a given regular expression.
assertNoRedirect()  : void
Asserts that the Location header is not set.
assertRedirect()  : void
Asserts that the Location header is correct. Comparison is made against a full URL.
assertRedirectContains()  : void
Asserts that the Location header contains a substring
assertRedirectEquals()  : void
Asserts that the Location header is correct. Comparison is made against exactly the URL provided.
assertRedirectNotContains()  : void
Asserts that the Location header does not contain a substring
assertRegExpSql()  : void
Assertion for comparing a regex pattern against a query having its identifiers quoted. It accepts queries quoted with the characters `<` and `>`. If the third parameter is set to true, it will alter the pattern to both accept quoted and unquoted queries
assertResponseCode()  : void
Asserts a specific response status code.
assertResponseContains()  : void
Asserts content exists in the response body.
assertResponseEmpty()  : void
Assert response content is empty.
assertResponseEquals()  : void
Asserts content in the response body equals.
assertResponseError()  : void
Asserts that the response status code is in the 4xx range.
assertResponseFailure()  : void
Asserts that the response status code is in the 5xx range.
assertResponseNotContains()  : void
Asserts content does not exist in the response body.
assertResponseNotEmpty()  : void
Assert response content is not empty.
assertResponseNotEquals()  : void
Asserts content in the response body not equals.
assertResponseNotRegExp()  : void
Asserts that the response body does not match a given regular expression.
assertResponseOk()  : void
Asserts that the response status code is in the 2xx range.
assertResponseRegExp()  : void
Asserts that the response body matches a given regular expression.
assertResponseSuccess()  : void
Asserts that the response status code is in the 2xx/3xx range.
assertSession()  : void
Asserts session contents
assertSessionHasKey()  : void
Asserts session key exists.
assertSessionNotHasKey()  : void
Asserts a session key does not exist.
assertTemplate()  : void
Asserts that the search string was in the template name.
assertTextContains()  : void
Assert that a string contains another string, ignoring differences in newlines.
assertTextEndsNotWith()  : void
Asserts that a string ends not with a given prefix, ignoring differences in newlines.
assertTextEndsWith()  : void
Asserts that a string ends with a given prefix, ignoring differences in newlines.
assertTextEquals()  : void
Assert text equality, ignoring differences in newlines.
assertTextNotContains()  : void
Assert that a text doesn't contain another text, ignoring differences in newlines.
assertTextNotEquals()  : void
Assert text equality, ignoring differences in newlines.
assertTextStartsNotWith()  : void
Asserts that a string starts not with a given prefix, ignoring differences in newlines.
assertTextStartsWith()  : void
Asserts that a string starts with a given prefix, ignoring differences in newlines.
cleanup()  : void
Clears the state used for requests.
clearPlugins()  : void
Clear all plugins from the global plugin collection.
configRequest()  : void
Configures the data for the *next* request.
controllerSpy()  : void
Adds additional event spies to the controller/view event manager.
cookie()  : void
Sets a request cookie for future requests.
cookieEncrypted()  : void
Sets a encrypted request cookie for future requests.
delete()  : void
Performs a DELETE request using the current request data.
deprecated()  : void
Helper method for check deprecation methods
disableErrorHandlerMiddleware()  : void
Disable the error handler middleware.
enableCsrfToken()  : void
Calling this method will add a CSRF token to the request.
enableRetainFlashMessages()  : void
Calling this method will re-store flash messages into the test session after being removed by the FlashHelper
enableSecurityToken()  : void
Calling this method will enable a SecurityComponent compatible token to be added to request data. This lets you easily test actions protected by SecurityComponent.
fetchTable()  : Table
Convenience method to get a table instance.
get()  : void
Performs a GET request using the current request data.
getFixtures()  : array<string|int, string>
Get the fixtures this test should use.
getMockForModel()  : Table|MockObject
Mock a model, maintain fixtures and table association
getTableLocator()  : LocatorInterface
Gets the table locator.
head()  : void
Performs a HEAD request using the current request data.
loadFixtures()  : void
Chooses which fixtures to load for a given test
loadPlugins()  : BaseApplication
Load plugins into a simulated application.
loadRoutes()  : void
Load routes for the application.
options()  : void
Performs an OPTIONS request using the current request data.
patch()  : void
Performs a PATCH request using the current request data.
post()  : void
Performs a POST request using the current request data.
put()  : void
Performs a PUT request using the current request data.
removePlugins()  : void
Remove plugins from the global plugin collection.
session()  : void
Sets session data.
setAppNamespace()  : string|null
Set the app namespace
setTableLocator()  : $this
Sets the table locator.
setUnlockedFields()  : void
Set list of fields that are excluded from field validation.
skipIf()  : bool
Overrides SimpleTestCase::skipIf to provide a boolean return value
testBadAssertNoRedirect()  : void
testBadAssertNoRedirect
viewVariable()  : mixed
Fetches a view variable by name.
withErrorReporting()  : void
Helper method for tests that needs to use error_reporting()
_addTokens()  : array<string|int, mixed>
Add the CSRF and Security Component tokens if necessary.
_assertAttributes()  : string|false
Check the attributes as part of an assertTags() check.
_buildRequest()  : array<string|int, mixed>
Creates a request object with the configured options and parameters.
_castToString()  : array<string|int, mixed>
Recursively casts all data to string as that is how data would be POSTed in the real world
_getBodyAsString()  : string
Get the response body as string
_getCookieEncryptionKey()  : string
Returns the encryption key to be used.
_getTableClassName()  : string
Gets the class name for the table.
_handleError()  : void
Attempts to render an error response for a given exception.
_makeDispatcher()  : MiddlewareDispatcher
Get the correct dispatcher instance.
_normalizePath()  : string
Normalize a path for comparison.
_sendRequest()  : void
Creates and send the request into a Dispatcher instance.
_url()  : array<string|int, mixed>
Creates a valid request url and parameter array more like Request::_url()
addFixture()  : $this
Adds a fixture to this test case.
assertNotWithinRange()  : void
Compatibility function to test if a value is not between an acceptable range.
assertPathEquals()  : void
Compatibility function to test paths.
assertWithinRange()  : void
Compatibility function to test if a value is between an acceptable range.
extractExceptionMessage()  : string
Extract verbose message for existing exception
extractVerboseMessage()  : string
Inspect controller to extract possible causes of the failed assertion
getFixtureStrategy()  : FixtureStrategyInterface
Returns fixture strategy used by these tests.
getSession()  : TestSession
setUp()  : void
Setup the test case, backup the static object values so they can be restored.
setupFixtures()  : void
Initialized and loads any use fixtures.
skipUnless()  : bool
Compatibility function for skipping.
tearDown()  : void
teardown any static object changes and restore them.
teardownFixtures()  : void
Unloads any use fixtures.

Properties

$autoFixtures

By default, all fixtures attached to this class will be truncated and reloaded after each test.

autoFixtures is only used by deprecated fixture features. This property will be removed in 5.0

public bool $autoFixtures = true

Set this to false to handle manually

$dropTables

Control table create/drops on each test method.

dropTables is only used by deprecated fixture features. This property will be removed in 5.0

public bool $dropTables = false

If true, tables will still be dropped at the end of each test runner execution.

$fixtureManager

The class responsible for managing the creation, loading and removing of fixtures

public static FixtureManager|null $fixtureManager

$_configure

Configure values to restore at end of test.

protected array<string|int, mixed> $_configure = []

Cookie data to use in the next request.

protected array<string|int, mixed> $_cookie = []

$_csrfKeyName

The name that will be used when retrieving the csrf token.

protected string $_csrfKeyName = 'csrfToken'

$_csrfToken

Boolean flag for whether the request should have a CSRF token added.

protected bool $_csrfToken = false

$_exception

The exception being thrown if the case.

protected Throwable|null $_exception

$_flashMessages

Stored flash messages before render

protected array<string|int, mixed> $_flashMessages = []

$_request

The data used to build the next request.

protected array<string|int, mixed> $_request = []

$_retainFlashMessages

Boolean flag for whether the request should re-store flash messages

protected bool $_retainFlashMessages = false

$_securityToken

Boolean flag for whether the request should have a SecurityComponent token added.

protected bool $_securityToken = false

$_session

Session data to use in the next request.

protected array<string|int, mixed> $_session = []

$_unlockedFields

List of fields that are excluded from field validation.

protected array<string|int, string> $_unlockedFields = []

$defaultTable

This object's default table alias.

protected string|null $defaultTable = null

$fixtures

Fixtures used by this test case.

protected array<string|int, string> $fixtures = []

Methods

assertContentType()

Asserts content type

public assertContentType(string $type[, string $message = '' ]) : void
Parameters
$type : string

The content-type to check for.

$message : string = ''

The failure message that will be appended to the generated message.

assertCookie()

Asserts cookie values

public assertCookie(mixed $expected, string $name[, string $message = '' ]) : void
Parameters
$expected : mixed

The expected contents.

$name : string

The cookie name.

$message : string = ''

The failure message that will be appended to the generated message.

assertCookieEncrypted()

Asserts cookie values which are encrypted by the CookieComponent.

public assertCookieEncrypted(mixed $expected, string $name[, string $encrypt = 'aes' ][, string|null $key = null ][, string $message = '' ]) : void

The difference from assertCookie() is this decrypts the cookie value like the CookieComponent for this assertion.

Parameters
$expected : mixed

The expected contents.

$name : string

The cookie name.

$encrypt : string = 'aes'

Encryption mode to use.

$key : string|null = null

Encryption key used. Defaults to Security.salt.

$message : string = ''

The failure message that will be appended to the generated message.

Tags
see
CookieCryptTrait::_encrypt()

assertCookieNotSet()

Asserts a cookie has not been set in the response

public assertCookieNotSet(string $cookie[, string $message = '' ]) : void
Parameters
$cookie : string

The cookie name to check

$message : string = ''

The failure message that will be appended to the generated message.

assertDirectoryDoesNotExist()

Asserts that a directory does not exist.

public static assertDirectoryDoesNotExist(string $directory[, string $message = '' ]) : void
Parameters
$directory : string

Directory

$message : string = ''

Message

Tags
throws
InvalidArgumentException
codeCoverageIgnore

assertDoesNotMatchRegularExpression()

Asserts that a string does not match a given regular expression.

public static assertDoesNotMatchRegularExpression(string $pattern, string $string[, string $message = '' ]) : void
Parameters
$pattern : string

Regex pattern

$string : string

String to test

$message : string = ''

Message

Tags
throws
InvalidArgumentException

assertEqualsSql()

Assert that a string matches SQL with db-specific characters like quotes removed.

public assertEqualsSql(string $expected, string $actual[, string $message = '' ]) : void
Parameters
$expected : string

The expected sql

$actual : string

The sql to compare

$message : string = ''

The message to display on failure

assertEventFired()

Asserts that a global event was fired. You must track events in your event manager for this assertion to work

public assertEventFired(string $name[, EventManager|null $eventManager = null ][, string $message = '' ]) : void
Parameters
$name : string

Event name

$eventManager : EventManager|null = null

Event manager to check, defaults to global event manager

$message : string = ''

Assertion failure message

assertEventFiredWith()

Asserts an event was fired with data

public assertEventFiredWith(string $name, string $dataKey, mixed $dataValue[, EventManager|null $eventManager = null ][, string $message = '' ]) : void

If a third argument is passed, that value is used to compare with the value in $dataKey

Parameters
$name : string

Event name

$dataKey : string

Data key

$dataValue : mixed

Data value

$eventManager : EventManager|null = null

Event manager to check, defaults to global event manager

$message : string = ''

Assertion failure message

assertFileDoesNotExist()

Asserts that a file does not exist.

public static assertFileDoesNotExist(string $filename[, string $message = '' ]) : void
Parameters
$filename : string

Filename

$message : string = ''

Message

Tags
throws
InvalidArgumentException
codeCoverageIgnore

assertFileResponse()

Asserts that a file with the given name was sent in the response

public assertFileResponse(string $expected[, string $message = '' ]) : void
Parameters
$expected : string

The absolute file path that should be sent in the response.

$message : string = ''

The failure message that will be appended to the generated message.

assertFlashElement()

Asserts a flash element was set

public assertFlashElement(string $expected[, string $key = 'flash' ][, string $message = '' ]) : void
Parameters
$expected : string

Expected element name

$key : string = 'flash'

Flash key

$message : string = ''

Assertion failure message

assertFlashElementAt()

Asserts a flash element was set at a certain index

public assertFlashElementAt(int $at, string $expected[, string $key = 'flash' ][, string $message = '' ]) : void
Parameters
$at : int

Flash index

$expected : string

Expected element name

$key : string = 'flash'

Flash key

$message : string = ''

Assertion failure message

assertFlashMessage()

Asserts a flash message was set

public assertFlashMessage(string $expected[, string $key = 'flash' ][, string $message = '' ]) : void
Parameters
$expected : string

Expected message

$key : string = 'flash'

Flash key

$message : string = ''

Assertion failure message

assertFlashMessageAt()

Asserts a flash message was set at a certain index

public assertFlashMessageAt(int $at, string $expected[, string $key = 'flash' ][, string $message = '' ]) : void
Parameters
$at : int

Flash index

$expected : string

Expected message

$key : string = 'flash'

Flash key

$message : string = ''

Assertion failure message

assertHeader()

Asserts response headers

public assertHeader(string $header, string $content[, string $message = '' ]) : void
Parameters
$header : string

The header to check

$content : string

The content to check for.

$message : string = ''

The failure message that will be appended to the generated message.

assertHeaderContains()

Asserts response header contains a string

public assertHeaderContains(string $header, string $content[, string $message = '' ]) : void
Parameters
$header : string

The header to check

$content : string

The content to check for.

$message : string = ''

The failure message that will be appended to the generated message.

assertHeaderNotContains()

Asserts response header does not contain a string

public assertHeaderNotContains(string $header, string $content[, string $message = '' ]) : void
Parameters
$header : string

The header to check

$content : string

The content to check for.

$message : string = ''

The failure message that will be appended to the generated message.

assertHtml()

Asserts HTML tags.

public assertHtml(array<string|int, mixed> $expected, string $string[, bool $fullDebug = false ]) : bool

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
$expected : array<string|int, mixed>

An array, see above

$string : string

An HTML/XHTML/XML string

$fullDebug : bool = false

Whether more verbose output should be used.

Return values
bool

assertLayout()

Asserts that the search string was in the layout name.

public assertLayout(string $content[, string $message = '' ]) : void
Parameters
$content : string

The content to check for.

$message : string = ''

The failure message that will be appended to the generated message.

assertMatchesRegularExpression()

Asserts that a string matches a given regular expression.

public static assertMatchesRegularExpression(string $pattern, string $string[, string $message = '' ]) : void
Parameters
$pattern : string

Regex pattern

$string : string

String to test

$message : string = ''

Message

Tags
throws
InvalidArgumentException
codeCoverageIgnore

assertNoRedirect()

Asserts that the Location header is not set.

public assertNoRedirect([string $message = '' ]) : void
Parameters
$message : string = ''

The failure message that will be appended to the generated message.

assertRedirect()

Asserts that the Location header is correct. Comparison is made against a full URL.

public assertRedirect([array<string|int, mixed>|string|null $url = null ][, string $message = '' ]) : void
Parameters
$url : array<string|int, mixed>|string|null = 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 : string = ''

The failure message that will be appended to the generated message.

assertRedirectContains()

Asserts that the Location header contains a substring

public assertRedirectContains(string $url[, string $message = '' ]) : void
Parameters
$url : string

The URL you expected the client to go to.

$message : string = ''

The failure message that will be appended to the generated message.

assertRedirectEquals()

Asserts that the Location header is correct. Comparison is made against exactly the URL provided.

public assertRedirectEquals([array<string|int, mixed>|string|null $url = null ][, string $message = '' ]) : void
Parameters
$url : array<string|int, mixed>|string|null = 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 : string = ''

The failure message that will be appended to the generated message.

assertRedirectNotContains()

Asserts that the Location header does not contain a substring

public assertRedirectNotContains(string $url[, string $message = '' ]) : void
Parameters
$url : string

The URL you expected the client to go to.

$message : string = ''

The failure message that will be appended to the generated message.

assertRegExpSql()

Assertion for comparing a regex pattern against a query having its identifiers quoted. It accepts queries quoted with the characters `<` and `>`. If the third parameter is set to true, it will alter the pattern to both accept quoted and unquoted queries

public assertRegExpSql(string $pattern, string $actual[, bool $optional = false ]) : void
Parameters
$pattern : string

The expected sql pattern

$actual : string

The sql to compare

$optional : bool = false

Whether quote characters (marked with <>) are optional

assertResponseCode()

Asserts a specific response status code.

public assertResponseCode(int $code[, string $message = '' ]) : void
Parameters
$code : int

Status code to assert.

$message : string = ''

Custom message for failure.

assertResponseContains()

Asserts content exists in the response body.

public assertResponseContains(string $content[, string $message = '' ][, bool $ignoreCase = false ]) : void
Parameters
$content : string

The content to check for.

$message : string = ''

The failure message that will be appended to the generated message.

$ignoreCase : bool = false

A flag to check whether we should ignore case or not.

assertResponseEmpty()

Assert response content is empty.

public assertResponseEmpty([string $message = '' ]) : void
Parameters
$message : string = ''

The failure message that will be appended to the generated message.

assertResponseEquals()

Asserts content in the response body equals.

public assertResponseEquals(mixed $content[, string $message = '' ]) : void
Parameters
$content : mixed

The content to check for.

$message : string = ''

The failure message that will be appended to the generated message.

assertResponseError()

Asserts that the response status code is in the 4xx range.

public assertResponseError([string $message = '' ]) : void
Parameters
$message : string = ''

Custom message for failure.

assertResponseFailure()

Asserts that the response status code is in the 5xx range.

public assertResponseFailure([string $message = '' ]) : void
Parameters
$message : string = ''

Custom message for failure.

assertResponseNotContains()

Asserts content does not exist in the response body.

public assertResponseNotContains(string $content[, string $message = '' ][, bool $ignoreCase = false ]) : void
Parameters
$content : string

The content to check for.

$message : string = ''

The failure message that will be appended to the generated message.

$ignoreCase : bool = false

A flag to check whether we should ignore case or not.

assertResponseNotEmpty()

Assert response content is not empty.

public assertResponseNotEmpty([string $message = '' ]) : void
Parameters
$message : string = ''

The failure message that will be appended to the generated message.

assertResponseNotEquals()

Asserts content in the response body not equals.

public assertResponseNotEquals(mixed $content[, string $message = '' ]) : void
Parameters
$content : mixed

The content to check for.

$message : string = ''

The failure message that will be appended to the generated message.

assertResponseNotRegExp()

Asserts that the response body does not match a given regular expression.

public assertResponseNotRegExp(string $pattern[, string $message = '' ]) : void
Parameters
$pattern : string

The pattern to compare against.

$message : string = ''

The failure message that will be appended to the generated message.

assertResponseOk()

Asserts that the response status code is in the 2xx range.

public assertResponseOk([string $message = '' ]) : void
Parameters
$message : string = ''

Custom message for failure.

assertResponseRegExp()

Asserts that the response body matches a given regular expression.

public assertResponseRegExp(string $pattern[, string $message = '' ]) : void
Parameters
$pattern : string

The pattern to compare against.

$message : string = ''

The failure message that will be appended to the generated message.

assertResponseSuccess()

Asserts that the response status code is in the 2xx/3xx range.

public assertResponseSuccess([string $message = '' ]) : void
Parameters
$message : string = ''

Custom message for failure.

assertSession()

Asserts session contents

public assertSession(mixed $expected, string $path[, string $message = '' ]) : void
Parameters
$expected : mixed

The expected contents.

$path : string

The session data path. Uses Hash::get() compatible notation

$message : string = ''

The failure message that will be appended to the generated message.

assertSessionHasKey()

Asserts session key exists.

public assertSessionHasKey(string $path[, string $message = '' ]) : void
Parameters
$path : string

The session data path. Uses Hash::get() compatible notation.

$message : string = ''

The failure message that will be appended to the generated message.

assertSessionNotHasKey()

Asserts a session key does not exist.

public assertSessionNotHasKey(string $path[, string $message = '' ]) : void
Parameters
$path : string

The session data path. Uses Hash::get() compatible notation.

$message : string = ''

The failure message that will be appended to the generated message.

assertTemplate()

Asserts that the search string was in the template name.

public assertTemplate(string $content[, string $message = '' ]) : void
Parameters
$content : string

The content to check for.

$message : string = ''

The failure message that will be appended to the generated message.

assertTextContains()

Assert that a string contains another string, ignoring differences in newlines.

public assertTextContains(string $needle, string $haystack[, string $message = '' ][, bool $ignoreCase = false ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$needle : string

The string to search for.

$haystack : string

The string to search through.

$message : string = ''

The message to display on failure.

$ignoreCase : bool = false

Whether the search should be case-sensitive.

assertTextEndsNotWith()

Asserts that a string ends not with a given prefix, ignoring differences in newlines.

public assertTextEndsNotWith(string $suffix, string $string[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$suffix : string

The suffix to not find.

$string : string

The string to search.

$message : string = ''

The message to use for failure.

assertTextEndsWith()

Asserts that a string ends with a given prefix, ignoring differences in newlines.

public assertTextEndsWith(string $suffix, string $string[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$suffix : string

The suffix to find.

$string : string

The string to search.

$message : string = ''

The message to use for failure.

assertTextEquals()

Assert text equality, ignoring differences in newlines.

public assertTextEquals(string $expected, string $result[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$expected : string

The expected value.

$result : string

The actual value.

$message : string = ''

The message to use for failure.

assertTextNotContains()

Assert that a text doesn't contain another text, ignoring differences in newlines.

public assertTextNotContains(string $needle, string $haystack[, string $message = '' ][, bool $ignoreCase = false ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$needle : string

The string to search for.

$haystack : string

The string to search through.

$message : string = ''

The message to display on failure.

$ignoreCase : bool = false

Whether the search should be case-sensitive.

assertTextNotEquals()

Assert text equality, ignoring differences in newlines.

public assertTextNotEquals(string $expected, string $result[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$expected : string

The expected value.

$result : string

The actual value.

$message : string = ''

The message to use for failure.

assertTextStartsNotWith()

Asserts that a string starts not with a given prefix, ignoring differences in newlines.

public assertTextStartsNotWith(string $prefix, string $string[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$prefix : string

The prefix to not find.

$string : string

The string to search.

$message : string = ''

The message to use for failure.

assertTextStartsWith()

Asserts that a string starts with a given prefix, ignoring differences in newlines.

public assertTextStartsWith(string $prefix, string $string[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$prefix : string

The prefix to check for.

$string : string

The string to search in.

$message : string = ''

The message to use for failure.

cleanup()

Clears the state used for requests.

public cleanup() : void
Tags
after
psalm-suppress

PossiblyNullPropertyAssignmentValue

clearPlugins()

Clear all plugins from the global plugin collection.

public clearPlugins() : void

Useful in test case teardown methods.

configRequest()

Configures the data for the *next* request.

public configRequest(array<string|int, mixed> $data) : void

This data is cleared in the tearDown() method.

You can call this method multiple times to append into the current state. Sub-keys like 'headers' will be reset, though.

Parameters
$data : array<string|int, mixed>

The request data to use.

Sets a request cookie for future requests.

public cookie(string $name, mixed $value) : void

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
$name : string

The cookie name to use.

$value : mixed

The value of the cookie.

cookieEncrypted()

Sets a encrypted request cookie for future requests.

public cookieEncrypted(string $name, mixed $value[, string|false $encrypt = 'aes' ][, string|null $key = null ]) : void

The difference from cookie() is this encrypts the cookie value like the CookieComponent.

Parameters
$name : string

The cookie name to use.

$value : mixed

The value of the cookie.

$encrypt : string|false = 'aes'

Encryption mode to use.

$key : string|null = null

Encryption key used. Defaults to Security.salt.

Tags
see
CookieCryptTrait::_encrypt()

delete()

Performs a DELETE request using the current request data.

public delete(array<string|int, mixed>|string $url) : void

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters
$url : array<string|int, mixed>|string

The URL to request.

deprecated()

Helper method for check deprecation methods

public deprecated(callable $callable) : void
Parameters
$callable : callable

callable function that will receive asserts

disableErrorHandlerMiddleware()

Disable the error handler middleware.

public disableErrorHandlerMiddleware() : void

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.

public enableCsrfToken([string $cookieName = 'csrfToken' ]) : void

Both the POST data and cookie will be populated when this option is enabled. The default parameter names will be used.

Parameters
$cookieName : string = 'csrfToken'

The name of the csrf token cookie.

enableRetainFlashMessages()

Calling this method will re-store flash messages into the test session after being removed by the FlashHelper

public enableRetainFlashMessages() : void

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.

public enableSecurityToken() : void

fetchTable()

Convenience method to get a table instance.

public fetchTable([string|null $alias = null ][, array<string, mixed> $options = [] ]) : Table
Parameters
$alias : string|null = null

The alias name you want to get. Should be in CamelCase format. If null then the value of $defaultTable property is used.

$options : array<string, mixed> = []

The options you want to build the table with. If a table has already been loaded the registry options will be ignored.

Tags
throws
CakeException

If $alias argument and $defaultTable property both are null.

see
TableLocator::get()
since
4.3.0
Return values
Table

get()

Performs a GET request using the current request data.

public get(array<string|int, mixed>|string $url) : void

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters
$url : array<string|int, mixed>|string

The URL to request.

getFixtures()

Get the fixtures this test should use.

public getFixtures() : array<string|int, string>
Return values
array<string|int, string>

getMockForModel()

Mock a model, maintain fixtures and table association

public getMockForModel(string $alias[, array<string|int, string> $methods = [] ][, array<string, mixed> $options = [] ]) : Table|MockObject
Parameters
$alias : string

The model to get a mock for.

$methods : array<string|int, string> = []

The list of methods to mock

$options : array<string, mixed> = []

The config data for the mock's constructor.

Tags
throws
MissingTableClassException
Return values
Table|MockObject

head()

Performs a HEAD request using the current request data.

public head(array<string|int, mixed>|string $url) : void

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters
$url : array<string|int, mixed>|string

The URL to request.

loadFixtures()

Chooses which fixtures to load for a given test

public loadFixtures() : void

Disabling auto-fixtures is deprecated and only available using FixtureInjector fixture system.

Each parameter is a model name that corresponds to a fixture, i.e. 'Posts', 'Authors', etc. Passing no parameters will cause all fixtures on the test case to load.

Tags
see
TestCase::$autoFixtures
throws
RuntimeException

when no fixture manager is available.

loadPlugins()

Load plugins into a simulated application.

public loadPlugins([array<string, mixed> $plugins = [] ]) : BaseApplication

Useful to test how plugins being loaded/not loaded interact with other elements in CakePHP or applications.

Parameters
$plugins : array<string, mixed> = []

List of Plugins to load.

Return values
BaseApplication

loadRoutes()

Load routes for the application.

public loadRoutes([array<string|int, mixed>|null $appArgs = null ]) : void

If no application class can be found an exception will be raised. Routes for plugins will not be loaded. Use loadPlugins() or use Cake\TestSuite\IntegrationTestCaseTrait to better simulate all routes and plugins being loaded.

Parameters
$appArgs : array<string|int, mixed>|null = null

Constructor parameters for the application class.

Tags
since
4.0.1

options()

Performs an OPTIONS request using the current request data.

public options(array<string|int, mixed>|string $url) : void

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters
$url : array<string|int, mixed>|string

The URL to request.

patch()

Performs a PATCH request using the current request data.

public patch(array<string|int, mixed>|string $url[, array<string|int, mixed>|string $data = [] ]) : void

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters
$url : array<string|int, mixed>|string

The URL to request.

$data : array<string|int, mixed>|string = []

The data for the request.

post()

Performs a POST request using the current request data.

public post(array<string|int, mixed>|string $url[, array<string|int, mixed>|string $data = [] ]) : void

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters
$url : array<string|int, mixed>|string

The URL to request.

$data : array<string|int, mixed>|string = []

The data for the request.

put()

Performs a PUT request using the current request data.

public put(array<string|int, mixed>|string $url[, array<string|int, mixed>|string $data = [] ]) : void

The response of the dispatched request will be stored as a property. You can use various assert methods to check the response.

Parameters
$url : array<string|int, mixed>|string

The URL to request.

$data : array<string|int, mixed>|string = []

The data for the request.

removePlugins()

Remove plugins from the global plugin collection.

public removePlugins([array<string|int, string> $names = [] ]) : void

Useful in test case teardown methods.

Parameters
$names : array<string|int, string> = []

A list of plugins you want to remove.

session()

Sets session data.

public session(array<string|int, mixed> $data) : void

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
$data : array<string|int, mixed>

The session data to use.

setAppNamespace()

Set the app namespace

public static setAppNamespace([string $appNamespace = 'TestApp' ]) : string|null
Parameters
$appNamespace : string = 'TestApp'

The app namespace, defaults to "TestApp".

Return values
string|null

The previous app namespace or null if not set.

setUnlockedFields()

Set list of fields that are excluded from field validation.

public setUnlockedFields([array<string|int, string> $unlockedFields = [] ]) : void
Parameters
$unlockedFields : array<string|int, string> = []

List of fields that are excluded from field validation.

skipIf()

Overrides SimpleTestCase::skipIf to provide a boolean return value

public skipIf(bool $shouldSkip[, string $message = '' ]) : bool
Parameters
$shouldSkip : bool

Whether the test should be skipped.

$message : string = ''

The message to display.

Return values
bool

viewVariable()

Fetches a view variable by name.

public viewVariable(string $name) : mixed

If the view variable does not exist, null will be returned.

Parameters
$name : string

The view variable to get.

Return values
mixed

The view variable if set.

withErrorReporting()

Helper method for tests that needs to use error_reporting()

public withErrorReporting(int $errorLevel, callable $callable) : void
Parameters
$errorLevel : int

value of error_reporting() that needs to use

$callable : callable

callable function that will receive asserts

_addTokens()

Add the CSRF and Security Component tokens if necessary.

protected _addTokens(string $url, array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
$url : string

The URL the form is being submitted on.

$data : array<string|int, mixed>

The request body data.

Return values
array<string|int, mixed>

The request body with tokens added.

_assertAttributes()

Check the attributes as part of an assertTags() check.

protected _assertAttributes(array<string, mixed> $assertions, string $string[, bool $fullDebug = false ][, array<string|int, mixed>|string $regex = '' ]) : string|false
Parameters
$assertions : array<string, mixed>

Assertions to run.

$string : string

The HTML string to check.

$fullDebug : bool = false

Whether more verbose output should be used.

$regex : array<string|int, mixed>|string = ''

Full regexp from assertHtml

Return values
string|false

_buildRequest()

Creates a request object with the configured options and parameters.

protected _buildRequest(string $url, string $method[, array<string|int, mixed>|string $data = [] ]) : array<string|int, mixed>
Parameters
$url : string

The URL

$method : string

The HTTP method

$data : array<string|int, mixed>|string = []

The request data.

Return values
array<string|int, mixed>

The request context

_castToString()

Recursively casts all data to string as that is how data would be POSTed in the real world

protected _castToString(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
$data : array<string|int, mixed>

POST data

Return values
array<string|int, mixed>

_getBodyAsString()

Get the response body as string

protected _getBodyAsString() : string
Return values
string

The response body.

_getCookieEncryptionKey()

Returns the encryption key to be used.

protected _getCookieEncryptionKey() : string
Return values
string

_getTableClassName()

Gets the class name for the table.

protected _getTableClassName(string $alias, array<string, mixed> $options) : string
Parameters
$alias : string

The model to get a mock for.

$options : array<string, mixed>

The config data for the mock's constructor.

Tags
throws
MissingTableClassException
psalm-return

class-string<\Cake\ORM\Table>

Return values
string

_handleError()

Attempts to render an error response for a given exception.

protected _handleError(Throwable $exception) : void

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 : Throwable

Exception to handle.

_makeDispatcher()

Get the correct dispatcher instance.

protected _makeDispatcher() : MiddlewareDispatcher
Return values
MiddlewareDispatcher

A dispatcher instance

_normalizePath()

Normalize a path for comparison.

protected _normalizePath(string $path) : string
Parameters
$path : string

Path separated by "/" slash.

Return values
string

Normalized path separated by DIRECTORY_SEPARATOR.

_sendRequest()

Creates and send the request into a Dispatcher instance.

protected _sendRequest(array<string|int, mixed>|string $url, string $method[, array<string|int, mixed>|string $data = [] ]) : void

Receives and stores the response for future inspection.

Parameters
$url : array<string|int, mixed>|string

The URL

$method : string

The HTTP method

$data : array<string|int, mixed>|string = []

The request data.

Tags
throws
Exception|Throwable

_url()

Creates a valid request url and parameter array more like Request::_url()

protected _url(string $url) : array<string|int, mixed>
Parameters
$url : string

The URL

Return values
array<string|int, mixed>

Qualified URL, the query parameters, and host data

addFixture()

Adds a fixture to this test case.

protected addFixture(string $fixture) : $this

Examples:

  • core.Tags
  • app.MyRecords
  • plugin.MyPluginName.MyModelName

Use this method inside your test cases' method to build up the fixture list.

Parameters
$fixture : string

Fixture

Return values
$this

assertNotWithinRange()

Compatibility function to test if a value is not between an acceptable range.

protected static assertNotWithinRange(float $expected, float $result, float $margin[, string $message = '' ]) : void
Parameters
$expected : float
$result : float
$margin : float

the rage of acceptation

$message : string = ''

the text to display if the assertion is not correct

assertPathEquals()

Compatibility function to test paths.

protected static assertPathEquals(string $expected, string $result[, string $message = '' ]) : void
Parameters
$expected : string
$result : string
$message : string = ''

the text to display if the assertion is not correct

assertWithinRange()

Compatibility function to test if a value is between an acceptable range.

protected static assertWithinRange(float $expected, float $result, float $margin[, string $message = '' ]) : void
Parameters
$expected : float
$result : float
$margin : float

the rage of acceptation

$message : string = ''

the text to display if the assertion is not correct

extractExceptionMessage()

Extract verbose message for existing exception

protected extractExceptionMessage(Exception $exception) : string
Parameters
$exception : Exception

Exception to extract

Return values
string

extractVerboseMessage()

Inspect controller to extract possible causes of the failed assertion

protected extractVerboseMessage(string $message) : string
Parameters
$message : string

Original message to use as a base

Return values
string

setUp()

Setup the test case, backup the static object values so they can be restored.

protected setUp() : void

Specifically backs up the contents of Configure and paths in App if they have not already been backed up.

setupFixtures()

Initialized and loads any use fixtures.

protected setupFixtures() : void

skipUnless()

Compatibility function for skipping.

protected skipUnless(bool $condition[, string $message = '' ]) : bool
Parameters
$condition : bool

Condition to trigger skipping

$message : string = ''

Message for skip

Return values
bool

tearDown()

teardown any static object changes and restore them.

protected tearDown() : void

teardownFixtures()

Unloads any use fixtures.

protected teardownFixtures() : void

        
On this page

Search results