Class TestCase
Cake TestCase class
Property Summary
-
$_configure protected
array
Configure values to restore at end of test.
-
$_pathRestore protected
array
Path settings to restore at the end of the test.
-
$autoFixtures 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
-
$dropTables public
bool
Control table create/drops on each test method.
-
$fixtureManager public
Cake\TestSuite\Fixture\FixtureManager
The class responsible for managing the creation, loading and removing of fixtures
Method Summary
-
_assertAttributes() protected
Check the attributes as part of an assertTags() check.
-
_normalizePath() protected
Normalize a path for comparison.
-
assertHtml() public
Asserts HTML tags.
-
assertNotWithinRange() protected static
Compatibility function to test if a value is not between an acceptable range.
-
assertPathEquals() protected static
Compatibility function to test paths.
-
assertTags() public deprecated
Asserts HTML tags.
-
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.
-
getMockForModel() public
Mock a model, maintain fixtures and table association
-
loadFixtures() public
Chooses which fixtures to load for a given test
-
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
teardown any static object changes and restore them.
Method Detail
_assertAttributes() ¶ protected
_assertAttributes(array $assertions, string $string, bool $fullDebug = false, array $regex = ''): string
Check the attributes as part of an assertTags() check.
Parameters
-
array
$assertions Assertions to run.
-
string
$string The HTML string to check.
-
bool
$fullDebug optional Whether or not more verbose output should be used.
-
array
$regex optional Full regexp from
assertHtml
Returns
string
_normalizePath() ¶ protected
_normalizePath(string $path): string
Normalize a path for comparison.
Parameters
-
string
$path Path separated by "/" slash.
Returns
string
assertHtml() ¶ 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
bool
assertNotWithinRange() ¶ 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
void
assertPathEquals() ¶ 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
void
assertTags() ¶ 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
void
assertTextContains() ¶ 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
void
assertTextEndsNotWith() ¶ 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
void
assertTextEndsWith() ¶ 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
void
assertTextEquals() ¶ 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
void
assertTextNotContains() ¶ 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
void
assertTextNotEquals() ¶ 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
void
assertTextStartsNotWith() ¶ 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
void
assertTextStartsWith() ¶ 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
void
assertWithinRange() ¶ 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
void
getMockForModel() ¶ public
getMockForModel(string $alias, mixed $methods = [], array $options = []): Cake\ORM\Table|PHPUnit_Framework_MockObject_MockObject
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
Cake\ORM\Table|PHPUnit_Framework_MockObject_MockObject
Throws
Cake\ORM\Exception\MissingTableClassException
loadFixtures() ¶ 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
void
Throws
Exception
when no fixture manager is available.
See Also
setUp() ¶ 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
void
skipIf() ¶ 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
bool
skipUnless() ¶ 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
bool
tearDown() ¶ public
tearDown(): void
teardown any static object changes and restore them.
Returns
void
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