Skip to content

Commit

Permalink
wip: fix readpreference tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Nov 27, 2017
1 parent 81f7080 commit 52d381c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 43 deletions.
50 changes: 7 additions & 43 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/ReadPreferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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());
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 52d381c

Please sign in to comment.