From abd37e02c7c69f1f3f2d97ebc3c6002ea05d00fe Mon Sep 17 00:00:00 2001 From: Mal Graty Date: Thu, 30 Jul 2015 15:16:40 +0100 Subject: [PATCH 1/3] Revert "Allow use of ReferenceExpectation in expect calls" This reverts commit e7598f8d5e30e3e464ccd68dca5907600555d54f. Conflicts: expectation.php --- expectation.php | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/expectation.php b/expectation.php index b1f185a8..3ba471a0 100644 --- a/expectation.php +++ b/expectation.php @@ -477,15 +477,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 +514,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; + } } /** From 5d2b91df24382f0bc2a2005500a31233cab9de17 Mon Sep 17 00:00:00 2001 From: Mal Graty Date: Mon, 27 Jul 2015 15:42:32 +0100 Subject: [PATCH 2/3] Recognise ReferenceExpectation as Expectation ReferenceExpectation does not inherit from SimpleExpectation (it requires a slightly different method signature to force passing args ByRef) and as a result is not recognised by isExpectation. --- expectation.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/expectation.php b/expectation.php index 3ba471a0..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') ); } } From 907af38bb3981405ae9cad630739423523506b78 Mon Sep 17 00:00:00 2001 From: Mal Graty Date: Thu, 30 Jul 2015 20:29:36 +0100 Subject: [PATCH 3/3] Enable ReferenceExpectation test which now passes --- test/expectation_test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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)); }