Source for file Assert.php
Documentation is available at Assert.php
// +------------------------------------------------------------------------+
// +------------------------------------------------------------------------+
// | Copyright (c) 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License, |
// | that is available at http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +------------------------------------------------------------------------+
// $Id: Assert.php,v 1.5.2.1 2004/09/04 07:17:31 sebastian Exp $
require_once 'PHPUnit2/Framework/AssertionFailedError.php';
require_once 'PHPUnit2/Framework/ComparisonFailure.php';
* A set of assert methods.
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright Copyright © 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
private static $looselyTyped = false;
// {{{ protected function __construct()
* Protect constructor since it is a static only class.
// {{{ public static function assertEquals($expected, $actual, $delta = 0, $message = '')
* Asserts that two variables are equal.
public static function assertEquals($expected, $actual, $delta = 0 , $message = '') {
self ::failNotEquals ($expected, $actual, $message);
self ::failNotEquals ($expected, $actual, $message);
self ::sortArrayRecursively ($actual);
self ::sortArrayRecursively ($expected);
if (self ::$looselyTyped) {
$actual = self ::convertToString ($actual);
$expected = self ::convertToString ($expected);
if (!(abs($expected - $actual) <= $delta)) {
self ::failNotEquals ($expected, $actual, $message);
if (self ::$looselyTyped) {
settype ($actual, gettype($expected));
self ::assertSame ($expected, $actual, $message);
// {{{ public static function assertTrue($condition, $message = '')
* Asserts that a condition is true.
* @param boolean $condition
public static function assertTrue($condition, $message = '') {
// {{{ public static function assertFalse($condition, $message = '')
* Asserts that a condition is false.
* @param boolean $condition
public static function assertFalse($condition, $message = '') {
self ::assertTrue (!$condition, $message);
// {{{ public static function assertNotNull($object, $message = '')
* Asserts that an object isn't null.
self ::assertTrue ($object !== null , $message);
// {{{ public static function assertNull($object, $message = '')
* Asserts that an object is null.
public static function assertNull($object, $message = '') {
self ::assertTrue ($object === null , $message);
// {{{ public static function assertSame($expected, $actual, $message = '')
* Asserts that two objects refer to the same object.
public static function assertSame($expected, $actual, $message = '') {
if ($expected !== $actual) {
self ::failNotSame ($expected, $actual, $message);
// {{{ public static function assertNotSame($expected, $actual, $message = '')
* Asserts that two objects refer not to the same object.
public static function assertNotSame($expected, $actual, $message = '') {
if ($expected === $actual) {
self ::failSame ($message);
// {{{ public static function assertType($expected, $actual, $message = '')
* Asserts that a variable is of a given type.
* @param string $expected
public static function assertType($expected, $actual, $message = '') {
if ($expected != $actual) {
// {{{ public static function assertRegExp($expected, $actual, $message = '')
* Asserts that a string matches a given
* @param string $expected
public static function assertRegExp($expected, $actual, $message = '') {
self ::failNotEquals ($expected, $actual, $message);
// {{{ public static function fail($message = '')
* Fails a test with the given message.
* @throws PHPUnit2_Framework_AssertionFailedError
public static function fail($message = '') {
// {{{ public static function format($expected, $actual, $message)
public static function format($expected, $actual, $message) {
'%s%sexpected: <%s> but was: <%s>',
($message != '') ? ' ' : '',
self ::objectToString ($expected),
self ::objectToString ($actual)
// {{{ public static function setLooselyTyped($looselyTyped)
* @param boolean $looselyTyped
self ::$looselyTyped = $looselyTyped;
// {{{ private static function convertToString($value)
* Converts a value to a string.
private static function convertToString ($value) {
foreach ($value as $k => $v) {
$value[$k] = self ::convertToString ($value[$k]);
$value[$k] = self ::objectToString ($value[$k]);
// {{{ private static function failSame($message)
* @throws PHPUnit2_Framework_AssertionFailedError
private static function failSame ($message) {
($message != '') ? ' ' : ''
// {{{ private static function failNotSame($expected, $actual, $message)
* @throws PHPUnit2_Framework_AssertionFailedError
private static function failNotSame ($expected, $actual, $message) {
'%s%sexpected same: <%s> was not: <%s>',
($message != '') ? ' ' : '',
self ::objectToString ($expected),
self ::objectToString ($actual)
// {{{ private static function failNotEquals($expected, $actual, $message)
* @throws PHPUnit2_Framework_AssertionFailedError
private static function failNotEquals ($expected, $actual, $message) {
self ::fail (self ::format ($expected, $actual, $message));
// {{{ private static function objectToString($object)
private static function objectToString ($object) {
// {{{ private static function sortArrayRecursively(&$array) {
* Sorts an array recursively by its keys.
* @author Adam Maccabee Trachtenberg <adam@trachtenberg.com>
private static function sortArrayRecursively (&$array) {
foreach($array as $k => $v) {
self ::sortArrayRecursively ($array[$k]);
* vim600: et sw=2 ts=2 fdm=marker
Documentation generated on Mon, 11 Mar 2019 13:55:58 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|