Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reference expectation #11

Merged
merged 3 commits into from
Sep 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 47 additions & 5 deletions expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/expectation_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ class DummyReferencedObject

class TestOfReference extends UnitTestCase
{
public function TODO_testReference()
public function testReference()
{
$foo = "foo";
$ref = &$foo;
$not_ref = $foo;
$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));
}
Expand Down