Skip to content

Commit

Permalink
Fix cloning of datetimes (#43)
Browse files Browse the repository at this point in the history
* Fix cloning of datetimes

* update tests

* php54 compat
  • Loading branch information
theofidry authored and mnapoli committed Sep 6, 2016
1 parent 752a382 commit da85297
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/DeepCopy/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ private function copyObject($object)
$newObject = clone $object;
$this->hashMap[$objectHash] = $newObject;

if ($newObject instanceof \DateTimeInterface) {
return $newObject;
}
foreach (ReflectionHelper::getProperties($reflectedObject) as $property) {
$this->copyObjectProperty($newObject, $property);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public function testPropertyObjectCopy()
$this->assertDeepCopyOf($o, $deepCopy->copy($o));
}

public function testPropertyObjectCopyWithDateTimes()
{
$o = new A();
$o->date1 = new \DateTime();
if (class_exists('DateTimeImmutable')) {
$o->date2 = new \DateTimeImmutable();
}

$deepCopy = new DeepCopy();
$c = $deepCopy->copy($o);

$this->assertDeepCopyOf($o, $c);

$c->date1->setDate(2015, 01, 04);
$this->assertNotEquals($c->date1, $o->date1);
}

public function testPrivatePropertyOfParentObjectCopy()
{
$o = new E();
Expand Down

0 comments on commit da85297

Please sign in to comment.