PHP Class lithium\test\Unit

Unit tests are used to check a small unit of functionality, such as if a method returns an expected result for a known input, or whether an adapter can successfully open a connection. Available assertions are (see assert methods for details): Equal, False, Identical, NoPattern, NotEqual, Null, Pattern, Tags, True. If an assertion is expected to produce an exception, the expectException method should be called before it.
Inheritance: extends lithium\core\Object
Show file Open project: unionofrad/lithium Class Usage Examples

Protected Properties

Property Type Description
$_expected string The list of expected exceptions.
$_internalTypes array Internal types and how to test for them
$_reporter string The Reference to a test reporter class.
$_results string The list of test results.

Public Methods

Method Description
assert ( boolean $expression, string | boolean $message = false, array $data = [] ) : boolean General assert method used by others for common output.
assertArrayHasKey ( $key, array $array, string | boolean $message = '{:message}' ) : boolean Assert that the result array has given key.
assertArrayNotHasKey ( $key, array $array, string | boolean $message = '{:message}' ) : boolean Assert that the result array does *not* have given key.
assertClassHasAttribute ( mixed $attributeName, string $class, string | boolean $message = '{:message}' ) : boolean Assert that a class has a given attribute.
assertClassHasStaticAttribute ( mixed $attributeName, string $class, string | boolean $message = '{:message}' ) : boolean Assert that a class does have a given _static_ attribute.
assertClassNotHasAttribute ( mixed $attributeName, string $class, string | boolean $message = '{:message}' ) : boolean Assert that a class does *not* have a given attribute.
assertClassNotHasStaticAttribute ( $attrName, string $class, string | boolean $message = '{:message}' ) : boolean Assert that a class does *not* have a given _static_ attribute.
assertContains ( string $needle, mixed $haystack, string | boolean $message = '{:message}' ) : boolean Assert that $haystack contains $needle as a value.
assertContainsOnly ( string $type, array | object $haystack, string | boolean $message = '{:message}' ) : boolean Assert that $haystack does only contain item of given type.
assertContainsOnlyInstancesOf ( string $class, array | object $haystack, string | boolean $message = '{:message}' ) : boolean Assert that $haystack contains only instances of given class.
assertCookie ( array $expected, array $headers = null ) : boolean Assert Cookie data is properly set in headers.
assertCount ( integer $expected, array $array, string | boolean $message = '{:message}' ) : boolean Assert that the passed result array has expected number of elements.
assertEmpty ( string $actual, string | boolean $message = '{:message}' ) : boolean Assert that given result is empty.
assertEqual ( mixed $expected, mixed $result, string | boolean $message = '{:message}' ) : boolean Assert that the actual result is equal, but not neccessarily identical, to the expected result.
assertException ( mixed $expected, Closure $closure, string $message = '{:message}' ) : boolean Assert that the code passed in a closure throws an exception or raises a PHP error. The first argument to this method specifies which class name or message the exception must have in order to make the assertion successful.
assertFalse ( mixed $result, string $message = '{:message}' ) : boolean Assert that the result strictly is false.
assertFileEquals ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean Assert that the file contents of $expected are equal to the contents of $actual.
assertFileExists ( string $actual, string | boolean $message = '{:message}' ) : boolean Assert that a file exists.
assertFileNotEquals ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean Assert that the file contents of $expected are *not* equal to the contents of $actual.
assertFileNotExists ( string $actual, string | boolean $message = '{:message}' ) : boolean Assert that a file does *not* exist.
assertGreaterThan ( float | integer $expected, float | integer $actual, string | boolean $message = '{:message}' ) : boolean Assert that $expected is greater than $actual.
assertGreaterThanOrEqual ( float | integer $expected, float | integer $actual, string | boolean $message = '{:message}' ) : boolean Assert that $expected is greater than or equal to $actual.
assertIdentical ( mixed $expected, mixed $result, string | boolean $message = '{:message}' ) : boolean Assert that the actual result and the expected result are identical using a strict comparison.
assertInstanceOf ( string $expected, object $actual, string | boolean $message = '{:message}' ) : boolean Assert that $actual is an instance of $expected.
assertInternalType ( string $expected, object $actual, string | boolean $message = '{:message}' ) : boolean Assert that $actual is of given type.
assertLessThan ( float | integer $expected, float | integer $actual, string | boolean $message = '{:message}' ) : boolean Assert that $expected is less than $actual.
assertLessThanOrEqual ( float | integer $expected, float | integer $actual, string | boolean $message = '{:message}' ) : boolean Assert that $expected is less than or equal to $actual.
assertNoCookie ( array $expected, array $headers = null ) : boolean Assert Cookie data is *not* set in headers.
assertNotContains ( string $needle, miexed $haystack, string | boolean $message = '{:message}' ) : boolean Assert that $haystack does *not* contain $needle as a value.
assertNotContainsOnly ( string $type, array | object $haystack, string | boolean $message = '{:message}' ) : boolean Assert that $haystack hasn't any items of given type.
assertNotCount ( integer $expected, array $array, string | boolean $message = '{:message}' ) : boolean Assert that the passed result array has *not* the expected number of elements.
assertNotEmpty ( string $actual, string | boolean $message = '{:message}' ) : boolean Assert that given result is *not* empty.
assertNotEqual ( mixed $expected, mixed $result, string | boolean $message = '{:message}' ) : boolean Assert that the actual result and the expected result are *not* equal to each other.
assertNotException ( mixed $expected, Closure $closure, string $message = '{:message}' ) : boolean Assert that the code passed in a closure does not throw an exception matching the passed expected exception.
assertNotIdentical ( mixed $expected, mixed $result, string | boolean $message = '{:message}' ) : boolean Assert that the actual result and the expected result are *not* identical using a strict comparison.
assertNotInstanceOf ( string $expected, object $actual, string | boolean $message = '{:message}' ) : boolean Assert that $actual is *not* an instance of $expected.
assertNotInternalType ( string $expected, object $actual, string | boolean $message = '{:message}' ) : boolean Assert that $actual is *not* of given type.
assertNotNull ( $actual, string $message = '{:message}' ) : boolean Assert that the result is *not* strictly null.
assertNotPattern ( mixed $expected, mixed $result, string $message = '{:message}' ) : boolean Assert that the regular expression $expected is *not* matched in the result.
assertNull ( mixed $result, string $message = '{:message}' ) : boolean Assert that the result is strictly null.
assertObjectHasAttribute ( string $attributeName, string $object, string | boolean $message = '{:message}' ) : boolean Assert that $object has given attribute.
assertObjectNotHasAttribute ( string $attributeName, string $object, string | boolean $message = '{:message}' ) : boolean Assert that $object does *not* have given attribute.
assertPattern ( mixed $expected, mixed $result, string $message = '{:message}' ) : boolean Assert that the regular expression $expected is matched in the result.
assertStringEndsWith ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean Assert given result string has given suffix.
assertStringMatchesFormat ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean Assert that given value matches the sprintf format.
assertStringNotMatchesFormat ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean Assert that given value does *not* matche the sprintf format.
assertStringStartsWith ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean Assert given result string has given prefix.
assertTags ( string $string, array $expected ) : boolean Takes an array $expected and generates a regex from it to match the provided $string.
assertTrue ( mixed $result, string $message = '{:message}' ) : boolean Assert that the result is strictly true.
expectException ( mixed $message = true ) : void Used before a call to assert*() if you expect the test assertion to generate an exception or PHP error. If no error or exception is thrown, a test failure will be reported. Can be called multiple times per assertion, if more than one error is expected.
fail ( string $message = false ) Generates a failed test with the given message.
get ( string $class ) : string Finds the test case for the corresponding class name.
methods ( ) : array Return test methods to run.
results ( ) : array Returns the current results.
run ( array $options = [] ) : array Runs the test methods in this test case, with the given options.
setUp ( ) : void Setup method run before every test method. Override in subclasses.
skip ( ) Subclasses should use this method to set conditions that, if failed, terminate further testing.
skipIf ( boolean $condition, string | boolean $message = false ) : mixed Skips test(s) if the condition is met.
subject ( ) : string Returns the class name that is the subject under test for this test case.
tearDown ( ) : void Teardown method run after every test method. Override in subclasses.

