From d1723cb87b0fd087397e6a4d61800fbaedb81d93 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Tue, 13 Dec 2011 13:43:15 +0400 Subject: [PATCH] improved MongoId processing and $fields to MongoModel::find() --- model/MongoModel.php | 21 ++++++++++-- tests/model/MongoModelTest.php | 74 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 85 insertions(+), 10 deletions(-) diff --git a/model/MongoModel.php b/model/MongoModel.php index 91f2bf7..f26c582 100644 --- a/model/MongoModel.php +++ b/model/MongoModel.php @@ -13,23 +13,40 @@ abstract class MongoModel extends Model { + protected $useMongoId = true; + + public function __construct($connection = 'default') + { + parent::__construct($connection); + } + public function count($query = array(), $limit = 0, $skip = 0) { return $this->db->count($this->table(), $query, $limit, $skip); } - public function find($condition = array()) + public function find($condition = array(), $fields = array()) { - return $this->db->find($this->table(), $condition); + return $this->db->find($this->table(), $condition, $fields); } public function get($id) { + if($this->useMongoId) { + if(! $id instanceof MongoId) { + $id = new MongoId($id); + } + } return $this->db->get($this->table(), array('_id' => $id))->fetch(); } public function delete($id) { + if($this->useMongoId) { + if(! $id instanceof MongoId) { + $id = new MongoId($id); + } + } return $this->db->delete($this->table(), array('_id' => $id)); } diff --git a/tests/model/MongoModelTest.php b/tests/model/MongoModelTest.php index c9cc58c..1a37774 100644 --- a/tests/model/MongoModelTest.php +++ b/tests/model/MongoModelTest.php @@ -40,7 +40,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase $conf = array('default' => array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017)); $this->dbSetUp($conf); - + Config::set('Db', $conf); if (!class_exists('MockModel')) { $this->model = $this->getMockForAbstractClass('MongoModel', array(), 'MongoMockModel'); @@ -71,6 +71,10 @@ class MongoModelTest extends PHPUnit_Framework_TestCase $this->assertInstanceOf('MongoStatement', $result); $this->assertEquals('milk', $result->limit(2)->order(array('name' => -1))->fetch()->name); $this->assertEquals('fish', $result->fetch()->name); + $result = $this->model->find(array(), array('name'))->fetch(); + $this->assertSame('bread', $result->name); + $this->setExpectedException('PHPUnit_Framework_Error'); + $this->assertNull($result->price); } /** @@ -144,11 +148,11 @@ class MongoModelTest extends PHPUnit_Framework_TestCase $mock = $this->getMock('CacheKey', array('set', 'get')); $mock->expects($this->exactly(3)) - ->method('set') - ->will($this->returnValue(true)); + ->method('set') + ->will($this->returnValue(true)); $mock->expects($this->exactly(3)) - ->method('get') - ->will($this->returnValue(false)); + ->method('get') + ->will($this->returnValue(false)); $model = new ReflectionClass('MongoModel'); $method = $model->getMethod('fetchField'); @@ -173,11 +177,66 @@ class MongoModelTest extends PHPUnit_Framework_TestCase $this->assertEquals('eggs', $result[0]->name); } + /** + * @runInSeparateProcess + * @group Mongo + */ + public function testUseMongoId() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + + $this->assertAttributeEquals(true, 'useMongoId', $this->model); + } + + /** + * @runInSeparateProcess + * @group Mongo + */ + public function testId() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + $class = new ReflectionClass('MongoModel'); + $prop = $class->getProperty('useMongoId'); + $prop->setAccessible(true); + + $this->model->insert(array('_id' => 1, 'name' => 'testbread', 'price' => 3.2, 'quantity' => 10)); + $this->model->insert(array('_id' => 2, 'name' => 'testbread', 'price' => 12, 'quantity' => 2)); + $this->assertSame(2, $this->model->count(array('name' => 'testbread'))); + + $prop->setValue($this->model, false); + $this->model->delete(1); + $this->assertSame(1, $this->model->count(array('name' => 'testbread'))); + } + + /** + * @runInSeparateProcess + * @group Mongo + */ + public function testIdToMongoId() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + + $this->model->insert(array('name' => 'testbread', 'price' => 3.2, 'quantity' => 10)); + $this->model->insert(array('name' => 'testbread', 'price' => 12, 'quantity' => 2)); + $this->assertSame(2, $this->model->count(array('name' => 'testbread'))); + $id = $this->model->find(array('name' => 'testbread'))->limit(1)->fetch()->_id->__toString(); + $this->assertInternalType('string', $id); + $item = $this->model->get($id); + $this->assertSame('testbread', $item->name); + $this->model->delete($id); + $this->assertSame(1, $this->model->count(array('name' => 'testbread'))); + } + public function tearDown() { $conf = array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017); - $connection = new Mongo('mongodb://' . $conf['hostname'] . ':' . $conf['port']); $db = $connection->selectDB($conf['database']); $db->authenticate($conf['username'], $conf['password']); @@ -230,9 +289,8 @@ class MongoModelTest extends PHPUnit_Framework_TestCase $collection = 'mongomock'; $db->dropCollection($collection); $collection = $db->selectCollection($collection); - foreach($data as $document) { + foreach ($data as $document) { $collection->insert($document); } } - }