|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Doctrine\ODM\MongoDB\Tests\Functional; |
| 4 | + |
| 5 | +use Doctrine\ODM\MongoDB\DocumentManager; |
| 6 | +use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
| 7 | +use Doctrine\ODM\MongoDB\Tests\BaseTest; |
| 8 | + |
| 9 | +class SplObjectHashCollisionsTest extends BaseTest |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @dataProvider provideParentAssociationsIsCleared |
| 13 | + */ |
| 14 | + public function testParentAssociationsIsCleared($f) |
| 15 | + { |
| 16 | + $d = new SplColDoc(); |
| 17 | + $d->one = new SplColEmbed('d.one.v1'); |
| 18 | + $d->many[] = new SplColEmbed('d.many.0.v1'); |
| 19 | + $d->many[] = new SplColEmbed('d.many.1.v1'); |
| 20 | + |
| 21 | + $this->dm->persist($d); |
| 22 | + $this->expectCount('parentAssociations', 3); |
| 23 | + $this->expectCount('embeddedDocumentsRegistry', 3); |
| 24 | + $f($this->dm, $d); |
| 25 | + $this->expectCount('parentAssociations', 0); |
| 26 | + $this->expectCount('embeddedDocumentsRegistry', 0); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @dataProvider provideParentAssociationsIsCleared |
| 31 | + */ |
| 32 | + public function testParentAssociationsLeftover($f, $leftover) |
| 33 | + { |
| 34 | + $d = new SplColDoc(); |
| 35 | + $d->one = new SplColEmbed('d.one.v1'); |
| 36 | + $d->many[] = new SplColEmbed('d.many.0.v1'); |
| 37 | + $d->many[] = new SplColEmbed('d.many.1.v1'); |
| 38 | + $this->dm->persist($d); |
| 39 | + $d->one = new SplColEmbed('d.one.v2'); |
| 40 | + $this->dm->flush(); |
| 41 | + |
| 42 | + $this->expectCount('parentAssociations', 4); |
| 43 | + $this->expectCount('embeddedDocumentsRegistry', 4); |
| 44 | + $f($this->dm, $d); |
| 45 | + $this->expectCount('parentAssociations', $leftover); |
| 46 | + $this->expectCount('embeddedDocumentsRegistry', $leftover); |
| 47 | + } |
| 48 | + |
| 49 | + public function provideParentAssociationsIsCleared() |
| 50 | + { |
| 51 | + return array( |
| 52 | + array( function (DocumentManager $dm) { $dm->clear(); }, 0 ), |
| 53 | + array( function (DocumentManager $dm, $doc) { $dm->clear(get_class($doc)); }, 1 ), |
| 54 | + array( function (DocumentManager $dm, $doc) { $dm->detach($doc); }, 1 ), |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + private function expectCount($prop, $expected) |
| 59 | + { |
| 60 | + $ro = new \ReflectionObject($this->uow); |
| 61 | + $rp = $ro->getProperty($prop); |
| 62 | + $rp->setAccessible(true); |
| 63 | + $this->assertCount($expected, $rp->getValue($this->uow)); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** @ODM\Document */ |
| 68 | +class SplColDoc |
| 69 | +{ |
| 70 | + /** @ODM\Id */ |
| 71 | + public $id; |
| 72 | + |
| 73 | + /** @ODM\Field(type="string") */ |
| 74 | + public $name; |
| 75 | + |
| 76 | + /** @ODM\EmbedOne */ |
| 77 | + public $one; |
| 78 | + |
| 79 | + /** @ODM\EmbedMany */ |
| 80 | + public $many = array(); |
| 81 | +} |
| 82 | + |
| 83 | +/** @ODM\EmbeddedDocument */ |
| 84 | +class SplColEmbed |
| 85 | +{ |
| 86 | + /** @ODM\Field(type="string") */ |
| 87 | + public $name; |
| 88 | + |
| 89 | + public function __construct($name) |
| 90 | + { |
| 91 | + $this->name = $name; |
| 92 | + } |
| 93 | +} |
0 commit comments