|
5 | 5 | use Doctrine\Common\Collections\ArrayCollection; |
6 | 6 | use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
7 | 7 | use Doctrine\ODM\MongoDB\PersistentCollection; |
| 8 | +use Documents\User; |
| 9 | +use MongoId; |
| 10 | +use stdClass; |
8 | 11 |
|
9 | 12 | class PersistentCollectionTest extends BaseTest |
10 | 13 | { |
@@ -68,6 +71,54 @@ public function testGetTypeClassWorksAfterUnserialization() |
68 | 71 | $this->assertInstanceOf(ClassMetadata::class, $unserialized->getTypeClass()); |
69 | 72 | } |
70 | 73 |
|
| 74 | + public function testMongoDataIsPreservedDuringSerialization() |
| 75 | + { |
| 76 | + $mongoData = [ |
| 77 | + [ |
| 78 | + '$ref' => 'group', |
| 79 | + '$id' => new MongoId() |
| 80 | + ], |
| 81 | + [ |
| 82 | + '$ref' => 'group', |
| 83 | + '$id' => new MongoId() |
| 84 | + ], |
| 85 | + ]; |
| 86 | + |
| 87 | + $collection = new PersistentCollection(new ArrayCollection(), $this->dm, $this->uow); |
| 88 | + $collection->setMongoData($mongoData); |
| 89 | + |
| 90 | + $serialized = serialize($collection); |
| 91 | + /** @var PersistentCollection $unserialized */ |
| 92 | + $unserialized = unserialize($serialized); |
| 93 | + |
| 94 | + $unserialized->setOwner(new \Documents\User(), $this->dm->getClassMetadata(User::class)->getFieldMapping('groups')); |
| 95 | + $unserialized->setDocumentManager($this->dm); |
| 96 | + |
| 97 | + $this->assertCount(2, $unserialized->getMongoData()); |
| 98 | + } |
| 99 | + |
| 100 | + public function testSnapshotIsPreservedDuringSerialization() |
| 101 | + { |
| 102 | + $collection = new PersistentCollection(new ArrayCollection(), $this->dm, $this->uow); |
| 103 | + $collection->add(new stdClass()); |
| 104 | + $collection->takeSnapshot(); |
| 105 | + |
| 106 | + $this->assertCount(1, $collection->getSnapshot()); |
| 107 | + $this->assertFalse($collection->isDirty()); |
| 108 | + $this->assertCount(1, $collection->unwrap()); |
| 109 | + |
| 110 | + $serialized = serialize($collection); |
| 111 | + /** @var PersistentCollection $unserialized */ |
| 112 | + $unserialized = unserialize($serialized); |
| 113 | + |
| 114 | + $unserialized->setOwner(new \Documents\User(), $this->dm->getClassMetadata(User::class)->getFieldMapping('groups')); |
| 115 | + $unserialized->setDocumentManager($this->dm); |
| 116 | + |
| 117 | + $this->assertCount(1, $unserialized->getSnapshot()); |
| 118 | + $this->assertFalse($unserialized->isDirty()); |
| 119 | + $this->assertCount(1, $unserialized->unwrap()); |
| 120 | + } |
| 121 | + |
71 | 122 | /** |
72 | 123 | * @param array $expected |
73 | 124 | * @param array $snapshot |
|
0 commit comments