Skip to content
This repository
Browse code

Merge branch 'master' into remove_duplicate_original_data

Conflicts:
	lib/Doctrine/ODM/PHPCR/UnitOfWork.php
  • Loading branch information...
commit d202d1a1154736df1d62414039c3673521ec1aaa 2 parents 3eb8ca8 + 97d9512
Lukas Kahwe Smith lsmith77 authored
6 lib/Doctrine/ODM/PHPCR/DocumentManager.php
@@ -645,10 +645,14 @@ public function getReferrers($document, $type = null, $name = null)
645 645 * Flush all current changes, that is save them within the phpcr session
646 646 * and commit that session to permanent storage.
647 647 *
648   - * @param object $document
  648 + * @param object|array|null $document
649 649 */
650 650 public function flush($document = null)
651 651 {
  652 + if (null !== $document && !is_object($document) && !is_array($document)) {
  653 + throw new \InvalidArgumentException(gettype($document));
  654 + }
  655 +
652 656 $this->errorIfClosed();
653 657 $this->unitOfWork->commit($document);
654 658 }
106 lib/Doctrine/ODM/PHPCR/UnitOfWork.php
@@ -686,62 +686,68 @@ private function getDocumentState($document)
686 686 }
687 687
688 688 /**
689   - * Detects the changes that need to be persisted
  689 + * Detects the changes for a single document
690 690 *
691 691 * @param object $document
692   - *
693 692 * @return void
694 693 */
695   - private function detectChangedDocuments($document = null)
  694 + private function computeSingleDocumentChangeSet($document)
