diff --git a/expectation.php b/expectation.php index b1f185a8..83549310 100644 --- a/expectation.php +++ b/expectation.php @@ -98,7 +98,8 @@ protected function getDumper() public static function isExpectation($expectation) { return is_object($expectation) && ( - is_a($expectation, 'SimpleExpectation') + is_a($expectation, 'SimpleExpectation') || + is_a($expectation, 'ReferenceExpectation') ); } } @@ -477,15 +478,29 @@ public function testMessage($compare) * @package SimpleTest * @subpackage UnitTester */ -class ReferenceExpectation extends IdenticalExpectation +class ReferenceExpectation { + private $value; + + /** + * Sets the reference value to compare against. + * @param mixed $value Test reference to match. + * @param string $message Customised message on failure. + * @access public + */ + public function __construct(&$value, $message = '%s') + { + $this->message = $message; + $this->value = &$value; + } + /** * Tests the expectation. True if it exactly references the held value. * @param mixed $compare Comparison reference. * @return boolean True if correct. * @access public */ - public function test($compare) + public function test(&$compare) { return SimpleTestCompatibility::isReference($this->value, $compare); } @@ -500,12 +515,39 @@ public function test($compare) public function testMessage($compare) { if ($this->test($compare)) { - return "Reference expectation [" . $this->dumper->describeValue($this->getValue()) . "]"; + return "Reference expectation [" . $this->dumper->describeValue($this->value) . "]"; } else { return "Reference expectation fails " . - $this->dumper->describeDifference($this->getValue(), $compare); + $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 + * or failure. + * @access public + */ + public function overlayMessage($compare, $dumper) { + $this->dumper = $dumper; + return sprintf($this->message, $this->testMessage($compare)); + } + + /** + * Accessor for the dumper. + * @return SimpleDumper Current value dumper. + * @access protected + */ + protected function getDumper() { + if (! $this->dumper) { + $dumper = new SimpleDumper(); + return $dumper; + } + return $this->dumper; + } } /** diff --git a/test/expectation_test.php b/test/expectation_test.php index dce0156d..f2c373de 100644 --- a/test/expectation_test.php +++ b/test/expectation_test.php @@ -212,7 +212,7 @@ class DummyReferencedObject class TestOfReference extends UnitTestCase { - public function TODO_testReference() + public function testReference() { $foo = "foo"; $ref = &$foo; @@ -220,7 +220,7 @@ public function TODO_testReference() $bar = "bar"; $expect = new ReferenceExpectation($foo); - $this->assertTrue($expect->test($ref)); // fails + $this->assertTrue($expect->test($ref)); $this->assertFalse($expect->test($not_ref)); $this->assertFalse($expect->test($bar)); }