Class ControllerTestCase
ControllerTestCase class
- PHPUnit_Framework_TestCase
- CakeTestCase
- ControllerTestCase
Package: Cake\TestSuite
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/TestSuite/ControllerTestCase.php
Properties summary
-
$_dirtyController
protectedboolean
Flag for checking if the controller instance is dirty. Once a test has been run on a controller it should be rebuilt to clean up properties.
-
$_responseClass
protectedstring
The class name to use for mocking the response object. -
$autoMock
publicboolean
Automatically mock controllers that aren't mocked -
$contents
publicstring
The resulting rendered layout+view of the last testAction call -
$controller
publicThe controller to test in testAction -
$headers
publicstring
The headers that would have been sent by the action -
$loadRoutes
publicboolean
Use custom routes during tests -
$result
publicstring
The returned result of the dispatch (requestAction), if any -
$vars
publicarray
The resulting view vars of the last testAction call -
$view
publicstring
The resulting rendered view of the last testAction call
Inherited Properties
Method Summary
-
__call() public
Used to enable calling ControllerTestCase::testAction() without the testing framework thinking that it's a test case
-
_createDispatcher() protected
Creates the test dispatcher class -
_testAction() protected
Lets you do functional tests of a controller action. -
generate() public
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. -
tearDown() public
Unsets some properties to free memory.
Method Detail
__call() 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
The return of _testAction.
Throws
when you call methods that don't exist.
_testAction() protected ¶
_testAction( string|array $url , array $options = array() )
Lets you do functional tests of a controller action.
Options:
data
Will be used as the request data. If themethod
is GET, data will be used a GET params. If themethod
is 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.method
POST or GET. Defaults to POST.return
Specify the return type you want. Choose from:vars
Get the set view variables.view
Get the rendered view, without a layout.contents
Get the rendered view including the layout.result
Get the return value of the controller action. Useful for testing requestAction methods.
Parameters
- string|array $url
- The URL to test.
- array $options optional array()
- See options
Returns
The specified return type.
Triggers
generate() 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:
methods
Methods to mock on the controller._stop()
is mocked by defaultmodels
Models 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. Iftrue
or no value is passed, the entire model will be mocked.components
Components 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 $mocks optional array()
- List of classes and methods to mock
Returns
Throws
MissingControllerException
When controllers could not be created.
MissingComponentException
When components could not be created.
Methods inherited from CakeTestCase
_assertAttributes() 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.
_buildMock() protected deprecated ¶
_buildMock( string $originalClassName , array $methods = array() , array $arguments = array() , string $mockClassName = '' , boolean $callOriginalConstructor = true , boolean $callOriginalClone = true , boolean $callAutoload = true )
Returns a mock object for the specified class.
Deprecated
getMockBuilder()
or createMock()
in new unit tests.Parameters
- string $originalClassName
- The class name of the object to be mocked.
- array $methods optional array()
By default, all methods of the given class are replaced with a test double that just returns NULL unless a return value is configured using will($this->returnValue()), for instance. When the second (optional) parameter is provided, only the methods whose names are in the array are replaced with a configurable test double. The behavior of the other methods is not changed. Providing NULL as the parameter means that no methods will be replaced.
- array $arguments optional array()
The third (optional) parameter may hold a parameter array that is passed to the original class' constructor (which is not replaced with a dummy implementation by default).
- string $mockClassName optional ''
The fourth (optional) parameter can be used to specify a class name for the generated test double class.
- boolean $callOriginalConstructor optional true
The fifth (optional) parameter can be used to disable the call to the original class' constructor.
- boolean $callOriginalClone optional true
The sixth (optional) parameter can be used to disable the call to the original class' clone constructor.
- boolean $callAutoload optional true
The seventh (optional) parameter can be used to disable __autoload() during the generation of the test double class.
Returns
See
assertEqual() protected static deprecated ¶
assertEqual( mixed $result , mixed $expected , string $message = '' )
Compatibility wrapper function for assertEquals
Deprecated
Parameters
- mixed $result
- mixed $expected
- string $message optional ''
- the text to display if the assertion is not correct
assertIdentical() protected static deprecated ¶
assertIdentical( mixed $actual , mixed $expected , string $message = '' )
Compatibility wrapper function for assertEquals
Deprecated
Parameters
- mixed $actual
- mixed $expected
- string $message optional ''
- the text to display if the assertion is not correct
assertIsA() protected static deprecated ¶
assertIsA( string $object , string $type , string $message = '' )
Compatibility wrapper for assertIsA
Deprecated
Parameters
- string $object
- string $type
- string $message optional ''
assertNoErrors() protected deprecated ¶
assertNoErrors( )
assert no errors
Deprecated
assertNoPattern() protected static deprecated ¶
assertNoPattern( mixed $pattern , string $string , string $message = '' )
Compatibility wrapper function for assertNotRegExp
Deprecated
Parameters
- mixed $pattern
- a regular expression
- string $string
- the text to be matched
- string $message optional ''
- the text to display if the assertion is not correct
assertNotEqual() protected static deprecated ¶
assertNotEqual( mixed $result , mixed $expected , string $message = '' )
Compatibility wrapper function for assertNotEquals
Deprecated
Parameters
- mixed $result
- mixed $expected
- string $message optional ''
- the text to display if the assertion is not correct
assertNotIdentical() protected static deprecated ¶
assertNotIdentical( mixed $actual , mixed $expected , string $message = '' )
Compatibility wrapper function for assertNotEquals
Deprecated
Parameters
- mixed $actual
- mixed $expected
- string $message optional ''
- the text to display if the assertion is not correct
assertPattern() protected static deprecated ¶
assertPattern( mixed $pattern , string $string , string $message = '' )
Compatibility wrapper function for assertRegexp
Deprecated
Parameters
- mixed $pattern
- a regular expression
- string $string
- the text to be matched
- string $message optional ''
- the text to display if the assertion is not correct
assertReference() protected static deprecated ¶
assertReference( mixed $first , mixed $second , string $message = '' )
Compatibility wrapper function for assertSame
Deprecated
Parameters
- mixed $first
- mixed $second
- string $message optional ''
- the text to display if the assertion is not correct
assertTags() 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 $fullDebug optional false
- Whether or not more verbose output should be used.
Returns
assertTextContains() public ¶
assertTextContains( string $needle , string $haystack , string $message = '' , boolean $ignoreCase = false )
Assert that a string contains another string, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
Parameters
- string $needle
- The string to search for.
- string $haystack
- The string to search through.
- string $message optional ''
- The message to display on failure.
- boolean $ignoreCase optional false
- Whether or not the search should be case-sensitive.
Returns
assertTextEndsNotWith() public ¶
assertTextEndsNotWith( string $suffix , string $string , string $message = '' )
Asserts that a string ends not with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
Parameters
- string $suffix
- The suffix to not find.
- string $string
- The string to search.
- string $message optional ''
- The message to use for failure.
Returns
assertTextEndsWith() public ¶
assertTextEndsWith( string $suffix , string $string , string $message = '' )
Asserts that a string ends with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
Parameters
- string $suffix
- The suffix to find.
- string $string
- The string to search.
- string $message optional ''
- The message to use for failure.
Returns
assertTextEquals() public ¶
assertTextEquals( string $expected , string $result , string $message = '' )
Assert text equality, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
Parameters
- string $expected
- The expected value.
- string $result
- The actual value.
- string $message optional ''
- message The message to use for failure.
Returns
assertTextNotContains() public ¶
assertTextNotContains( string $needle , string $haystack , string $message = '' , boolean $ignoreCase = false )
Assert that a text doesn't contain another text, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
Parameters
- string $needle
- The string to search for.
- string $haystack
- The string to search through.
- string $message optional ''
- The message to display on failure.
- boolean $ignoreCase optional false
- Whether or not the search should be case-sensitive.
Returns
assertTextNotEquals() public ¶
assertTextNotEquals( string $expected , string $result , string $message = '' )
Assert text equality, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
Parameters
- string $expected
- The expected value.
- string $result
- The actual value.
- string $message optional ''
- The message to use for failure.
Returns
assertTextStartsNotWith() public ¶
assertTextStartsNotWith( string $prefix , string $string , string $message = '' )
Asserts that a string starts not with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
Parameters
- string $prefix
- The prefix to not find.
- string $string
- The string to search.
- string $message optional ''
- The message to use for failure.
Returns
assertTextStartsWith() public ¶
assertTextStartsWith( string $prefix , string $string , string $message = '' )
Asserts that a string starts with a given prefix, ignoring differences in newlines. Helpful for doing cross platform tests of blocks of text.
Parameters
- string $prefix
- The prefix to check for.
- string $string
- The string to search in.
- string $message optional ''
- The message to use for failure.
Returns
assertWithinMargin() 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 $message optional ''
- the text to display if the assertion is not correct
date() public static ¶
date( string $format = 'Y-m-d H:i:s' )
See CakeTestSuiteDispatcher::date()
Parameters
- string $format optional 'Y-m-d H:i:s'
- format to be used.
Returns
endTest() 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.
expectError() protected deprecated ¶
expectError( mixed $expected = false , string $message = '' )
Compatibility wrapper function for setExpectedException
Deprecated
Parameters
- mixed $expected optional false
- the name of the Exception or error
- string $message optional ''
- the text to display if the assertion is not correct
expectException() public deprecated ¶
expectException( mixed $name = 'Exception' , string $message = '' )
Compatibility wrapper function for setExpectedException
Deprecated
Parameters
- mixed $name optional 'Exception'
- The name of the expected Exception.
- string $message optional ''
- the text to display if the assertion is not correct
getMock() public deprecated ¶
getMock( string $originalClassName , array $methods = array() , array $arguments = array() , string $mockClassName = '' , boolean $callOriginalConstructor = true , boolean $callOriginalClone = true , boolean $callAutoload = true , boolean $cloneArguments = false , boolean $callOriginalMethods = false , string $proxyTarget = null )
Returns a mock object for the specified class.
Deprecated
getMockBuilder()
or createMock()
in new unit tests.Parameters
- string $originalClassName
- The class name of the object to be mocked.
- array $methods optional array()
By default, all methods of the given class are replaced with a test double that just returns NULL unless a return value is configured using will($this->returnValue()), for instance. When the second (optional) parameter is provided, only the methods whose names are in the array are replaced with a configurable test double. The behavior of the other methods is not changed. Providing NULL as the parameter means that no methods will be replaced.
- array $arguments optional array()
The third (optional) parameter may hold a parameter array that is passed to the original class' constructor (which is not replaced with a dummy implementation by default).
- string $mockClassName optional ''
The fourth (optional) parameter can be used to specify a class name for the generated test double class.
- boolean $callOriginalConstructor optional true
The fifth (optional) parameter can be used to disable the call to the original class' constructor.
- boolean $callOriginalClone optional true
The sixth (optional) parameter can be used to disable the call to the original class' clone constructor.
- boolean $callAutoload optional true
The seventh (optional) parameter can be used to disable __autoload() during the generation of the test double class.
- boolean $cloneArguments optional false
- Not supported.
- boolean $callOriginalMethods optional false
- Not supported.
- string $proxyTarget optional null
- Not supported.
Returns
Throws
When not supported parameters are set.
See
getMockForModel() 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 $methods optional array()
- The list of methods to mock
- array $config optional array()
- The config data for the mock's constructor.
Returns
Throws
loadFixtures() 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
when no fixture manager is available.
See
run() 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 $result optional null
- The test result object
Returns
Throws
setUp() 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.
skipIf() public ¶
skipIf( boolean $shouldSkip , string $message = '' )
Overrides SimpleTestCase::skipIf to provide a boolean return value
Parameters
- boolean $shouldSkip
- Whether or not the test should be skipped.
- string $message optional ''
- The message to display.
Returns
skipUnless() protected ¶
skipUnless( boolean $condition , string $message = '' )
Compatibility function for skipping.
Parameters
- boolean $condition
- Condition to trigger skipping
- string $message optional ''
- Message for skip
Returns
startTest() 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.
Magic methods summary
Properties detail
$_dirtyController ¶
Flag for checking if the controller instance is dirty. Once a test has been run on a controller it should be rebuilt to clean up properties.
false
$_responseClass ¶
The class name to use for mocking the response object.
'CakeResponse'