CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeTestCase
  • CakeTestLoader
  • CakeTestRunner
  • CakeTestSuite
  • CakeTestSuiteCommand
  • CakeTestSuiteDispatcher
  • ControllerTestCase
  • ControllerTestDispatcher
  • InterceptContentHelper

Class ControllerTestCase

ControllerTestCase class

PHPUnit_Framework_TestCase
Extended by CakeTestCase
Extended by ControllerTestCase
Abstract
Package: Cake\TestSuite
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
License: License (http://www.opensource.org/licenses/mit-license.php)
Location: Cake/TestSuite/ControllerTestCase.php

Properties summary

  • $autoMock public
    boolean
    Automatically mock controllers that aren't mocked
  • $contents public
    string
    The resulting rendered layout+view of the last testAction call
  • $controller public
    Controller
    The controller to test in testAction
  • $headers public
    string
    The headers that would have been sent by the action
  • $loadRoutes public
    boolean
    Use custom routes during tests
  • $result public
    string
    The returned result of the dispatch (requestAction), if any
  • $vars public
    array
    The resulting view vars of the last testAction call
  • $view public
    string
    The resulting rendered view of the last testAction call

Inherited Properties

  • _configure, _pathRestore, autoFixtures, dropTables, fixtureManager

Method Summary

  • __call() public

    Used to enable calling ControllerTestCase::testAction() without the testing framework thinking that it's a test case

  • _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.

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
Function

_testAction() protected ¶

_testAction( string $url = '' , array $options = array() )

Lets you do functional tests of a controller action.

Options:

  • data Will be used as the request data. If the method is GET, data will be used a GET params. If the method 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 $url optional ''
The url to test
array $options optional array()
See options

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 default
  • models Models to mock. Models are added to the ClassRegistry so they 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. If true 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
Controller
Mocked controller
Throws
MissingControllerException
When controllers could not be created.
MissingComponentException
When components could not be created.

Methods inherited from CakeTestCase

_arrayPermute() protected ¶

_arrayPermute( array $items , $perms = array() )

Generates all permutation of an array $items and returns them in a new array.

Parameters
array $items
An array of items
$perms optional array()
Returns
array

assertEqual() protected static ¶

assertEqual( mixed $result , mixed $expected , string $message = '' )

Compatibility wrapper function for assertEquals

Parameters
mixed $result
mixed $expected
string $message optional ''
the text to display if the assertion is not correct

assertIdentical() protected static ¶

assertIdentical( mixed $actual , mixed $expected , string $message = '' )

Compatibility wrapper function for assertEquals

Parameters
mixed $actual
mixed $expected
string $message optional ''
the text to display if the assertion is not correct

assertIsA() protected static ¶

assertIsA( string $object , string $type , string $message = '' )

Compatibility wrapper for assertIsA

Parameters
string $object
string $type
string $message optional ''

assertNoErrors() protected ¶

assertNoErrors( )

assertNoPattern() protected static ¶

assertNoPattern( mixed $pattern , string $string , string $message = '' )

Compatibility wrapper function for assertNotRegExp

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 ¶

assertNotEqual( mixed $result , mixed $expected , string $message = '' )

Compatibility wrapper function for assertNotEquals

Parameters
mixed $result
mixed $expected
string $message optional ''
the text to display if the assertion is not correct

assertNotIdentical() protected static ¶

assertNotIdentical( mixed $actual , mixed $expected , string $message = '' )

Compatibility wrapper function for assertNotEquals

Parameters
mixed $actual
mixed $expected
string $message optional ''
the text to display if the assertion is not correct

assertPattern() protected static ¶

assertPattern( mixed $pattern , string $string , string $message = '' )

Compatibility wrapper function for assertRegexp

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

assertPostConditions() protected ¶

assertPostConditions( string $method ,… )

Announces the end of a test.

Parameters
string $method ,…
Test method just finished.

assertPreConditions() protected ¶

assertPreConditions( string $method ,… )

Announces the start of a test.

Parameters
string $method ,…
Test method just started.

assertReference() protected static ¶

assertReference( mixed $first , mixed $second , string $message = '' )

Compatibility wrapper function for assertSame

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
$message SimpleTest failure output string
Returns
boolean

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
string $haystack
string $message optional ''
boolean $ignoreCase optional false
Returns
boolean

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
string $string
string $message optional ''
Returns
boolean

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
string $string
string $message optional ''
Returns
boolean

assertTextEquals() public ¶

assertTextEquals( string $expected , string $result , message $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.
message $message optional ''
message to use for failure.
Returns
boolean

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
string $haystack
string $message optional ''
boolean $ignoreCase optional false
Returns
boolean

assertTextNotEquals() public ¶

assertTextNotEquals( string $expected , string $result , message $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.
message $message optional ''
message to use for failure.
Returns
boolean

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
string $string
string $message optional ''
Returns
boolean

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
string $string
string $message optional ''
Returns
boolean

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
string

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 ¶

expectError( mixed $expected = false , string $message = '' )

Compatibility wrapper function for setExpectedException

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() protected ¶

expectException( mixed $name = 'Exception' , string $message = '' )

Compatibility wrapper function for setExpectedException

Parameters
mixed $name optional 'Exception'
$expected the name of the Exception
string $message optional ''
the text to display if the assertion is not correct

loadFixtures() public ¶

loadFixtures( string $fixture ,… )

Chooses which fixtures to load for a given test

Parameters
string $fixture ,…

Each parameter is a model name that corresponds to a fixture, i.e. 'Post', 'Author', etc.

Throws
Exception
when no fixture manager is available.
See
CakeTestCase::$autoFixtures

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
Returns
PHPUnit_Framework_TestResult
Throws
InvalidArgumentException

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
string $message optional ''
Returns
boolean

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
boolean

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.

tearDown() public ¶

tearDown( )

teardown any static object changes and restore them.

Properties detail

$autoMock ¶

public boolean

Automatically mock controllers that aren't mocked

true

$contents ¶

public string

The resulting rendered layout+view of the last testAction call

null

$controller ¶

public Controller

The controller to test in testAction

null

$headers ¶

public string

The headers that would have been sent by the action

null

$loadRoutes ¶

public boolean

Use custom routes during tests

true

$result ¶

public string

The returned result of the dispatch (requestAction), if any

null

$vars ¶

public array

The resulting view vars of the last testAction call

null

$view ¶

public string

The resulting rendered view of the last testAction call

null
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs