From 52d381ce5b1e6cf04cc093a1093ed9714b6d86b1 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 27 Nov 2017 20:02:10 +0100 Subject: [PATCH] wip: fix readpreference tests --- .../Tests/Functional/ReadPreferenceTest.php | 50 +++---------------- .../Doctrine/ODM/MongoDB/Tests/QueryTest.php | 18 +++++++ 2 files changed, 25 insertions(+), 43 deletions(-) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReadPreferenceTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReadPreferenceTest.php index f52f1be59c..211a79c623 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReadPreferenceTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReadPreferenceTest.php @@ -48,39 +48,12 @@ public function testHintIsSetOnQuery($readPreference, array $tags = []) ->getQuery() ->execute(); - $this->assertReadPreferenceHint($readPreference, $cursor->getHints()); - $this->assertReadPreferenceTagsHint($tags, $cursor->getHints()); + $this->assertReadPreferenceHint($readPreference, $cursor->getHints(), $tags); $user = $cursor->getSingleResult(); $this->assertInstanceOf('Doctrine\ODM\MongoDB\PersistentCollection', $user->getGroups()); - $this->assertReadPreferenceHint($readPreference, $user->getGroups()->getHints()); - $this->assertReadPreferenceTagsHint($tags, $user->getGroups()->getHints()); - } - - /** - * @group replication_lag - * @dataProvider provideReadPreferenceHints - */ - public function testHintIsSetOnCursor($readPreference, array $tags = []) - { - $cursor = $this->dm->getRepository('Documents\User') - ->createQueryBuilder() - ->getQuery() - ->execute(); - - $cursor->setHints(array( - Query::HINT_READ_PREFERENCE => new ReadPreference($readPreference, $tags), - )); - - $this->assertReadPreferenceHint($readPreference, $cursor->getHints()); - $this->assertReadPreferenceTagsHint($tags, $cursor->getHints()); - - $user = $cursor->getSingleResult(); - - $this->assertInstanceOf('Doctrine\ODM\MongoDB\PersistentCollection', $user->getGroups()); - $this->assertReadPreferenceHint($readPreference, $user->getGroups()->getHints()); - $this->assertReadPreferenceTagsHint($tags, $user->getGroups()->getHints()); + $this->assertReadPreferenceHint($readPreference, $user->getGroups()->getHints(), $tags); } /** @@ -105,8 +78,7 @@ public function testHintIsSetOnPersistentCollection($readPreference, array $tags Query::HINT_READ_PREFERENCE => new ReadPreference($readPreference, $tags), )); - $this->assertReadPreferenceHint($readPreference, $user->getGroups()->getHints()); - $this->assertReadPreferenceTagsHint($tags, $user->getGroups()->getHints()); + $this->assertReadPreferenceHint($readPreference, $user->getGroups()->getHints(), $tags); } public function provideReadPreferenceHints() @@ -128,13 +100,11 @@ public function testDocumentLevelReadPreferenceIsSetInCollection() public function testDocumentLevelReadPreferenceIsAppliedInQueryBuilder() { - $cursor = $this->dm->getRepository(DocumentWithReadPreference::class) + $query = $this->dm->getRepository(DocumentWithReadPreference::class) ->createQueryBuilder() - ->getQuery() - ->execute(); + ->getQuery(); - $this->assertReadPreferenceHint(ReadPreference::RP_NEAREST, $cursor->getHints()); - $this->assertReadPreferenceTagsHint([ ['dc' => 'east'] ], $cursor->getHints()); + $this->assertReadPreferenceHint(ReadPreference::RP_NEAREST, $query->getQuery(), [ ['dc' => 'east'] ]); } public function testDocumentLevelReadPreferenceCanBeOverriddenInQueryBuilder() @@ -146,18 +116,12 @@ public function testDocumentLevelReadPreferenceCanBeOverriddenInQueryBuilder() ->execute(); $this->assertReadPreferenceHint(ReadPreference::RP_SECONDARY, $cursor->getHints()); - $this->assertReadPreferenceTagsHint([], $cursor->getHints()); } - private function assertReadPreferenceHint($readPreference, $hints) + private function assertReadPreferenceHint($readPreference, array $tags = [], $hints) { $this->assertInstanceOf(ReadPreference::class, $hints[Query::HINT_READ_PREFERENCE]); $this->assertEquals($readPreference, $hints[Query::HINT_READ_PREFERENCE]->getMode()); - } - - private function assertReadPreferenceTagsHint(array $tags = [], $hints) - { - $this->assertInstanceOf(ReadPreference::class, $hints[Query::HINT_READ_PREFERENCE]); $this->assertEquals($tags, $hints[Query::HINT_READ_PREFERENCE]->getTagSets()); } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php index e6dfa6ffbf..ab82ab60b8 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php @@ -449,6 +449,24 @@ public function testCountWithOptions() $this->assertSame(100, $query->execute()); } + public function testReadPreference() + { + $readPreference = new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED); + + $collection = $this->getMockCollection(); + $collection->expects($this->once()) + ->method('count') + ->with(['foo' => 'bar'], ['readPreference' => $readPreference]) + ->will($this->returnValue(0)); + + $queryQrray = [ + 'type' => Query::TYPE_COUNT, + 'query' => ['foo' => 'bar'], + ]; + $query = new Query($this->dm, new ClassMetadata(User::class), $collection, $queryQrray, ['readPreference' => $readPreference]); + $query->execute(); + } + private function getMockCollection() { return $this->getMockBuilder(Collection::class)