2011-11-17 15:13:11 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @copyright NetMonsters <team@netmonsters.ru>
|
|
|
|
* @link http://netmonsters.ru
|
|
|
|
* @package Majestic
|
|
|
|
* @subpackage UnitTests
|
|
|
|
* @since 2011-11-7
|
|
|
|
*
|
|
|
|
* Unit tests for MongoModel class
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once dirname(__FILE__) . '/../../Registry.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../Config.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../cache/Cacher.php';
|
2012-10-19 20:15:26 +04:00
|
|
|
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
2011-11-17 15:13:11 +04:00
|
|
|
require_once dirname(__FILE__) . '/../../model/DbExpr.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../model/Db.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../model/DbStatement.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../model/MongoStatement.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../model/MongoDriver.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../model/Model.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../model/MongoModel.php';
|
|
|
|
|
|
|
|
class MongoModelTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
|
2012-10-19 20:15:26 +04:00
|
|
|
/**
|
|
|
|
* @var MongoModel
|
|
|
|
*/
|
2011-11-17 15:13:11 +04:00
|
|
|
private $model;
|
|
|
|
|
2012-10-19 20:15:26 +04:00
|
|
|
/**
|
|
|
|
* @var ReflectionMethod
|
|
|
|
*/
|
|
|
|
private $method_count;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ReflectionMethod
|
|
|
|
*/
|
|
|
|
private $method_fetch;
|
|
|
|
|
2011-11-17 15:13:11 +04:00
|
|
|
public function run(PHPUnit_Framework_TestResult $result = NULL)
|
|
|
|
{
|
|
|
|
$this->setPreserveGlobalState(false);
|
|
|
|
return parent::run($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$conf = array('default' => array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017));
|
|
|
|
|
|
|
|
$this->dbSetUp($conf);
|
2011-12-13 13:43:15 +04:00
|
|
|
|
2011-11-17 15:13:11 +04:00
|
|
|
Config::set('Db', $conf);
|
|
|
|
if (!class_exists('MockModel')) {
|
|
|
|
$this->model = $this->getMockForAbstractClass('MongoModel', array(), 'MongoMockModel');
|
|
|
|
} else {
|
|
|
|
$this->model = new MongoMockModel();
|
|
|
|
}
|
2012-10-19 20:15:26 +04:00
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2011-11-17 15:13:11 +04:00
|
|
|
set_new_overload(array($this, 'newCallback'));
|
|
|
|
}
|
|
|
|
|
2011-12-06 16:53:16 +04:00
|
|
|
/**
|
|
|
|
* @group Mongo
|
|
|
|
*/
|
2011-11-17 15:13:11 +04:00
|
|
|
public function testModel()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf('MongoMockModel', $this->model);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
2011-12-06 16:53:16 +04:00
|
|
|
* @group Mongo
|
2011-11-17 15:13:11 +04:00
|
|
|
*/
|
2012-10-19 20:15:26 +04:00
|
|
|
public function testFetch()
|
2011-11-17 15:13:11 +04:00
|
|
|
{
|
2012-07-09 13:47:32 +04:00
|
|
|
Config::set('DEBUG', false);
|
2012-10-19 20:15:26 +04:00
|
|
|
|
|
|
|
$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')));
|
2011-12-13 13:43:15 +04:00
|
|
|
$this->assertSame('bread', $result->name);
|
2012-10-19 20:15:26 +04:00
|
|
|
$result = $this->method_fetch->invoke($this->model, array());
|
2011-12-13 13:43:15 +04:00
|
|
|
$this->setExpectedException('PHPUnit_Framework_Error');
|
2012-10-19 20:15:26 +04:00
|
|
|
$this->assertNull($result->pounds);
|
2011-11-17 15:13:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
2011-12-06 16:53:16 +04:00
|
|
|
* @group Mongo
|
2011-11-17 15:13:11 +04:00
|
|
|
*/
|
2012-10-19 20:15:26 +04:00
|
|
|
public function testFetchField()
|
2011-11-17 15:13:11 +04:00
|
|
|
{
|
2012-07-09 13:47:32 +04:00
|
|
|
Config::set('DEBUG', false);
|
2012-10-19 20:15:26 +04:00
|
|
|
|
|
|
|
$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);
|
2011-11-17 15:13:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
2011-12-06 16:53:16 +04:00
|
|
|
* @group Mongo
|
2011-11-17 15:13:11 +04:00
|
|
|
*/
|
2012-10-19 20:15:26 +04:00
|
|
|
public function testFetchAll()
|
2011-11-17 15:13:11 +04:00
|
|
|
{
|
|