696 695 {
697   - if ($document) {
698   - $state = $this->getDocumentState($document);
699   - if ($state !== self::STATE_MANAGED && $state !== self::STATE_MOVED) {
700   - throw new \InvalidArgumentException('Document has to be managed or moved for single computation '.self::objToStr($document, $this->dm));
701   - }
  696 + $state = $this->getDocumentState($document);
  697 + if ($state !== self::STATE_MANAGED && $state !== self::STATE_MOVED) {
  698 + throw new \InvalidArgumentException('Document has to be managed or moved for single computation '.self::objToStr($document, $this->dm));
  699 + }
702 700
703   - foreach ($this->scheduledInserts as $insertedDocument) {
704   - $class = $this->dm->getClassMetadata(get_class($insertedDocument));
705   - $this->computeChangeSet($class, $insertedDocument);
706   - }
  701 + foreach ($this->scheduledInserts as $insertedDocument) {
  702 + $class = $this->dm->getClassMetadata(get_class($insertedDocument));
  703 + $this->computeChangeSet($class, $insertedDocument);
  704 + }
707 705
708   - // Ignore uninitialized proxy objects
709   - if ($document instanceof Proxy && !$document->__isInitialized()) {
710   - return;
711   - }
  706 + // Ignore uninitialized proxy objects
  707 + if ($document instanceof Proxy && !$document->__isInitialized()) {
  708 + return;
  709 + }
712 710
713   - $oid = spl_object_hash($document);
714   - if (!isset($this->scheduledInserts[$oid])) {
  711 + $oid = spl_object_hash($document);
  712 + if (!isset($this->scheduledInserts[$oid])) {
  713 + $class = $this->dm->getClassMetadata(get_class($document));
  714 + $this->computeChangeSet($class, $document);
  715 + }
  716 + }
  717 +
  718 + /**
  719 + * Detects the changes that need to be persisted
  720 + *
  721 + * @return void
  722 + */
  723 + private function computeChangeSets()
  724 + {
  725 + foreach ($this->identityMap as $document) {
  726 + $state = $this->getDocumentState($document);
  727 + if ($state === self::STATE_MANAGED || $state === self::STATE_MOVED) {
715 728 $class = $this->dm->getClassMetadata(get_class($document));
716 729 $this->computeChangeSet($class, $document);
717 730 }
718   - } else {
719   - foreach ($this->identityMap as $document) {
720   - $state = $this->getDocumentState($document);
721   - if ($state === self::STATE_MANAGED || $state === self::STATE_MOVED) {
722   - $class = $this->dm->getClassMetadata(get_class($document));
723   - $this->computeChangeSet($class, $document);
724   - }
725   - }
726 731 }
727 732 }
728 733
729 734 /**
  735 + * Get a documents actual data, flattening all the objects to arrays.
  736 + *
730 737 * @param ClassMetadata $class
731 738 * @param object $document
732   - * @return void
  739 + * @return array
733 740 */
734   - private function computeChangeSet(ClassMetadata $class, $document)
  741 + private function getDocumentActualData(ClassMetadata $class, $document)
735 742 {
736   - if ($document instanceof Proxy && !$document->__isInitialized()) {
737   - return;
738   - }
739   -
740   - $id = $this->getDocumentId($document);
741   - $oid = spl_object_hash($document);
742   -
743 743 $actualData = array();
744 744 foreach ($class->reflFields as $fieldName => $reflProperty) {
  745 + // do not set the version info fields if they have values, they are not to be managed by the user in write scenarios.
  746 + if ($fieldName === $class->versionNameField
  747 + || $fieldName === $class->versionCreatedField
  748 + ) {
  749 + continue;
  750 + }
745 751 $value = $reflProperty->getValue($document);
746 752 if ($class->isCollectionValuedAssociation($fieldName)
747 753 && $value !== null
@@ -762,12 +768,24 @@ private function computeChangeSet(ClassMetadata $class, $document)
762 768 }
763 769 }
764 770
765   - // unset the version info fields if they have values, they are not to be managed by the user in write scenarios.
766   - if ($class->versionable) {
767   - unset($actualData[$class->versionNameField]);
768   - unset($actualData[$class->versionCreatedField]);
  771 + return $actualData;
  772 + }
  773 +
  774 + /**
  775 + * @param ClassMetadata $class
  776 + * @param object $document
  777 + * @return void
  778 + */
  779 + private function computeChangeSet(ClassMetadata $class, $document)
  780 + {
  781 + if ($document instanceof Proxy && !$document->__isInitialized()) {
  782 + return;
769 783 }
770 784
  785 + $actualData = $this->getDocumentActualData($class, $document);
  786 + $id = $this->getDocumentId($document);
  787 + $oid = spl_object_hash($document);
  788 +
771 789 if ($class->parentMapping && isset($actualData[$class->parentMapping])) {
772 790 $parentClass = $this->dm->getClassMetadata(get_class($actualData[$class->parentMapping]));
773 791 }
@@ -1056,7 +1074,15 @@ private function cascadeDetach($document, array &$visited)
1056 1074 */
1057 1075 public function commit($document = null)
1058 1076 {
1059   - $this->detectChangedDocuments($document);
  1077 + if ($document === null) {
  1078 + $this->computeChangeSets();
  1079 + } else if (is_object($document)) {
  1080 + $this->computeSingleDocumentChangeSet($document);
  1081 + } else if (is_array($document)) {
  1082 + foreach ($document as $object) {
  1083 + $this->computeSingleDocumentChangeSet($object);
  1084 + }
  1085 + }
1060 1086
1061 1087 if ($this->evm->hasListeners(Event::onFlush)) {
1062 1088 $this->evm->dispatchEvent(Event::onFlush, new OnFlushEventArgs($this->dm));
31 tests/Doctrine/Tests/Models/CMS/CmsTeamUser.php
... ... @@ -0,0 +1,31 @@
  1 +<?php
  2 +
  3 +namespace Doctrine\Tests\Models\CMS;
  4 +
  5 +use Doctrine\Common\Collections\ArrayCollection;
  6 +use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
  7 +use Doctrine\ODM\PHPCR\DocumentRepository;
  8 +use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface;
  9 +
  10 +/**
  11 + * @PHPCRODM\Document(repositoryClass="Doctrine\Tests\Models\CMS\CmsTeamUserRepository")
  12 + */
  13 +class CmsTeamUser extends CmsUser
  14 +{
  15 + /** @PHPCRODM\ParentDocument */
  16 + public $parent;
  17 +}
  18 +
  19 +class CmsTeamUserRepository extends DocumentRepository implements RepositoryIdInterface
  20 +{
  21 + /**
  22 + * Generate a document id
  23 + *
  24 + * @param object $document
  25 + * @return string
  26 + */
  27 + public function generateId($document)
  28 + {
  29 + return $document->parent->id.'/'.$document->username;
  30 + }
  31 +}
402 tests/Doctrine/Tests/ODM/PHPCR/Functional/BasicCrudTest.php
@@ -6,9 +6,6 @@
6 6 Doctrine\ODM\PHPCR\DocumentRepository,
7 7 Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
8 8
9   -use Doctrine\Tests\Models\CMS\CmsUser;
10   -use Doctrine\Tests\Models\CMS\CmsAddress;
11   -
12 9 /**
13 10 * @group functional
14 11 */
@@ -132,142 +129,6 @@ public function testMultivaluePropertyWithOnlyOneValueUpdatedToMultiValue()
132 129 $this->assertEquals($userNew->numbers->toArray(), $userNew2->numbers->toArray());
133 130 }
134 131
135   - public function testMove()
136   - {
137   - $this->dm->clear();
138   - $user = $this->dm->find($this->type, '/functional/user');
139   - $this->assertNotNull($user, 'User must exist');
140   -
141   - $this->dm->move($user, '/functional/user2');
142   - $this->dm->flush();
143   -
144   - $user = $this->dm->find($this->type, '/functional/user2');
145   - $this->assertNotNull($user, 'User must exist');
146   -
147   - $user = $this->dm->find($this->type, '/functional/user');
148   - $this->assertNull($user, 'User must not exist');
149   - }
150   -
151   - public function testMoveWithClear()
152   - {
153   - $this->dm->clear();
154   - $user = $this->dm->find($this->type, '/functional/user');
155   - $this->assertNotNull($user, 'User must exist');
156   -
157   - $this->dm->move($user, '/functional/user2');
158   - $user = $this->dm->find($this->type, '/functional/user');
159   - $this->assertNotNull($user, 'User must exist');
160   - $this->dm->flush();
161   - $this->dm->clear();
162   -
163   - $user = $this->dm->find($this->type, '/functional/user2');
164   - $this->assertNotNull($user, 'User must exist');
165   - }
166   -
167   - public function testMoveWithPersist()
168   - {
169   - $this->dm->clear();
170   - $user = $this->dm->find($this->type, '/functional/user');
171   - $this->assertNotNull($user, 'User must exist');
172   -
173   - $this->dm->move($user, '/functional/user2');
174   - $this->dm->persist($user);
175   - $this->dm->flush();
176   - $this->dm->clear();
177   -
178   - $user = $this->dm->find($this->type, '/functional/user');
179   - $this->assertNotNull($user, 'User must exist');
180   - }
181   -
182   - public function testMoveFirstPersist()
183   - {
184   - $this->dm->clear();
185   - $user = $this->dm->find($this->type, '/functional/user');
186   - $this->assertNotNull($user, 'User must exist');
187   -
188   - $user->username = 'new name';
189   - $this->dm->persist($user);
190   - $this->dm->move($user, '/functional/user2');
191   - $this->dm->flush();
192   - $this->dm->clear();
193   -
194   - $user = $this->dm->find($this->type, '/functional/user2');
195   - $this->assertNotNull($user, 'User must exist');
196   - $this->assertEquals('new name', $user->username);
197   - }
198   -
199   - public function testMoveThenRemove()
200   - {
201   - $this->dm->clear();
202   - $user = $this->dm->find($this->type, '/functional/user');
203   - $this->assertNotNull($user, 'User must exist');
204   -
205   - $this->dm->move($user, '/functional/user2');
206   - $this->dm->remove($user);
207   - $this->dm->flush();
208   -
209   - $user = $this->dm->find($this->type, '/functional/user');
210   - $this->assertNull($user, 'User must be null after deletion');
211   - $user = $this->dm->find($this->type, '/functional/user2');
212   - $this->assertNull($user, 'User must be null after deletion');
213   - }
214   -
215   - public function testMoveNoFlush()
216   - {
217   - $this->dm->clear();
218   - $user = $this->dm->find($this->type, '/functional/user');
219   - $this->assertNotNull($user, 'User must exist');
220   -
221   - $this->dm->move($user, '/functional/user2');
222   - $user = $this->dm->find($this->type, '/functional/user');
223   - $this->assertNotNull($user, 'User must exist');
224   - }
225   -
226   - public function testMoveWithChild()
227   - {
228   - $this->dm->clear();
229   - $user1 = $this->dm->find($this->type, '/functional/user');
230   - $this->assertNotNull($user1, 'User must exist');
231   -
232   - $user2 = new TeamUser();
233   - $user2->username = 'jwage';
234   - $user2->id = '/functional/user/team';
235   - $user2->parent = $user1;
236   - $user3 = new TeamUser();
237   - $user3->username = 'beberlei';
238   - $user3->id = '/functional/user/team/team';
239   - $user3->parent = $user2;
240   -
241   - $this->dm->persist($user3);
242   -
243   - $this->dm->flush();
244   -
245   - $user1 = $this->dm->find($this->type, '/functional/user');
246   - $this->dm->move($user1, '/functional/user2');
247   - $this->dm->flush();
248   -
249   - $this->dm->clear();
250   -
251   - $user1 = $this->dm->find($this->type, '/functional/user2');
252   -
253   - $user = $this->dm->find($this->type, '/functional/user2');
254   - $this->assertNotNull($user, 'User must exist');
255   - $user = $this->dm->find($this->type, '/functional/user2/team');
256   - $this->assertNotNull($user, 'User must exist');
257   - $user = $this->dm->find($this->type, '/functional/user2/team/team');
258   - $this->assertNotNull($user, 'User must exist');
259   -
260   - $this->dm->move($user1, '/functional/user');
261   - $this->dm->flush();
262   -
263   - $user = $this->dm->find($this->type, '/functional/user');
264   - $this->assertNotNull($user, 'User must exist');
265   - $user = $this->dm->find($this->type, '/functional/user/team');
266   - $this->assertNotNull($user, 'User must exist');
267   - $user = $this->dm->find($this->type, '/functional/user/team/team');
268   - $this->assertNotNull($user, 'User must exist');
269   - }
270   -
271 132 public function testRemove()
272 133 {
273 134 $this->dm->clear();
@@ -297,23 +158,6 @@ public function testRemoveWithClear()
297 158 $this->assertNull($user, 'User must be null after deletion');
298 159 }
299 160
300   - public function testRemoveThenMove()
301   - {
302   - $this->dm->clear();
303   - $user = $this->dm->find($this->type, '/functional/user');
304   - $this->assertNotNull($user, 'User must exist');
305   -
306   - $this->dm->remove($user);
307   - $this->dm->move($user, '/functional/user2');
308   - $this->dm->flush();
309   -
310   - $user = $this->dm->find($this->type, '/functional/user2');
311   - $this->assertNotNull($user, 'User must exist');
312   -
313   - $user = $this->dm->find($this->type, '/functional/user');
314   - $this->assertNull($user, 'User must be null after deletion');
315   - }
316   -
317 161 public function testRemoveWithPersist()
318 162 {
319 163 $this->dm->clear();
@@ -543,252 +387,6 @@ public function testNoIdProperty()
543 387 $this->assertEquals('test2', $userNew->username);
544 388 $this->assertEquals($user->numbers->toArray(), $userNew->numbers->toArray());
545 389 }
546   -
547   - public function testFlushSingleDocument()
548   - {
549   - $user1 = new User();
550   - $user1->username = 'romanb';
551   - $user1->id = '/functional/test';
552   - $user2 = new User();
553   - $user2->username = 'jwage';
554   - $user2->id = '/functional/test2';
555   - $this->dm->persist($user1);
556   - $this->dm->persist($user2);
557   - $this->dm->flush();
558   - $this->dm->clear();
559   -
560   - $user1 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test');
561   - $this->assertEquals('romanb', $user1->username);
562   -
563   - $user2 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test2');
564   - $this->assertEquals('jwage', $user2->username);
565   -
566   - $user1->username = 'changed';
567   - $user2->username = 'changed';
568   - $this->dm->flush($user1);
569   - $this->dm->clear();
570   -
571   - $check = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test');
572   - $this->assertEquals('changed', $check->username);
573   -
574   - $check = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test2');
575   - $this->assertEquals('jwage', $check->username);
576   - }
577   -
578   - public function testFlushSingleDocumentThenFlush()
579   - {
580   - $user1 = new User();
581   - $user1->username = 'romanb';
582   - $user1->id = '/functional/test';
583   - $user2 = new User();
584   - $user2->username = 'jwage';
585   - $user2->id = '/functional/test2';
586   - $this->dm->persist($user1);
587   - $this->dm->persist($user2);
588   - $this->dm->flush();
589   -
590   - $user1->username = 'changed';
591   - $user2->username = 'changed';
592   - $this->dm->flush($user1);
593   -
594   - $check = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test');
595   - $this->assertEquals('changed', $check->username);
596   -
597   - $this->dm->flush();
598   -
599   - $check = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test2');
600   - $this->assertEquals('changed', $check->username);
601   - }
602   -
603   - public function testFlushSingleDocumentWithParent()
604   - {
605   - $user1 = new User();
606   - $user1->username = 'romanb';
607   - $user1->id = '/functional/test';
608   - $user2 = new TeamUser();
609   - $user2->username = 'jwage';
610   - $user2->id = '/functional/test/team';
611   - $user2->parent = $user1;
612   - $user3 = new TeamUser();
613   - $user3->username = 'beberlei';
614   - $user3->id = '/functional/test/team/team';
615   - $user3->parent = $user2;
616   - $this->dm->persist($user3);
617   - $this->dm->flush($user3);
618   -
619   - $user1 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test');
620   - $this->assertEquals('romanb', $user1->username);
621   -
622   - $user2 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test/team');
623   - $this->assertEquals('jwage', $user2->username);
624   -
625   - $user3 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test/team/team');
626   - $this->assertEquals('beberlei', $user3->username);
627   -
628   - $user1->username = 'changed';
629   - $user2->username = 'changed';
630   - $user3->username = 'changed';
631   - $this->dm->flush($user3);
632   -
633   - $user1 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test');
634   - $this->assertEquals('changed', $user1->username);
635   -
636   - $user2 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test/team');
637   - $this->assertEquals('changed', $user2->username);
638   -
639   - $user3 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test/team/team');
640   - $this->assertEquals('changed', $user3->username);
641   - }
642   -
643   - public function testFlushSingleManagedDocument()
644   - {
645   - $user = new CmsUser;
646   - $user->name = 'Dominik';
647   - $user->username = 'domnikl';
648   - $user->status = 'developer';
649   -
650   - $this->dm->persist($user);
651   - $this->dm->flush();
652   -
653   - $user->status = 'administrator';
654   - $this->dm->flush($user);
655   - $this->dm->clear();
656   -
657   - $user = $this->dm->find(get_class($user), $user->id);
658   - $this->assertEquals('administrator', $user->status);
659   - }
660   -
661   - public function testFlushSingleUnmanagedDocument()
662   - {
663   - $user = new CmsUser;
664   - $user->name = 'Dominik';
665   - $user->username = 'domnikl';
666   - $user->status = 'developer';
667   -
668   - $this->setExpectedException('InvalidArgumentException');
669   - $this->dm->flush($user);
670   - }
671   -
672   - public function testFlushSingleAndNewDocument()
673   - {
674   - $user = new CmsUser;
675   - $user->name = 'Dominik';
676   - $user->username = 'domnikl';
677   - $user->status = 'developer';
678   -
679   - $this->dm->persist($user);
680   - $this->dm->flush();
681   -
682   - $otherUser = new CmsUser;
683   - $otherUser->name = 'Dominik2';
684   - $otherUser->username = 'domnikl2';
685   - $otherUser->status = 'developer';
686   -
687   - $user->status = 'administrator';
688   -
689   - $this->dm->persist($otherUser);
690   - $this->dm->flush($user);
691   -
692   - $this->assertTrue($this->dm->contains($otherUser), "Other user is not contained in DocumentManager");
693   - $this->assertTrue($otherUser->id != null, "other user has no id");
694   - }
695   -
696   - public function testFlushAndCascadePersist()
697   - {
698   - $user = new CmsUser;
699   - $user->name = 'Dominik';
700   - $user->username = 'domnikl';
701   - $user->status = 'developer';
702   -
703   - $this->dm->persist($user);
704   - $this->dm->flush();
705   -
706   - $address = new CmsAddress();
707   - $address->city = "Springfield";
708   - $address->zip = "12354";
709   - $address->country = "Germany";
710   - $address->street = "Foo Street";
711   - $address->user = $user;
712   - $user->address = $address;
713   -
714   - $this->dm->flush($user);
715   -
716   - $this->assertTrue($this->dm->contains($address), "Address is not contained in DocumentManager");
717   - $this->assertTrue($address->id != null, "address user has no id");
718   - }
719   -
720   - public function testProxyIsIgnored()
721   - {
722   - $user = new CmsUser;
723   - $user->name = 'Dominik';
724   - $user->username = 'domnikl';
725   - $user->status = 'developer';
726   -
727   - $this->dm->persist($user);
728   - $this->dm->flush();
729   - $this->dm->clear();
730   -
731   - $user = $this->dm->getReference(get_class($user), $user->id);
732   -
733   - $otherUser = new CmsUser;
734   - $otherUser->name = 'Dominik2';
735   - $otherUser->username = 'domnikl2';
736   - $otherUser->status = 'developer';
737   -
738   - $this->dm->persist($otherUser);
739   - $this->dm->flush($user);
740   -
741   - $this->assertTrue($this->dm->contains($otherUser), "Other user is contained in DocumentManager");
742   - $this->assertTrue($otherUser->id != null, "other user has no id"); }
743   -
744   - public function testDetach()
745   - {
746   - $user = $this->dm->find($this->type, '/functional/user');
747   - $user->username = "new-name";
748   -
749   - $this->dm->detach($user);
750   - $this->dm->flush();
751   - $this->dm->clear();
752   -
753   - $newUser = $this->dm->find($this->type, '/functional/user');
754   - $this->assertEquals('lsmith', $newUser->username);
755   - }
756   -
757   - /**
758   - * @expectedException \InvalidArgumentException
759   - */
760   - public function testDetachWithPerist()
761   - {
762   - $user = $this->dm->find($this->type, '/functional/user');
763   - $user->username = "new-name";
764   -
765   - $this->dm->detach($user);
766   - $this->dm->persist($user);
767   - }
768   -
769   - /**
770   - * @expectedException \InvalidArgumentException
771   - */
772   - public function testDetachWithMove()
773   - {
774   - $user = $this->dm->find($this->type, '/functional/user');
775   - $user->username = "new-name";
776   -
777   - $this->dm->detach($user);
778   - $this->dm->move($user, '/functional/user2');
779   - }
780   -
781   - /**
782   - * @expectedException \InvalidArgumentException
783   - */
784   - public function testDetachWithRemove()
785   - {
786   - $user = $this->dm->find($this->type, '/functional/user');
787   - $user->username = "new-name";
788   -
789   - $this->dm->detach($user);
790   - $this->dm->remove($user);
791   - }
792 390 }
793 391
794 392 /**
60 tests/Doctrine/Tests/ODM/PHPCR/Functional/DetachTest.php
@@ -11,10 +11,19 @@ class DetachTest extends \Doctrine\Tests\ODM\PHPCR\PHPCRFunctionalTestCase
11 11
12 12 private $node;
13 13
  14 + private $type;
  15 +
14 16 public function setUp()
15 17 {
16   - $this->dm = $this->createDocumentManager();
  18 + $this->type = 'Doctrine\Tests\Models\CMS\CmsUser';
  19 + $this->dm = $this->createDocumentManager(array(__DIR__));
17 20 $this->node = $this->resetFunctionalNode($this->dm);
  21 +
  22 + $user = $this->node->addNode('lsmith');
  23 + $user->setProperty('username', 'lsmith');
  24 + $user->setProperty('numbers', array(3, 1, 2));
  25 + $user->setProperty('phpcr:class', $this->type, \PHPCR\PropertyType::STRING);
  26 + $this->dm->getPhpcrSession()->save();
18 27 }
19 28
20 29 public function testDetachNewObject()
@@ -47,4 +56,53 @@ public function testDetachedKnownObject()
47 56 $this->dm->detach($user);
48 57 $this->dm->persist($user);
49 58 }
  59 +
  60 + public function testDetach()
  61 + {
  62 + $user = $this->dm->find($this->type, '/functional/lsmith');
  63 + $user->username = "new-name";
  64 +
  65 + $this->dm->detach($user);
  66 + $this->dm->flush();
  67 + $this->dm->clear();
  68 +
  69 + $newUser = $this->dm->find($this->type, '/functional/lsmith');
  70 + $this->assertEquals('lsmith', $newUser->username);
  71 + }
  72 +
  73 + /**
  74 + * @expectedException \InvalidArgumentException
  75 + */
  76 + public function testDetachWithPerist()
  77 + {
  78 + $user = $this->dm->find($this->type, '/functional/lsmith');
  79 + $user->username = "new-name";
  80 +
  81 + $this->dm->detach($user);
  82 + $this->dm->persist($user);
  83 + }
  84 +
  85 + /**
  86 + * @expectedException \InvalidArgumentException
  87 + */
  88 + public function testDetachWithMove()
  89 + {
  90 + $user = $this->dm->find($this->type, '/functional/lsmith');
  91 + $user->username = "new-name";
  92 +
  93 + $this->dm->detach($user);
  94 + $this->dm->move($user, '/functional/user2');
  95 + }
  96 +
  97 + /**
  98 + * @expectedException \InvalidArgumentException
  99 + */
  100 + public function testDetachWithRemove()
  101 + {
  102 + $user = $this->dm->find($this->type, '/functional/lsmith');
  103 + $user->username = "new-name";
  104 +
  105 + $this->dm->detach($user);
  106 + $this->dm->remove($user);
  107 + }
50 108 }
240 tests/Doctrine/Tests/ODM/PHPCR/Functional/FlushTest.php
... ... @@ -1,2 +1,242 @@
1 1 <?php
2 2
  3 +namespace Doctrine\Tests\ODM\PHPCR\Functional;
  4 +
  5 +use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface,
  6 +Doctrine\ODM\PHPCR\DocumentRepository,
  7 +Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
  8 +
  9 +use Doctrine\Tests\Models\CMS\CmsUser;
  10 +use Doctrine\Tests\Models\CMS\CmsTeamUser;
  11 +use Doctrine\Tests\Models\CMS\CmsAddress;
  12 +
  13 +/**
  14 + * @group functional
  15 + */
  16 +class FlushTest extends \Doctrine\Tests\ODM\PHPCR\PHPCRFunctionalTestCase
  17 +{
  18 + /**
  19 + * @var DocumentManager
  20 + */
  21 + private $dm;
  22 +
  23 + private $type;
  24 +
  25 + public function setUp()
  26 + {
  27 + $this->type = 'Doctrine\Tests\Models\CMS\CmsUser';
  28 + $this->dm = $this->createDocumentManager(array(__DIR__));
  29 + $this->resetFunctionalNode($this->dm);
  30 + }
  31 +
  32 + public function testFlushSingleDocument()
  33 + {
  34 + $user1 = new CmsUser();
  35 + $user1->username = 'romanb';
  36 + $user2 = new CmsUser();
  37 + $user2->username = 'jwage';
  38 + $this->dm->persist($user1);
  39 + $this->dm->persist($user2);
  40 + $this->dm->flush();
  41 + $this->dm->clear();
  42 +
  43 + $user1 = $this->dm->find($this->type, '/functional/romanb');
  44 + $this->assertEquals('romanb', $user1->username);
  45 +
  46 + $user2 = $this->dm->find($this->type, '/functional/jwage');
  47 + $this->assertEquals('jwage', $user2->username);
  48 +
  49 + $user1->username = 'changed';
  50 + $user2->username = 'changed';
  51 + $this->dm->flush($user1);
  52 + $this->dm->clear();
  53 +
  54 + $check = $this->dm->find($this->type, '/functional/romanb');
  55 + $this->assertEquals('changed', $check->username);
  56 +
  57 + $check = $this->dm->find($this->type, '/functional/jwage');
  58 + $this->assertEquals('jwage', $check->username);
  59 + }
  60 +
  61 + public function testFlushSingleDocumentThenFlush()
  62 + {
  63 + $user1 = new CmsUser();
  64 + $user1->username = 'romanb';
  65 + $user2 = new CmsUser();
  66 + $user2->username = 'jwage';
  67 + $this->dm->persist($user1);
  68 + $this->dm->persist($user2);
  69 + $this->dm->flush();
  70 +
  71 + $user1->username = 'changed';
  72 + $user2->username = 'changed';
  73 + $this->dm->flush($user1);
  74 +
  75 + $check = $this->dm->find($this->type, '/functional/romanb');
  76 + $this->assertEquals('changed', $check->username);
  77 +
  78 + $this->dm->flush();
  79 +
  80 + $check = $this->dm->find($this->type, '/functional/jwage');
  81 + $this->assertEquals('changed', $check->username);
  82 + }
  83 +
  84 + public function testFlushSingleDocumentWithParent()
  85 + {
  86 + $user1 = new CmsUser();
  87 + $user1->username = 'romanb';
  88 + $user2 = new CmsTeamUser();
  89 + $user2->username = 'jwage';
  90 + $user2->parent = $user1;
  91 + $user3 = new CmsTeamUser();
  92 + $user3->username = 'beberlei';
  93 + $user3->parent = $user2;
  94 + $this->dm->persist($user3);
  95 + $this->dm->flush($user3);
  96 +
  97 + $user1 = $this->dm->find($this->type, '/functional/romanb');
  98 + $this->assertEquals('romanb', $user1->username);
  99 +
  100 + $user2 = $this->dm->find($this->type, '/functional/romanb/jwage');
  101 + $this->assertEquals('jwage', $user2->username);
  102 +
  103 + $user3 = $this->dm->find($this->type, '/functional/romanb/jwage/beberlei');
  104 + $this->assertEquals('beberlei', $user3->username);
  105 +
  106 + $user1->username = 'changed';
  107 + $user2->username = 'changed';
  108 + $user3->username = 'changed';
  109 + $this->dm->flush($user3);
  110 +
  111 + $user1 = $this->dm->find($this->type, '/functional/romanb');
  112 + $this->assertEquals('changed', $user1->username);
  113 +
  114 + $user2 = $this->dm->find($this->type, '/functional/romanb/jwage');
  115 + $this->assertEquals('changed', $user2->username);
  116 +
  117 + $user3 = $this->dm->find($this->type, '/functional/romanb/jwage/beberlei');
  118 + $this->assertEquals('changed', $user3->username);
  119 + }
  120 +
  121 + public function testFlushSingleManagedDocument()
  122 + {
  123 + $user = new CmsUser();
  124 + $user->name = 'Dominik';
  125 + $user->username = 'domnikl';
  126 + $user->status = 'developer';
  127 +
  128 + $this->dm->persist($user);
  129 + $this->dm->flush();
  130 +
  131 + $user->status = 'administrator';
  132 + $this->dm->flush($user);
  133 + $this->dm->clear();
  134 +
  135 + $user = $this->dm->find(get_class($user), $user->id);
  136 + $this->assertEquals('administrator', $user->status);
  137 + }
  138 +
  139 + public function testFlushManyExplicitDocuments()
  140 + {
  141 + $userA = new CmsUser('userA');
  142 + $userA->username = 'userA';
  143 + $userB = new CmsUser('userB');
  144 + $userB->username = 'userB';
  145 + $userC = new CmsUser('userC');
  146 + $userC->username = 'userC';
  147 +
  148 + $this->dm->persist($userA);
  149 + $this->dm->persist($userB);
  150 + $this->dm->persist($userC);
  151 +
  152 + $this->dm->flush(array($userA, $userB, $userC));
  153 +
  154 + $this->assertNotNull($userA->id);
  155 + $this->assertNotNull($userB->id);
  156 + $this->assertNotNull($userC->id);
  157 + }
  158 +
  159 + public function testFlushSingleUnmanagedDocument()
  160 + {
  161 + $user = new CmsUser();
  162 + $user->name = 'Dominik';
  163 + $user->username = 'domnikl';
  164 + $user->status = 'developer';
  165 +
  166 + $this->setExpectedException('InvalidArgumentException');
  167 + $this->dm->flush($user);
  168 + }
  169 +
  170 + public function testFlushSingleAndNewDocument()
  171 + {
  172 + $user = new CmsUser();
  173 + $user->name = 'Dominik';
  174 + $user->username = 'domnikl';
  175 + $user->status = 'developer';
  176 +
  177 + $this->dm->persist($user);
  178 + $this->dm->flush();
  179 +
  180 + $otherUser = new CmsUser;
  181 + $otherUser->name = 'Dominik2';
  182 + $otherUser->username = 'domnikl2';
  183 + $otherUser->status = 'developer';
  184 +
  185 + $user->status = 'administrator';
  186 +
  187 + $this->dm->persist($otherUser);
  188 + $this->dm->flush($user);
  189 +
  190 + $this->assertTrue($this->dm->contains($otherUser), "Other user is not contained in DocumentManager");
  191 + $this->assertTrue($otherUser->id != null, "other user has no id");
  192 + }
  193 +
  194 + public function testFlushAndCascadePersist()
  195 + {
  196 + $user = new CmsUser();
  197 + $user->name = 'Dominik';
  198 + $user->username = 'domnikl';
  199 + $user->status = 'developer';
  200 +
  201 + $this->dm->persist($user);
  202 + $this->dm->flush();
  203 +
  204 + $address = new CmsAddress();
  205 + $address->city = "Springfield";
  206 + $address->zip = "12354";
  207 + $address->country = "Germany";
  208 + $address->street = "Foo Street";
  209 + $address->user = $user;
  210 + $user->address = $address;
  211 +
  212 + $this->dm->flush($user);
  213 +
  214 + $this->assertTrue($this->dm->contains($address), "Address is not contained in DocumentManager");
  215 + $this->assertTrue($address->id != null, "address user has no id");
  216 + }
  217 +
  218 + public function testProxyIsIgnored()
  219 + {
  220 + $user = new CmsUser();
  221 + $user->name = 'Dominik';
  222 + $user->username = 'domnikl';
  223 + $user->status = 'developer';
  224 +
  225 + $this->dm->persist($user);
  226 + $this->dm->flush();
  227 + $this->dm->clear();
  228 +
  229 + $user = $this->dm->getReference(get_class($user), $user->id);
  230 +
  231 + $otherUser = new CmsUser;
  232 + $otherUser->name = 'Dominik2';
  233 + $otherUser->username = 'domnikl2';
  234 + $otherUser->status = 'developer';
  235 +
  236 + $this->dm->persist($otherUser);
  237 + $this->dm->flush($user);
  238 +
  239 + $this->assertTrue($this->dm->contains($otherUser), "Other user is contained in DocumentManager");
  240 + $this->assertTrue($otherUser->id != null, "other user has no id");
  241 + }
  242 +}
189 tests/Doctrine/Tests/ODM/PHPCR/Functional/MoveTest.php
... ... @@ -0,0 +1,189 @@
  1 +<?php
  2 +
  3 +namespace Doctrine\Tests\ODM\PHPCR\Functional;
  4 +
  5 +use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface,
  6 + Doctrine\ODM\PHPCR\DocumentRepository,
  7 + Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
  8 +
  9 +use Doctrine\Tests\Models\CMS\CmsUser;
  10 +use Doctrine\Tests\Models\CMS\CmsTeamUser;
  11 +
  12 +/**
  13 + * @group functional
  14 + */
  15 +class MoveTest extends \Doctrine\Tests\ODM\PHPCR\PHPCRFunctionalTestCase
  16 +{
  17 + /**
  18 + * @var DocumentManager
  19 + */
  20 + private $dm;
  21 +
  22 + private $type;
  23 +
  24 + private $node;
  25 +
  26 + public function setUp()
  27 + {
  28 + $this->type = 'Doctrine\Tests\Models\CMS\CmsUser';
  29 + $this->dm = $this->createDocumentManager(array(__DIR__));
  30 + $this->node = $this->resetFunctionalNode($this->dm);
  31 +
  32 + $user = $this->node->addNode('lsmith');
  33 + $user->setProperty('username', 'lsmith');
  34 + $user->setProperty('numbers', array(3, 1, 2));
  35 + $user->setProperty('phpcr:class', $this->type, \PHPCR\PropertyType::STRING);
  36 + $this->dm->getPhpcrSession()->save();
  37 + }
  38 +
  39 + public function testMove()
  40 + {
  41 + $this->dm->clear();
  42 + $user = $this->dm->find($this->type, '/functional/lsmith');
  43 + $this->assertNotNull($user, 'User must exist');
  44 +
  45 + $this->dm->move($user, '/functional/user2');
  46 + $this->dm->flush();
  47 +
  48 + $user = $this->dm->find($this->type, '/functional/user2');
  49 + $this->assertNotNull($user, 'User must exist');
  50 +
  51 + $user = $this->dm->find($this->type, '/functional/lsmith');
  52 + $this->assertNull($user, 'User must not exist');
  53 + }
  54 +
  55 + public function testMoveWithClear()
  56 + {
  57 + $this->dm->clear();
  58 + $user = $this->dm->find($this->type, '/functional/lsmith');
  59 + $this->assertNotNull($user, 'User must exist');
  60 +
  61 + $this->dm->move($user, '/functional/user2');
  62 + $user = $this->dm->find($this->type, '/functional/lsmith');
  63 + $this->assertNotNull($user, 'User must exist');
  64 + $this->dm->flush();
  65 + $this->dm->clear();
  66 +
  67 + $user = $this->dm->find($this->type, '/functional/user2');
  68 + $this->assertNotNull($user, 'User must exist');
  69 + }
  70 +
  71 + public function testMoveWithPersist()
  72 + {
  73 + $this->dm->clear();
  74 + $user = $this->dm->find($this->type, '/functional/lsmith');
  75 + $this->assertNotNull($user, 'User must exist');
  76 +
  77 + $this->dm->move($user, '/functional/user2');
  78 + $this->dm->persist($user);
  79 + $this->dm->flush();
  80 + $this->dm->clear();
  81 +
  82 + $user = $this->dm->find($this->type, '/functional/lsmith');
  83 + $this->assertNotNull($user, 'User must exist');
  84 + }
  85 +
  86 + public function testMoveFirstPersist()
  87 + {
  88 + $this->dm->clear();
  89 + $user = $this->dm->find($this->type, '/functional/lsmith');
  90 + $this->assertNotNull($user, 'User must exist');
  91 +
  92 + $user->username = 'new name';
  93 + $this->dm->persist($user);
  94 + $this->dm->move($user, '/functional/user2');
  95 + $this->dm->flush();
  96 + $this->dm->clear();
  97 +
  98 + $user = $this->dm->find($this->type, '/functional/user2');
  99 + $this->assertNotNull($user, 'User must exist');
  100 + $this->assertEquals('new name', $user->username);
  101 + }
  102 +
  103 + public function testMoveThenRemove()
  104 + {
  105 + $this->dm->clear();
  106 + $user = $this->dm->find($this->type, '/functional/lsmith');
  107 + $this->assertNotNull($user, 'User must exist');
  108 +
  109 + $this->dm->move($user, '/functional/user2');
  110 + $this->dm->remove($user);
  111 + $this->dm->flush();
  112 +
  113 + $user = $this->dm->find($this->type, '/functional/lsmith');
  114 + $this->assertNull($user, 'User must be null after deletion');
  115 + $user = $this->dm->find($this->type, '/functional/user2');
  116 + $this->assertNull($user, 'User must be null after deletion');
  117 + }
  118 +
  119 + public function testMoveNoFlush()
  120 + {
  121 + $this->dm->clear();
  122 + $user = $this->dm->find($this->type, '/functional/lsmith');
  123 + $this->assertNotNull($user, 'User must exist');
  124 +
  125 + $this->dm->move($user, '/functional/user2');
  126 + $user = $this->dm->find($this->type, '/functional/lsmith');