refactored MongoModel to hide fetch method, fixed PHPDoc errors
This commit is contained in:
@ -16,7 +16,7 @@ require_once dirname(__FILE__) . '/../../model/MongoDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
|
||||
class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
||||
class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
private $conf = array();
|
||||
@ -323,11 +323,14 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testToString()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT, new CollectionMock());
|
||||
$cmd->bindParam('condition', array());
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT);
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
$cmd->bindParam('collection', new CollectionMock());
|
||||
$this->assertStringStartsWith("\n" . 'Collection: CollectionMock', $cmd->__toString());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, new CollectionMock());
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
$cmd->bindParam('collection', new CollectionMock());
|
||||
$cmd->bindParam('condition', array());
|
||||
$this->assertStringStartsWith("\n" . 'Collection: CollectionMock', $cmd->__toString());
|
||||
$this->assertContains('Condition: ' . '[]' . PHP_EOL, $cmd->__toString());
|
||||
|
@ -204,22 +204,6 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject();
|
||||
$this->assertEquals(20, $eggs->quantity);
|
||||
$eggs = $mongo->get('items', array('name' => 'eggs'))->fetch();
|
||||
$this->assertEquals('eggs', $eggs->name);
|
||||
$this->assertInstanceOf('MongoId', $eggs->_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testRemove()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
@ -246,7 +230,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertNotEmpty($mongo->getInsertId());
|
||||
$this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
|
||||
$this->assertEquals(0, $mongo->insert('items', array('name' => 'meat', 'weight' => 230)));
|
||||
$this->assertEquals(230, $mongo->get('items', array('name' => 'meat'))->fetch()->weight);
|
||||
$this->assertEquals(230, $mongo->find('items', array('name' => 'meat'))->fetch()->weight);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -304,7 +288,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
|
||||
$this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')));
|
||||
$this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs')));
|
||||
$fish = $mongo->get('items', array('name' => 'fish'))->fetch();
|
||||
$fish = $mongo->find('items', array('name' => 'fish'))->fetch();
|
||||
$this->assertEquals(200, $fish->price);
|
||||
$this->assertEquals('today', $fish->date);
|
||||
$this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
|
||||
@ -321,8 +305,9 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
|
||||
$mongo->insert('items', array('name' => 'bread'));
|
||||
$this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
|
||||
$this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date);
|
||||
$this->assertInstanceOf('MongoId', $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
|
||||
$this->assertEquals('today', $mongo->find('items', array('name' => 'ball'))->fetch()->date);
|
||||
$this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'eggs'), true, true));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -335,10 +320,10 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
|
||||
$this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
|
||||
$this->assertEquals(10, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity);
|
||||
$result = $mongo->findAndModify('items', array('name' => 'bread'), array('$set' => array('quantity' => 20)));
|
||||
$this->assertEquals(10, $result->fetch()->quantity);
|
||||
$this->assertEquals(20, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
|
||||
$this->assertEquals(20, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -351,10 +336,10 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
|
||||
$this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
|
||||
$this->assertEquals(10, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity);
|
||||
$result = $mongo->findAndModify('items', array('name' => 'breading'), array('$set' => array('quantity' => 20)))->fetch();
|
||||
$this->assertFalse($result);
|
||||
$this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
|
||||
$this->assertEquals(10, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -13,6 +13,7 @@
|
||||
require_once dirname(__FILE__) . '/../../Registry.php';
|
||||
require_once dirname(__FILE__) . '/../../Config.php';
|
||||
require_once dirname(__FILE__) . '/../../cache/Cacher.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbExpr.php';
|
||||
require_once dirname(__FILE__) . '/../../model/Db.php';
|
||||
require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php';
|
||||
@ -27,8 +28,21 @@ require_once dirname(__FILE__) . '/../../model/MongoModel.php';
|
||||
class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var MongoModel
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* @var ReflectionMethod
|
||||
*/
|
||||
private $method_count;
|
||||
|
||||
/**
|
||||
* @var ReflectionMethod
|
||||
*/
|
||||
private $method_fetch;
|
||||
|
||||
public function run(PHPUnit_Framework_TestResult $result = NULL)
|
||||
{
|
||||
$this->setPreserveGlobalState(false);
|
||||
@ -47,6 +61,13 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
} else {
|
||||
$this->model = new MongoMockModel();
|
||||
}
|
||||
|
||||
$model = new ReflectionClass('MongoModel');
|
||||
$this->method_count = $model->getMethod('count');
|
||||
$this->method_count->setAccessible(true);
|
||||
$this->method_fetch = $model->getMethod('fetch');
|
||||
$this->method_fetch->setAccessible(true);
|
||||
|
||||
set_new_overload(array($this, 'newCallback'));
|
||||
}
|
||||
|
||||
@ -62,17 +83,133 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFind()
|
||||
public function testFetch()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$result = $this->model->find();
|
||||
$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();
|
||||
|
||||
$mock = $this->getMock('CacheKey', array('set', 'get'));
|
||||
$mock->expects($this->once())
|
||||
->method('set')
|
||||
->will($this->returnValue(true));
|
||||
$mock->expects($this->once())
|
||||
->method('get')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => -1), 'limit' => 2), $mock);
|
||||
$this->assertInstanceOf('ArrayObject', $result);
|
||||
$this->assertEquals('milk', $result->name);
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('fields' => array('name')));
|
||||
$this->assertSame('bread', $result->name);
|
||||
$result = $this->method_fetch->invoke($this->model, array());
|
||||
$this->setExpectedException('PHPUnit_Framework_Error');
|
||||
$this->assertNull($result->price);
|
||||
$this->assertNull($result->pounds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchField()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$mock = $this->getMock('CacheKey', array('set', 'get'));
|
||||
$mock->expects($this->exactly(2))
|
||||
->method('set')
|
||||
->will($this->returnValue(true));
|
||||
$mock->expects($this->exactly(2))
|
||||
->method('get')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$model = new ReflectionClass('MongoModel');
|
||||
$method = $model->getMethod('fetchField');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$result = $method->invoke($this->model, array('name' => 'milk'), array(), 'quantity', $mock);
|
||||
$this->assertEquals(1, $result);
|
||||
$result = $method->invoke($this->model, array(), array('skip' => 2), 'quantity', $mock);
|
||||
$this->assertEquals($result, $this->method_fetch->invoke($this->model, array(), array('skip' => 2))->quantity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchAll()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$mock = $this->getMock('CacheKey', array('set', 'get'));
|
||||
$mock->expects($this->once())
|
||||
->method('set')
|
||||
->will($this->returnValue(true));
|
||||
$mock->expects($this->once())
|
||||
->method('get')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$model = new ReflectionClass('MongoModel');
|
||||
$method = $model->getMethod('fetchAll');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$result = $method->invoke($this->model, array('name' => 'eggs'), array(), $mock);
|
||||
$this->assertEquals(2, count($result));
|
||||
$result = $method->invoke($this->model, array(), array('skip' => 2));
|
||||
$this->assertEquals(3, count($result));
|
||||
$this->assertEquals('fish', $result[0]->name);
|
||||
$this->assertEquals('milk', $result[1]->name);
|
||||
$this->assertEquals('eggs', $result[2]->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchOrderParam()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('order' => 'name'));
|
||||
$this->assertSame('bread', $result->name);
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1, 'quantity' => -1), 'skip' => 1));
|
||||
$this->assertSame(2.1, $result->price);
|
||||
$this->setExpectedException('GeneralException', 'Wrong order parameter given to query.');
|
||||
$this->method_fetch->invoke($this->model, array(), array('order' => 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchSkipLimitParam()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('order' => 'name'));
|
||||
$this->assertSame('bread', $result->name);
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1, 'quantity' => -1), 'skip' => 1, 'limit' => 1));
|
||||
$this->assertSame(2.1, $result->price);
|
||||
|
||||
$model = new ReflectionClass('MongoModel');
|
||||
$method = $model->getMethod('fetchAll');
|
||||
$method->setAccessible(true);
|
||||
$this->assertCount(3, $method->invoke($this->model, array(), array('limit' => 3)));
|
||||
$this->assertCount(2, $method->invoke($this->model, array(), array('skip' => 3)));
|
||||
$this->assertCount(5, $method->invoke($this->model, array(), array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchFieldsParam()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('fields' => 'name'));
|
||||
$this->assertTrue(!isset($result->quantity));
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('fields' => array('name', 'price')));
|
||||
$this->assertTrue(!isset($result->quantity));
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('fields' => array('name' => 1, 'price' => 1)));
|
||||
$this->assertTrue(!isset($result->quantity));
|
||||
$this->setExpectedException('GeneralException', 'Wrong fields parameter given to query.');
|
||||
$this->method_fetch->invoke($this->model, array(), array('fields' => 1));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,8 +219,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
public function testGet()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$result = $this->model->find()->limit(1)->order(array('name' => 1));
|
||||
$result = $result->fetch();
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1)));
|
||||
$this->assertEquals('bread', $result->name);
|
||||
$id = $result->_id;
|
||||
$this->assertEquals(10, $this->model->get($id)->quantity);
|
||||
@ -96,10 +232,26 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
public function testDelete()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$result = $this->model->find()->limit(1)->order(array('name' => 1));
|
||||
$id = $result->fetch()->_id;
|
||||
$result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1)));
|
||||
$id = $result->_id;
|
||||
$this->assertEquals(1, $this->model->delete($id));
|
||||
$this->assertFalse($this->model->find(array('name' => 'bread'))->fetch());
|
||||
$this->assertFalse($this->method_fetch->invoke($this->model, array('name' => 'bread')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testUpdate()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$this->model->insert(array('name' => 'testbread', 'price' => 3.2, 'quantity' => 10));
|
||||
$result = $this->method_fetch->invoke($this->model, array('name' => 'testbread'));
|
||||
$this->assertEquals(10, $result->quantity);
|
||||
$this->model->update(array('$set' => array('quantity' => 3)), $result->_id);
|
||||
$this->assertEquals(3, $this->model->get($result->_id)->quantity);
|
||||
$this->model->update(array('$set' => array('quantity' => 13)), (string)$result->_id);
|
||||
$this->assertEquals(13, $this->model->get($result->_id)->quantity);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,22 +268,8 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
array('name' => 'equal object')
|
||||
);
|
||||
$this->model->batchInsert($data);
|
||||
$this->assertEquals(1, $this->model->count(array('name' => 'first object')));
|
||||
$this->assertEquals(2, $this->model->count(array('name' => 'equal object')));
|
||||
$this->model->batchInsert(array());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testDeleteAll()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$this->assertEquals(2, $this->model->count(array('name' => 'eggs')));
|
||||
$this->assertEquals(0, $this->model->deleteAll(array('name' => 'eggs')));
|
||||
$this->assertFalse($this->model->find(array('name' => 'eggs'))->fetch());
|
||||
$this->assertEquals(1, $this->method_count->invoke($this->model, array('name' => 'first object')));
|
||||
$this->assertEquals(2, $this->method_count->invoke($this->model, array('name' => 'equal object')));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -141,47 +279,8 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
public function testCount()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$this->assertEquals(5, $this->model->count());
|
||||
$this->assertEquals(2, $this->model->count(array('name' => 'eggs')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetch()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$mock = $this->getMock('CacheKey', array('set', 'get'));
|
||||
$mock->expects($this->exactly(3))
|
||||
->method('set')
|
||||
->will($this->returnValue(true));
|
||||
$mock->expects($this->exactly(3))
|
||||
->method('get')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$model = new ReflectionClass('MongoModel');
|
||||
$method = $model->getMethod('fetchField');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$result = $method->invoke($this->model, array('name' => 'milk'), array(), 'quantity', $mock);
|
||||
$this->assertEquals(1, $result);
|
||||
|
||||
$model = new ReflectionClass('MongoModel');
|
||||
$method = $model->getMethod('fetch');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$result = $method->invoke($this->model, array('name' => 'bread'), array(), $mock);
|
||||
$this->assertEquals('bread', $result->name);
|
||||
|
||||
$model = new ReflectionClass('MongoModel');
|
||||
$method = $model->getMethod('fetchAll');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$result = $method->invoke($this->model, array('name' => 'eggs'), array(), $mock);
|
||||
$this->assertEquals(2, count($result));
|
||||
$this->assertEquals('eggs', $result[0]->name);
|
||||
$this->assertEquals(5, $this->method_count->invoke($this->model));
|
||||
$this->assertEquals(2, $this->method_count->invoke($this->model, array('name' => 'eggs')));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -208,11 +307,11 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
$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')));
|
||||
$this->assertSame(2, $this->method_count->invoke($this->model, array('name' => 'testbread')));
|
||||
|
||||
$prop->setValue($this->model, false);
|
||||
$this->model->delete(1);
|
||||
$this->assertSame(1, $this->model->count(array('name' => 'testbread')));
|
||||
$this->assertSame(1, $this->method_count->invoke($this->model, array('name' => 'testbread')));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -225,13 +324,13 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
$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->assertSame(2, $this->method_count->invoke($this->model, array('name' => 'testbread')));
|
||||
$id = $this->method_fetch->invoke($this->model, array('name' => 'testbread'))->_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')));
|
||||
$this->assertSame(1, $this->method_count->invoke($this->model, array('name' => 'testbread')));
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
@ -242,7 +341,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
$db = $connection->selectDB($conf['database']);
|
||||
$db->authenticate($conf['username'], $conf['password']);
|
||||
$collection = 'mongomock';
|
||||
$db->dropCollection($collection);
|
||||
$db->selectCollection($collection)->remove(array());
|
||||
}
|
||||
|
||||
protected function newCallback($className)
|
||||
|
Reference in New Issue
Block a user