Protected Methods

Method Description
_arrayPermute ( array $items, array $perms = [] ) : array Generates all permutation of an array $items and returns them in a new array.
_cleanUp ( string $path = null ) : void Removes everything from resources/tmp/tests directory. Call from inside of your test method or tearDown().
_compare ( string $type, mixed $expected, mixed $result = null, string $trace = null ) : array Compare the expected with the result. If $result is null $expected equals $type and $result equals $expected.
_cookieMatch ( array $expected, array $headers ) : boolean Match an $expected cookie with the given headers. If no headers are provided, then the value of headers_list() will be used.
_handleException ( mixed $exception, integer $lineFlag = null ) : void Normalizes Exception objects and PHP error data into a single array format then the error data is logged to the test results.
_message ( array &$data = [], string $message = null ) : string Returns a basic message for the data returned from _result().
_normalizeLineEndings ( mixed $expected, mixed $result ) : array Fixes some issues regarding the used EOL character(s).
_reportException ( array $exception, string $lineFlag = null ) : void Convert an exception object to an exception result array for test reporting.
_result ( string $type, array $info, array $options = [] ) : void Reports test result messages.
_runTestMethod ( string $method, array $options ) : mixed Runs an individual test method, collecting results and catching exceptions along the way.

Method Details

_arrayPermute() protected method

