3 * base include file for SimpleTest
5 * @subpackage UnitTester
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__
) . '/dumper.php');
13 require_once(dirname(__FILE__
) . '/compatibility.php');
17 * Assertion that can display failure information.
18 * Also includes various helper methods.
20 * @subpackage UnitTester
23 class SimpleExpectation
{
28 * Creates a dumper for displaying values and sets
30 * @param string $message Customised message on failure.
32 function SimpleExpectation($message = '%s') {
33 $this->_message
= $message;
37 * Tests the expectation. True if correct.
38 * @param mixed $compare Comparison value.
39 * @return boolean True if correct.
43 function test($compare) {
47 * Returns a human readable test message.
48 * @param mixed $compare Comparison value.
49 * @return string Description of success
54 function testMessage($compare) {
58 * Overlays the generated message onto the stored user
59 * message. An additional message can be interjected.
60 * @param mixed $compare Comparison value.
61 * @param SimpleDumper $dumper For formatting the results.
62 * @return string Description of success
66 function overlayMessage($compare, $dumper) {
67 $this->_dumper
= $dumper;
68 return sprintf($this->_message
, $this->testMessage($compare));
72 * Accessor for the dumper.
73 * @return SimpleDumper Current value dumper.
76 function &_getDumper() {
77 return $this->_dumper
;
81 * Test to see if a value is an expectation object.
82 * A useful utility method.
83 * @param mixed $expectation Hopefully an Epectation
85 * @return boolean True if descended from
90 function isExpectation($expectation) {
91 return is_object($expectation) &&
92 SimpleTestCompatibility
::isA($expectation, 'SimpleExpectation');
97 * A wildcard expectation always matches.
99 * @subpackage MockObjects
101 class AnythingExpectation
extends SimpleExpectation
{
104 * Tests the expectation. Always true.
105 * @param mixed $compare Ignored.
106 * @return boolean True.
109 function test($compare) {
114 * Returns a human readable test message.
115 * @param mixed $compare Comparison value.
116 * @return string Description of success
120 function testMessage($compare) {
121 $dumper = &$this->_getDumper();
122 return 'Anything always matches [' . $dumper->describeValue($compare) . ']';
127 * An expectation that passes on boolean true.
128 * @package SimpleTest
129 * @subpackage MockObjects
131 class TrueExpectation
extends SimpleExpectation
{
134 * Tests the expectation.
135 * @param mixed $compare Should be true.
136 * @return boolean True on match.
139 function test($compare) {
140 return (boolean
)$compare;
144 * Returns a human readable test message.
145 * @param mixed $compare Comparison value.
146 * @return string Description of success
150 function testMessage($compare) {
151 $dumper = &$this->_getDumper();
152 return 'Expected true, got [' . $dumper->describeValue($compare) . ']';
157 * An expectation that passes on boolean false.
158 * @package SimpleTest
159 * @subpackage MockObjects
161 class FalseExpectation
extends SimpleExpectation
{
164 * Tests the expectation.
165 * @param mixed $compare Should be false.
166 * @return boolean True on match.
169 function test($compare) {
170 return ! (boolean
)$compare;
174 * Returns a human readable test message.
175 * @param mixed $compare Comparison value.
176 * @return string Description of success
180 function testMessage($compare) {
181 $dumper = &$this->_getDumper();
182 return 'Expected false, got [' . $dumper->describeValue($compare) . ']';
188 * @package SimpleTest
189 * @subpackage UnitTester
191 class EqualExpectation
extends SimpleExpectation
{
195 * Sets the value to compare against.
196 * @param mixed $value Test value to match.
197 * @param string $message Customised message on failure.
200 function EqualExpectation($value, $message = '%s') {
201 $this->SimpleExpectation($message);
202 $this->_value
= $value;
206 * Tests the expectation. True if it matches the
208 * @param mixed $compare Comparison value.
209 * @return boolean True if correct.
212 function test($compare) {
213 return (($this->_value
== $compare) && ($compare == $this->_value
));
217 * Returns a human readable test message.
218 * @param mixed $compare Comparison value.
219 * @return string Description of success
223 function testMessage($compare) {
224 if ($this->test($compare)) {
225 return "Equal expectation [" . $this->_dumper
->describeValue($this->_value
) . "]";
227 return "Equal expectation fails " .
228 $this->_dumper
->describeDifference($this->_value
, $compare);
233 * Accessor for comparison value.
234 * @return mixed Held value to compare with.
237 function _getValue() {
238 return $this->_value
;
243 * Test for inequality.
244 * @package SimpleTest
245 * @subpackage UnitTester
247 class NotEqualExpectation
extends EqualExpectation
{
250 * Sets the value to compare against.
251 * @param mixed $value Test value to match.
252 * @param string $message Customised message on failure.
255 function NotEqualExpectation($value, $message = '%s') {
256 $this->EqualExpectation($value, $message);
260 * Tests the expectation. True if it differs from the
262 * @param mixed $compare Comparison value.
263 * @return boolean True if correct.
266 function test($compare) {
267 return ! parent
::test($compare);
271 * Returns a human readable test message.
272 * @param mixed $compare Comparison value.
273 * @return string Description of success
277 function testMessage($compare) {
278 $dumper = &$this->_getDumper();
279 if ($this->test($compare)) {
280 return "Not equal expectation passes " .
281 $dumper->describeDifference($this->_getValue(), $compare);
283 return "Not equal expectation fails [" .
284 $dumper->describeValue($this->_getValue()) .
291 * Test for being within a range.
292 * @package SimpleTest
293 * @subpackage UnitTester
295 class WithinMarginExpectation
extends SimpleExpectation
{
300 * Sets the value to compare against and the fuzziness of
301 * the match. Used for comparing floating point values.
302 * @param mixed $value Test value to match.
303 * @param mixed $margin Fuzziness of match.
304 * @param string $message Customised message on failure.
307 function WithinMarginExpectation($value, $margin, $message = '%s') {
308 $this->SimpleExpectation($message);
309 $this->_upper
= $value +
$margin;
310 $this->_lower
= $value - $margin;
314 * Tests the expectation. True if it matches the
316 * @param mixed $compare Comparison value.
317 * @return boolean True if correct.
320 function test($compare) {
321 return (($compare <= $this->_upper
) && ($compare >= $this->_lower
));
325 * Returns a human readable test message.
326 * @param mixed $compare Comparison value.
327 * @return string Description of success
331 function testMessage($compare) {
332 if ($this->test($compare)) {
333 return $this->_withinMessage($compare);
335 return $this->_outsideMessage($compare);
340 * Creates a the message for being within the range.
341 * @param mixed $compare Value being tested.
344 function _withinMessage($compare) {
345 return "Within expectation [" . $this->_dumper
->describeValue($this->_lower
) . "] and [" .
346 $this->_dumper
->describeValue($this->_upper
) . "]";
350 * Creates a the message for being within the range.
351 * @param mixed $compare Value being tested.
354 function _outsideMessage($compare) {
355 if ($compare > $this->_upper
) {
356 return "Outside expectation " .
357 $this->_dumper
->describeDifference($compare, $this->_upper
);
359 return "Outside expectation " .
360 $this->_dumper
->describeDifference($compare, $this->_lower
);
366 * Test for being outside of a range.
367 * @package SimpleTest
368 * @subpackage UnitTester
370 class OutsideMarginExpectation
extends WithinMarginExpectation
{
373 * Sets the value to compare against and the fuzziness of
374 * the match. Used for comparing floating point values.
375 * @param mixed $value Test value to not match.
376 * @param mixed $margin Fuzziness of match.
377 * @param string $message Customised message on failure.
380 function OutsideMarginExpectation($value, $margin, $message = '%s') {
381 $this->WithinMarginExpectation($value, $margin, $message);
385 * Tests the expectation. True if it matches the
387 * @param mixed $compare Comparison value.
388 * @return boolean True if correct.
391 function test($compare) {
392 return ! parent
::test($compare);
396 * Returns a human readable test message.
397 * @param mixed $compare Comparison value.
398 * @return string Description of success
402 function testMessage($compare) {
403 if (! $this->test($compare)) {
404 return $this->_withinMessage($compare);
406 return $this->_outsideMessage($compare);
413 * @package SimpleTest
414 * @subpackage UnitTester
416 class IdenticalExpectation
extends EqualExpectation
{
419 * Sets the value to compare against.
420 * @param mixed $value Test value to match.
421 * @param string $message Customised message on failure.
424 function IdenticalExpectation($value, $message = '%s') {
425 $this->EqualExpectation($value, $message);
429 * Tests the expectation. True if it exactly
430 * matches the held value.
431 * @param mixed $compare Comparison value.
432 * @return boolean True if correct.
435 function test($compare) {
436 return SimpleTestCompatibility
::isIdentical($this->_getValue(), $compare);
440 * Returns a human readable test message.
441 * @param mixed $compare Comparison value.
442 * @return string Description of success
446 function testMessage($compare) {
447 $dumper = &$this->_getDumper();
448 if ($this->test($compare)) {
449 return "Identical expectation [" . $dumper->describeValue($this->_getValue()) . "]";
451 return "Identical expectation [" . $dumper->describeValue($this->_getValue()) .
453 $dumper->describeValue($compare) . "] " .
454 $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS
);
460 * Test for non-identity.
461 * @package SimpleTest
462 * @subpackage UnitTester
464 class NotIdenticalExpectation
extends IdenticalExpectation
{
467 * Sets the value to compare against.
468 * @param mixed $value Test value to match.
469 * @param string $message Customised message on failure.
472 function NotIdenticalExpectation($value, $message = '%s') {
473 $this->IdenticalExpectation($value, $message);
477 * Tests the expectation. True if it differs from the
479 * @param mixed $compare Comparison value.
480 * @return boolean True if correct.
483 function test($compare) {
484 return ! parent
::test($compare);
488 * Returns a human readable test message.
489 * @param mixed $compare Comparison value.
490 * @return string Description of success
494 function testMessage($compare) {
495 $dumper = &$this->_getDumper();
496 if ($this->test($compare)) {
497 return "Not identical expectation passes " .
498 $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS
);
500 return "Not identical expectation [" . $dumper->describeValue($this->_getValue()) . "] matches";
506 * Test for a pattern using Perl regex rules.
507 * @package SimpleTest
508 * @subpackage UnitTester
510 class PatternExpectation
extends SimpleExpectation
{
514 * Sets the value to compare against.
515 * @param string $pattern Pattern to search for.
516 * @param string $message Customised message on failure.
519 function PatternExpectation($pattern, $message = '%s') {
520 $this->SimpleExpectation($message);
521 $this->_pattern
= $pattern;
525 * Accessor for the pattern.
526 * @return string Perl regex as string.
529 function _getPattern() {
530 return $this->_pattern
;
534 * Tests the expectation. True if the Perl regex
535 * matches the comparison value.
536 * @param string $compare Comparison value.
537 * @return boolean True if correct.
540 function test($compare) {
541 return (boolean
)preg_match($this->_getPattern(), $compare);
545 * Returns a human readable test message.
546 * @param mixed $compare Comparison value.
547 * @return string Description of success
551 function testMessage($compare) {
552 if ($this->test($compare)) {
553 return $this->_describePatternMatch($this->_getPattern(), $compare);
555 $dumper = &$this->_getDumper();
556 return "Pattern [" . $this->_getPattern() .
557 "] not detected in [" .
558 $dumper->describeValue($compare) . "]";
563 * Describes a pattern match including the string
564 * found and it's position.
565 * @package SimpleTest
566 * @subpackage UnitTester
567 * @param string $pattern Regex to match against.
568 * @param string $subject Subject to search.
571 function _describePatternMatch($pattern, $subject) {
572 preg_match($pattern, $subject, $matches);
573 $position = strpos($subject, $matches[0]);
574 $dumper = $this->_getDumper();
575 return "Pattern [$pattern] detected at character [$position] in [" .
576 $dumper->describeValue($subject) . "] as [" .
577 $matches[0] . "] in region [" .
578 $dumper->clipString($subject, 100, $position) . "]";
585 class WantedPatternExpectation
extends PatternExpectation
{
589 * Fail if a pattern is detected within the
591 * @package SimpleTest
592 * @subpackage UnitTester
594 class NoPatternExpectation
extends PatternExpectation
{
597 * Sets the reject pattern
598 * @param string $pattern Pattern to search for.
599 * @param string $message Customised message on failure.
602 function NoPatternExpectation($pattern, $message = '%s') {
603 $this->PatternExpectation($pattern, $message);
607 * Tests the expectation. False if the Perl regex
608 * matches the comparison value.
609 * @param string $compare Comparison value.
610 * @return boolean True if correct.
613 function test($compare) {
614 return ! parent
::test($compare);
618 * Returns a human readable test message.
619 * @param string $compare Comparison value.
620 * @return string Description of success
624 function testMessage($compare) {
625 if ($this->test($compare)) {
626 $dumper = &$this->_getDumper();
627 return "Pattern [" . $this->_getPattern() .
628 "] not detected in [" .
629 $dumper->describeValue($compare) . "]";
631 return $this->_describePatternMatch($this->_getPattern(), $compare);
637 * @package SimpleTest
638 * @subpackage UnitTester
641 class UnwantedPatternExpectation
extends NoPatternExpectation
{
645 * Tests either type or class name if it's an object.
646 * @package SimpleTest
647 * @subpackage UnitTester
649 class IsAExpectation
extends SimpleExpectation
{
653 * Sets the type to compare with.
654 * @param string $type Type or class name.
655 * @param string $message Customised message on failure.
658 function IsAExpectation($type, $message = '%s') {
659 $this->SimpleExpectation($message);
660 $this->_type
= $type;
664 * Accessor for type to check against.
665 * @return string Type or class name.
668 function _getType() {
673 * Tests the expectation. True if the type or
674 * class matches the string value.
675 * @param string $compare Comparison value.
676 * @return boolean True if correct.
679 function test($compare) {
680 if (is_object($compare)) {
681 return SimpleTestCompatibility
::isA($compare, $this->_type
);
683 return (strtolower(gettype($compare)) == $this->_canonicalType($this->_type
));
688 * Coerces type name into a gettype() match.
689 * @param string $type User type.
690 * @return string Simpler type.
693 function _canonicalType($type) {
694 $type = strtolower($type);
700 if (isset($map[$type])) {
707 * Returns a human readable test message.
708 * @param mixed $compare Comparison value.
709 * @return string Description of success
713 function testMessage($compare) {
714 $dumper = &$this->_getDumper();
715 return "Value [" . $dumper->describeValue($compare) .
716 "] should be type [" . $this->_type
. "]";
721 * Tests either type or class name if it's an object.
722 * Will succeed if the type does not match.
723 * @package SimpleTest
724 * @subpackage UnitTester
726 class NotAExpectation
extends IsAExpectation
{
730 * Sets the type to compare with.
731 * @param string $type Type or class name.
732 * @param string $message Customised message on failure.
735 function NotAExpectation($type, $message = '%s') {
736 $this->IsAExpectation($type, $message);
740 * Tests the expectation. False if the type or
741 * class matches the string value.
742 * @param string $compare Comparison value.
743 * @return boolean True if different.
746 function test($compare) {
747 return ! parent
::test($compare);
751 * Returns a human readable test message.
752 * @param mixed $compare Comparison value.
753 * @return string Description of success
757 function testMessage($compare) {
758 $dumper = &$this->_getDumper();
759 return "Value [" . $dumper->describeValue($compare) .
760 "] should not be type [" . $this->_getType() . "]";
765 * Tests for existance of a method in an object
766 * @package SimpleTest
767 * @subpackage UnitTester
769 class MethodExistsExpectation
extends SimpleExpectation
{
773 * Sets the value to compare against.
774 * @param string $method Method to check.
775 * @param string $message Customised message on failure.
779 function MethodExistsExpectation($method, $message = '%s') {
780 $this->SimpleExpectation($message);
781 $this->_method
= &$method;
785 * Tests the expectation. True if the method exists in the test object.
786 * @param string $compare Comparison method name.
787 * @return boolean True if correct.
790 function test($compare) {
791 return (boolean
)(is_object($compare) && method_exists($compare, $this->_method
));
795 * Returns a human readable test message.
796 * @param mixed $compare Comparison value.
797 * @return string Description of success
801 function testMessage($compare) {
802 $dumper = &$this->_getDumper();
803 if (! is_object($compare)) {
804 return 'No method on non-object [' . $dumper->describeValue($compare) . ']';
806 $method = $this->_method
;
807 return "Object [" . $dumper->describeValue($compare) .
808 "] should contain method [$method]";