Source for file expectation.php
Documentation is available at expectation.php
* base include file for SimpleTest
* @version $Id: expectation.php 1905 2009-07-29 13:54:00Z dgheath $
* include other SimpleTest class files
require_once(dirname(__FILE__
) .
'/dumper.php');
require_once(dirname(__FILE__
) .
'/compatibility.php');
* Assertion that can display failure information.
* Also includes various helper methods.
* Creates a dumper for displaying values and sets
* @param string $message Customised message on failure.
$this->message =
$message;
* Tests the expectation. True if correct.
* @param mixed $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
* Overlays the generated message onto the stored user
* message. An additional message can be interjected.
* @param mixed $compare Comparison value.
* @param SimpleDumper $dumper For formatting the results.
* @return string Description of success
* Accessor for the dumper.
* @return SimpleDumper Current value dumper.
* Test to see if a value is an expectation object.
* A useful utility method.
* @param mixed $expectation Hopefully an Expectation
* @return boolean True if descended from
* A wildcard expectation always matches.
* @subpackage MockObjects
* Tests the expectation. Always true.
* @param mixed $compare Ignored.
function test($compare) {
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
return 'Anything always matches [' .
$dumper->describeValue($compare) .
']';
* An expectation that never matches.
* @subpackage MockObjects
* Tests the expectation. Always false.
* @param mixed $compare Ignored.
function test($compare) {
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of failure.
return 'Failed expectation never matches [' .
$dumper->describeValue($compare) .
']';
* An expectation that passes on boolean true.
* @subpackage MockObjects
* @param mixed $compare Should be true.
* @return boolean True on match.
function test($compare) {
return (boolean)
$compare;
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
return 'Expected true, got [' .
$dumper->describeValue($compare) .
']';
* An expectation that passes on boolean false.
* @subpackage MockObjects
* @param mixed $compare Should be false.
* @return boolean True on match.
function test($compare) {
return ! (boolean)
$compare;
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
return 'Expected false, got [' .
$dumper->describeValue($compare) .
']';
* Sets the value to compare against.
* @param mixed $value Test value to match.
* @param string $message Customised message on failure.
* Tests the expectation. True if it matches the
* @param mixed $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
return (($this->value ==
$compare) &&
($compare ==
$this->value));
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
return "Equal expectation [" .
$this->dumper->describeValue($this->value) .
"]";
return "Equal expectation fails " .
$this->dumper->describeDifference($this->value, $compare);
* Accessor for comparison value.
* @return mixed Held value to compare with.
* Sets the value to compare against.
* @param mixed $value Test value to match.
* @param string $message Customised message on failure.
* Tests the expectation. True if it differs from the
* @param mixed $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
return ! parent::test($compare);
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
return "Not equal expectation passes " .
$dumper->describeDifference($this->getValue(), $compare);
return "Not equal expectation fails [" .
$dumper->describeValue($this->getValue()) .
* Test for being within a range.
* Sets the value to compare against and the fuzziness of
* the match. Used for comparing floating point values.
* @param mixed $value Test value to match.
* @param mixed $margin Fuzziness of match.
* @param string $message Customised message on failure.
function __construct($value, $margin, $message =
'%s') {
$this->upper =
$value +
$margin;
$this->lower =
$value -
$margin;
* Tests the expectation. True if it matches the
* @param mixed $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
return (($compare <=
$this->upper) &&
($compare >=
$this->lower));
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
* Creates a the message for being within the range.
* @param mixed $compare Value being tested.
return "Within expectation [" .
$this->dumper->describeValue($this->lower) .
"] and [" .
$this->dumper->describeValue($this->upper) .
"]";
* Creates a the message for being within the range.
* @param mixed $compare Value being tested.
if ($compare >
$this->upper) {
return "Outside expectation " .
$this->dumper->describeDifference($compare, $this->upper);
return "Outside expectation " .
$this->dumper->describeDifference($compare, $this->lower);
* Test for being outside of a range.
* Sets the value to compare against and the fuzziness of
* the match. Used for comparing floating point values.
* @param mixed $value Test value to not match.
* @param mixed $margin Fuzziness of match.
* @param string $message Customised message on failure.
function __construct($value, $margin, $message =
'%s') {
* Tests the expectation. True if it matches the
* @param mixed $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
return ! parent::test($compare);
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
if (! $this->test($compare)) {
* Sets the reference value to compare against.
* @param mixed $value Test reference to match.
* @param string $message Customised message on failure.
$this->message =
$message;
* Tests the expectation. True if it exactly
* references the held value.
* @param mixed $compare Comparison reference.
* @return boolean True if correct.
function test(&$compare) {
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
return "Reference expectation [" .
$this->dumper->describeValue($this->value) .
"]";
return "Reference expectation fails " .
$this->dumper->describeDifference($this->value, $compare);
* Overlays the generated message onto the stored user
* message. An additional message can be interjected.
* @param mixed $compare Comparison value.
* @param SimpleDumper $dumper For formatting the results.
* @return string Description of success
* Accessor for the dumper.
* @return SimpleDumper Current value dumper.
* Sets the value to compare against.
* @param mixed $value Test value to match.
* @param string $message Customised message on failure.
* Tests the expectation. True if it exactly
* matches the held value.
* @param mixed $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
return "Identical expectation [" .
$dumper->describeValue($this->getValue()) .
"]";
return "Identical expectation [" .
$dumper->describeValue($this->getValue()) .
$dumper->describeValue($compare) .
"] " .
* Sets the value to compare against.
* @param mixed $value Test value to match.
* @param string $message Customised message on failure.
* Tests the expectation. True if it differs from the
* @param mixed $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
return ! parent::test($compare);
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
return "Not identical expectation passes " .
return "Not identical expectation [" .
$dumper->describeValue($this->getValue()) .
"] matches";
* Test for a pattern using Perl regex rules.
* Sets the value to compare against.
* @param string $pattern Pattern to search for.
* @param string $message Customised message on failure.
$this->pattern =
$pattern;
* Accessor for the pattern.
* @return string Perl regex as string.
* Tests the expectation. True if the Perl regex
* matches the comparison value.
* @param string $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
$dumper->describeValue($compare) .
"]";
* Describes a pattern match including the string
* found and it's position.
* @param string $pattern Regex to match against.
* @param string $subject Subject to search.
$position =
strpos($subject, $matches[0]);
return "Pattern [$pattern] detected at character [$position] in [" .
$dumper->describeValue($subject) .
"] as [" .
$matches[0] .
"] in region [" .
$dumper->clipString($subject, 100, $position) .
"]";
* Fail if a pattern is detected within the
* Sets the reject pattern
* @param string $pattern Pattern to search for.
* @param string $message Customised message on failure.
* Tests the expectation. False if the Perl regex
* matches the comparison value.
* @param string $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
return ! parent::test($compare);
* Returns a human readable test message.
* @param string $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
$dumper->describeValue($compare) .
"]";
* Tests either type or class name if it's an object.
* Sets the type to compare with.
* @param string $type Type or class name.
* @param string $message Customised message on failure.
* Accessor for type to check against.
* @return string Type or class name.
* Tests the expectation. True if the type or
* class matches the string value.
* @param string $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
* Coerces type name into a gettype() match.
* @param string $type User type.
* @return string Simpler type.
if (isset
($map[$type])) {
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
return "Value [" .
$dumper->describeValue($compare) .
"] should be type [" .
$this->type .
"]";
* Tests either type or class name if it's an object.
* Will succeed if the type does not match.
* Sets the type to compare with.
* @param string $type Type or class name.
* @param string $message Customised message on failure.
* Tests the expectation. False if the type or
* class matches the string value.
* @param string $compare Comparison value.
* @return boolean True if different.
function test($compare) {
return ! parent::test($compare);
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
return "Value [" .
$dumper->describeValue($compare) .
"] should not be type [" .
$this->getType() .
"]";
* Tests for existance of a method in an object
* Sets the value to compare against.
* @param string $method Method to check.
* @param string $message Customised message on failure.
$this->method =
&$method;
* Tests the expectation. True if the method exists in the test object.
* @param string $compare Comparison method name.
* @return boolean True if correct.
function test($compare) {
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
return 'No method on non-object [' .
$dumper->describeValue($compare) .
']';
return "Object [" .
$dumper->describeValue($compare) .
"] should contain method [$method]";
* Compares an object member's value even if private.
* Sets the value to compare against.
* @param string $method Method to check.
* @param string $message Customised message on failure.
* Tests the expectation. True if the property value is identical.
* @param object $actual Comparison object.
* @return boolean True if identical.
return parent::test($this->getProperty($this->name, $actual));
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
return parent::testMessage($this->getProperty($this->name, $actual));
* Extracts the member value even if private using reflection.
* @param string $name Property name.
* @param object $object Object to read.
* @return mixed Value of property.
private function getProperty($name, $object) {
$reflection =
new ReflectionObject($object);
$property =
$reflection->getProperty($name);
$property->setAccessible(true);
return $property->getValue($object);
} catch
(ReflectionException $e) {
return $this->getPrivatePropertyNoMatterWhat($name, $object);
* Extracts a private member's value when reflection won't play ball.
* @param string $name Property name.
* @param object $object Object to read.
* @return mixed Value of property.
private function getPrivatePropertyNoMatterWhat($name, $object) {
foreach ((array)
$object as $mangled_name =>
$value) {
if ($this->unmangle($mangled_name) ==
$name) {
* Removes crud from property name after it's been converted
* @param string $mangled Name from array cast.
* @return string Cleaned up name.
$parts =
preg_split('/[^a-zA-Z0-9_\x7f-\xff]+/', $mangled);
Documentation generated on Thu, 01 Oct 2009 20:54:49 -0500 by phpDocumentor 1.4.2