Skip to content

Loading…

CollectionTest refactoring and get/setReadPreference() methods #92

Merged
merged 3 commits into from

2 participants

@jmikola
Doctrine member

No description provided.

@jwage
Doctrine member

:+1:

@jmikola
Doctrine member

I will make a 1.0.x branch before merging this, since it'd be released as 1.1.

@jmikola jmikola added a commit to jmikola/mongo-php-driver-legacy that referenced this pull request
@jmikola jmikola Fixed PHP-668: Allow empty setReadPreference() tags param for primary…
… mode

See doctrine/mongodb#92 for why this was helpful.
99850ee
@jmikola jmikola added a commit to mongodb/mongo-php-driver-legacy that referenced this pull request
@jmikola jmikola Fixed PHP-668: Allow empty setReadPreference() tags param for primary…
… mode

See doctrine/mongodb#92 for why this was helpful.
befc886
@jmikola jmikola merged commit bc59129 into master

1 check passed

Details default The Travis build passed
@jmikola jmikola deleted the read-pref-methods branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
View
14 lib/Doctrine/MongoDB/Collection.php
@@ -666,6 +666,20 @@ public function getSlaveOkay()
return \MongoClient::RP_PRIMARY !== $readPref['type'];
}
+ public function getReadPreference()
+ {
+ return $this->getMongoCollection()->getReadPreference();
+ }
+
+ public function setReadPreference($readPreference, array $tags = null)
+ {
+ if (isset($tags)) {
+ return $this->getMongoCollection()->setReadPreference($readPreference, $tags);
+ }
+
+ return $this->getMongoCollection()->setReadPreference($readPreference);
+ }
+
public function validate($scanData = false)
{
return $this->getMongoCollection()->validate($scanData);
View
14 lib/Doctrine/MongoDB/Connection.php
@@ -258,6 +258,20 @@ public function selectDatabase($name)
return $database;
}
+ public function getReadPreference()
+ {
+ return $this->mongo->getReadPreference();
+ }
+
+ public function setReadPreference($readPreference, array $tags = null)
+ {
+ if (isset($tags)) {
+ return $this->mongo->setReadPreference($readPreference, $tags);
+ }
+
+ return $this->mongo->setReadPreference($readPreference);
+ }
+
/**
* Method which creates a Doctrine\MongoDB\Database instance.
*
View
14 lib/Doctrine/MongoDB/Cursor.php
@@ -387,6 +387,20 @@ public function getSingleResult()
return $result;
}
+ public function getReadPreference()
+ {
+ return $this->getMongoDB()->getReadPreference();
+ }
+
+ public function setReadPreference($readPreference, array $tags = null)
+ {
+ if (isset($tags)) {
+ return $this->mongoCursor->setReadPreference($readPreference, $tags);
+ }
+
+ return $this->mongoCursor->setReadPreference($readPreference);
+ }
+
protected function retry(\Closure $retry, $recreate = false)
{
if ($this->numRetries) {
View
14 lib/Doctrine/MongoDB/Database.php
@@ -250,6 +250,20 @@ public function getSlaveOkay()
return \MongoClient::RP_PRIMARY !== $readPref['type'];
}
+ public function getReadPreference()
+ {
+ return $this->getMongoDB()->getReadPreference();
+ }
+
+ public function setReadPreference($readPreference, array $tags = null)
+ {
+ if (isset($tags)) {
+ return $this->getMongoDB()->setReadPreference($readPreference, $tags);
+ }
+
+ return $this->getMongoDB()->setReadPreference($readPreference);
+ }
+
public function getProfilingLevel()
{
return $this->getMongoDB()->getProfilingLevel();
View
518 tests/Doctrine/MongoDB/Tests/CollectionTest.php
@@ -4,394 +4,414 @@
use Doctrine\MongoDB\Collection;
use Doctrine\MongoDB\Connection;
-use Doctrine\MongoDB\LoggableCollection;
use Doctrine\MongoDB\Database;
use Doctrine\Common\EventManager;
-use MongoCollection;
-use PHPUnit_Framework_TestCase;
-class CollectionTest extends PHPUnit_Framework_TestCase
+class CollectionTest extends \PHPUnit_Framework_TestCase
{
- public function testLog()
+ public function testBatchInsert()
{
- $mockConnection = $this->getMockConnection();
- $mongoCollection = $this->getMockMongoCollection();
- $mongoCollection->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('collection'));
+ $docs = array(array('x' => 1, 'y' => 2));
+ $options = array('w'=> 0);
- $mockDatabase = $this->getMockDatabase();
- $mockDatabase->expects($this->once())
- ->method('getName')
- ->will($this->returnValue('db'));
+ $mongoCollection = $this->getMockMongoCollection();
- $called = false;
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase, function($msg) use (&$called) {
- $called = $msg;
- });
- $coll->log(array('test' => 'test'));
- $this->assertEquals(array('collection' => 'collection', 'db' => 'db', 'test' => 'test'), $called);
- }
+ $mongoCollection->expects($this->once())
+ ->method('batchInsert')
+ ->with($docs, $options)
+ ->will($this->returnValue(true));
- public function testBatchInsert()
- {
- $mockConnection = $this->getMockConnection();
- $mongoCollection = $this->getMockMongoCollection();
- $mockDatabase = $this->getMockDatabase();
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $doc = array();
- $result = $coll->batchInsert($doc, array());
- $this->assertEquals(array(), $result);
+ $this->assertEquals($docs, $coll->batchInsert($docs, $options));
}
public function testUpdate()
{
- $mockConnection = $this->getMockConnection();
+ $criteria = array('x' => 1);
+ $newObj = array('$set' => array('x' => 2));
+ $options = array('w'=> 0);
+
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('update')
- ->with(array(), array(), array())
- ->will($this->returnValue(array()));
+ ->with($criteria, $newObj, $options)
+ ->will($this->returnValue(true));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->update(array(), array(), array());
- $this->assertEquals(array(), $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertTrue($coll->update($criteria, $newObj, $options));
}
public function testFind()
{
- $mockConnection = $this->getMockConnection();
- $mockMongoCursor = $this->getMockMongoCursor();
+ $query = array('x' => 1);
+ $fields = array('x' => 1, 'y' => 1, '_id' => 0);
+
+ $mongoCursor = $this->getMockMongoCursor();
$mongoCollection = $this->getMockMongoCollection();
$mongoCollection->expects($this->once())
->method('find')
- ->with(array(), array())
- ->will($this->returnValue($mockMongoCursor));
+ ->with($query, $fields)
+ ->will($this->returnValue($mongoCursor));
+
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+ $result = $coll->find($query, $fields);
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->find(array(), array());
- $this->assertEquals($mockMongoCursor, $result->getMongoCursor());
+ $this->assertInstanceOf('Doctrine\MongoDB\Cursor', $result);
+ $this->assertSame($mongoCursor, $result->getMongoCursor());
}
public function testFindOne()
{
- $mockConnection = $this->getMockConnection();
+ $query = array('x' => 1);
+ $fields = array('x' => 1, 'y' => 1, '_id' => 0);
+ $document = array('x' => 1, 'y' => 'foo');
+
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('findOne')
- ->with(array(), array())
- ->will($this->returnValue(array()));
+ ->with($query, $fields)
+ ->will($this->returnValue($document));
+
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->findOne(array(), array());
- $this->assertEquals(array(), $result);
+ $this->assertEquals($document, $coll->findOne($query, $fields));
}
public function testFindAndRemove()
{
- $mockConnection = $this->getMockConnection();
- $mongoCollection = $this->getMockMongoCollection();
- $mongoCollection->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('coll_name'));
+ $query = array('completed' => true);
- $query = array('name' => 'jon');
- $options = array('safe' => true);
$command = array(
- 'findandmodify' => 'coll_name',
- 'safe' => true,
+ 'findandmodify' => 'collection',
'query' => $query,
'remove' => true,
);
- $document = array('_id' => new \MongoId(), 'test' => 'cool');
- $mockDatabase = $this->getMockDatabase();
- $mockDatabase->expects($this->once())
+ $document = array('_id' => 1, 'completed' => true);
+
+ $mongoCollection = $this->getMockMongoCollection();
+ $database = $this->getMockDatabase();
+
+ $mongoCollection->expects($this->any())
+ ->method('getName')
+ ->will($this->returnValue('collection'));
+
+ $database->expects($this->once())
->method('command')
->with($command)
->will($this->returnValue(array('value' => $document)));
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->findAndRemove($query, $options);
- $this->assertEquals($document, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $database);
+
+ $this->assertEquals($document, $coll->findAndRemove($query));
}
public function testFindAndModify()
{
- $mockConnection = $this->getMockConnection();
- $mongoCollection = $this->getMockMongoCollection();
- $mongoCollection->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('coll_name'));
+ $query = array('inprogress' => false);
+ $newObj = array('$set' => array('inprogress' => true));
+ $options = array('new' => true);
- $query = array('name' => 'jon');
- $newObj = array('name' => 'ok');
- $options = array('safe' => true);
$command = array(
- 'findandmodify' => 'coll_name',
+ 'findandmodify' => 'collection',
'query' => $query,
- 'safe' => true,
- 'update' => array(
- 'name' => 'ok'
- ),
+ 'update' => $newObj,
+ 'new' => true,
);
- $document = array('_id' => new \MongoId(), 'test' => 'cool');
- $mockDatabase = $this->getMockDatabase();
- $mockDatabase->expects($this->once())
+ $document = array('_id' => 1, 'inprogress' => true);
+
+ $mongoCollection = $this->getMockMongoCollection();
+ $database = $this->getMockDatabase();
+
+ $mongoCollection->expects($this->any())
+ ->method('getName')
+ ->will($this->returnValue('collection'));
+
+ $database->expects($this->once())
->method('command')
->with($command)
->will($this->returnValue(array('value' => $document)));
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->findAndUpdate($query, $newObj, $options);
- $this->assertEquals($document, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $database);
+
+ $this->assertEquals($document, $coll->findAndUpdate($query, $newObj, $options));
}
- public function testCount()
+ public function testCountWithParameters()
{
- $mockConnection = $this->getMockConnection();
+ $query = array('x' => 1);
+
$mongoCollection = $this->getMockMongoCollection();
+
+ $mongoCollection->expects($this->once())
+ ->method('count')
+ ->with($query, 1, 1)
+ ->will($this->returnValue(1));
+
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertEquals(1, $coll->count($query, 1, 1));
+ }
+
+ public function testCountWithoutParameters()
+ {
+ $mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('count')
->with(array(), 0, 0)
->will($this->returnValue(1));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->count(array(), 0, 0);
- $this->assertEquals(1, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertEquals(1, $coll->count());
}
public function testCreateDBRef()
{
- $mockConnection = $this->getMockConnection();
+ $document = array('_id' => 1);
+ $dbRef = array('$ref' => 'test', '$id' => 1);
+
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('createDBRef')
- ->with(array())
- ->will($this->returnValue(true));
+ ->with($document)
+ ->will($this->returnValue($dbRef));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->createDBRef(array());
- $this->assertEquals(true, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertEquals($dbRef, $coll->createDBRef($document));
}
public function testDeleteIndex()
{
- $mockConnection = $this->getMockConnection();
$mongoCollection = $this->getMockMongoCollection();
$mongoCollection->expects($this->once())
->method('deleteIndex')
- ->with(array())
- ->will($this->returnValue(true));
+ ->with('foo')
+ ->will($this->returnValue(array()));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->deleteIndex(array());
- $this->assertEquals(true, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertEquals(array(), $coll->deleteIndex('foo'));
}
public function testDeleteIndexes()
{
- $mockConnection = $this->getMockConnection();
$mongoCollection = $this->getMockMongoCollection();
$mongoCollection->expects($this->once())
->method('deleteIndexes')
- ->will($this->returnValue(true));
+ ->will($this->returnValue(array()));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->deleteIndexes();
- $this->assertEquals(true, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertEquals(array(), $coll->deleteIndexes());
}
public function testDrop()
{
- $mockConnection = $this->getMockConnection();
$mongoCollection = $this->getMockMongoCollection();
$mongoCollection->expects($this->once())
->method('drop')
->will($this->returnValue(true));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
$result = $coll->drop();
$this->assertEquals(true, $result);
}
public function testEnsureIndex()
{
- $mockConnection = $this->getMockConnection();
+ $keys = array('x' => 1);
+ $options = array('w' => 0);
+
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('ensureIndex')
- ->with(array(), array())
+ ->with($keys, $options)
->will($this->returnValue(true));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->ensureIndex(array(), array());
- $this->assertEquals(true, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertTrue($coll->ensureIndex($keys, $options));
}
public function testGetDBRef()
{
- $mockConnection = $this->getMockConnection();
+ $document = array('_id' => 1);
+ $dbRef = array('$ref' => 'test', '$id' => 1);
+
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('getDBRef')
- ->with(array())
- ->will($this->returnValue(true));
+ ->with($dbRef)
+ ->will($this->returnValue($document));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->getDBRef(array());
- $this->assertEquals(true, $result);
- }
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
- public function testGetIndexInfo()
- {
- $mockConnection = $this->getMockConnection();
- $mongoCollection = $this->getMockMongoCollection();
- $mongoCollection->expects($this->once())
- ->method('getIndexInfo')
- ->will($this->returnValue(true));
-
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->getIndexInfo();
- $this->assertEquals(true, $result);
+ $this->assertEquals($document, $coll->getDBRef($dbRef));
}
- public function testIsFieldIndexedTrue()
+ /**
+ * @covers Collection::getIndexInfo
+ * @dataProvider provideIsFieldIndex
+ */
+ public function testIsFieldIndexed($indexInfo, $field, $expectedResult)
{
- $mockConnection = $this->getMockConnection();
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('getIndexInfo')
- ->will($this->returnValue(array(array('key' => array('test' => 1)))));
+ ->will($this->returnValue($indexInfo));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->isFieldIndexed('test');
- $this->assertEquals(true, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertEquals($expectedResult, $coll->isFieldIndexed($field));
}
- public function testIsFieldIndexedFalse()
+ public function provideIsFieldIndex()
{
- $mockConnection = $this->getMockConnection();
- $mongoCollection = $this->getMockMongoCollection();
- $mongoCollection->expects($this->once())
- ->method('getIndexInfo')
- ->will($this->returnValue(array(array('key' => array('test' => 1)))));
+ $indexInfo = array(
+ array(
+ 'name' => '_id_',
+ 'ns' => 'test.foo',
+ 'key' => array('_id' => 1),
+ ),
+ array(
+ 'name' => 'bar_1_bat_-1',
+ 'ns' => 'test.foo',
+ 'key' => array('bar' => 1, 'bat' => -1)
+ ),
+ );
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->isFieldIndexed('doesnt-exist');
- $this->assertEquals(false, $result);
+ return array(
+ array($indexInfo, '_id', true),
+ array($indexInfo, 'bar', true),
+ array($indexInfo, 'bat', true),
+ array($indexInfo, 'baz', false),
+ );
}
public function testGetName()
{
- $mockConnection = $this->getMockConnection();
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('getName')
- ->will($this->returnValue(true));
+ ->will($this->returnValue('collection'));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->getName();
- $this->assertEquals(true, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertEquals('collection', $coll->getName());
}
public function testGroupWithNonEmptyOptionsArray()
{
- $expectedOptions = array(
- 'condition' => array(),
- 'finalize' => new \MongoCode(''),
+ $keys = array('category' => 1);
+ $initial = array('items' => array());
+ $options = array('finalize' => '');
+ $grouped = array(
+ array('category' => 'fruit', 'items' => array('apple', 'peach', 'banana')),
+ array('category' => 'veggie', 'items' => array('corn', 'broccoli')),
);
- $mockConnection = $this->getMockConnection();
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('group')
- ->with(array(), array(), $this->isInstanceOf('MongoCode'), $this->equalTo($expectedOptions))
- ->will($this->returnValue(array()));
+ ->with($keys, $initial, $this->isInstanceOf('MongoCode'), $this->callback(function($options) {
+ return $options['finalize'] instanceof \MongoCode;
+ }))
+ ->will($this->returnValue($grouped));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->group(array(), array(), '', array('condition' => array(), 'finalize' => ''));
- $this->assertEquals(new \Doctrine\MongoDB\ArrayIterator(array()), $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+ $result = $coll->group($keys, $initial, '', $options);
+
+ $this->assertInstanceOf('Doctrine\MongoDB\ArrayIterator', $result);
+ $this->assertEquals($grouped, $result->toArray());
}
public function testGroupWithEmptyOptionsArray()
{
- $mockConnection = $this->getMockConnection();
+ $keys = array('category' => 1);
+ $initial = array('items' => array());
+ $grouped = array(
+ array('category' => 'fruit', 'items' => array('apple', 'peach', 'banana')),
+ array('category' => 'veggie', 'items' => array('corn', 'broccoli')),
+ );
+
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('group')
- ->with(array(), array(), $this->isInstanceOf('MongoCode'))
- ->will($this->returnValue(array()));
+ ->with($keys, $initial, $this->isInstanceOf('MongoCode'))
+ ->will($this->returnValue($grouped));
+
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+ $result = $coll->group($keys, $initial, '');
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->group(array(), array(), '');
- $this->assertEquals(new \Doctrine\MongoDB\ArrayIterator(array()), $result);
+ $this->assertInstanceOf('Doctrine\MongoDB\ArrayIterator', $result);
+ $this->assertEquals($grouped, $result->toArray());
}
public function testInsert()
{
- $mockConnection = $this->getMockConnection();
+ $document = array('x' => 1);
+ $options = array('w' => 0);
+
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('insert')
- ->with(array(), array())
+ ->with($document, $options)
->will($this->returnValue(true));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $document = array();
- $result = $coll->insert($document, array());
- $this->assertEquals(true, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertTrue($coll->insert($document, $options));
}
public function testRemove()
{
- $mockConnection = $this->getMockConnection();
+ $criteria = array('x' => 1);
+ $options = array('w' => 0);
+
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('remove')
- ->with(array(), array())
+ ->with($criteria, $options)
->will($this->returnValue(true));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->remove(array(), array());
- $this->assertEquals(true, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertTrue($coll->remove($criteria, $options));
}
public function testSave()
{
- $mockConnection = $this->getMockConnection();
+ $document = array('x' => 1);
+ $options = array('w' => 0);
+
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('save')
- ->with(array(), array())
+ ->with($document, $options)
->will($this->returnValue(true));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $document = array();
- $result = $coll->save($document, array());
- $this->assertArrayHasKeyValue(array('ok' => 1.0), $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertTrue($coll->save($document, $options));
}
public function testGetSetSlaveOkay()
@@ -470,92 +490,90 @@ public function testSetSlaveOkayPreservesReadPreferenceTags()
$this->assertEquals(true, $collection->setSlaveOkay(true));
}
+ public function testSetReadPreference()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
+ $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
+ }
+
+ $mongoCollection = $this->getMockMongoCollection();
+
+ $mongoCollection->expects($this->at(1))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_PRIMARY)
+ ->will($this->returnValue(true));
+
+ $mongoCollection->expects($this->at(2))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))
+ ->will($this->returnValue(true));
+
+ $collection = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertTrue($collection->setReadPreference(\MongoClient::RP_PRIMARY));
+ $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east'))));
+ }
+
public function testValidate()
{
- $mockConnection = $this->getMockConnection();
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('validate')
->will($this->returnValue(true));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->validate();
- $this->assertEquals(true, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertTrue($coll->validate());
}
public function testToString()
{
- $mockConnection = $this->getMockConnection();
$mongoCollection = $this->getMockMongoCollection();
+
$mongoCollection->expects($this->once())
->method('__toString')
- ->will($this->returnValue(true));
+ ->will($this->returnValue('collection'));
- $mockDatabase = $this->getMockDatabase();
- $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $document = array();
- $result = $coll->__toString();
- $this->assertEquals(true, $result);
+ $coll = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertEquals('collection', $coll->__toString());
}
private function getMockMongoCursor()
{
- return $this->getMock('MongoCursor', array(), array(), '', false, false);
+ return $this->getMockBuilder('MongoCursor')
+ ->disableOriginalConstructor()
+ ->getMock();
}
private function getMockMongoCollection()
{
- return $this->getMock('MongoCollection', array(), array(), '', false, false);
- }
-
- private function getMockMongoDB()
- {
- return $this->getMock('MongoDB', array(), array(), '', false, false);
+ return $this->getMockBuilder('MongoCollection')
+ ->disableOriginalConstructor()
+ ->getMock();
}
private function getMockDatabase()
{
- return $this->getMock('Doctrine\MongoDB\Database', array(), array(), '', false, false);
+ return $this->getMockBuilder('Doctrine\MongoDB\Database')
+ ->disableOriginalConstructor()
+ ->getMock();
}
private function getMockConnection()
{
- return $this->getMock('Doctrine\MongoDB\Connection', array(), array(), '', false, false);
+ return $this->getMockBuilder('Doctrine\MongoDB\Connection')
+ ->disableOriginalConstructor()
+ ->getMock();
}
- private function getTestCollection(Connection $connection, MongoCollection $mongoCollection, Database $db, $loggerCallable = null)
+ private function getTestCollection(Connection $connection, \MongoCollection $mongoCollection, Database $db)
{
- if (null === $loggerCallable) {
- $collection = new TestCollectionStub($connection, $mongoCollection->getName(), $db, new EventManager(), '$');
- $collection->setMongoCollection($mongoCollection);
- return $collection;
- }
- $collection = new TestLoggableCollectionStub($connection, $mongoCollection->getName(), $db, new EventManager(), '$', $loggerCallable);
+ $collection = new TestCollectionStub($connection, $mongoCollection->getName(), $db, new EventManager(), '$');
$collection->setMongoCollection($mongoCollection);
return $collection;
}
-
- private function assertArrayHasKeyValue($expected, $array, $message = '')
- {
- foreach ((array) $expected as $key => $value) {
- $this->assertArrayHasKey($key, $expected, $message);
- $this->assertEquals($value, $expected[$key], $message);
- }
- }
-}
-
-class TestLoggableCollectionStub extends LoggableCollection
-{
- public function setMongoCollection($mongoCollection)
- {
- $this->mongoCollection = $mongoCollection;
- }
-
- public function getMongoCollection()
- {
- return $this->mongoCollection;
- }
}
class TestCollectionStub extends Collection
View
35 tests/Doctrine/MongoDB/Tests/ConnectionTest.php
@@ -167,6 +167,30 @@ public function testSelectDatabase()
$this->assertSame($mockMongoDB, $result->getMongoDB());
}
+ public function testSetReadPreference()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
+ $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
+ }
+
+ $mongoClient = $this->getMockMongoClient();
+
+ $mongoClient->expects($this->at(0))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_PRIMARY)
+ ->will($this->returnValue(true));
+
+ $mongoClient->expects($this->at(1))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))
+ ->will($this->returnValue(true));
+
+ $conn = $this->getTestConnection($mongoClient);
+
+ $this->assertTrue($conn->setReadPreference(\MongoClient::RP_PRIMARY));
+ $this->assertTrue($conn->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east'))));
+ }
+
public function testToString()
{
$mockMongo = $this->getMockMongo();
@@ -178,9 +202,9 @@ public function testToString()
$this->assertEquals('Test', (string) $conn);
}
- private function getTestConnection(Mongo $mongo)
+ private function getTestConnection($mongo)
{
- return new \Doctrine\MongoDB\Connection($mongo);
+ return new Connection($mongo);
}
private function getMockMongo()
@@ -188,6 +212,13 @@ private function getMockMongo()
return $this->getMock('Mongo', array(), array(), '', false, false);
}
+ private function getMockMongoClient()
+ {
+ return $this->getMockBuilder('MongoClient')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
private function getMockMongoDB()
{
return $this->getMock('MongoDB', array(), array(), '', false, false);
View
24 tests/Doctrine/MongoDB/Tests/CursorTest.php
@@ -184,6 +184,30 @@ public function testSlaveOkayPreservesReadPreferenceTags()
$cursor->slaveOkay(true);
}
+ public function testSetReadPreference()
+ {
+ if (!method_exists('MongoCursor', 'setReadPreference')) {
+ $this->markTestSkipped('This test is not applicable to drivers without MongoCursor::setReadPreference()');
+ }
+
+ $mongoCursor = $this->getMockMongoCursor();
+
+ $mongoCursor->expects($this->at(0))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_PRIMARY)
+ ->will($this->returnValue(true));
+
+ $mongoCursor->expects($this->at(1))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))
+ ->will($this->returnValue(true));
+
+ $cursor = $this->getTestCursor($this->getMockConnection(), $this->getMockCollection(), $mongoCursor);
+
+ $this->assertTrue($cursor->setReadPreference(\MongoClient::RP_PRIMARY));
+ $this->assertTrue($cursor->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east'))));
+ }
+
private function getMockMongoCursor()
{
return $this->getMockBuilder('MongoCursor')
View
22 tests/Doctrine/MongoDB/Tests/DatabaseTest.php
@@ -100,6 +100,28 @@ public function testSetSlaveOkayPreservesReadPreferenceTags()
$this->assertEquals(true, $database->setSlaveOkay(true));
}
+ public function testSetReadPreference()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
+ $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
+ }
+
+ $this->mongodb->expects($this->at(0))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_PRIMARY)
+ ->will($this->returnValue(true));
+
+ $this->mongodb->expects($this->at(1))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))
+ ->will($this->returnValue(true));
+
+ $database = new Database($this->connection, 'test', $this->getMockEventManager(), '$');
+
+ $this->assertTrue($database->setReadPreference(\MongoClient::RP_PRIMARY));
+ $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east'))));
+ }
+
private function getMockConnection()
{
return $this->getMockBuilder('Doctrine\MongoDB\Connection')
View
43 tests/Doctrine/MongoDB/Tests/LoggableCollectionTest.php
@@ -0,0 +1,43 @@
+<?php
+
+namespace Doctrine\MongoDB\Tests;
+
+use Doctrine\MongoDB\LoggableCollection;
+use Doctrine\Common\EventManager;
+
+class LoggableCollectionTest extends \PHPUnit_Framework_TestCase
+{
+ public function testLog()
+ {
+ $called = false;
+
+ $loggerCallable = function($msg) use (&$called) {
+ $called = $msg;
+ };
+
+ $database = $this->getMockDatabase();
+
+ $database->expects($this->once())
+ ->method('getName')
+ ->will($this->returnValue('test'));
+
+ $coll = new LoggableCollection($this->getMockConnection(), 'foo', $database, new EventManager(), '$', $loggerCallable);
+ $coll->log(array('test' => 'test'));
+
+ $this->assertEquals(array('collection' => 'foo', 'db' => 'test', 'test' => 'test'), $called);
+ }
+
+ private function getMockDatabase()
+ {
+ return $this->getMockBuilder('Doctrine\MongoDB\Database')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ private function getMockConnection()
+ {
+ return $this->getMockBuilder('Doctrine\MongoDB\Connection')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+}
Something went wrong with that request. Please try again.