From e9e0970d95037b00fe980bb71877fc6c4f84f0c2 Mon Sep 17 00:00:00 2001 From: scroach Date: Mon, 7 Oct 2019 20:32:34 +0200 Subject: [PATCH] Fix nesting error when comparing objects #268 --- .../Argument/Token/ExactValueToken.php | 4 +- tests/Argument/Token/ExactValueTokenTest.php | 96 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/Argument/Token/ExactValueTokenTest.php diff --git a/src/Prophecy/Argument/Token/ExactValueToken.php b/src/Prophecy/Argument/Token/ExactValueToken.php index aa960f3fb..045a1b90f 100644 --- a/src/Prophecy/Argument/Token/ExactValueToken.php +++ b/src/Prophecy/Argument/Token/ExactValueToken.php @@ -59,7 +59,9 @@ public function scoreArgument($argument) try { $comparator->assertEquals($argument, $this->value); return 10; - } catch (ComparisonFailure $failure) {} + } catch (ComparisonFailure $failure) { + return false; + } } // If either one is an object it should be castable to a string diff --git a/tests/Argument/Token/ExactValueTokenTest.php b/tests/Argument/Token/ExactValueTokenTest.php new file mode 100644 index 000000000..12be30f30 --- /dev/null +++ b/tests/Argument/Token/ExactValueTokenTest.php @@ -0,0 +1,96 @@ +scoreArgument($child2)); + } + + /** + * @test + */ + public function scores_10_for_objects_with_same_fields() { + $child1 = new ChildClass('A', new ParentClass()); + $child2 = new ChildClass('A', new ParentClass()); + + $exactValueToken = new ExactValueToken($child1); + self::assertEquals(10, $exactValueToken->scoreArgument($child2)); + } + + /** + * @test + */ + public function scores_false_for_object_and_string() { + $child1 = new ChildClass('A', new ParentClass()); + + $exactValueToken = new ExactValueToken($child1); + self::assertEquals(false, $exactValueToken->scoreArgument("A")); + } + + /** + * @test + */ + public function scores_false_for_object_and_int() { + $child1 = new ChildClass('A', new ParentClass()); + + $exactValueToken = new ExactValueToken($child1); + self::assertEquals(false, $exactValueToken->scoreArgument(100)); + } + + /** + * @test + */ + public function scores_false_for_object_and_stdclass() { + $child1 = new ChildClass('A', new ParentClass()); + + $exactValueToken = new ExactValueToken($child1); + self::assertEquals(false, $exactValueToken->scoreArgument(new \stdClass())); + } + + /** + * @test + */ + public function scores_false_for_object_and_null() { + $child1 = new ChildClass('A', new ParentClass()); + + $exactValueToken = new ExactValueToken($child1); + self::assertEquals(false, $exactValueToken->scoreArgument(null)); + } + +} + + +class ParentClass { + + public $children = []; + + public function addChild(ChildClass $child) { + $this->children[] = $child; + } +} + +class ChildClass { + + public $parent = null; + public $name = null; + + public function __construct(string $name, ParentClass $parent) { + $this->name = $name; + $this->parent = $parent; + $this->parent->addChild($this); + } + +} \ No newline at end of file