From 80b379dcb52f15a472f0f2833f2da1622500365b Mon Sep 17 00:00:00 2001 From: Leszek Manicki Date: Fri, 6 Apr 2018 12:22:22 +0200 Subject: [PATCH] ListPatcher: use non-strict comparison when removing objects --- src/Patcher/ListPatcher.php | 3 ++- tests/phpunit/Patcher/ListPatcherTest.php | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Patcher/ListPatcher.php b/src/Patcher/ListPatcher.php index 98327ec..981e896 100644 --- a/src/Patcher/ListPatcher.php +++ b/src/Patcher/ListPatcher.php @@ -44,7 +44,8 @@ public function patch( array $base, Diff $diff ): array { if ( $diffOp instanceof DiffOpAdd ) { $base[] = $diffOp->getNewValue(); } elseif ( $diffOp instanceof DiffOpRemove ) { - $key = array_search( $diffOp->getOldValue(), $base, true ); + $needle = $diffOp->getOldValue(); + $key = array_search( $needle, $base, !is_object( $needle ) ); if ( $key === false ) { $this->handleError( 'Cannot remove an element from a list if it is not present' ); diff --git a/tests/phpunit/Patcher/ListPatcherTest.php b/tests/phpunit/Patcher/ListPatcherTest.php index 5a85658..e93eb54 100644 --- a/tests/phpunit/Patcher/ListPatcherTest.php +++ b/tests/phpunit/Patcher/ListPatcherTest.php @@ -10,6 +10,7 @@ use Diff\Patcher\ListPatcher; use Diff\Patcher\Patcher; use Diff\Tests\DiffTestCase; +use stdClass; /** * @covers Diff\Patcher\ListPatcher @@ -91,9 +92,28 @@ public function patchProvider() { $argLists[] = array( $patcher, $base, $diff, $expected ); + $patcher = new ListPatcher(); + $base = array( + $this->newObject( 'foo' ), + $this->newObject( 'bar' ), + ); + $diff = new Diff( array( + new DiffOpRemove( $this->newObject( 'foo') ), + new DiffOpAdd( $this->newObject( 'baz' ) ), + ) ); + $expected = array( $this->newObject( 'bar' ), $this->newObject( 'baz' ) ); + + $argLists[] = array( $patcher, $base, $diff, $expected ); + return $argLists; } + private function newObject( $value ) : stdClass { + $object = new stdClass(); + $object->element = $value; + return $object; + } + /** * @dataProvider patchProvider *