Skip to content

Commit

Permalink
Updated existing tests for CollectionPersister for tracking executed …
Browse files Browse the repository at this point in the history
…queries where it is required. Added documents for future tests for this service.
  • Loading branch information
watari committed Nov 28, 2018
1 parent 50eef5e commit 06fbf52
Showing 1 changed file with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Tests\Functional;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\MongoDB\APM\CommandLogger;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Persisters\CollectionPersister;
use Doctrine\ODM\MongoDB\Persisters\PersistenceBuilder;
Expand All @@ -13,6 +14,24 @@

class CollectionPersisterTest extends BaseTest
{
/** @var CommandLogger */
private $logger;

public function setUp()
{
parent::setUp();

$this->logger = new CommandLogger();
$this->logger->register();
}

public function tearDown()
{
$this->logger->unregister();

return parent::tearDown();
}

public function testDeleteReferenceMany()
{
$persister = $this->getCollectionPersister();
Expand Down Expand Up @@ -76,47 +95,55 @@ public function testDeleteAllReferenceMany()
$this->assertArrayNotHasKey('phonenumbers', $user, 'Test that the phonenumbers field was deleted');
}

public function testDeleteAllNestedEmbedMany()
public function testDeleteAllNestedEmbedMany(): void
{
$persister = $this->getCollectionPersister();
$user = $this->getTestUser('jwage');
$this->logger->clear();
$persister->deleteAll(
$user,
[$user->categories[0]->children[0]->children, $user->categories[0]->children[1]->children],
[]
);
$this->assertCount(1, $this->logger, 'Deletion of several embedded-many collections of one document requires one query');
$check = $this->dm->getDocumentCollection(CollectionPersisterUser::class)->findOne(['username' => 'jwage']);
$this->assertFalse(isset($check['categories']['0']['children'][0]['children']));
$this->assertFalse(isset($check['categories']['0']['children'][1]['children']));
$this->logger->clear();
$persister->deleteAll(
$user,
[$user->categories[0]->children, $user->categories[1]->children],
[]
);
$this->assertCount(1, $this->logger, 'Deletion of several embedded-many collections of one document requires one query');
$check = $this->dm->getDocumentCollection(CollectionPersisterUser::class)->findOne(['username' => 'jwage']);
$this->assertFalse(isset($check['categories'][0]['children']), 'Test that the nested children categories field was deleted');
$this->assertTrue(isset($check['categories'][0]), 'Test that the category with the children still exists');
$this->assertFalse(isset($check['categories'][1]['children']), 'Test that the nested children categories field was deleted');
$this->assertTrue(isset($check['categories'][1]), 'Test that the category with the children still exists');
}

public function testDeleteAllNestedEmbedManyAndNestedParent()
public function testDeleteAllNestedEmbedManyAndNestedParent(): void
{
$persister = $this->getCollectionPersister();
$user = $this->getTestUser('jwage');
$this->logger->clear();
$persister->deleteAll(
$user,
[$user->categories[0]->children[0]->children, $user->categories[0]->children[1]->children],
[]
);
$this->assertCount(1, $this->logger, 'Deletion of several embedded-many collections of one document requires one query');
$check = $this->dm->getDocumentCollection(CollectionPersisterUser::class)->findOne(['username' => 'jwage']);
$this->assertFalse(isset($check['categories']['0']['children'][0]['children']));
$this->assertFalse(isset($check['categories']['0']['children'][1]['children']));
$this->logger->clear();
$persister->deleteAll(
$user,
[$user->categories[0]->children, $user->categories[0]->children[1]->children, $user->categories],
[]
);
$this->assertCount(1, $this->logger, 'Deletion of several embedded-many collections of one document requires one query');
$check = $this->dm->getDocumentCollection(CollectionPersisterUser::class)->findOne(['username' => 'jwage']);
$this->assertFalse(isset($check['categories']), 'Test that the nested categories field was deleted');
}
Expand All @@ -136,7 +163,9 @@ public function testDeleteRows()
unset($user->categories[0]->children[1]->children[0]);
unset($user->categories[0]->children[1]->children[1]);

$this->logger->clear();
$this->dm->flush();
$this->assertCount(2, $this->logger, 'Modification of several embedded-many collections of one document requires two queries');

$check = $this->dm->getDocumentCollection(CollectionPersisterUser::class)->findOne(['username' => 'jwage']);

Expand All @@ -151,7 +180,9 @@ public function testDeleteRows()

unset($user->categories[0]);
unset($user->categories[1]);
$this->logger->clear();
$this->dm->flush();
$this->assertCount(2, $this->logger, 'Modification of embedded-many collection of one document requires two queries');

$check = $this->dm->getDocumentCollection(CollectionPersisterUser::class)->findOne(['username' => 'jwage']);
$this->assertFalse(isset($check['categories'][0]));
Expand All @@ -163,7 +194,9 @@ public function testInsertRows()
$user = $this->getTestUser('jwage');
$user->phonenumbers[] = new CollectionPersisterPhonenumber('6155139185');
$user->phonenumbers[] = new CollectionPersisterPhonenumber('6155139185');
$this->logger->clear();
$this->dm->flush();
$this->assertCount(2, $this->logger, 'Modification of embedded-many collection of one document requires two queries');

$check = $this->dm->getDocumentCollection(CollectionPersisterUser::class)->findOne(['username' => 'jwage']);
$this->assertCount(4, $check['phonenumbers']);
Expand All @@ -172,7 +205,13 @@ public function testInsertRows()

$user->categories[] = new CollectionPersisterCategory('Test');
$user->categories[] = new CollectionPersisterCategory('Test');
$this->logger->clear();
$this->dm->flush();
$this->assertCount(
1,
$this->logger,
'Modification of embedded-many collection of one document requires one query since no existing collection elements was removed'
);

$check = $this->dm->getDocumentCollection(CollectionPersisterUser::class)->findOne(['username' => 'jwage']);
$this->assertCount(4, $check['categories']);
Expand All @@ -181,7 +220,13 @@ public function testInsertRows()
$user->categories[3]->children[1] = new CollectionPersisterCategory('Test');
$user->categories[3]->children[1]->children[0] = new CollectionPersisterCategory('Test');
$user->categories[3]->children[1]->children[1] = new CollectionPersisterCategory('Test');
$this->logger->clear();
$this->dm->flush();
$this->assertCount(
1,
$this->logger,
'Modification of embedded-many collection of one document requires one query since no existing collection elements was removed'
);

$check = $this->dm->getDocumentCollection(CollectionPersisterUser::class)->findOne(['username' => 'jwage']);
$this->assertCount(2, $check['categories'][3]['children']);
Expand Down Expand Up @@ -234,7 +279,14 @@ public function testNestedEmbedManySetStrategy()
$commentA->comments->set('b', $commentAB);

$this->dm->persist($post);
$this->logger->clear();
$this->dm->flush();
$this->assertCount(
1,
$this->logger,
'Modification of embedded-many collection of one document requires one query since no existing collection elements was removed'
);


$doc = $this->dm->getDocumentCollection(CollectionPersisterPost::class)->findOne(['post' => 'postA']);

Expand All @@ -255,7 +307,14 @@ public function testNestedEmbedManySetStrategy()
$commentB->comments->set('a', $commentBA);

$this->dm->persist($post);
$this->logger->clear();
$this->dm->flush();
$this->assertCount(
1,
$this->logger,
'Modification of embedded-many collection of one document requires one query since no existing collection elements was removed'
);


$doc = $this->dm->getDocumentCollection(CollectionPersisterPost::class)->findOne(['post' => 'postA']);

Expand All @@ -275,7 +334,14 @@ public function testNestedEmbedManySetStrategy()
$commentAB->comment = 'commentAB-modified';

$this->dm->persist($post);
$this->logger->clear();
$this->dm->flush();
$this->assertCount(
1,
$this->logger,
'Modification of embedded-many collection of one document requires one query since no existing collection elements was removed'
);


$doc = $this->dm->getDocumentCollection(CollectionPersisterPost::class)->findOne(['post' => 'postA']);

Expand All @@ -292,7 +358,14 @@ public function testNestedEmbedManySetStrategy()
unset($post->comments['b']);

$this->dm->persist($post);
$this->logger->clear();
$this->dm->flush();
$this->assertCount(
1,
$this->logger,
'Modification of embedded-many collection of one document requires one query since collection, from which element was removed, have "set" store strategy.'
);


$doc = $this->dm->getDocumentCollection(CollectionPersisterPost::class)->findOne(['post' => 'postA']);

Expand Down Expand Up @@ -408,3 +481,44 @@ public function __construct($comment, $by)
$this->by = $by;
}
}

/** @ODM\Document(collection="structure_collection_persister_test") */
class CollectionPersisterStructure
{
/** @ODM\Id */
public $id;

/** @ODM\EmbedMany(targetDocument=CollectionPersisterNestedStructure::class, strategy="addToSet") */
public $addToSet;

/** @ODM\EmbedMany(targetDocument=CollectionPersisterNestedStructure::class, strategy="set") */
public $set;

/** @ODM\EmbedMany(targetDocument=CollectionPersisterNestedStructure::class, strategy="setArray") */
public $setArray;

/** @ODM\EmbedMany(targetDocument=CollectionPersisterNestedStructure::class, strategy="pushAll") */
public $pushAll;

public function __construct()
{
$this->addToSet = new ArrayCollection();
$this->set = new ArrayCollection();
$this->setArray = new ArrayCollection();
$this->pushAll = new ArrayCollection();
}
}
/** @ODM\EmbeddedDocument */
class CollectionPersisterNestedStructure
{
/** @ODM\Id */
public $id;

/** @ODM\Field(type="string") */
public $field;

public function __construct(string $field)
{
$this->field = $field;
}
}

0 comments on commit 06fbf52

Please sign in to comment.