Skip to content

Commit

Permalink
Merge pull request #460 from ciaranmcnulty/callable-matching
Browse files Browse the repository at this point in the history
Compare callables as an exact match if they are identical
  • Loading branch information
ciaranmcnulty authored Dec 22, 2019
2 parents 7e810ce + 6bfa26f commit 422f3f3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
6 changes: 3 additions & 3 deletions spec/Prophecy/Comparator/ClosureComparatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ function it_accepts_only_closures()
$this->accepts(function(){}, function(){})->shouldReturn(true);
}

function it_asserts_that_all_closures_are_different()
function it_asserts_that_different_closures_are_different()
{
$this->shouldThrow()->duringAssertEquals(function(){}, function(){});
}

function it_asserts_that_all_closures_are_different_even_if_its_the_same_closure()
function it_asserts_that_closures_are_equal_if_its_the_same_closure()
{
$closure = function(){};

$this->shouldThrow()->duringAssertEquals($closure, $closure);
$this->shouldNotThrow()->duringAssertEquals($closure, $closure);
}
}
20 changes: 11 additions & 9 deletions src/Prophecy/Comparator/ClosureComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public function accepts($expected, $actual)

public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array())
{
throw new ComparisonFailure(
$expected,
$actual,
// we don't need a diff
'',
'',
false,
'all closures are born different'
);
if ($expected !== $actual) {
throw new ComparisonFailure(
$expected,
$actual,
// we don't need a diff
'',
'',
false,
'all closures are different if not identical'
);
}
}
}
13 changes: 11 additions & 2 deletions tests/Argument/Token/ExactValueTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public function scores_10_for_objects_with_same_fields() {
self::assertEquals(10, $exactValueToken->scoreArgument($child2));
}

/**
* @test
*/
public function scores_10_for_matching_callables() {
$callable = function() {};

$exactValueToken = new ExactValueToken($callable);
self::assertEquals(10, $exactValueToken->scoreArgument($callable));
}

/**
* @test
*/
Expand Down Expand Up @@ -69,7 +79,6 @@ public function scores_false_for_object_and_null() {
$exactValueToken = new ExactValueToken($child1);
self::assertEquals(false, $exactValueToken->scoreArgument(null));
}

}


Expand All @@ -93,4 +102,4 @@ public function __construct($name, $parent) {
$this->parent->addChild($this);
}

}
}

0 comments on commit 422f3f3

Please sign in to comment.