improved MongoId processing and $fields to MongoModel::find()
This commit is contained in:
@ -13,23 +13,40 @@
|
|||||||
abstract class MongoModel extends Model
|
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)
|
public function count($query = array(), $limit = 0, $skip = 0)
|
||||||
{
|
{
|
||||||
return $this->db->count($this->table(), $query, $limit, $skip);
|
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)
|
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();
|
return $this->db->get($this->table(), array('_id' => $id))->fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete($id)
|
public function delete($id)
|
||||||
{
|
{
|
||||||
|
if($this->useMongoId) {
|
||||||
|
if(! $id instanceof MongoId) {
|
||||||
|
$id = new MongoId($id);
|
||||||
|
}
|
||||||
|
}
|
||||||
return $this->db->delete($this->table(), array('_id' => $id));
|
return $this->db->delete($this->table(), array('_id' => $id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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));
|
$conf = array('default' => array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017));
|
||||||
|
|
||||||
$this->dbSetUp($conf);
|
$this->dbSetUp($conf);
|
||||||
|
|
||||||
Config::set('Db', $conf);
|
Config::set('Db', $conf);
|
||||||
if (!class_exists('MockModel')) {
|
if (!class_exists('MockModel')) {
|
||||||
$this->model = $this->getMockForAbstractClass('MongoModel', array(), 'MongoMockModel');
|
$this->model = $this->getMockForAbstractClass('MongoModel', array(), 'MongoMockModel');
|
||||||
@ -71,6 +71,10 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertInstanceOf('MongoStatement', $result);
|
$this->assertInstanceOf('MongoStatement', $result);
|
||||||
$this->assertEquals('milk', $result->limit(2)->order(array('name' => -1))->fetch()->name);
|
$this->assertEquals('milk', $result->limit(2)->order(array('name' => -1))->fetch()->name);
|
||||||
$this->assertEquals('fish', $result->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 = $this->getMock('CacheKey', array('set', 'get'));
|
||||||
$mock->expects($this->exactly(3))
|
$mock->expects($this->exactly(3))
|
||||||
->method('set')
|
->method('set')
|
||||||
->will($this->returnValue(true));
|
->will($this->returnValue(true));
|
||||||
$mock->expects($this->exactly(3))
|
$mock->expects($this->exactly(3))
|
||||||
->method('get')
|
->method('get')
|
||||||
->will($this->returnValue(false));
|
->will($this->returnValue(false));
|
||||||
|
|
||||||
$model = new ReflectionClass('MongoModel');
|
$model = new ReflectionClass('MongoModel');
|
||||||
$method = $model->getMethod('fetchField');
|
$method = $model->getMethod('fetchField');
|
||||||
@ -173,11 +177,66 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('eggs', $result[0]->name);
|
$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()
|
public function tearDown()
|
||||||
{
|
{
|
||||||
$conf = array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017);
|
$conf = array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017);
|
||||||
|
|
||||||
|
|
||||||
$connection = new Mongo('mongodb://' . $conf['hostname'] . ':' . $conf['port']);
|
$connection = new Mongo('mongodb://' . $conf['hostname'] . ':' . $conf['port']);
|
||||||
$db = $connection->selectDB($conf['database']);
|
$db = $connection->selectDB($conf['database']);
|
||||||
$db->authenticate($conf['username'], $conf['password']);
|
$db->authenticate($conf['username'], $conf['password']);
|
||||||
@ -230,9 +289,8 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
|||||||
$collection = 'mongomock';
|
$collection = 'mongomock';
|
||||||
$db->dropCollection($collection);
|
$db->dropCollection($collection);
|
||||||
$collection = $db->selectCollection($collection);
|
$collection = $db->selectCollection($collection);
|
||||||
foreach($data as $document) {
|
foreach ($data as $document) {
|
||||||
$collection->insert($document);
|
$collection->insert($document);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user