From fcbffe210a81f846f727400a97d910f249c4e4aa Mon Sep 17 00:00:00 2001 From: Fabien Villepinte Date: Mon, 29 Jun 2020 11:59:57 +0200 Subject: [PATCH] Fix copying objects extending ArrayObject (#152) (#155) --- fixtures/f011/ArrayObjectExtended.php | 16 ++++++++++++++++ .../TypeFilter/Spl/ArrayObjectFilter.php | 12 ++++++------ tests/DeepCopyTest/DeepCopyTest.php | 19 ++++++++++++++++++- .../TypeFilter/Spl/ArrayObjectFilterTest.php | 4 ++-- 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 fixtures/f011/ArrayObjectExtended.php diff --git a/fixtures/f011/ArrayObjectExtended.php b/fixtures/f011/ArrayObjectExtended.php new file mode 100644 index 0000000..f2d17e8 --- /dev/null +++ b/fixtures/f011/ArrayObjectExtended.php @@ -0,0 +1,16 @@ +x = $x; + } +} diff --git a/src/DeepCopy/TypeFilter/Spl/ArrayObjectFilter.php b/src/DeepCopy/TypeFilter/Spl/ArrayObjectFilter.php index dbc25a5..1784601 100644 --- a/src/DeepCopy/TypeFilter/Spl/ArrayObjectFilter.php +++ b/src/DeepCopy/TypeFilter/Spl/ArrayObjectFilter.php @@ -1,7 +1,6 @@ copier->copy($arrayObject->getArrayCopy()), - $arrayObject->getFlags(), - $arrayObject->getIteratorClass() - ); + $clone = clone $arrayObject; + foreach ($arrayObject->getArrayCopy() as $k => $v) { + $clone->offsetSet($k, $this->copier->copy($v)); + } + + return $clone; } } diff --git a/tests/DeepCopyTest/DeepCopyTest.php b/tests/DeepCopyTest/DeepCopyTest.php index 4a9e924..a079c23 100644 --- a/tests/DeepCopyTest/DeepCopyTest.php +++ b/tests/DeepCopyTest/DeepCopyTest.php @@ -18,6 +18,7 @@ use DeepCopy\f007; use DeepCopy\f008; use DeepCopy\f009; +use DeepCopy\f011; use DeepCopy\Filter\KeepFilter; use DeepCopy\Filter\SetNullFilter; use DeepCopy\Matcher\PropertyNameMatcher; @@ -391,7 +392,7 @@ public function test_it_can_deep_copy_an_array_object() { $foo = new f003\Foo('foo'); $foo->setProp('bar'); - $object = new ArrayObject(['foo' => $foo, \ArrayObject::ARRAY_AS_PROPS, \RecursiveArrayIterator::class]); + $object = new ArrayObject(['foo' => $foo, ArrayObject::ARRAY_AS_PROPS, \RecursiveArrayIterator::class]); $copy = deep_copy($object); @@ -399,6 +400,22 @@ public function test_it_can_deep_copy_an_array_object() $this->assertEqualButNotSame($foo, $copy['foo']); } + /** + * @ticket https://github.com/myclabs/DeepCopy/issues/152 + */ + public function test_it_clones_objects_extending_array_object() + { + $object = new f011\ArrayObjectExtended('foo'); + $object->setFlags(ArrayObject::ARRAY_AS_PROPS); + $object->setIteratorClass(\RecursiveArrayIterator::class); + $object['a'] = new f011\ArrayObjectExtended('bar'); + + $copy = deep_copy($object); + + $this->assertEqualButNotSame($object, $copy); + $this->assertEqualButNotSame($object['a'], $copy['a']); + } + /** * @ticket https://github.com/myclabs/DeepCopy/pull/49 */ diff --git a/tests/DeepCopyTest/TypeFilter/Spl/ArrayObjectFilterTest.php b/tests/DeepCopyTest/TypeFilter/Spl/ArrayObjectFilterTest.php index b16449a..12cf81d 100644 --- a/tests/DeepCopyTest/TypeFilter/Spl/ArrayObjectFilterTest.php +++ b/tests/DeepCopyTest/TypeFilter/Spl/ArrayObjectFilterTest.php @@ -37,11 +37,11 @@ protected function setUp(): void public function test_it_deep_copies_an_array_object(): void { $arrayObject = new ArrayObject(['foo' => 'bar'], ArrayObject::ARRAY_AS_PROPS, RecursiveArrayIterator::class); - $this->copierProphecy->copy(['foo' => 'bar'])->willReturn(['copy' => 'bar']); + $this->copierProphecy->copy('bar')->willReturn('baz'); /** @var \ArrayObject $newArrayObject */ $newArrayObject = $this->arrayObjectFilter->apply($arrayObject); - $this->assertSame(['copy' => 'bar'], $newArrayObject->getArrayCopy()); + $this->assertSame(['foo' => 'baz'], $newArrayObject->getArrayCopy()); $this->assertSame(ArrayObject::ARRAY_AS_PROPS, $newArrayObject->getFlags()); $this->assertSame(RecursiveArrayIterator::class, $newArrayObject->getIteratorClass()); }