Class ControllerTestCase
ControllerTestCase class
- PHPUnit_Framework_TestCase
-
CakeTestCase -
ControllerTestCase
Package: Cake\TestSuite
Copyright: Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
License: MIT License
Located at Cake/TestSuite/ControllerTestCase.php
Method Detail
__callsource public
__call( string $name , array $arguments )
Used to enable calling ControllerTestCase::testAction() without the testing framework thinking that it's a test case
Parameters
- string
$name - The name of the function
- array
$arguments - Array of arguments
Returns
mixedThe return of _testAction.
Throws
BadMethodCallExceptionwhen you call methods that don't exist.
_testActionsource protected
_testAction( string|array $url , array $options array() )
Lets you do functional tests of a controller action.
Options:
-
dataWill be used as the request data. If themethodis GET, data will be used a GET params. If themethodis POST, it will be used as POST data. By setting$options['data']to a string, you can simulate XML or JSON payloads to your controllers allowing you to test REST webservices. -
methodPOST or GET. Defaults to POST. -
returnSpecify the return type you want. Choose from: -varsGet the set view variables. -viewGet the rendered view, without a layout. -contentsGet the rendered view including the layout. -resultGet the return value of the controller action. Useful for testing requestAction methods.
Parameters
- string|array
$url - The URL to test.
- array
$optionsoptional array() - See options
Returns
mixedThe specified return type.
Triggers
ControllerTestCase $Dispatch, array('request' => $request)generatesource public
generate( string $controller , array $mocks array() )
Generates a mocked controller and mocks any classes passed to $mocks. By default, _stop() is stubbed as is sending the response headers, so to not interfere with testing.
Mocks:
-
methodsMethods to mock on the controller._stop()is mocked by default -
modelsModels to mock. Models are added to the ClassRegistry so any time they are instantiated the mock will be created. Pass as key value pairs with the value being specific methods on the model to mock. Iftrueor no value is passed, the entire model will be mocked. -
componentsComponents to mock. Components are only mocked on this controller and not within each other (i.e., components on components)
Parameters
- string
$controller - Controller name
- array
$mocksoptional array() - List of classes and methods to mock
Returns
ControllerMocked controller
Throws
MissingControllerExceptionWhen controllers could not be created.
MissingComponentExceptionWhen components could not be created.
Methods inherited from CakeTestCase
_assertAttributessource protected
_assertAttributes( array $assertions , string $string )
Check the attributes as part of an assertTags() check.
Parameters
- array
$assertions - Assertions to run.
- string
$string - The HTML string to check.
assertEqualsource protected static
assertEqual( mixed $result , mixed $expected , string $message '' )
Compatibility wrapper function for assertEquals
Deprecated
3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0Parameters
- mixed
$result - mixed
$expected - string
$messageoptional '' - the text to display if the assertion is not correct
assertIdenticalsource protected static
assertIdentical( mixed $actual , mixed $expected , string $message '' )
Compatibility wrapper function for assertEquals
Deprecated
3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0Parameters
- mixed
$actual - mixed
$expected - string
$messageoptional '' - the text to display if the assertion is not correct
assertIsAsource protected static
assertIsA( string $object , string $type , string $message '' )
Compatibility wrapper for assertIsA
Deprecated
3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0Parameters
- string
$object - string
$type - string
$messageoptional ''
assertNoErrorssource protected
assertNoErrors( )
assert no errors
Deprecated
3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0assertNoPatternsource protected static
assertNoPattern( mixed $pattern , string $string , string $message '' )
Compatibility wrapper function for assertNotRegExp
Deprecated
3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0Parameters
- mixed
$pattern - a regular expression
- string
$string - the text to be matched
- string
$messageoptional '' - the text to display if the assertion is not correct
assertNotEqualsource protected static
assertNotEqual( mixed $result , mixed $expected , string $message '' )
Compatibility wrapper function for assertNotEquals
Deprecated
3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0Parameters
- mixed
$result - mixed
$expected - string
$messageoptional '' - the text to display if the assertion is not correct
assertNotIdenticalsource protected static
assertNotIdentical( mixed $actual , mixed $expected , string $message '' )
Compatibility wrapper function for assertNotEquals
Deprecated
3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0Parameters
- mixed
$actual - mixed
$expected - string
$messageoptional '' - the text to display if the assertion is not correct
assertPatternsource protected static
assertPattern( mixed $pattern , string $string , string $message '' )
Compatibility wrapper function for assertRegexp
Deprecated
3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0Parameters
- mixed
$pattern - a regular expression
- string
$string - the text to be matched
- string
$messageoptional '' - the text to display if the assertion is not correct
assertReferencesource protected static
assertReference( mixed $first , mixed $second , string $message '' )
Compatibility wrapper function for assertSame
Deprecated
3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0Parameters
- mixed
$first - mixed
$second - string
$messageoptional '' - the text to display if the assertion is not correct
assertTagssource public
assertTags( string $string , array $expected , string $fullDebug false )
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':
array('input' => array('name', 'id' => 'my-input'))
Checks for two p elements with some text in them:
array( array('p' => true), 'textA', '/p', array('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:
array( array('input' => array('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
- string
$string - An HTML/XHTML/XML string
- array
$expected - An array, see above
- string
$fullDebugoptional false - Whether or not more verbose output should be used.
Returns
booleanbool
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
$messageoptional '' - The message to display on failure.
- boolean
$ignoreCaseoptional false - Whether or not the search should be case-sensitive.
Returns
booleanbool
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
$messageoptional '' - The message to use for failure.
Returns
booleanbool
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
$messageoptional '' - The message to use for failure.
Returns
booleanbool
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
$messageoptional '' - message The message to use for failure.
Returns
booleanbool
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
$messageoptional '' - The message to display on failure.
- boolean
$ignoreCaseoptional false - Whether or not the search should be case-sensitive.
Returns
booleanbool
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
$messageoptional '' - The message to use for failure.
Returns
booleanbool
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
$messageoptional '' - The message to use for failure.
Returns
booleanbool
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
$messageoptional '' - The message to use for failure.
Returns
booleanbool
assertWithinMarginsource protected static
assertWithinMargin( mixed $result , mixed $expected , mixed $margin , string $message '' )
Compatibility function to test if value is between an acceptable range
Parameters
- mixed
$result - mixed
$expected - mixed
$margin - the rage of acceptation
- string
$messageoptional '' - the text to display if the assertion is not correct
datesource public static
date( string $format 'Y-m-d H:i:s' )
See CakeTestSuiteDispatcher::date()
Parameters
- string
$formatoptional 'Y-m-d H:i:s' - format to be used.
Returns
stringstring
endTestsource public
endTest( string $method )
Called when a test case method has been executed (to be overridden when needed.)
Parameters
- string
$method - Test method about that was executed.
expectErrorsource protected
expectError( mixed $expected false , string $message '' )
Compatibility wrapper function for setExpectedException
Deprecated
3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0Parameters
- mixed
$expectedoptional false - the name of the Exception or error
- string
$messageoptional '' - the text to display if the assertion is not correct
expectExceptionsource protected
expectException( mixed $name 'Exception' , string $message '' )
Compatibility wrapper function for setExpectedException
Deprecated
3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0.Parameters
- mixed
$nameoptional 'Exception' - The name of the expected Exception.
- string
$messageoptional '' - the text to display if the assertion is not correct
getMockForModelsource public
getMockForModel( string $model , mixed $methods array() , array $config array() )
Mock a model, maintain fixtures and table association
Parameters
- string
$model - The model to get a mock for.
- mixed
$methodsoptional array() - The list of methods to mock
- array
$configoptional array() - The config data for the mock's constructor.
Returns
ModelModel
Throws
MissingModelExceptionMissingModelException
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. 'Post', 'Author', etc.
Throws
Exceptionwhen no fixture manager is available.
See
CakeTestCase::$autoFixturesrunsource public
run( PHPUnit_Framework_TestResult $result null )
Runs the test case and collects the results in a TestResult object. If no TestResult object is passed a new one will be created. This method is run for each test method in this class
Parameters
- PHPUnit_Framework_TestResult
$resultoptional null - The test result object
Returns
PHPUnit_Framework_TestResultPHPUnit_Framework_TestResult
Throws
InvalidArgumentExceptionInvalidArgumentException
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
$messageoptional '' - The message to display.
Returns
booleanbool
skipUnlesssource protected
skipUnless( boolean $condition , string $message '' )
Compatibility function for skipping.
Parameters
- boolean
$condition - Condition to trigger skipping
- string
$messageoptional '' - Message for skip
Returns
booleanbool
startTestsource public
startTest( string $method )
Called when a test case method is about to start (to be overridden when needed.)
Parameters
- string
$method - Test method about to get executed.
Properties summary
Properties inherited from CakeTestCase
$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.
Set this to false to avoid tables to be dropped if they already exist between each test method. Tables will still be dropped at the end of each test runner execution.
true $fixtureManagersource
public CakeFixtureManager
The class responsible for managing the creation, loading and removing of fixtures
null
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/2.7/class-ControllerTestCase.html