From 8b4488c5f8bc0b59e56a4f0b161b8140c1f13998 Mon Sep 17 00:00:00 2001 From: Maciej Malarz Date: Sat, 6 Jan 2018 15:29:29 +0100 Subject: [PATCH 1/2] Remove and modify related tests --- .../Functional/CollectionPersisterTest.php | 2 +- .../MongoDB/Tests/Functional/FlushTest.php | 120 ------------------ .../Tests/Functional/FunctionalTest.php | 21 --- .../Tests/Functional/Ticket/GH1435Test.php | 2 +- .../Tests/Functional/Ticket/GH301Test.php | 44 ------- .../Tests/Functional/Ticket/GH567Test.php | 45 ------- .../Tests/Functional/Ticket/GH597Test.php | 10 +- .../Tests/Functional/Ticket/GH774Test.php | 17 --- .../ODM/MongoDB/Tests/UnitOfWorkTest.php | 30 ----- 9 files changed, 7 insertions(+), 284 deletions(-) delete mode 100644 tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH301Test.php delete mode 100644 tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH567Test.php diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/CollectionPersisterTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/CollectionPersisterTest.php index 2f37a39464..49158759ad 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/CollectionPersisterTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/CollectionPersisterTest.php @@ -143,7 +143,7 @@ private function getTestUser($username) $user->categories[0]->children[1]->children[1] = new CollectionPersisterCategory('Child of Category1_1 2'); $this->dm->persist($user); - $this->dm->flush(null, array()); + $this->dm->flush(); return $user; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/FlushTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/FlushTest.php index 904f64e9f0..f6be0a1e70 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/FlushTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/FlushTest.php @@ -53,126 +53,6 @@ public function testFlush() $this->assertSize(1); } - public function testFlushManyExplicitDocuments() - { - $userA = new FriendUser('userA'); - $userB = new FriendUser('userB'); - $userC = new FriendUser('userC'); - - $this->dm->persist($userA); - $this->dm->persist($userB); - $this->dm->persist($userC); - - $this->dm->flush(array($userA, $userB, $userC)); - - $this->assertNotNull($userA->id); - $this->assertNotNull($userB->id); - $this->assertNotNull($userC->id); - } - - public function testFlushSingleManagedDocument() - { - $user = new CmsUser; - $user->name = 'Dominik'; - $user->username = 'domnikl'; - $user->status = 'developer'; - - $this->dm->persist($user); - $this->dm->flush(); - - $user->status = 'administrator'; - $this->dm->flush($user); - $this->dm->clear(); - - $user = $this->dm->find(get_class($user), $user->id); - $this->assertEquals('administrator', $user->status); - } - - public function testFlushSingleUnmanagedDocument() - { - $user = new CmsUser; - $user->name = 'Dominik'; - $user->username = 'domnikl'; - $user->status = 'developer'; - - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Document has to be managed or scheduled for removal for single computation'); - $this->dm->flush($user); - } - - public function testFlushSingleAndNewDocument() - { - $user = new CmsUser; - $user->name = 'Dominik'; - $user->username = 'domnikl'; - $user->status = 'developer'; - - $this->dm->persist($user); - $this->dm->flush(); - - $otherUser = new CmsUser; - $otherUser->name = 'Dominik2'; - $otherUser->username = 'domnikl2'; - $otherUser->status = 'developer'; - - $user->status = 'administrator'; - - $this->dm->persist($otherUser); - $this->dm->flush($user); - - $this->assertTrue($this->dm->contains($otherUser), "Other user is contained in DocumentManager"); - $this->assertGreaterThan(0, $otherUser->id, "other user has an id"); - } - - public function testFlushAndCascadePersist() - { - $user = new CmsUser; - $user->name = 'Dominik'; - $user->username = 'domnikl'; - $user->status = 'developer'; - - $this->dm->persist($user); - $this->dm->flush(); - - $address = new CmsAddress(); - $address->city = "Springfield"; - $address->zip = "12354"; - $address->country = "Germany"; - $address->street = "Foo Street"; - $address->user = $user; - $user->address = $address; - - $this->dm->flush($user); - - $this->assertTrue($this->dm->contains($address), "Other user is contained in DocumentManager"); - $this->assertGreaterThan(0, $address->id, "other user has an id"); - } - - public function testProxyIsIgnored() - { - $user = new CmsUser; - $user->name = 'Dominik'; - $user->username = 'domnikl'; - $user->status = 'developer'; - - $this->dm->persist($user); - $this->dm->flush(); - $this->dm->clear(); - - $user = $this->dm->getReference(get_class($user), $user->id); - - $otherUser = new CmsUser; - $otherUser->name = 'Dominik2'; - $otherUser->username = 'domnikl2'; - $otherUser->status = 'developer'; - - $this->dm->persist($otherUser); - $this->dm->flush($user); - - $this->assertTrue($this->dm->contains($otherUser), "Other user is contained in DocumentManager"); - $this->assertGreaterThan(0, $otherUser->id, "other user has an id"); - } - protected function assertSize($size) { $this->assertEquals($size, $this->dm->getUnitOfWork()->size()); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php index 77a2cec967..0fb20513fb 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php @@ -108,27 +108,6 @@ public function testInheritedAssociationMappings() $this->assertTrue(isset($class->associationMappings['groups'])); } - public function testFlushSingleDocument() - { - $user1 = new \Documents\ForumUser(); - $user1->username = 'romanb'; - $user2 = new \Documents\ForumUser(); - $user2->username = 'jwage'; - $this->dm->persist($user1); - $this->dm->persist($user2); - $this->dm->flush(); - - $user1->username = 'changed'; - $user2->username = 'changed'; - $this->dm->flush($user1); - - $check = $this->dm->getDocumentCollection('Documents\ForumUser')->find(array('username' => 'jwage')); - $this->assertNotNull($check); - - $check = $this->dm->getDocumentCollection('Documents\ForumUser')->find(array('username' => 'changed')); - $this->assertNotNull($check); - } - public function testNestedCategories() { $root = new \Documents\Category('Root'); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH1435Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH1435Test.php index ee959a8fba..220ffd847c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH1435Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH1435Test.php @@ -46,7 +46,7 @@ public function testUpdateWithIncrement() $document->name = 'test'; $this->dm->persist($document); - $this->dm->flush($document); + $this->dm->flush(); $this->dm->clear(); $document = $this->dm->getRepository(GH1435DocumentIncrement::class)->findOneBy([]); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH301Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH301Test.php deleted file mode 100644 index e2e1516e80..0000000000 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH301Test.php +++ /dev/null @@ -1,44 +0,0 @@ -name = 'referenced'; - - $doc = new GH301Document(); - $doc->name = 'parent'; - $doc->refOnePersist = $ref; - - $this->dm->persist($doc); - $this->dm->flush($doc); - $this->dm->clear(); - - $docId = $doc->id; unset($doc); - $refId = $ref->id; unset($ref); - - $doc = $this->dm->find(__NAMESPACE__ . '\GH301Document', $docId); - $this->assertNotNull($doc); - $this->assertEquals($refId, $doc->refOnePersist->id); - } -} - -/** @ODM\Document */ -class GH301Document -{ - /** @ODM\Id */ - public $id; - - /** @ODM\Field(type="string") */ - public $name; - - /** - * @ODM\ReferenceOne(targetDocument="GH301Document", cascade="persist") - */ - public $refOnePersist; -} diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH567Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH567Test.php deleted file mode 100644 index e0f60ec023..0000000000 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH567Test.php +++ /dev/null @@ -1,45 +0,0 @@ -repository = $this->dm->getRepository($class); - } - - public function testRemoveSingleDocument() - { - $document = new GH567Document(); - $this->dm->persist($document); - $this->dm->flush(); - $this->dm->clear(); - - $foundDocument = $this->repository->find($document->id); - - $this->dm->remove($foundDocument); - $this->dm->flush($foundDocument); - $this->dm->clear(); - - $foundDocument = $this->repository->find($document->id); - $this->assertNull($foundDocument); - } -} - -/** - * @ODM\Document - */ -class GH567Document -{ - /** - * @ODM\Id - */ - public $id; -} diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH597Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH597Test.php index 305adf0c2d..21c90b7267 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH597Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH597Test.php @@ -11,7 +11,7 @@ public function testEmbedManyGetsUnset() { $post = new GH597Post(); $this->dm->persist($post); - $this->dm->flush($post); + $this->dm->flush(); $this->dm->clear(); // default behavior on inserts already leaves out embedded documents @@ -26,7 +26,7 @@ public function testEmbedManyGetsUnset() new GH597Comment('Comment 3') )); $this->dm->persist($post); - $this->dm->flush($post); + $this->dm->flush(); $this->dm->clear(); $expectedDocument = array( @@ -43,7 +43,7 @@ public function testEmbedManyGetsUnset() $post = $this->dm->find(__NAMESPACE__ . '\GH597Post', $post->getId()); $this->assertCount(3, $post->getComments()); $post->comments = null; - $this->dm->flush($post); + $this->dm->flush(); $this->dm->clear(); $post = $this->dm->find(__NAMESPACE__ . '\GH597Post', $post->getId()); @@ -75,7 +75,7 @@ public function testReferenceManyGetsUnset() $post->referenceMany = new ArrayCollection(array($referenceMany1, $referenceMany2)); $this->dm->persist($post); - $this->dm->flush($post); + $this->dm->flush(); $this->dm->clear(); $expectedDocument = array( @@ -91,7 +91,7 @@ public function testReferenceManyGetsUnset() $post = $this->dm->find(__NAMESPACE__ . '\GH597Post', $post->getId()); $this->assertCount(2, $post->getReferenceMany()); $post->referenceMany = null; - $this->dm->flush($post); + $this->dm->flush(); $this->dm->clear(); $post = $this->dm->find(__NAMESPACE__ . '\GH597Post', $post->getId()); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH774Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH774Test.php index 58fa0438cb..a799817f95 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH774Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH774Test.php @@ -24,23 +24,6 @@ public function testUpsert() $this->assertEquals('test', $thread->permalink); } - public function testUpsertSingleFlush() - { - $id = (string) new \MongoDB\BSON\ObjectId(); - - $thread = new GH774Thread(); - $thread->id = $id; - $thread->permalink = 'test'; - - $this->dm->persist($thread); - $this->dm->flush($thread); - $this->dm->clear(); - - $thread = $this->dm->find(get_class($thread), $id); - $this->assertNotNull($thread); - $this->assertEquals('test', $thread->permalink); - } - protected function createMetadataDriverImpl() { return new XmlDriver(__DIR__ . '/GH774'); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php b/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php index 805d52386b..819831c770 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php @@ -238,36 +238,6 @@ public function testChangeTrackingNotify() $this->assertSame($updates[0], $item); } - /** - * @doesNotPerformAssertions - */ - public function testDoubleCommitWithChangeTrackingNotify() - { - $pb = $this->getMockPersistenceBuilder(); - - $class = $this->dm->getClassMetadata('Doctrine\ODM\MongoDB\Tests\NotifyChangedDocument'); - $persister = $this->getMockDocumentPersister($pb, $class); - $this->uow->setDocumentPersister($class->name, $persister); - - $entity = new NotifyChangedDocument(); - $entity->setId(2); - $this->uow->persist($entity); - - $this->uow->commit($entity); - - // Use a custom error handler that will fail the test if the next commit() call raises a notice error - set_error_handler(function() { - restore_error_handler(); - - $this->fail('Expected not to get a notice error after committing an entity multiple times using the NOTIFY change tracking policy.'); - }, E_NOTICE); - - $this->uow->commit($entity); - - // Restore previous error handler if no errors have been raised - restore_error_handler(); - } - public function testGetDocumentStateWithAssignedIdentity() { $pb = $this->getMockPersistenceBuilder(); From f876a77a2a6c24f74806d9f895c3c04071b6749b Mon Sep 17 00:00:00 2001 From: Maciej Malarz Date: Sat, 6 Jan 2018 15:28:53 +0100 Subject: [PATCH 2/2] Remove flushing only chosen documents --- lib/Doctrine/ODM/MongoDB/DocumentManager.php | 8 +-- lib/Doctrine/ODM/MongoDB/UnitOfWork.php | 59 +------------------- 2 files changed, 4 insertions(+), 63 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/DocumentManager.php b/lib/Doctrine/ODM/MongoDB/DocumentManager.php index 7c5cf51a4f..e3b439299c 100644 --- a/lib/Doctrine/ODM/MongoDB/DocumentManager.php +++ b/lib/Doctrine/ODM/MongoDB/DocumentManager.php @@ -508,17 +508,13 @@ public function getRepository($documentName) * This effectively synchronizes the in-memory state of managed objects with the * database. * - * @param object $document * @param array $options Array of options to be used with batchInsert(), update() and remove() * @throws \InvalidArgumentException */ - public function flush($document = null, array $options = array()) + public function flush(array $options = array()) { - if (null !== $document && ! is_object($document) && ! is_array($document)) { - throw new \InvalidArgumentException(gettype($document)); - } $this->errorIfClosed(); - $this->unitOfWork->commit($document, $options); + $this->unitOfWork->commit($options); } /** diff --git a/lib/Doctrine/ODM/MongoDB/UnitOfWork.php b/lib/Doctrine/ODM/MongoDB/UnitOfWork.php index 12926cac5c..ff2c060d7a 100644 --- a/lib/Doctrine/ODM/MongoDB/UnitOfWork.php +++ b/lib/Doctrine/ODM/MongoDB/UnitOfWork.php @@ -360,10 +360,9 @@ public function setDocumentPersister($documentName, Persisters\DocumentPersister * 2) All document updates * 3) All document deletions * - * @param object $document * @param array $options Array of options to be used with batchInsert(), update() and remove() */ - public function commit($document = null, array $options = array()) + public function commit(array $options = array()) { // Raise preFlush if ($this->evm->hasListeners(Events::preFlush)) { @@ -371,15 +370,7 @@ public function commit($document = null, array $options = array()) } // Compute changes done since last commit. - if ($document === null) { - $this->computeChangeSets(); - } elseif (is_object($document)) { - $this->computeSingleDocumentChangeSet($document); - } elseif (is_array($document)) { - foreach ($document as $object) { - $this->computeSingleDocumentChangeSet($object); - } - } + $this->computeChangeSets(); if ( ! ($this->documentInsertions || $this->documentUpserts || @@ -517,52 +508,6 @@ private function computeScheduleUpsertsChangeSets() } } - /** - * Only flush the given document according to a ruleset that keeps the UoW consistent. - * - * 1. All documents scheduled for insertion and (orphan) removals are processed as well! - * 2. Proxies are skipped. - * 3. Only if document is properly managed. - * - * @param object $document - * @throws \InvalidArgumentException If the document is not STATE_MANAGED - * @return void - */ - private function computeSingleDocumentChangeSet($document) - { - $state = $this->getDocumentState($document); - - if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) { - throw new \InvalidArgumentException('Document has to be managed or scheduled for removal for single computation ' . $this->objToStr($document)); - } - - $class = $this->dm->getClassMetadata(get_class($document)); - - if ($state === self::STATE_MANAGED && $class->isChangeTrackingDeferredImplicit()) { - $this->persist($document); - } - - // Compute changes for INSERTed and UPSERTed documents first. This must always happen even in this case. - $this->computeScheduleInsertsChangeSets(); - $this->computeScheduleUpsertsChangeSets(); - - // Ignore uninitialized proxy objects - if ($document instanceof Proxy && ! $document->__isInitialized__) { - return; - } - - // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here. - $oid = spl_object_hash($document); - - if ( ! isset($this->documentInsertions[$oid]) - && ! isset($this->documentUpserts[$oid]) - && ! isset($this->documentDeletions[$oid]) - && isset($this->documentStates[$oid]) - ) { - $this->computeChangeSet($class, $document); - } - } - /** * Gets the changeset for a document. *