Generates all permutation of an array $items and returns them in a new array.
protected _arrayPermute ( array $items, array $perms = [] ) : array
$items array An array of items
$perms array
return array

_cleanUp() protected method

Uses DIRECTORY_SEPARATOR as getPathname() is used in a a direct string comparison. The method may contain slashes and backslashes. If the file to unlink is readonly, it throws a exception (Permission denied) on Windows. So, the file is checked before an unlink is tried. (this will make the tests run slower but is prefered over a if (!unlink { chmod; unlink }. http://stringoftheseus.com/blog/2010/12/22/php-unlink-permisssion-denied-error-on-windows/
protected _cleanUp ( string $path = null ) : void
$path string Path to directory with contents to remove. If first character is NOT a slash (`/`) or a Windows drive letter (`C:`) prepends `Libraries::get(true, 'resources')/tmp/`.
return void

_compare() protected method

Compare the expected with the result. If $result is null $expected equals $type and $result equals $expected.
protected _compare ( string $type, mixed $expected, mixed $result = null, string $trace = null ) : array
$type string The type of comparison either `'identical'` or `'equal'` (default).
$expected mixed The expected value.
$result mixed An optional result value, defaults to `null`
$trace string An optional trace used internally to track arrays and objects, defaults to `null`.
return array Data with the keys `trace'`, `'expected'` and `'result'`.

_cookieMatch() protected method

Match an $expected cookie with the given headers. If no headers are provided, then the value of headers_list() will be used.
protected _cookieMatch ( array $expected, array $headers ) : boolean
$expected array
$headers array When empty, value of `headers_list()` will be used.
return boolean `true` if the assertion succeeded, `false` otherwise.

_handleException() protected method

Normalizes Exception objects and PHP error data into a single array format then the error data is logged to the test results.
See also: lithium\test\Unit::_reportException()
protected _handleException ( mixed $exception, integer $lineFlag = null ) : void
$exception mixed An `Exception` object instance, or an array containing the following keys: `'message'`, `'file'`, `'line'`, `'trace'` (in `debug_backtrace()` format) and optionally `'code'` (error code number) and `'context'` (an array of variables relevant to the scope of where the error occurred).
$lineFlag integer A flag used for determining the relevant scope of the call stack. Set to the line number where test methods are called.
return void

_message() protected method

Returns a basic message for the data returned from _result().
See also: lithium\test\Unit::assert()
See also: lithium\test\Unit::_result()
protected _message ( array &$data = [], string $message = null ) : string
$data array The data to use for creating the message.
$message string The string prepended to the generate message in the current scope.
return string

_normalizeLineEndings() protected method

On linux EOL is LF, on Windows it is normally CRLF, but the latter may depend also on the git config core.autocrlf setting. As some tests use heredoc style (<<<) to specify multiline expectations, this EOL issue may cause tests to fail only because of a difference in EOL's used. in assertEqual, assertNotEqual, assertPattern and assertNotPattern this function is called to get rid of any EOL differences.
protected _normalizeLineEndings ( mixed $expected, mixed $result ) : array
$expected mixed
$result mixed
return array Array with the normalized elements i.e. `array($expected, $result)`.

_reportException() protected method

Convert an exception object to an exception result array for test reporting.
protected _reportException ( array $exception, string $lineFlag = null ) : void
$exception array The exception data to report on. Statistics are gathered and added to the reporting stack contained in `Unit::$_results`.
$lineFlag string
return void

_result() protected method

Reports test result messages.
protected _result ( string $type, array $info, array $options = [] ) : void
$type string The type of result being reported. Can be `'pass'`, `'fail'`, `'skip'` or `'exception'`.
$info array An array of information about the test result. At a minimum, this should contain a `'message'` key. Other possible keys are `'file'`, `'line'`, `'class'`, `'method'`, `'assertion'` and `'data'`.
$options array Currently unimplemented.
return void

_runTestMethod() protected method

Runs an individual test method, collecting results and catching exceptions along the way.
protected _runTestMethod ( string $method, array $options ) : mixed
$method string The name of the test method to run.
$options array
return mixed

assert() public method

General assert method used by others for common output.
public assert ( boolean $expression, string | boolean $message = false, array $data = [] ) : boolean
$expression boolean
$message string | boolean The message to output. If the message is not a string, then it will be converted to '{:message}'. Use '{:message}' in the string and it will use the `$data` to format the message with `String::insert()`.
$data array
return boolean `true` if the assertion succeeded, `false` otherwise.

assertArrayHasKey() public method

$this->assertArrayHasKey('bar', array('bar' => 'baz')); // succeeds $this->assertArrayHasKey('foo', array('bar' => 'baz')); // fails
See also: lithium\test\Unit::assert()
public assertArrayHasKey ( $key, array $array, string | boolean $message = '{:message}' ) : boolean
$array array
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertArrayNotHasKey() public method

$this->assertArrayNotHasKey('foo', array('bar' => 'baz')); // succeeds $this->assertArrayNotHasKey('bar', array('bar' => 'baz')); // fails
See also: lithium\test\Unit::assert()
public assertArrayNotHasKey ( $key, array $array, string | boolean $message = '{:message}' ) : boolean
$array array
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertClassHasAttribute() public method

$this->assertClassHasAttribute('__construct', 'ReflectionClass'); // succeeds $this->assertClassHasAttribute('name', 'ReflectionClass'); // fails
See also: lithium\test\Unit::assert()
See also: lithium\test\Unit::assertObjectHasAttribute()
public assertClassHasAttribute ( mixed $attributeName, string $class, string | boolean $message = '{:message}' ) : boolean
$attributeName mixed
$class string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertClassHasStaticAttribute() public method

$this->assertClassHasStaticAttribute('_methodFilters', '\lithium\core\StaticObject'); // succeeds $this->assertClassHasStaticAttribute('foobar', '\lithium\core\StaticObject'); // fails
See also: lithium\test\Unit::assert()
public assertClassHasStaticAttribute ( mixed $attributeName, string $class, string | boolean $message = '{:message}' ) : boolean
$attributeName mixed
$class string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertClassNotHasAttribute() public method

$this->assertClassNotHasAttribute('name', 'ReflectionClass'); // succeeds $this->assertClassNotHasAttribute('__construct', 'ReflectionClass'); // fails
See also: lithium\test\Unit::assert()
See also: lithium\test\Unit::assertObjectHasAttribute()
public assertClassNotHasAttribute ( mixed $attributeName, string $class, string | boolean $message = '{:message}' ) : boolean
$attributeName mixed
$class string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertClassNotHasStaticAttribute() public method

$this->assertClassNotHasStaticAttribute('foobar', '\lithium\core\StaticObject'); // succeeds $this->assertClassNotHasStaticAttribute('_methodFilters', '\lithium\core\StaticObject'); // fails
See also: lithium\test\Unit::assert()
public assertClassNotHasStaticAttribute ( $attrName, string $class, string | boolean $message = '{:message}' ) : boolean
$class string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertContains() public method

$this->assertContains('foo', array('foo', 'bar', 'baz')); // succeeds $this->assertContains(4, array(1,2,3)); // fails
See also: lithium\test\Unit::assert()
public assertContains ( string $needle, mixed $haystack, string | boolean $message = '{:message}' ) : boolean
$needle string The needle you are looking for.
$haystack mixed An array, iterable object, or string.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertContainsOnly() public method

$this->assertContainsOnly('integer', array(1,2,3)); // succeeds $this->assertContainsOnly('integer', array('foo', 'bar', 'baz')); // fails
See also: lithium\test\Unit::$_internalTypes
See also: lithium\test\Unit::assert()
public assertContainsOnly ( string $type, array | object $haystack, string | boolean $message = '{:message}' ) : boolean
$type string
$haystack array | object Array or iterable object.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertContainsOnlyInstancesOf() public method

$this->assertContainsOnlyInstancesOf('stdClass', array(new \stdClass)); // succeeds $this->assertContainsOnlyInstancesOf('stdClass', array(new \lithium\test\Unit)); // fails
See also: lithium\test\Unit::assert()
public assertContainsOnlyInstancesOf ( string $class, array | object $haystack, string | boolean $message = '{:message}' ) : boolean
$class string
$haystack array | object Array or iterable object.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertCookie() public method

The value passed to exepected is an array of the cookie data, with at least the key and value expected, but can support any of the following keys: - key: the expected key - value: the expected value - path: optionally specifiy a path - name: optionally specify the cookie name - expires: optionally assert a specific expire time
See also: lithium\test\Unit::assert()
public assertCookie ( array $expected, array $headers = null ) : boolean
$expected array
$headers array When empty, value of `headers_list()` is used.
return boolean `true` if the assertion succeeded, `false` otherwise.

assertCount() public method

$this->assertCount(1, array('foo')); // succeeds $this->assertCount(2, array('foo', 'bar', 'bar')); // fails
See also: lithium\test\Unit::assert()
public assertCount ( integer $expected, array $array, string | boolean $message = '{:message}' ) : boolean
$expected integer
$array array
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertEmpty() public method

$this->assertEmpty(''); // succeeds $this->assertEmpty(0); // succeeds $this->assertEmpty(0.0); // succeeds $this->assertEmpty('0'); // succeeds $this->assertEmpty(null); // succeeds $this->assertEmpty(false); // succeeds $this->assertEmpty(array()); // succeeds $this->assertEmpty(1); // fails
See also: lithium\test\Unit::assert()
public assertEmpty ( string $actual, string | boolean $message = '{:message}' ) : boolean
$actual string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertEqual() public method

Assert that the actual result is equal, but not neccessarily identical, to the expected result.
See also: lithium\test\Unit::assert()
public assertEqual ( mixed $expected, mixed $result, string | boolean $message = '{:message}' ) : boolean
$expected mixed
$result mixed
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertException() public method

Assert that the code passed in a closure throws an exception or raises a PHP error. The first argument to this method specifies which class name or message the exception must have in order to make the assertion successful.
See also: lithium\test\Unit::assert()
public assertException ( mixed $expected, Closure $closure, string $message = '{:message}' ) : boolean
$expected mixed A string indicating what the error text is expected to be. This can be an exact string, a /-delimited regular expression, or true, indicating that any error text is acceptable.
$closure Closure A closure containing the code that should throw the exception.
$message string
return boolean `true` if the assertion succeeded, `false` otherwise.

assertFalse() public method

$this->assertFalse(false, 'Boolean false'); // succeeds $this->assertFalse('', 'String is empty'); // fails $this->assertFalse(0, 'Zero value'); // fails
See also: lithium\test\Unit::assert()
public assertFalse ( mixed $result, string $message = '{:message}' ) : boolean
$result mixed
$message string
return boolean `true` if the assertion succeeded, `false` otherwise.

assertFileEquals() public method

$this->assertFileEquals('/tmp/foo.txt', '/tmp/foo.txt'); // succeeds $this->assertFileEquals('/tmp/foo.txt', '/tmp/bar.txt'); // fails
See also: lithium\test\Unit::assert()
public assertFileEquals ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean
$expected string Absolute path to the expected file.
$actual string Absolute path to the actual file.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertFileExists() public method

$this->assertFileExists(__FILE__); // succeeds $this->assertFileExists('/tmp/bar.txt'); // fails
See also: lithium\test\Unit::assert()
public assertFileExists ( string $actual, string | boolean $message = '{:message}' ) : boolean
$actual string Absolute path to the actual file.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertFileNotEquals() public method

$this->assertFileNotEquals('/tmp/foo.txt', '/tmp/bar.txt'); // succeeds $this->assertFileNotEquals('/tmp/foo.txt', '/tmp/foo.txt'); // fails
See also: lithium\test\Unit::assert()
public assertFileNotEquals ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean
$expected string Absolute path to the expected file.
$actual string Absolute path to the actual file.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertFileNotExists() public method

$this->assertFileNotExists('/tmp/bar.txt'); // succeeds $this->assertFileNotExists(__FILE__); // fails
See also: lithium\test\Unit::assert()
public assertFileNotExists ( string $actual, string | boolean $message = '{:message}' ) : boolean
$actual string Absolute path to the actual file.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertGreaterThan() public method

$this->assertGreaterThan(5, 3); // succeeds $this->assertGreaterThan(3, 5); // fails
See also: lithium\test\Unit::assert()
public assertGreaterThan ( float | integer $expected, float | integer $actual, string | boolean $message = '{:message}' ) : boolean
$expected float | integer
$actual float | integer
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertGreaterThanOrEqual() public method

$this->assertGreaterThanOrEqual(5, 5); // succeeds $this->assertGreaterThanOrEqual(3, 5); // fails
See also: lithium\test\Unit::assert()
public assertGreaterThanOrEqual ( float | integer $expected, float | integer $actual, string | boolean $message = '{:message}' ) : boolean
$expected float | integer
$actual float | integer
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertIdentical() public method

Assert that the actual result and the expected result are identical using a strict comparison.
See also: lithium\test\Unit::assert()
public assertIdentical ( mixed $expected, mixed $result, string | boolean $message = '{:message}' ) : boolean
$expected mixed
$result mixed
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertInstanceOf() public method

$this->assertInstanceOf('stdClass', new stdClass); // succeeds $this->assertInstanceOf('ReflectionClass', new stdClass); // fails
See also: lithium\test\Unit::assert()
public assertInstanceOf ( string $expected, object $actual, string | boolean $message = '{:message}' ) : boolean
$expected string Fully namespaced expected class.
$actual object Object you are testing.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertInternalType() public method

$this->assertInternalType('string', 'foobar'); // succeeds $this->assertInternalType('integer', 'foobar'); // fails
See also: lithium\test\Unit::$_internalTypes
See also: lithium\test\Unit::assert()
public assertInternalType ( string $expected, object $actual, string | boolean $message = '{:message}' ) : boolean
$expected string Internal type.
$actual object Object you are testing.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertLessThan() public method

$this->assertLessThan(3, 5); // succeeds $this->assertLessThan(5, 3); // fails
See also: lithium\test\Unit::assert()
public assertLessThan ( float | integer $expected, float | integer $actual, string | boolean $message = '{:message}' ) : boolean
$expected float | integer
$actual float | integer
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertLessThanOrEqual() public method

$this->assertLessThanOrEqual(5, 5); // succeeds $this->assertLessThanOrEqual(5, 3); // fails
See also: lithium\test\Unit::assert()
public assertLessThanOrEqual ( float | integer $expected, float | integer $actual, string | boolean $message = '{:message}' ) : boolean
$expected float | integer
$actual float | integer
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNoCookie() public method

The value passed to expected is an array of the cookie data, with at least the key and value expected, but can support any of the following keys: - key: the expected key - value: the expected value - path: optionally specify a path - name: optionally specify the cookie name - expires: optionally assert a specific expire time
See also: lithium\test\Unit::assert()
public assertNoCookie ( array $expected, array $headers = null ) : boolean
$expected array
$headers array When empty, value of `headers_list()` is used.
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotContains() public method

$this->assertNotContains(4, array(1,2,3)); // succeeds $this->assertNotContains('foo', array('foo', 'bar', 'baz')); // fails
See also: lithium\test\Unit::assert()
public assertNotContains ( string $needle, miexed $haystack, string | boolean $message = '{:message}' ) : boolean
$needle string The needle you are looking for.
$haystack miexed Array or iterable object or a string.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotContainsOnly() public method

$this->assertNotContainsOnly('integer', array('foo', 'bar', 'baz')); // succeeds $this->assertNotContainsOnly('integer', array(1,2,3)); // fails
See also: lithium\test\Unit::$_internalTypes
See also: lithium\test\Unit::assert()
public assertNotContainsOnly ( string $type, array | object $haystack, string | boolean $message = '{:message}' ) : boolean
$type string
$haystack array | object Array or iterable object.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotCount() public method

$this->assertNotCount(2, array('foo', 'bar', 'bar')); // succeeds $this->assertNotCount(1, array('foo')); // fails
See also: lithium\test\Unit::assert()
public assertNotCount ( integer $expected, array $array, string | boolean $message = '{:message}' ) : boolean
$expected integer
$array array
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotEmpty() public method

$this->assertNotEmpty(1); // succeeds $this->assertNotEmpty(array()); // fails
See also: lithium\test\Unit::assert()
public assertNotEmpty ( string $actual, string | boolean $message = '{:message}' ) : boolean
$actual string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotEqual() public method

Assert that the actual result and the expected result are *not* equal to each other.
See also: lithium\test\Unit::assert()
public assertNotEqual ( mixed $expected, mixed $result, string | boolean $message = '{:message}' ) : boolean
$expected mixed
$result mixed
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotException() public method

The value passed to exepected is either an exception class name or the expected message.
See also: lithium\test\Unit::assert()
public assertNotException ( mixed $expected, Closure $closure, string $message = '{:message}' ) : boolean
$expected mixed A string indicating what the error text is not expected to be. This can be an exact string, a /-delimited regular expression, or true, indicating that any error text is acceptable.
$closure Closure A closure containing the code that should throw the exception.
$message string
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotIdentical() public method

Assert that the actual result and the expected result are *not* identical using a strict comparison.
See also: lithium\test\Unit::assert()
public assertNotIdentical ( mixed $expected, mixed $result, string | boolean $message = '{:message}' ) : boolean
$expected mixed
$result mixed
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotInstanceOf() public method

$this->assertNotInstanceOf('ReflectionClass', new stdClass); // succeeds $this->assertNotInstanceOf('stdClass', new stdClass); // fails
See also: lithium\test\Unit::assert()
public assertNotInstanceOf ( string $expected, object $actual, string | boolean $message = '{:message}' ) : boolean
$expected string Fully namespaced expected class.
$actual object Object you are testing.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotInternalType() public method

$this->assertNotInternalType('integer', 'foobar'); // succeeds $this->assertNotInternalType('string', 'foobar'); // fails
See also: lithium\test\Unit::$_internalTypes
See also: lithium\test\Unit::assert()
public assertNotInternalType ( string $expected, object $actual, string | boolean $message = '{:message}' ) : boolean
$expected string Internal type.
$actual object Object you are testing.
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotNull() public method

$this->assertNotNull(1); // succeeds $this->assertNotNull(null); // fails
See also: lithium\test\Unit::assert()
public assertNotNull ( $actual, string $message = '{:message}' ) : boolean
$message string
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNotPattern() public method

Assert that the regular expression $expected is *not* matched in the result.
See also: lithium\test\Unit::assert()
public assertNotPattern ( mixed $expected, mixed $result, string $message = '{:message}' ) : boolean
$expected mixed
$result mixed
$message string
return boolean `true` if the assertion succeeded, `false` otherwise.

assertNull() public method

Assert that the result is strictly null.
See also: lithium\test\Unit::assert()
public assertNull ( mixed $result, string $message = '{:message}' ) : boolean
$result mixed
$message string
return boolean `true` if the assertion succeeded, `false` otherwise.

assertObjectHasAttribute() public method

$this->assertObjectHasAttribute('__construct', 'ReflectionClass'); // succeeds $this->assertObjectHasAttribute('name', 'ReflectionClass'); // fails
See also: lithium\test\Unit::assert()
public assertObjectHasAttribute ( string $attributeName, string $object, string | boolean $message = '{:message}' ) : boolean
$attributeName string
$object string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertObjectNotHasAttribute() public method

$this->assertObjectNotHasAttribute('name', 'ReflectionClass'); // succeeds $this->assertObjectNotHasAttribute('__construct', 'ReflectionClass'); // fails
See also: lithium\test\Unit::assert()
public assertObjectNotHasAttribute ( string $attributeName, string $object, string | boolean $message = '{:message}' ) : boolean
$attributeName string
$object string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertPattern() public method

Assert that the regular expression $expected is matched in the result.
See also: lithium\test\Unit::assert()
public assertPattern ( mixed $expected, mixed $result, string $message = '{:message}' ) : boolean
$expected mixed
$result mixed
$message string
return boolean `true` if the assertion succeeded, `false` otherwise.

assertStringEndsWith() public method

$this->assertStringEndsWith('bar', 'foobar'); // succeeds $this->assertStringEndsWith('foo', 'foobar'); // fails
public assertStringEndsWith ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean
$expected string The suffix to check for.
$actual string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertStringMatchesFormat() public method

$this->assertStringMatchesFormat('%d', '10'); // succeeds $this->assertStringMatchesFormat('%d', '10.555'); // fails
See also: lithium\test\Unit::assert()
public assertStringMatchesFormat ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean
$expected string Expected format using sscanf's format.
$actual string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertStringNotMatchesFormat() public method

$this->assertStringNotMatchesFormat('%d', '10.555'); // succeeds $this->assertStringNotMatchesFormat('%d', '10'); // fails
See also: lithium\test\Unit::assert()
public assertStringNotMatchesFormat ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean
$expected string Expected format using sscanf's format.
$actual string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertStringStartsWith() public method

$this->assertStringStartsWith('foo', 'foobar'); // succeeds $this->assertStringStartsWith('bar', 'foobar'); // fails
public assertStringStartsWith ( string $expected, string $actual, string | boolean $message = '{:message}' ) : boolean
$expected string The prefix to check for.
$actual string
$message string | boolean
return boolean `true` if the assertion succeeded, `false` otherwise.

assertTags() public method

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 whitespaces between specified tags.
See also: lithium\test\Unit::assert()
public assertTags ( string $string, array $expected ) : boolean
$string string An HTML/XHTML/XML string
$expected array An array, see above
return boolean `true` if the assertion succeeded, `false` otherwise.

assertTrue() public method

$this->assertTrue(true, 'Boolean true'); // succeeds $this->assertTrue('false', 'String has content'); // fails $this->assertTrue(10, 'Non-Zero value'); // fails
See also: lithium\test\Unit::assert()
public assertTrue ( mixed $result, string $message = '{:message}' ) : boolean
$result mixed
$message string
return boolean `true` if the assertion succeeded, `false` otherwise.

expectException() public method

Used before a call to assert*() if you expect the test assertion to generate an exception or PHP error. If no error or exception is thrown, a test failure will be reported. Can be called multiple times per assertion, if more than one error is expected.
Deprecation:
public expectException ( mixed $message = true ) : void
$message mixed A string indicating what the error text is expected to be. This can be an exact string, a /-delimited regular expression, or true, indicating that any error text is acceptable.
return void

fail() public method

Generates a failed test with the given message.
public fail ( string $message = false )
$message string

get() public static method

Finds the test case for the corresponding class name.
public static get ( string $class ) : string
$class string A fully-namespaced class reference for which to find a test case.
return string Returns the class name of a test case for `$class`, or `null` if none exists.

methods() public method

Return test methods to run.
public methods ( ) : array
return array

results() public method

Returns the current results.
public results ( ) : array
return array The Results... currently.

run() public method

Installs a temporary error handler that will convert regular errors to exceptions in order to make both errors and exceptions be handled in a unified way. ErrorExceptions created like this, will get the error's code as their severity. As this comes closest to their meaning. The error handler honors the PHP error_level and will not convert errors to exceptions if they are masked by the error_level. This allows test methods to run assertions against i.e. deprecated functions. Usually the error_level is set by the test runner so that all errors are converted.
public run ( array $options = [] ) : array
$options array The options to use when running the test. Available options are: - `'methods'`: An arbitrary array of method names to execute. If unspecified, all methods starting with 'test' are run. - `'reporter'`: A closure which gets called after each test result, which may modify the results presented. - `'handler'`: A closure which gets registered as the temporary error handler.
return array

setUp() public method

Setup method run before every test method. Override in subclasses.
public setUp ( ) : void
return void

skip() public method

For example: public function skip() { $connection = Connections::get('test', array('config' => true)); $this->skipIf(!$connection, 'Test database is unavailable.'); }
public skip ( )

skipIf() public method

When used within a subclass' skip method, all tests are ignored if the condition is met, otherwise processing continues as normal. For other methods, only the remainder of the method is skipped, when the condition is met.
public skipIf ( boolean $condition, string | boolean $message = false ) : mixed
$condition boolean
$message string | boolean Message to pass if the condition is met.
return mixed

subject() public method

Returns the class name that is the subject under test for this test case.
public subject ( ) : string
return string

tearDown() public method

Teardown method run after every test method. Override in subclasses.
public tearDown ( ) : void
return void

Property Details

$_expected protected property

The list of expected exceptions.
Deprecation:
protected string $_expected
return string

$_internalTypes protected static property

Internal types and how to test for them
protected static array $_internalTypes
return array

$_reporter protected property

The Reference to a test reporter class.
protected string $_reporter
return string

$_results protected property

The list of test results.
protected string $_results
return string