refactored MongoModel to hide fetch method, fixed PHPDoc errors

This commit is contained in:
Anton Grebnev
2012-10-19 20:15:26 +04:00
parent efb6c2d34c
commit 11a5968105
14 changed files with 436 additions and 187 deletions

View File

@ -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)