Skip to content

Commit 53538f0

Browse files
authored
Merge pull request #655 from stof/refactor_comparator
Refactor the ProphecyComparator to use composition
2 parents 8cce4df + 88f58bc commit 53538f0

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

spec/Prophecy/Comparator/ProphecyComparatorSpec.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace spec\Prophecy\Comparator;
44

55
use PhpSpec\ObjectBehavior;
6-
use Prophecy\Argument;
76
use Prophecy\Prophet;
7+
use SebastianBergmann\Comparator\Comparator;
8+
use SebastianBergmann\Comparator\Factory;
89

910
class ProphecyComparatorSpec extends ObjectBehavior
1011
{
1112
function it_is_a_comparator()
1213
{
13-
$this->shouldHaveType('SebastianBergmann\Comparator\ObjectComparator');
14+
$this->shouldHaveType(Comparator::class);
1415
}
1516

1617
function it_accepts_only_prophecy_objects()
@@ -35,6 +36,9 @@ function it_asserts_that_an_object_is_equal_to_its_revealed_prophecy()
3536
$prophecy = $prophet->prophesize('Prophecy\Prophecy\ObjectProphecy');
3637
$prophecy->__call('reveal', array())->willReturn(new \stdClass());
3738

39+
$factory = new Factory();
40+
$factory->register($this->getWrappedObject());
41+
3842
$this->shouldNotThrow()->duringAssertEquals($prophecy->reveal(), $prophecy);
3943
}
4044
}

src/Prophecy/Comparator/ProphecyComparator.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@
1212
namespace Prophecy\Comparator;
1313

1414
use Prophecy\Prophecy\ProphecyInterface;
15+
use SebastianBergmann\Comparator\Comparator;
16+
use SebastianBergmann\Comparator\Factory;
1517
use SebastianBergmann\Comparator\ObjectComparator;
1618

1719
/**
1820
* @final
1921
*/
20-
class ProphecyComparator extends ObjectComparator
22+
class ProphecyComparator extends Comparator
2123
{
2224
/**
2325
* @param mixed $expected
2426
* @param mixed $actual
2527
*/
2628
public function accepts($expected, $actual): bool
2729
{
28-
return is_object($expected) && is_object($actual) && $actual instanceof ProphecyInterface;
30+
return \is_object($expected) && $actual instanceof ProphecyInterface;
2931
}
3032

3133
/**
@@ -34,11 +36,23 @@ public function accepts($expected, $actual): bool
3436
* @param float $delta
3537
* @param bool $canonicalize
3638
* @param bool $ignoreCase
37-
* @param mixed[] $processed
3839
*/
39-
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array()): void
40+
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false): void
4041
{
4142
\assert($actual instanceof ProphecyInterface);
42-
parent::assertEquals($expected, $actual->reveal(), $delta, $canonicalize, $ignoreCase, $processed);
43+
$this->getComparatorFactory()->getComparatorFor($expected, $actual->reveal())->assertEquals($expected, $actual->reveal(), $delta, $canonicalize, $ignoreCase);
44+
}
45+
46+
private function getComparatorFactory(): Factory
47+
{
48+
// sebastianbergmann/comparator 5+
49+
// @phpstan-ignore function.alreadyNarrowedType
50+
if (\method_exists($this, 'factory')) {
51+
return $this->factory();
52+
}
53+
54+
// sebastianbergmann/comparator <5
55+
// @phpstan-ignore property.private
56+
return $this->factory;
4357
}
4458
}

0 commit comments

Comments
 (0)