Merge branch 'mongo'
This commit is contained in:
@ -44,65 +44,7 @@ class DbStatementTest extends PHPUnit_Framework_TestCase
|
||||
public function testConstruct()
|
||||
{
|
||||
$this->assertAttributeEquals($this->driver, 'driver', $this->stmt);
|
||||
$this->assertAttributeEquals($this->sql, 'sql', $this->stmt);
|
||||
}
|
||||
|
||||
public function testBindParam()
|
||||
{
|
||||
$val = $this->getMockBuilder('DbExpr')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->assertFalse($this->stmt->bindParam('var', $val));
|
||||
$this->assertTrue($this->stmt->bindParam('place', $val));
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO: change Exception Message 'Placeholder must be an array or string' - no arrays available
|
||||
*/
|
||||
public function testBindParamExceptionParam()
|
||||
{
|
||||
$val = 1;
|
||||
$this->setExpectedException('GeneralException', 'Placeholder must be an array or string');
|
||||
$this->stmt->bindParam(array(), $val);
|
||||
}
|
||||
|
||||
|
||||
public function testBindParamExceptionWrongObject()
|
||||
{
|
||||
$val = $this->getMock('NotDbExpr');
|
||||
$this->setExpectedException('GeneralException', 'Objects excepts DbExpr not allowed.');
|
||||
$this->stmt->bindParam('paa', $val);
|
||||
}
|
||||
|
||||
public function testExecute()
|
||||
{
|
||||
$this->stmt
|
||||
->expects($this->once())
|
||||
->method('driverExecute')
|
||||
->with($this->anything())
|
||||
->will($this->returnCallback(array($this, 'dbStatementAssemble')));
|
||||
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('quote')
|
||||
->with($this->anything())
|
||||
->will($this->returnCallback(array($this, 'driverQuote')));
|
||||
|
||||
$result = $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertSame('SELECT * place_val FROM place_val AND new_val', $result);
|
||||
}
|
||||
|
||||
public function testExecuteNoPlaceholders()
|
||||
{
|
||||
$this->sql = 'PLAIN SQL';
|
||||
$this->stmt = $this->getMockForAbstractClass('DbStatement', array($this->driver, $this->sql));
|
||||
$this->stmt
|
||||
->expects($this->once())
|
||||
->method('driverExecute')
|
||||
->with($this->anything())
|
||||
->will($this->returnCallback(array($this, 'dbStatementAssemble')));
|
||||
$result = $this->stmt->execute(array());
|
||||
$this->assertSame('PLAIN SQL', $result);
|
||||
$this->assertAttributeEquals($this->sql, 'request', $this->stmt);
|
||||
}
|
||||
|
||||
public function testFetch()
|
||||
@ -124,19 +66,9 @@ class DbStatementTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertSame(11, $result[0]->one);
|
||||
$this->assertSame(32, $result[2]->two);
|
||||
|
||||
reset($this->testData);
|
||||
$result = $this->stmt->fetchPairs();
|
||||
$this->assertSame(31, $result['one']);
|
||||
}
|
||||
|
||||
public function dbStatementAssemble($val)
|
||||
{
|
||||
return $val;
|
||||
}
|
||||
|
||||
public function driverQuote($val)
|
||||
{
|
||||
return $val;
|
||||
// reset($this->testData);
|
||||
// $result = $this->stmt->fetchPairs();
|
||||
// $this->assertEquals(31, $result['one']);
|
||||
}
|
||||
|
||||
public function dbStatementFetch($style)
|
||||
|
312
tests/model/MongoDbCommandTest.php
Normal file
312
tests/model/MongoDbCommandTest.php
Normal file
@ -0,0 +1,312 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-10
|
||||
*
|
||||
* Unit tests for MongoDriver class
|
||||
*/
|
||||
|
||||
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/MongoDbCommand.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
|
||||
class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
private $conf = array();
|
||||
|
||||
private $driver;
|
||||
|
||||
private $collection;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->conf = array(
|
||||
'hostname' => 'localhost',
|
||||
'database' => 'test',
|
||||
'username' => 'test',
|
||||
'password' => '1234',
|
||||
'port' => 27017
|
||||
);
|
||||
$this->driver = new MongoDriver($this->conf);
|
||||
$connection = $this->driver->getConnection();
|
||||
|
||||
$db = $connection->selectDB($this->conf['database']);
|
||||
$db->authenticate($this->conf['username'], $this->conf['password']);
|
||||
$collection = 'items';
|
||||
$db->dropCollection($collection);
|
||||
$this->collection = $db->selectCollection($collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCommandFactory()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
|
||||
$this->assertInstanceOf('MongoDbCommand', $cmd);
|
||||
$this->assertInstanceOf('FindMongoCommand', $cmd);
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
|
||||
$this->assertInstanceOf('MongoDbCommand', $cmd);
|
||||
$this->assertInstanceOf('InsertMongoCommand', $cmd);
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE);
|
||||
$this->assertInstanceOf('MongoDbCommand', $cmd);
|
||||
$this->assertInstanceOf('UpdateMongoCommand', $cmd);
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE);
|
||||
$this->assertInstanceOf('MongoDbCommand', $cmd);
|
||||
$this->assertInstanceOf('RemoveMongoCommand', $cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFindCommand()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$cmd->bindParam('condition', array('name' => 'bread'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(0, $result->count());
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
$cmd
|
||||
->bindParam('data', array('name' => 'insert'))
|
||||
->bindParam('safe', true);
|
||||
$cmd->execute();
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
$cmd
|
||||
->bindParam('data', array('name' => 'insert'))
|
||||
->bindParam('safe', true);
|
||||
$cmd->execute();
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
|
||||
$this->assertEquals(2, $cmd->execute()->count());
|
||||
|
||||
$cmd
|
||||
->bindParam('condition', array('name' => 'insert'))
|
||||
->bindParam('fields', array())
|
||||
->bindParam('multiple', false);
|
||||
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals('insert', $result['name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFindCommandNotAllParamsBinded()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$cmd->bindParam('condition', array('name' => 'bread'));
|
||||
$this->setExpectedException('GeneralException', 'FindMongoCommand error. Bind all required params first');
|
||||
$cmd->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testInsertCommand()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
|
||||
$cmd
|
||||
->bindParam('data', array('name' => 'insert'))
|
||||
->bindParam('safe', true);
|
||||
|
||||
$this->assertFalse($cmd->getInsertId());
|
||||
|
||||
$this->assertArrayHasKey('n', $cmd->execute());
|
||||
$this->assertNotEmpty($cmd->getInsertId());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(1, $result->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testInsertCommandNotAllParamsBinded()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
$this->setExpectedException('GeneralException', 'InsertMongoCommand error. Bind all required params first');
|
||||
$cmd->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testUpdateCommand()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
$cmd
|
||||
->bindParam('data', array('name' => 'insert'))
|
||||
->bindParam('safe', true);
|
||||
$this->assertArrayHasKey('n', $cmd->execute());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
|
||||
$cmd
|
||||
->bindParam('condition', array('name' => 'insert'))
|
||||
->bindParam('data', array('$set' => array('name' => 'update')))
|
||||
->bindParam('upsert', false)
|
||||
->bindParam('safe', true);
|
||||
$this->assertArrayHasKey('n', $cmd->execute());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(0, $result->count());
|
||||
$cmd->bindParam('condition', array('name' => 'update'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(1, $result->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testUpdateCommandNotAllParamsBinded()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
|
||||
$cmd->bindParam('data', array('name' => 'bread'));
|
||||
$this->setExpectedException('GeneralException', 'UpdateMongoCommand error. Bind all required params first');
|
||||
$cmd->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testRemoveCommand()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
$cmd
|
||||
->bindParam('data', array('name' => 'insert'))
|
||||
->bindParam('safe', true);
|
||||
$this->assertArrayHasKey('n', $cmd->execute());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(1, $result->count());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
|
||||
$cmd
|
||||
->bindParam('condition', array('name' => 'insert'))
|
||||
->bindParam('safe', true);
|
||||
$this->assertArrayHasKey('n', $cmd->execute());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(0, $result->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testRemoveCommandNotAllParamsBinded()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
|
||||
$this->setExpectedException('GeneralException', 'RemoveMongoCommand error. Bind all required params first.');
|
||||
$cmd->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCommandCommandNotAllParamsBinded()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->collection);
|
||||
$this->setExpectedException('GeneralException', 'CommandMongoCommand error. Bind all required params first');
|
||||
$cmd->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCommandCommandNotMongoDb()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
|
||||
$cmd->bindParam('command', array());
|
||||
$this->assertFalse($cmd->execute());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCommandCommand()
|
||||
{
|
||||
$col = new CollectionMock();
|
||||
$col->db = $this->getMock('MongoDb', array('command'), array(), 'SomeMongoMock', false);
|
||||
$col->db
|
||||
->expects($this->once())
|
||||
->method('command')
|
||||
->will($this->returnValue(true));
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $col);
|
||||
$cmd->bindParam('command', array());
|
||||
$this->assertTrue($cmd->execute());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testToStringParamsNotSet()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testToString()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
$cmd->bindParam('command', array());
|
||||
$this->assertStringStartsWith('Collection: CollectionMock', $cmd->__toString());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
$cmd
|
||||
->bindParam('data', array('name' => 'insert'))
|
||||
->bindParam('safe', true);
|
||||
$this->assertStringStartsWith('Collection: ', $cmd->__toString());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
|
||||
$this->assertStringStartsWith('Collection: ', $cmd->__toString());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
$cmd
|
||||
->bindParam('condition', array('name' => 'insert'))
|
||||
->bindParam('safe', true);
|
||||
$this->assertStringStartsWith('Collection: ', $cmd->__toString());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
$cmd
|
||||
->bindParam('condition', array('name' => 'insert'))
|
||||
->bindParam('data', array('$set' => array('name' => 'update')))
|
||||
->bindParam('upsert', false)
|
||||
->bindParam('safe', true);
|
||||
$this->assertStringStartsWith('Collection: ', $cmd->__toString());
|
||||
}
|
||||
}
|
||||
|
||||
class CollectionMock
|
||||
{
|
||||
public $db = array();
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return 'CollectionMock';
|
||||
}
|
||||
}
|
343
tests/model/MongoDriverTest.php
Normal file
343
tests/model/MongoDriverTest.php
Normal file
@ -0,0 +1,343 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-10
|
||||
*
|
||||
* Unit tests for MongoDriver class
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../model/Db.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.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/MongoDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
|
||||
class MongoDriverTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
private $conf = array();
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->conf = array(
|
||||
'hostname' => 'localhost',
|
||||
'database' => 'test',
|
||||
'username' => 'test',
|
||||
'password' => '1234',
|
||||
'port' => 27017
|
||||
);
|
||||
|
||||
$data = array(
|
||||
array(
|
||||
'name' => 'bread',
|
||||
'price' => 3.2,
|
||||
'quantity' => 10
|
||||
),
|
||||
array(
|
||||
'name' => 'eggs',
|
||||
'price' => 2.1,
|
||||
'quantity' => 20
|
||||
),
|
||||
array(
|
||||
'name' => 'fish',
|
||||
'price' => 13.2,
|
||||
'quantity' => 2
|
||||
),
|
||||
array(
|
||||
'name' => 'milk',
|
||||
'price' => 3.8,
|
||||
'quantity' => 1
|
||||
),
|
||||
array(
|
||||
'name' => 'eggs',
|
||||
'price' => 2.3,
|
||||
'quantity' => 5
|
||||
)
|
||||
);
|
||||
$connection = new Mongo('mongodb://' . $this->conf['hostname'] . ':' . $this->conf['port']);
|
||||
$db = $connection->selectDB($this->conf['database']);
|
||||
$db->authenticate($this->conf['username'], $this->conf['password']);
|
||||
$collection = 'items';
|
||||
$db->dropCollection($collection);
|
||||
$collection = $db->selectCollection($collection);
|
||||
foreach($data as $document) {
|
||||
$collection->insert($document);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGetConnectionNoHostname()
|
||||
{
|
||||
unset($this->conf['hostname']);
|
||||
$this->setExpectedException('GeneralException', 'Configuration must have a "hostname"');
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGetConnectionWrongPassword()
|
||||
{
|
||||
$this->conf['password'] = 'nopass';
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$this->setExpectedException('MongoConnectionException', 'Couldn\'t authenticate with database');
|
||||
$this->assertInstanceOf('MongoDB', $mongo->getConnection());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGetConnection()
|
||||
{
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
|
||||
$this->assertFalse($mongo->isConnected());
|
||||
$this->assertInstanceOf('Mongo', $mongo->getConnection());
|
||||
$this->assertTrue($mongo->isConnected());
|
||||
$mongo->getConnection();
|
||||
$mongo->disconnect();
|
||||
$this->assertFalse($mongo->isConnected());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFind()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
|
||||
$this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
|
||||
$eggs = $mongo->find('items', array('name' => 'eggs'));
|
||||
$egg = $eggs->fetch();
|
||||
$this->assertEquals(20, $egg->quantity);
|
||||
$egg = $eggs->fetchObject();
|
||||
$this->assertEquals('eggs', $egg->name);
|
||||
$this->assertFalse($eggs->fetchObject());
|
||||
|
||||
$this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->numRows());
|
||||
$data = $mongo->find('items', array('price' => array('$lt' => 3.5)));
|
||||
$count = 0;
|
||||
while($row = $data->fetch(Db::FETCH_ASSOC)) {
|
||||
$count++;
|
||||
$this->assertLessThan(3.5, $row['price']);
|
||||
}
|
||||
$this->assertEquals(3, $count);
|
||||
$this->assertEquals(5, $mongo->find('items', array())->numRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testOrderSkipLimit()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
|
||||
$count = $mongo->find('items', array())->numRows();
|
||||
|
||||
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
|
||||
$this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
|
||||
$mongo->insert('items', array('name' => 'fdsbssc'));
|
||||
$mongo->insert('items', array('name' => 'boc'));
|
||||
$mongo->insert('items', array('name' => 'abc'));
|
||||
$mongo->insert('items', array('name' => 'vcxxc'));
|
||||
$mongo->insert('items', array('name' => 'abbc'));
|
||||
$mongo->insert('items', array('name' => 'dsbssc'));
|
||||
$mongo->insert('items', array('name' => 'bssc'));
|
||||
|
||||
$data = $mongo->find('items', array());
|
||||
$this->assertEquals($count + 7, $data->numRows());
|
||||
$data->order(array('name' => 1));
|
||||
$this->assertEquals('abbc', $data->fetch()->name);
|
||||
$this->assertEquals('abc', $data->fetch()->name);
|
||||
$this->assertEquals('boc', $data->fetch()->name);
|
||||
$data = $mongo->find('items', array());
|
||||
$data->order(array('name' => -1));
|
||||
$data->skip(3);
|
||||
$data->limit(1);
|
||||
while($row = $data->fetch()) {
|
||||
$this->assertEquals('fdsbssc', $row->name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCount()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$this->assertEquals(5, $mongo->count('items'));
|
||||
$this->assertEquals(2, $mongo->count('items', array('name' => 'eggs')));
|
||||
$this->assertEquals(1, $mongo->count('items', array('name' => 'eggs'), 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('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()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
|
||||
$this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs')));
|
||||
$this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs')));
|
||||
$this->assertEquals(1, $mongo->delete('items', array('name' => 'bread')));
|
||||
$this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->numRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testInsert()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
|
||||
$this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGetInsertId()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$this->assertEquals(0, $mongo->getInsertId());
|
||||
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
|
||||
$this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
|
||||
$this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
|
||||
$id1 = $mongo->getInsertId();
|
||||
$this->assertNotEmpty($id1);
|
||||
$this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
|
||||
$id2 = $mongo->getInsertId();
|
||||
$this->assertNotEmpty($id2);
|
||||
$this->assertNotEquals($id1, $id2);
|
||||
$this->assertEquals(3, $mongo->find('items', array('name' => 'bread'))->numRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testUpdate()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$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();
|
||||
$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')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testUpsert()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFindAndModify()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
|
||||
$this->assertEquals(10, $mongo->get('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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCommand()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$result = $mongo->command('items', array('distinct' =>'items', 'key' => 'name'));
|
||||
$this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC)));
|
||||
}
|
||||
}
|
238
tests/model/MongoModelTest.php
Normal file
238
tests/model/MongoModelTest.php
Normal file
@ -0,0 +1,238 @@
|
||||
<?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__) . '/../../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
|
||||
{
|
||||
|
||||
private $model;
|
||||
|
||||
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);
|
||||
|
||||
Config::set('Db', $conf);
|
||||
if (!class_exists('MockModel')) {
|
||||
$this->model = $this->getMockForAbstractClass('MongoModel', array(), 'MongoMockModel');
|
||||
} else {
|
||||
$this->model = new MongoMockModel();
|
||||
}
|
||||
set_new_overload(array($this, 'newCallback'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testModel()
|
||||
{
|
||||
$this->assertInstanceOf('MongoMockModel', $this->model);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFind()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$result = $this->model->find()->limit(1)->order(array('name' => 1));
|
||||
$result = $result->fetch();
|
||||
$this->assertEquals('bread', $result->name);
|
||||
$id = $result->_id;
|
||||
$this->assertEquals(10, $this->model->get($id)->quantity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testDelete()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$result = $this->model->find()->limit(1)->order(array('name' => 1));
|
||||
$id = $result->fetch()->_id;
|
||||
$this->assertEquals(1, $this->model->delete($id));
|
||||
$this->assertFalse($this->model->find(array('name' => 'bread'))->fetch());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testDeleteAll()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('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());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCount()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->assertEquals(5, $this->model->count());
|
||||
$this->assertEquals(2, $this->model->count(array('name' => 'eggs')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetch()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('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);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
$conf = array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017);
|
||||
|
||||
|
||||
$connection = new Mongo('mongodb://' . $conf['hostname'] . ':' . $conf['port']);
|
||||
$db = $connection->selectDB($conf['database']);
|
||||
$db->authenticate($conf['username'], $conf['password']);
|
||||
$collection = 'mongomock';
|
||||
$db->dropCollection($collection);
|
||||
}
|
||||
|
||||
protected function newCallback($className)
|
||||
{
|
||||
switch ($className) {
|
||||
case 'CacheKey':
|
||||
return 'MockCacheKey';
|
||||
default:
|
||||
return $className;
|
||||
}
|
||||
}
|
||||
|
||||
public function dbSetUp($conf)
|
||||
{
|
||||
$data = array(
|
||||
array(
|
||||
'name' => 'bread',
|
||||
'price' => 3.2,
|
||||
'quantity' => 10
|
||||
),
|
||||
array(
|
||||
'name' => 'eggs',
|
||||
'price' => 2.1,
|
||||
'quantity' => 20
|
||||
),
|
||||
array(
|
||||
'name' => 'fish',
|
||||
'price' => 13.2,
|
||||
'quantity' => 2
|
||||
),
|
||||
array(
|
||||
'name' => 'milk',
|
||||
'price' => 3.8,
|
||||
'quantity' => 1
|
||||
),
|
||||
array(
|
||||
'name' => 'eggs',
|
||||
'price' => 2.3,
|
||||
'quantity' => 5
|
||||
)
|
||||
);
|
||||
$connection = new Mongo('mongodb://' . $conf['default']['hostname'] . ':' . $conf['default']['port']);
|
||||
$db = $connection->selectDB($conf['default']['database']);
|
||||
$db->authenticate($conf['default']['username'], $conf['default']['password']);
|
||||
$collection = 'mongomock';
|
||||
$db->dropCollection($collection);
|
||||
$collection = $db->selectCollection($collection);
|
||||
foreach($data as $document) {
|
||||
$collection->insert($document);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
464
tests/model/MongoStatementTest.php
Normal file
464
tests/model/MongoStatementTest.php
Normal file
@ -0,0 +1,464 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-15
|
||||
*
|
||||
* Unit tests for MySQLiStatement class
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../util/profiler/CommandProfiler.php';
|
||||
require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php';
|
||||
require_once dirname(__FILE__) . '/../../model/Db.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbStatement.php';
|
||||
require_once dirname(__FILE__) . '/../../model/MongoStatement.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
|
||||
class MongoStatementTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
|
||||
private $driver;
|
||||
|
||||
private $stmt;
|
||||
|
||||
private $request;
|
||||
|
||||
private $testData = array(
|
||||
array('one' => 11, 'two' => 12),
|
||||
array('one' => 21, 'two' => 22),
|
||||
array('one' => 31, 'two' => 32),
|
||||
);
|
||||
|
||||
public function run(PHPUnit_Framework_TestResult $result = NULL)
|
||||
{
|
||||
$this->setPreserveGlobalState(false);
|
||||
return parent::run($result);
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
if (!isset($this->stmt)) {
|
||||
$this->driver = $this->getMockBuilder('DbDriverMock')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getConnection'))
|
||||
->getMock();
|
||||
$this->request = $this->getMockBuilder('MongoDbCommandMock')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('execute', 'bindParam', 'getInsertId'))
|
||||
->getMock();
|
||||
$this->stmt = new MongoStatement($this->driver, $this->request);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testAffectedNumRowsNoResult()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->assertFalse($this->stmt->affectedRows());
|
||||
$this->assertFalse($this->stmt->numRows());
|
||||
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->any())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array()));
|
||||
$this->stmt->execute();
|
||||
$this->assertFalse($this->stmt->affectedRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testAffectedNumRows()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('n' => 20, 'ok' => 1)));
|
||||
$this->stmt->execute();
|
||||
$this->assertEquals(20, $this->stmt->affectedRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGetInsertId()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
|
||||
$this->request = $this->getMockBuilder('InsertMongoCommand')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('execute', 'bindParam', 'getInsertId'))
|
||||
->getMock();
|
||||
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('n' => 20, 'ok' => 1)));
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('getInsertId')
|
||||
->will($this->returnValue('4b0rrs'));
|
||||
|
||||
$this->stmt = new MongoStatement($this->driver, $this->request);
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->assertEquals('4b0rrs', $this->stmt->getInsertId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testExecute()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
|
||||
$this->assertTrue($this->stmt->execute());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testExecuteNoResult()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->any())
|
||||
->method('execute')
|
||||
->will($this->returnValue(false));
|
||||
$this->setExpectedException('GeneralException', 'MongoDB request error.');
|
||||
$this->stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testExecuteNoConnection()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('getConnection')
|
||||
->will($this->returnValue(false));
|
||||
$this->setExpectedException('GeneralException', 'No connection to MongoDB server.');
|
||||
$this->stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testExecuteWithDebug()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
|
||||
$this->assertTrue($this->stmt->execute());
|
||||
$this->assertEquals(10, $this->stmt->numRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testBindParam()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('bindParam')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
|
||||
$this->assertTrue($this->stmt->execute(array('one' => 'two')));
|
||||
|
||||
$this->assertAttributeNotEquals(null, 'result', $this->stmt);
|
||||
$this->stmt->close();
|
||||
$this->assertAttributeEquals(null, 'result', $this->stmt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetch()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->assertFalse($this->stmt->fetch());
|
||||
|
||||
$this->setDriverGetConnectionMethod()->setRequestForFetch();
|
||||
|
||||
$this->stmt->execute();
|
||||
$result = $this->stmt->fetch();
|
||||
$this->assertEquals('prev', $result->next);
|
||||
$this->assertFalse($this->stmt->fetch());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchWithInitialArray()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->assertFalse($this->stmt->fetch());
|
||||
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('some' => 'val')));
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->assertFalse($this->stmt->fetch());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchAssocFromCursor()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->assertFalse($this->stmt->fetch());
|
||||
|
||||
$this->setDriverGetConnectionMethod()->setRequestForFetch();
|
||||
|
||||
$this->stmt->execute();
|
||||
$result = $this->stmt->fetch(Db::FETCH_ASSOC);
|
||||
$this->assertEquals('prev', $result['next']);
|
||||
$this->assertEquals(array(), $this->stmt->fetch(Db::FETCH_ASSOC));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchAssocFromArray()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->assertFalse($this->stmt->fetch());
|
||||
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('some' => 'val')));
|
||||
|
||||
$this->stmt->execute();
|
||||
$result = $this->stmt->fetch(Db::FETCH_ASSOC);
|
||||
$this->assertEquals('val', $result['some']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchWrongMode()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->assertFalse($this->stmt->fetch());
|
||||
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('some' => 'val')));
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->setExpectedException('GeneralException', 'Invalid fetch mode "222" specified');
|
||||
$this->stmt->fetch(222);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testSkipOrderLimit()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod()->setRequestForFetch();
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->assertInstanceOf('MongoStatement', $this->stmt->order(array('id' => 1)));
|
||||
$this->assertInstanceOf('MongoStatement', $this->stmt->limit(10));
|
||||
$this->assertInstanceOf('MongoStatement', $this->stmt->skip(1));
|
||||
|
||||
$this->stmt->fetch();
|
||||
$this->stmt->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testOrderException()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('some' => 'val')));
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->setExpectedException('GeneralException', 'MongoStatement error. Impossible order results of opened cursor');
|
||||
$this->stmt->order(array('id' => 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testSkipException()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('some' => 'val')));
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->setExpectedException('GeneralException', 'MongoStatement error. Impossible skip results of opened cursor');
|
||||
$this->stmt->skip(array('id' => 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testLimitException()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('some' => 'val')));
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->setExpectedException('GeneralException', 'MongoStatement error. Impossible limit results of opened cursor');
|
||||
$this->stmt->limit(array('id' => 1));
|
||||
}
|
||||
|
||||
private function setDriverGetConnectionMethod()
|
||||
{
|
||||
$mongoMock = $this->getMock('Mongo');
|
||||
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('getConnection')
|
||||
->will($this->returnValue($mongoMock));
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setRequestExecuteMethod()
|
||||
{
|
||||
$resultMock = $this->getMockBuilder('MongoCursor')
|
||||
->setMethods(array('count'))
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('count')
|
||||
->will($this->returnValue(10));
|
||||
|
||||
$this->request
|
||||
->expects($this->any())
|
||||
->method('execute')
|
||||
->will($this->returnValue($resultMock));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setRequestForFetch()
|
||||
{
|
||||
$resultMock = $this->getMockBuilder('MongoCursor')
|
||||
->setMethods(array('count', 'getNext', 'limit', 'sort', 'skip'))
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('limit');
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('sort');
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('skip');
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('count')
|
||||
->will($this->returnValue(10));
|
||||
$resultMock
|
||||
->expects($this->at(0))
|
||||
->method('getNext')
|
||||
->will($this->returnValue(array('next' => 'prev', '_id' => 10)));
|
||||
$resultMock
|
||||
->expects($this->at(1))
|
||||
->method('getNext')
|
||||
->will($this->returnValue(array()));
|
||||
|
||||
$this->request
|
||||
->expects($this->any())
|
||||
->method('execute')
|
||||
->will($this->returnValue($resultMock));
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
60
tests/model/MyDbDriver.php
Normal file
60
tests/model/MyDbDriver.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
abstract class MyDbDriver extends DbDriver
|
||||
{
|
||||
public function getInsertId($table = null, $key = null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function quoteIdentifier($param)
|
||||
{
|
||||
return $param;
|
||||
}
|
||||
|
||||
public function quote($param)
|
||||
{
|
||||
return $param;
|
||||
}
|
||||
|
||||
public function insert($table, $bind, $on_duplicate = array())
|
||||
{
|
||||
return $table;
|
||||
}
|
||||
|
||||
public function update($table, $bind, $where = '')
|
||||
{
|
||||
return $table;
|
||||
}
|
||||
|
||||
public function delete($table, $where = '')
|
||||
{
|
||||
return $table;
|
||||
}
|
||||
|
||||
public function query($sql, $params = array())
|
||||
{
|
||||
$conf = array('driver' => 'MockDbDriver', 'hostname' => 'somehost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
|
||||
return new MockDbDriver($conf);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function fetchField($field)
|
||||
{
|
||||
return $field;
|
||||
}
|
||||
|
||||
public function fetchAll()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ require_once dirname(__FILE__) . '/../../model/Db.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbStatement.php';
|
||||
require_once dirname(__FILE__) . '/../../model/MySQLiStatement.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/SqlDbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/MySQLiDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
|
||||
|
@ -26,6 +26,8 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
private $stmt;
|
||||
|
||||
private $sql;
|
||||
|
||||
private $testData = array(
|
||||
array('one' => 11, 'two' => 12),
|
||||
array('one' => 21, 'two' => 22),
|
||||
@ -51,6 +53,65 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testBindParam()
|
||||
{
|
||||
$val = $this->getMockBuilder('DbExpr')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->assertFalse($this->stmt->bindParam('var', $val));
|
||||
$this->assertTrue($this->stmt->bindParam('place', $val));
|
||||
}
|
||||
|
||||
public function testBindParamExceptionParam()
|
||||
{
|
||||
$val = 1;
|
||||
$this->setExpectedException('GeneralException', 'Placeholder must be an integer or string');
|
||||
$this->stmt->bindParam(array(), $val);
|
||||
}
|
||||
|
||||
public function testBindParamExceptionWrongObject()
|
||||
{
|
||||
$val = $this->getMock('NotDbExpr');
|
||||
$this->setExpectedException('GeneralException', 'Objects excepts DbExpr not allowed.');
|
||||
$this->stmt->bindParam('paa', $val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testExecute()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('quote')
|
||||
->with($this->anything())
|
||||
->will($this->returnCallback(array($this, 'driverQuote')));
|
||||
|
||||
$this->setDriverGetConnectionMethod();
|
||||
|
||||
$result = $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testExecuteNoPlaceholders()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
|
||||
$this->sql = 'PLAIN SQL';
|
||||
$result = $this->stmt->execute(array());
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group MySQL
|
||||
@ -112,6 +173,49 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
|
||||
* @runInSeparateProcess
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testFetchPairs()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$resultMock = $this->getMockBuilder('mysqli_result')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('fetch_array', 'close'))
|
||||
->setMockClassName('Mysqli_Result_Mock')
|
||||
->getMock();
|
||||
$resultMock
|
||||
->expects($this->at(0))
|
||||
->method('fetch_array')
|
||||
->will($this->returnValue(array('pair', 'value')));
|
||||
$resultMock
|
||||
->expects($this->at(1))
|
||||
->method('fetch_array')
|
||||
->will($this->returnValue(false));
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('close')
|
||||
->will($this->returnValue(TRUE));
|
||||
|
||||
$mysqliMock = $this->getMock('MysqliDrvr', array('query'));
|
||||
$mysqliMock
|
||||
->expects($this->any())
|
||||
->method('query')
|
||||
->with($this->anything())
|
||||
->will($this->returnValue($resultMock));
|
||||
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('getConnection')
|
||||
->will($this->returnValue($mysqliMock));
|
||||
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertSame(array('pair' => 'value'), $this->stmt->fetchPairs());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testFetchWithDebug()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
@ -192,7 +296,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
|
||||
$this->setExpectedException('GeneralException');
|
||||
|
||||
|
||||
$this->stmt->fetch(324);
|
||||
}
|
||||
|
||||
@ -226,7 +330,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
|
||||
->method('query')
|
||||
->with($this->anything())
|
||||
->will($this->returnValue($resultMock));
|
||||
|
||||
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('getConnection')
|
||||
@ -254,4 +358,14 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function driverQuote($val)
|
||||
{
|
||||
return $val;
|
||||
}
|
||||
|
||||
public function dbStatementAssemble($val)
|
||||
{
|
||||
return $val;
|
||||
}
|
||||
|
||||
}
|
@ -13,23 +13,24 @@
|
||||
require_once dirname(__FILE__) . '/../../model/DbExpr.php';
|
||||
require_once dirname(__FILE__) . '/../../model/Db.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/SqlDbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
|
||||
class DbDriverTest extends PHPUnit_Framework_TestCase
|
||||
class SqlDbDriverTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $driver;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$conf = array('hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
|
||||
$this->driver = $this->getMockForAbstractClass('DbDriver', array($conf));
|
||||
$this->driver = $this->getMockForAbstractClass('SqlDbDriver', array($conf));
|
||||
}
|
||||
|
||||
public function testConstruct()
|
||||
{
|
||||
$conf = array('hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
|
||||
try {
|
||||
$this->driver = $this->getMockForAbstractClass('DbDriver', array($conf));
|
||||
$this->driver = $this->getMockForAbstractClass('SqlDbDriver', array($conf));
|
||||
}
|
||||
catch (GeneralException $expected) {
|
||||
$this->fail($expected->getMessage());
|
||||
@ -43,7 +44,7 @@ class DbDriverTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
$this->setExpectedException('GeneralException', 'Configuration must have a "username"');
|
||||
|
||||
$this->getMockForAbstractClass('DbDriver', array($conf));
|
||||
$this->getMockForAbstractClass('SqlDbDriver', array($conf));
|
||||
}
|
||||
|
||||
public function testGetConnection()
|
@ -16,9 +16,11 @@ require_once dirname(__FILE__) . '/../../cache/Cacher.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbExpr.php';
|
||||
require_once dirname(__FILE__) . '/../../model/Db.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
||||
require_once dirname(__FILE__) . '/MyDbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/Model.php';
|
||||
require_once dirname(__FILE__) . '/../../model/SqlModel.php';
|
||||
|
||||
class ModelTest extends PHPUnit_Framework_TestCase
|
||||
class SqlModelTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
private $model;
|
||||
@ -32,7 +34,7 @@ class ModelTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
Config::set('Db', $conf);
|
||||
if (!class_exists('MockModel')) {
|
||||
$this->model = $this->getMockForAbstractClass('Model', array(), 'MockModel');
|
||||
$this->model = $this->getMockForAbstractClass('SqlModel', array(), 'MockModel');
|
||||
} else {
|
||||
$this->model = new MockModel();
|
||||
}
|
||||
@ -41,7 +43,7 @@ class ModelTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testModel()
|
||||
{
|
||||
$this->assertInstanceOf('Model', $this->model);
|
||||
$this->assertInstanceOf('SqlModel', $this->model);
|
||||
}
|
||||
|
||||
public function testGetInsertId()
|
||||
@ -83,22 +85,19 @@ class ModelTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testOrder()
|
||||
{
|
||||
$model = new ReflectionClass('Model');
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('order');
|
||||
$method->setAccessible(true);
|
||||
$this->assertSame(' ORDER BY id DESC', $method->invoke($this->model, array('sort' => 'id', 'order' => 'desc')));
|
||||
$this->assertSame(' ORDER BY name ASC', $method->invoke($this->model, array('sort' => 'name'), array('id', 'name')));
|
||||
$this->assertEmpty($method->invoke($this->model, array()));
|
||||
|
||||
/**
|
||||
* @TODO: Model::order - check DESC condition - make case insensitive
|
||||
*/
|
||||
$this->assertSame(' ORDER BY name ASC', $method->invoke($this->model, array('sort' => 'name', 'order' => 'DESC'), array('id', 'name')));
|
||||
}
|
||||
|
||||
public function testSearch()
|
||||
{
|
||||
$model = new ReflectionClass('Model');
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('search');
|
||||
$method->setAccessible(true);
|
||||
$this->assertEmpty($method->invoke($this->model, array()));
|
||||
@ -108,7 +107,7 @@ class ModelTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testFetch()
|
||||
{
|
||||
$model = new ReflectionClass('Model');
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('fetch');
|
||||
$method->setAccessible(true);
|
||||
$key = $this->getCacheKeyMockGetSet();
|
||||
@ -117,7 +116,7 @@ class ModelTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testFetchField()
|
||||
{
|
||||
$model = new ReflectionClass('Model');
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('fetchField');
|
||||
$method->setAccessible(true);
|
||||
|
||||
@ -127,7 +126,7 @@ class ModelTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testFetchAll()
|
||||
{
|
||||
$model = new ReflectionClass('Model');
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('fetchAll');
|
||||
$method->setAccessible(true);
|
||||
|
||||
@ -166,7 +165,7 @@ class ModelTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testAddCleanCache()
|
||||
{
|
||||
$model = new ReflectionClass('Model');
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('addCleanCache');
|
||||
$method->setAccessible(true);
|
||||
|
||||
@ -179,7 +178,7 @@ class ModelTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testCleanCaches()
|
||||
{
|
||||
$model = new ReflectionClass('Model');
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('addCleanCache');
|
||||
$method->setAccessible(true);
|
||||
|
||||
@ -189,7 +188,7 @@ class ModelTest extends PHPUnit_Framework_TestCase
|
||||
$method->invoke($this->model, $key);
|
||||
$this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model);
|
||||
|
||||
$model = new ReflectionClass('Model');
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('cleanCaches');
|
||||
$method->setAccessible(true);
|
||||
|
||||
@ -244,63 +243,4 @@ class ModelTest extends PHPUnit_Framework_TestCase
|
||||
return $className;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class MyDbDriver extends DbDriver
|
||||
{
|
||||
public function getInsertId($table = null, $key = null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function quoteIdentifier($param)
|
||||
{
|
||||
return $param;
|
||||
}
|
||||
|
||||
public function quote($param)
|
||||
{
|
||||
return $param;
|
||||
}
|
||||
|
||||
public function insert($table, $bind, $on_duplicate = array())
|
||||
{
|
||||
return $table;
|
||||
}
|
||||
|
||||
public function update($table, $bind, $where = '')
|
||||
{
|
||||
return $table;
|
||||
}
|
||||
|
||||
public function delete($table, $where = '')
|
||||
{
|
||||
return $table;
|
||||
}
|
||||
|
||||
public function query($sql, $params = array())
|
||||
{
|
||||
$conf = array('driver' => 'MockDbDriver', 'hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
|
||||
return new MockDbDriver($conf);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function fetchField($field)
|
||||
{
|
||||
return $field;
|
||||
}
|
||||
|
||||
public function fetchAll()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
110
tests/session/SessionModelTest.php
Normal file
110
tests/session/SessionModelTest.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-15
|
||||
*
|
||||
* Unit tests for SessionModel class
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../Registry.php';
|
||||
require_once dirname(__FILE__) . '/../../Config.php';
|
||||
require_once dirname(__FILE__) . '/../../classes/Env.class.php';
|
||||
require_once dirname(__FILE__) . '/../../model/Db.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../model/MyDbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/Model.php';
|
||||
require_once dirname(__FILE__) . '/../../model/SqlModel.php';
|
||||
require_once dirname(__FILE__) . '/../../session/Session.model.php';
|
||||
|
||||
class SessionModelTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $model;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$conf = array('default' => array('driver' => 'MockDbDriver', 'hostname' => 'somehost', 'database' => 'db', 'username' => 'test', 'password' => '1234'));
|
||||
if (!class_exists('MockDbDriver')) {
|
||||
$this->getMockForAbstractClass('MyDbDriver', array($conf), 'MockDbDriver', false);
|
||||
}
|
||||
if (!class_exists('MockDbExpr')) {
|
||||
$this->getMock('DbExpr', array(), array(), 'MockDbExpr', false);
|
||||
}
|
||||
|
||||
Config::set('Db', $conf);
|
||||
|
||||
set_new_overload(array($this, 'newCallback'));
|
||||
}
|
||||
|
||||
public function testOpen()
|
||||
{
|
||||
$this->model = new SessionModel();
|
||||
$this->assertTrue($this->model->open('path', 'name'));
|
||||
}
|
||||
|
||||
public function testClose()
|
||||
{
|
||||
$this->model = new SessionModel();
|
||||
$this->assertTrue($this->model->close());
|
||||
}
|
||||
|
||||
public function testRead()
|
||||
{
|
||||
$this->model = new SessionModel();
|
||||
$this->assertEquals('data', $this->model->read(1));
|
||||
}
|
||||
|
||||
public function testWrite()
|
||||
{
|
||||
$this->model = new SessionModel();
|
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||
$this->assertEmpty(Env::Server('HTTP_X_FORWARDED_FOR'));
|
||||
|
||||
$this->assertTrue($this->model->write(2, 'user|.s:20;id=2;id=2'));
|
||||
}
|
||||
|
||||
public function testDestroy()
|
||||
{
|
||||
$this->model = new SessionModel();
|
||||
$this->assertTrue($this->model->destroy(2));
|
||||
}
|
||||
public function testGc()
|
||||
{
|
||||
$this->model = new SessionModel();
|
||||
$this->assertTrue($this->model->gc(2000));
|
||||
}
|
||||
|
||||
public function testDestroyByUserId()
|
||||
{
|
||||
$this->model = new SessionModel();
|
||||
$this->assertEquals('session', $this->model->destroyByUserId(12));
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
Config::getInstance()->offsetUnset('Db');
|
||||
$config = new ReflectionClass('Db');
|
||||
$registry = $config->getProperty('connections');
|
||||
$registry->setAccessible(true);
|
||||
$registry->setValue('Db', array());
|
||||
unset_new_overload();
|
||||
}
|
||||
|
||||
protected function newCallback($className)
|
||||
{
|
||||
switch ($className) {
|
||||
case 'DbExpr':
|
||||
return 'MockDbExpr';
|
||||
case 'MockDbDriver':
|
||||
return 'MockDbDriver';
|
||||
case 'CacheKey':
|
||||
return 'MockCacheKey';
|
||||
default:
|
||||
return $className;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user