Files
majestic/tests/model/MongoModelTest.php

397 lines
14 KiB
PHP
Raw Normal View History

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';
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
{
/**
* @var MongoModel
*/
2011-11-17 15:13:11 +04:00
private $model;
/**
* @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-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();
}
$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
*/
public function testFetch()
2011-11-17 15:13:11 +04:00
{
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));
$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->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
*/
public function testFetchField()
2011-11-17 15:13:11 +04:00
{
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);
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
*/
public function testFetchAll()
2011-11-17 15:13:11 +04:00
{