Add namespace.
This commit is contained in:
23
Tests/model/DbExprTest.php
Normal file
23
Tests/model/DbExprTest.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-3
|
||||
*
|
||||
* Unit tests for DbExpr class
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../model/DbExpr.php';
|
||||
|
||||
class DbExprTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testExpr()
|
||||
{
|
||||
$expr = new DbExpr('THIS IS SQL EXPRESSION');
|
||||
$str = (string) $expr;
|
||||
$this->assertSame('THIS IS SQL EXPRESSION', $str);
|
||||
}
|
||||
}
|
102
Tests/model/DbStatementTest.php
Normal file
102
Tests/model/DbStatementTest.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-3
|
||||
*
|
||||
* Unit tests for DbStatement class
|
||||
*/
|
||||
|
||||
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__) . '/../../exception/GeneralException.php';
|
||||
|
||||
class DbStatementTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $driver;
|
||||
|
||||
private $sql;
|
||||
|
||||
private $stmt;
|
||||
|
||||
private $testData = array(
|
||||
array('one' => 11, 'two' => 12),
|
||||
array('one' => 21, 'two' => 22),
|
||||
array('one' => 31, 'two' => 32),
|
||||
);
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
if (!isset($this->stmt)) {
|
||||
$this->driver = $this->getMockBuilder('DbDriverMock')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('quote'))
|
||||
->getMock();
|
||||
$this->sql = 'SELECT * :place FROM :place AND :new';
|
||||
$this->stmt = $this->getMockForAbstractClass('DbStatement', array($this->driver, $this->sql));
|
||||
}
|
||||
}
|
||||
|
||||
public function testConstruct()
|
||||
{
|
||||
$this->assertAttributeEquals($this->driver, 'driver', $this->stmt);
|
||||
$this->assertAttributeEquals($this->sql, 'request', $this->stmt);
|
||||
}
|
||||
|
||||
public function testFetch()
|
||||
{
|
||||
$this->stmt
|
||||
->expects($this->any())
|
||||
->method('fetch')
|
||||
->with($this->anything())
|
||||
->will($this->returnCallback(array($this, 'dbStatementFetch')));
|
||||
|
||||
$this->assertSame(11, $this->stmt->fetchField('one'));
|
||||
$this->assertFalse($this->stmt->fetchField('zero'));
|
||||
|
||||
reset($this->testData);
|
||||
$this->assertSame(array(11, 21, 31), $this->stmt->fetchColumn('one'));
|
||||
|
||||
reset($this->testData);
|
||||
$result = $this->stmt->fetchAll();
|
||||
$this->assertSame(11, $result[0]->one);
|
||||
$this->assertSame(32, $result[2]->two);
|
||||
|
||||
// reset($this->testData);
|
||||
// $result = $this->stmt->fetchPairs();
|
||||
// $this->assertEquals(31, $result['one']);
|
||||
}
|
||||
|
||||
public function dbStatementFetch($style)
|
||||
{
|
||||
$result = null;
|
||||
switch ($style) {
|
||||
case Db::FETCH_OBJ:
|
||||
$data = each($this->testData);
|
||||
if ($data !== false) {
|
||||
$result = new ArrayObject($data['value'], ArrayObject::ARRAY_AS_PROPS);
|
||||
} else {
|
||||
$result = false;
|
||||
}
|
||||
break;
|
||||
case Db::FETCH_ASSOC:
|
||||
$data = each($this->testData);
|
||||
$result = $data['value'];
|
||||
break;
|
||||
case Db::FETCH_NUM:
|
||||
$data = each($this->testData);
|
||||
if ($data !== false) {
|
||||
$data = $data['value'];
|
||||
$result[0] = key($data);
|
||||
$result[1] = current($data);
|
||||
} else {
|
||||
$result = false;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
62
Tests/model/DbTest.php
Normal file
62
Tests/model/DbTest.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-1
|
||||
*
|
||||
* Unit tests for Db class
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../Registry.php';
|
||||
require_once dirname(__FILE__) . '/../../Config.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/Db.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
|
||||
|
||||
class DbTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConnectNoParams()
|
||||
{
|
||||
$this->setExpectedException('InitializationException', 'Trying to get property of non-object');
|
||||
Db::connect();
|
||||
}
|
||||
public function testConnectConfigNotArray()
|
||||
{
|
||||
$this->setExpectedException('InitializationException', 'Connection parameters must be an array');
|
||||
Db::connect('name', 'config');
|
||||
}
|
||||
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testConnectGlobalConfig()
|
||||
{
|
||||
$conf = array('hostname' => 'localhost', 'driver' => 'MySQLiDriverGlobalConfMock', 'database' => 'db', 'username' => 'test', 'password' => '1234');
|
||||
$this->getMockForAbstractClass('DbDriver', array(), 'MySQLiDriverGlobalConfMock', false);
|
||||
Config::set('Db', array('global' =>$conf));
|
||||
$driver = Db::connect('global');
|
||||
$this->assertInstanceOf('DbDriver', $driver);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testConnectWithConfigParam()
|
||||
{
|
||||
$conf = array('hostname' => 'localhost', 'driver' => 'MySQLiDriverMock', 'database' => 'db', 'username' => 'test', 'password' => '1234');
|
||||
$this->getMockForAbstractClass('DbDriver', array(), 'MySQLiDriverMock', false);
|
||||
$driver = Db::connect('mysql', $conf);
|
||||
$this->assertInstanceOf('DbDriver', $driver);
|
||||
}
|
||||
|
||||
public function testConnectWithWrongDriver()
|
||||
{
|
||||
$this->getMock('NotDbDriver', array(), array(), 'NoDbDriverMock');
|
||||
$this->setExpectedException('InitializationException', 'Database driver must extends DbDriver');
|
||||
$driver = Db::connect('nodb', array('hostname' => 'localhost', 'driver' => 'NoDbDriverMock'));
|
||||
}
|
||||
}
|
389
Tests/model/MongoDbCommandTest.php
Normal file
389
Tests/model/MongoDbCommandTest.php
Normal file
@ -0,0 +1,389 @@
|
||||
<?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 testCountCommand()
|
||||
{
|
||||
$count_cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT, $this->collection);
|
||||
$count_cmd->bindParam('condition', array('name' => 'bread'));
|
||||
$count_result = $count_cmd->execute();
|
||||
$find_cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$find_cmd->bindParam('condition', array('name' => 'bread'))->bindParam('fields', array());
|
||||
$find_result = $find_cmd->execute();
|
||||
$this->assertEquals(0, $count_result);
|
||||
$this->assertEquals($count_result, $find_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();
|
||||
|
||||
$count_cmd->bindParam('condition', array('name' => 'insert'));
|
||||
$this->assertEquals(2, $count_cmd->execute());
|
||||
$find_cmd->bindParam('condition', array('name' => 'insert'));
|
||||
$this->assertEquals($find_cmd->execute()->count(), $count_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 testInsertCommandMultipleObjects()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
|
||||
$data = array(
|
||||
array('name' => 'first object'),
|
||||
array('name' => 'second object'),
|
||||
array('name' => 'equal object'),
|
||||
array('name' => 'equal object')
|
||||
);
|
||||
$cmd
|
||||
->bindParam('data', $data)
|
||||
->bindParam('multiple', true)
|
||||
->bindParam('safe', true);
|
||||
|
||||
$this->assertFalse($cmd->getInsertId());
|
||||
|
||||
$this->assertArrayHasKey('n', $cmd->execute());
|
||||
|
||||
$cmd->bindParam('data', array());
|
||||
$cmd->execute();
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$cmd->bindParam('condition', array('name' => 'first object'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(1, $result->count());
|
||||
$cmd->bindParam('condition', array('name' => 'second object'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(1, $result->count());
|
||||
$cmd->bindParam('condition', array('name' => 'equal object'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(2, $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::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);
|
||||
$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());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
$cmd->bindParam('command', array());
|
||||
$this->assertStringStartsWith("\n" . '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("\n" . 'Collection: ', $cmd->__toString());
|
||||
$this->assertContains('Bulk insert: FALSE', $cmd->__toString());
|
||||
$this->assertContains('Data: ' . '[' . PHP_EOL . "\tname = insert" . PHP_EOL . ']' . PHP_EOL, $cmd->__toString());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
$cmd
|
||||
->bindParam('data', array('name' => 'insert'))
|
||||
->bindParam('multiple', true)
|
||||
->bindParam('safe', true);
|
||||
$this->assertContains('Bulk insert: TRUE', $cmd->__toString());
|
||||
|
||||
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
|
||||
$this->assertStringStartsWith("\n" . '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("\n" . '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("\n" . 'Collection: ', $cmd->__toString());
|
||||
}
|
||||
}
|
||||
|
||||
class CollectionMock
|
||||
{
|
||||
public $db = array();
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return PHP_EOL . 'CollectionMock';
|
||||
}
|
||||
}
|
387
Tests/model/MongoDriverTest.php
Normal file
387
Tests/model/MongoDriverTest.php
Normal file
@ -0,0 +1,387 @@
|
||||
<?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()
|
||||
{
|
||||
Config::set('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()
|
||||
{
|
||||
Config::set('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()
|
||||
{
|
||||
Config::set('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 testCursorCount()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$this->assertEquals(5, $mongo->find('items')->count(5));
|
||||
$this->assertCount(3, $mongo->find('items')->limit(3)->fetchAll());
|
||||
$this->assertEquals(5, $mongo->count('items'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testRemove()
|
||||
{
|
||||
Config::set('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()
|
||||
{
|
||||
Config::set('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->find('items', array('name' => 'meat'))->fetch()->weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testBatchInsert()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$data = array(
|
||||
array('name' => 'first object'),
|
||||
array('name' => 'second object'),
|
||||
array('name' => 'equal object'),
|
||||
array('name' => 'equal object')
|
||||
);
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$mongo->insert('items', $data, true);
|
||||
$this->assertEquals(1, $mongo->find('items', array('name' => 'first object'))->numRows());
|
||||
$this->assertEquals(1, $mongo->find('items', array('name' => 'second object'))->numRows());
|
||||
$this->assertEquals(2, $mongo->find('items', array('name' => 'equal object'))->numRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGetInsertId()
|
||||
{
|
||||
Config::set('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()
|
||||
{
|
||||
Config::set('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->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')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testUpsert()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
|
||||
$mongo->insert('items', array('name' => 'bread'));
|
||||
$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));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFindAndModify()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
|
||||
$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->find('items', array('name' => 'bread'))->fetch()->quantity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFindAndModifyNoItem()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
|
||||
$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->find('items', array('name' => 'bread'))->fetch()->quantity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testEvalCommand()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$result = $mongo->command('items', array('$eval' => 'function() { return db.items.count();}'));
|
||||
$this->assertEquals(5, $result->fetch());
|
||||
$this->assertEquals(5, $mongo->count('items'));
|
||||
$result = $mongo->command('items', array('$eval' => 'function() { return "HELLO!";}'));
|
||||
$this->assertEquals("HELLO!", $result->fetch());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testEval()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$result = $mongo->command('items', array('$eval' => 'function() {return true; }'));
|
||||
$this->assertTrue($result->fetch());
|
||||
$result = $mongo->command('items', array('$eval' => 'function() {return "Hello"; }'));
|
||||
$this->assertSame('Hello', $result->fetch());
|
||||
$result = $mongo->command('items', array('$eval' => 'function() {return db.items.count(); }'));
|
||||
$this->assertEquals(5, $result->fetch());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCommand()
|
||||
{
|
||||
Config::set('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)));
|
||||
}
|
||||
}
|
432
Tests/model/MongoModelTest.php
Normal file
432
Tests/model/MongoModelTest.php
Normal file
@ -0,0 +1,432 @@
|
||||
<?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';
|
||||
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
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* @var ReflectionMethod
|
||||
*/
|
||||
private $method_count;
|
||||
|
||||
/**
|
||||
* @var ReflectionMethod
|
||||
*/
|
||||
private $method_fetch;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
$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'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testModel()
|
||||
{
|
||||
$this->assertInstanceOf('MongoMockModel', $this->model);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetch()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testDelete()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$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->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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testBatchInsert()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$data = array(
|
||||
array('name' => 'first object'),
|
||||
array('name' => 'second object'),
|
||||
array('name' => 'equal object'),
|
||||
array('name' => 'equal object')
|
||||
);
|
||||
$this->model->batchInsert($data);
|
||||
$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')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCount()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$this->assertEquals(5, $this->method_count->invoke($this->model));
|
||||
$this->assertEquals(2, $this->method_count->invoke($this->model, array('name' => 'eggs')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testUseMongoId()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$this->assertAttributeEquals(true, 'useMongoId', $this->model);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testId()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$class = new ReflectionClass('MongoModel');
|
||||
$prop = $class->getProperty('useMongoId');
|
||||
$prop->setAccessible(true);
|
||||
|
||||
$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->method_count->invoke($this->model, array('name' => 'testbread')));
|
||||
|
||||
$prop->setValue($this->model, false);
|
||||
$this->model->delete(1);
|
||||
$this->assertSame(1, $this->method_count->invoke($this->model, array('name' => 'testbread')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testIdToMongoId()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$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->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->method_count->invoke($this->model, array('name' => 'testbread')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testAddCondition()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$model = new ReflectionClass('MongoModel');
|
||||
$method = $model->getMethod('addCondition');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$query = array();
|
||||
$result = $method->invokeArgs($this->model, array(&$query, 'name', 'tony'));
|
||||
$this->assertSame(array('name' => 'tony'), $query);
|
||||
$this->assertSame($this->model, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testAddConditionEmptyValue()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$model = new ReflectionClass('MongoModel');
|
||||
$method = $model->getMethod('addCondition');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$query = array();
|
||||
$method->invokeArgs($this->model, array(&$query, 'name', false));
|
||||
$this->assertEmpty($query);
|
||||
$method->invokeArgs($this->model, array(&$query, 'name', null));
|
||||
$this->assertEmpty($query);
|
||||
}
|
||||
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->selectCollection($collection)->remove(array());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
480
Tests/model/MongoStatementTest.php
Normal file
480
Tests/model/MongoStatementTest.php
Normal file
@ -0,0 +1,480 @@
|
||||
<?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__) . '/../../Registry.php';
|
||||
require_once dirname(__FILE__) . '/../../Config.php';
|
||||
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', '__toString'))
|
||||
->getMock();
|
||||
$this->stmt = new MongoStatement($this->driver, $this->request);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testAffectedNumRowsNoResult()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', false);
|
||||
|
||||
$this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
|
||||
$this->assertTrue($this->stmt->execute());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testExecuteNoResult()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', true);
|
||||
$this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
|
||||
$this->assertTrue($this->stmt->execute());
|
||||
$this->assertEquals(10, $this->stmt->numRows());
|
||||
$this->assertContains('Queries: 1', Profiler::getInstance()->getCli());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testBindParam()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', false);
|
||||
$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());
|
||||
$this->assertContains('Queries not counted.', Profiler::getInstance()->getCli());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchWithInitialArray()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', true);
|
||||
$this->assertFalse($this->stmt->fetch());
|
||||
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('retval' => 'val')));
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->assertEquals('val', $this->stmt->fetch());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testFetchAssocFromCursor()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', 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 testCount()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', true);
|
||||
$this->setDriverGetConnectionMethod()->setRequestForFetch();
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->assertSame(10, $this->stmt->count());
|
||||
|
||||
$this->stmt->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCountException()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', 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 count result of opened cursor');
|
||||
$this->stmt->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testOrderException()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', 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;
|
||||
}
|
||||
}
|
206
Tests/model/MySQLiDriverTest.php
Normal file
206
Tests/model/MySQLiDriverTest.php
Normal file
@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-7
|
||||
*
|
||||
* Unit tests for MySQLiDriver class
|
||||
*/
|
||||
|
||||
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';
|
||||
|
||||
class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase
|
||||
{
|
||||
static private $pdo = null;
|
||||
|
||||
private $conn = null;
|
||||
|
||||
private $conf = array();
|
||||
|
||||
|
||||
|
||||
protected function getConnection()
|
||||
{
|
||||
if ($this->conn === null) {
|
||||
if (self::$pdo == null) {
|
||||
self::$pdo = new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
|
||||
}
|
||||
$this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DBNAME']);
|
||||
}
|
||||
|
||||
$this->conf = array('hostname' => 'localhost', 'database' => $GLOBALS['DB_DBNAME'], 'username' => $GLOBALS['DB_USER'], 'password' => $GLOBALS['DB_PASSWD']);
|
||||
|
||||
return $this->conn;
|
||||
}
|
||||
|
||||
protected function getDataSet()
|
||||
{
|
||||
return $this->createFlatXMLDataSet(dirname(__FILE__) . '/testData.xml');
|
||||
}
|
||||
|
||||
public function run(PHPUnit_Framework_TestResult $result = NULL)
|
||||
{
|
||||
$this->setPreserveGlobalState(false);
|
||||
return parent::run($result);
|
||||
}
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testDriver()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$driver = new MySQLiDriver($this->conf);
|
||||
|
||||
$queryTable = $this->getConnection()->createQueryTable(
|
||||
'table', 'SELECT * FROM `table`'
|
||||
);
|
||||
$expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
|
||||
->getTable("table");
|
||||
$this->assertTablesEqual($expectedTable, $queryTable);
|
||||
|
||||
$this->assertSame(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
|
||||
$this->assertSame(3, $this->getConnection()->getRowCount('table'));
|
||||
}
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testGetConnection()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$driver = new MySQLiDriver($this->conf);
|
||||
$this->assertInstanceOf('mysqli', $driver->getConnection());
|
||||
$this->assertTrue($driver->isConnected());
|
||||
}
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testGetConnectionWrongConfig()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$this->conf['database'] = 'nodb';
|
||||
$driver = new MySQLiDriver($this->conf);
|
||||
|
||||
$this->setExpectedException('GeneralException', 'Unknown database \'nodb\'');
|
||||
|
||||
$this->assertNull('mysqli', $driver->getConnection());
|
||||
}
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testDisconnect()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$driver = new MySQLiDriver($this->conf);
|
||||
|
||||
$driver->disconnect();
|
||||
$this->assertAttributeEquals(null, 'connection', $driver);
|
||||
|
||||
$this->assertInstanceOf('mysqli', $driver->getConnection());
|
||||
$this->assertAttributeInstanceOf('mysqli', 'connection', $driver);
|
||||
$driver->disconnect();
|
||||
$this->assertAttributeEquals(null, 'connection', $driver);
|
||||
}
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testInsert()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$driver = new MySQLiDriver($this->conf);
|
||||
|
||||
$this->assertSame(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
|
||||
$this->assertSame(3, $driver->getInsertId());
|
||||
$this->assertSame(1, $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
|
||||
$this->assertSame(4, $driver->getInsertId());
|
||||
$this->assertSame(1, $driver->insert('table', array('id' => null, 'user' => true, 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
|
||||
$this->assertSame(5, $driver->getInsertId());
|
||||
$this->assertSame(2, $driver->insert('table', array('id' => '5', 'user' => true, 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'), array('id' => 8)));
|
||||
$this->assertSame(8, $driver->getInsertId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO: DbDriver::getInsertId($table = null, $key = null) - params not used
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testGetInsertId()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$driver = new MySQLiDriver($this->conf);
|
||||
|
||||
$this->assertSame(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
|
||||
$this->assertSame(3, $driver->getInsertId());
|
||||
}
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testTransaction()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$driver = new MySQLiDriver($this->conf);
|
||||
|
||||
$queryTable = $this->getConnection()->createQueryTable(
|
||||
'table', 'SELECT * FROM `table`'
|
||||
);
|
||||
$expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
|
||||
->getTable("table");
|
||||
$this->assertTablesEqual($expectedTable, $queryTable);
|
||||
|
||||
$driver->getConnection();
|
||||
$driver->beginTransaction();
|
||||
$driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
|
||||
$driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
|
||||
$driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
|
||||
$driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
|
||||
|
||||
$queryTable = $this->getConnection()->createQueryTable(
|
||||
'table', 'SELECT * FROM `table`'
|
||||
);
|
||||
$driver->commit();
|
||||
$expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testDataAfterCommit.xml')
|
||||
->getTable("table");
|
||||
$this->assertTablesEqual($expectedTable, $queryTable);
|
||||
}
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testRollback()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
|
||||
$driver = new MySQLiDriver($this->conf);
|
||||
|
||||
$queryTable = $this->getConnection()->createQueryTable(
|
||||
'table', 'SELECT * FROM `table`'
|
||||
);
|
||||
$expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
|
||||
->getTable("table");
|
||||
$this->assertTablesEqual($expectedTable, $queryTable);
|
||||
|
||||
$driver->getConnection();
|
||||
$driver->beginTransaction();
|
||||
$driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
|
||||
$driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
|
||||
$driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
|
||||
$driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
|
||||
$driver->rollback();
|
||||
$queryTable = $this->getConnection()->createQueryTable(
|
||||
'table', 'SELECT * FROM `table`'
|
||||
);
|
||||
$this->assertTablesEqual($expectedTable, $queryTable);
|
||||
}
|
||||
}
|
360
Tests/model/MySQLiStatementTest.php
Normal file
360
Tests/model/MySQLiStatementTest.php
Normal file
@ -0,0 +1,360 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-4
|
||||
*
|
||||
* Unit tests for MySQLiStatement class
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../Registry.php';
|
||||
require_once dirname(__FILE__) . '/../../Config.php';
|
||||
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/MySQLiStatement.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
|
||||
class MySQLiStatementTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var MySQLiDriver|PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $driver;
|
||||
|
||||
/**
|
||||
* @var MySQLiStatement
|
||||
*/
|
||||
private $stmt;
|
||||
|
||||
private $sql;
|
||||
|
||||
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('quote', 'getConnection'))
|
||||
->getMock();
|
||||
$this->sql = 'SELECT * :place FROM :place AND :new';
|
||||
$this->stmt = new MySQLiStatement($this->driver, $this->sql);
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', false);
|
||||
$this->setDriverGetConnectionMethod();
|
||||
|
||||
$this->sql = 'PLAIN SQL';
|
||||
$result = $this->stmt->execute(array());
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testFetchNoResult()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', false);
|
||||
$this->assertFalse($this->stmt->fetch());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testDriverExecuteNoResult()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', false);
|
||||
$this->setDriverGetConnectionWrongResultMethod();
|
||||
$this->setExpectedException('GeneralException', 'ERROR');
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testFetch()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', false);
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertSame('OBJECT', $this->stmt->fetch());
|
||||
$this->assertSame('ARRAY', $this->stmt->fetch(Db::FETCH_NUM));
|
||||
$this->assertSame('ASSOC', $this->stmt->fetch(Db::FETCH_ASSOC));
|
||||
$this->assertSame('ARRAY', $this->stmt->fetch(Db::FETCH_BOTH));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testFetchObject()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', false);
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertSame('OBJECT', $this->stmt->fetchObject());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testFetchPairs()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', 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()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', true);
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertSame('OBJECT', $this->stmt->fetch());
|
||||
$this->assertContains('Queries: 1', Profiler::getInstance()->getCli());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testClose()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
Config::set('PROFILER_DETAILS', false);
|
||||
$this->assertAttributeEquals(null, 'result', $this->stmt);
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertAttributeNotEquals(null, 'result', $this->stmt);
|
||||
$this->stmt->close();
|
||||
$this->assertAttributeEquals(null, 'result', $this->stmt);
|
||||
$this->assertContains('Queries not counted.', Profiler::getInstance()->getCli());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testAffectedRows()
|
||||
{
|
||||
$mysqliMock = $this->getMockBuilder('MysqliDrvr');
|
||||
$mysqliMock->affected_rows = 'AFFECTED_ROWS';
|
||||
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('getConnection')
|
||||
->will($this->returnValue($mysqliMock));
|
||||
|
||||
$this->assertSame('AFFECTED_ROWS', $this->stmt->affectedRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testNumRowsNoResult()
|
||||
{
|
||||
$this->assertFalse($this->stmt->numRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @TODO: exception just for code coverage - could not mock mysqli properties
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testNumRows()
|
||||
{
|
||||
$this->markTestSkipped('Unable to execute a method or access a property of an incomplete object.');
|
||||
Config::set('PROFILER_DETAILS', false);
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertNull($this->stmt->numRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group MySQL
|
||||
*/
|
||||
public function testFetchInvalidMode()
|
||||
{
|
||||
Config::set('PROFILER_DETAILS', false);
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
|
||||
$this->setExpectedException('GeneralException');
|
||||
|
||||
$this->stmt->fetch(324);
|
||||
}
|
||||
|
||||
private function setDriverGetConnectionMethod()
|
||||
{
|
||||
$resultMock = $this->getMockBuilder('mysqli_result')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('fetch_object', 'fetch_array', 'fetch_assoc', 'close'))
|
||||
->setMockClassName('Mysqli_Result_Mock')
|
||||
->getMock();
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('fetch_object')
|
||||
->will($this->returnValue('OBJECT'));
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('fetch_array')
|
||||
->will($this->returnValue('ARRAY'));
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('fetch_assoc')
|
||||
->will($this->returnValue('ASSOC'));
|
||||
$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));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function setDriverGetConnectionWrongResultMethod()
|
||||
{
|
||||
$mysqliMock = $this->getMock('MysqliDrvr', array('query'));
|
||||
$mysqliMock
|
||||
->expects($this->any())
|
||||
->method('query')
|
||||
->with($this->anything())
|
||||
->will($this->returnValue(false));
|
||||
$mysqliMock->error = 'ERROR';
|
||||
$mysqliMock->errno = 0;
|
||||
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('getConnection')
|
||||
->will($this->returnValue($mysqliMock));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function driverQuote($val)
|
||||
{
|
||||
return $val;
|
||||
}
|
||||
|
||||
public function dbStatementAssemble($val)
|
||||
{
|
||||
return $val;
|
||||
}
|
||||
|
||||
}
|
177
Tests/model/SqlDbDriverTest.php
Normal file
177
Tests/model/SqlDbDriverTest.php
Normal file
@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-3
|
||||
*
|
||||
* Unit tests for DbDriver class
|
||||
*/
|
||||
|
||||
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 SqlDbDriverTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $driver;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$conf = array('hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
|
||||
$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('SqlDbDriver', array($conf));
|
||||
}
|
||||
catch (GeneralException $expected) {
|
||||
$this->fail($expected->getMessage());
|
||||
}
|
||||
$this->assertInstanceOf('DbDriver', $this->driver);
|
||||
}
|
||||
|
||||
public function testConstructWrongConfig()
|
||||
{
|
||||
$conf = array('hostname' => 'localhost', 'database' => 'db');
|
||||
|
||||
$this->setExpectedException('GeneralException', 'Configuration must have a "username"');
|
||||
|
||||
$this->getMockForAbstractClass('SqlDbDriver', array($conf));
|
||||
}
|
||||
|
||||
public function testGetConnection()
|
||||
{
|
||||
$this->assertNull($this->driver->getConnection());
|
||||
}
|
||||
|
||||
public function testBeginTransaction()
|
||||
{
|
||||
$this->assertSame($this->driver, $this->driver->beginTransaction());
|
||||
}
|
||||
|
||||
public function testCommit()
|
||||
{
|
||||
$this->assertSame($this->driver, $this->driver->commit());
|
||||
}
|
||||
|
||||
public function testRollback()
|
||||
{
|
||||
$this->assertSame($this->driver, $this->driver->rollback());
|
||||
}
|
||||
|
||||
public function testQuery()
|
||||
{
|
||||
$this->setDriverPrepareFunction();
|
||||
|
||||
$stmt = $this->driver->query('SELECT * FROM table');
|
||||
$this->assertInstanceOf('DbStmt', $stmt);
|
||||
$this->assertSame('SELECT * FROM table', $stmt->string());
|
||||
|
||||
$stmt = $this->driver->query('SELECT * FROM table', 'simple');
|
||||
$this->assertInstanceOf('DbStmt', $stmt);
|
||||
$this->assertSame('SELECT * FROM table', $stmt->string());
|
||||
}
|
||||
|
||||
public function testInsert()
|
||||
{
|
||||
$this->setDriverPrepareFunction()
|
||||
->setDriverQuoteFunction();
|
||||
$bind = array('table.name' => 'tony', 'chair.age' => 21, 'height' => 185);
|
||||
$sql = $this->driver->insert('users', $bind);
|
||||
$this->assertSame('INSERT INTO `users` (`table`.`name`, `chair`.`age`, `height`) VALUES (tony, 21, 185)', $sql);
|
||||
}
|
||||
|
||||
public function testUpdate()
|
||||
{
|
||||
$this->setDriverPrepareFunction()
|
||||
->setDriverQuoteFunction();
|
||||
|
||||
$bind = array('table.name' => 'tony', 'chair.age' => 21, 'height' => 185);
|
||||
$sql = $this->driver->update('users', $bind);
|
||||
$this->assertSame('UPDATE `users` SET `table`.`name`=tony, `chair`.`age`=21, `height`=185', $sql);
|
||||
}
|
||||
|
||||
public function testDeleteNoWHERE()
|
||||
{
|
||||
$this->setDriverPrepareFunction();
|
||||
|
||||
$sql = $this->driver->delete('users');
|
||||
$this->assertSame('DELETE FROM `users`', $sql);
|
||||
}
|
||||
|
||||
public function testDeleteWithWHEREArray()
|
||||
{
|
||||
$this->setDriverPrepareFunction()
|
||||
->setDriverQuoteFunction();
|
||||
|
||||
$sql = $this->driver->delete('users', array('name?tony' => new DbExpr('='), 'height?185' => '>'));
|
||||
$this->assertSame('DELETE FROM `users` WHERE name=tony AND height>185', $sql);
|
||||
}
|
||||
|
||||
public function testDeleteWithWHERESimpleCond()
|
||||
{
|
||||
$this->setDriverPrepareFunction();
|
||||
|
||||
$sql = $this->driver->delete('users', 'name=tony');
|
||||
$this->assertSame('DELETE FROM `users` WHERE name=tony', $sql);
|
||||
}
|
||||
|
||||
public function testDeleteWithWHEREKeyArray()
|
||||
{
|
||||
$this->setDriverPrepareFunction();
|
||||
|
||||
$sql = $this->driver->delete('users', array('name=tony' => 0));
|
||||
$this->assertSame('DELETE FROM `users` WHERE name=tony', $sql);
|
||||
}
|
||||
|
||||
public function testDeleteWithWHEREDbExpr()
|
||||
{
|
||||
$this->setDriverPrepareFunction();
|
||||
|
||||
$sql = $this->driver->delete('users', new DbExpr('name=tony'));
|
||||
$this->assertSame('DELETE FROM `users` WHERE name=tony', $sql);
|
||||
}
|
||||
|
||||
protected function setDriverPrepareFunction()
|
||||
{
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('prepare')
|
||||
->with($this->anything())
|
||||
->will($this->returnCallback(array($this, 'dbDriverPrepare')));
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setDriverQuoteFunction()
|
||||
{
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('driverQuote')
|
||||
->with($this->anything())
|
||||
->will($this->returnArgument(0));
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function dbDriverPrepare($sql)
|
||||
{
|
||||
$stmt = $this->getMock('DbStmt', array('execute', 'string', 'affectedRows'));
|
||||
$stmt->expects($this->any())
|
||||
->method('execute')
|
||||
->with($this->anything());
|
||||
$stmt->expects($this->any())
|
||||
->method('string')
|
||||
->will($this->returnValue($sql));
|
||||
$stmt->expects($this->any())
|
||||
->method('affectedRows')
|
||||
->will($this->returnValue($sql));
|
||||
return $stmt;
|
||||
}
|
||||
}
|
246
Tests/model/SqlModelTest.php
Normal file
246
Tests/model/SqlModelTest.php
Normal file
@ -0,0 +1,246 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-7
|
||||
*
|
||||
* Unit tests for Model 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/DbDriver.php';
|
||||
require_once dirname(__FILE__) . '/MyDbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/Model.php';
|
||||
require_once dirname(__FILE__) . '/../../model/SqlModel.php';
|
||||
|
||||
class SqlModelTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
private $model;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$conf = array('default' => array('driver' => 'MockDbDriver', 'hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234'));
|
||||
if (!class_exists('MockDbDriver')) {
|
||||
$this->getMockForAbstractClass('MyDbDriver', array($conf), 'MockDbDriver', false);
|
||||
}
|
||||
|
||||
Config::set('Db', $conf);
|
||||
if (!class_exists('MockModel')) {
|
||||
$this->model = $this->getMockForAbstractClass('SqlModel', array(), 'MockModel');
|
||||
} else {
|
||||
$this->model = new MockModel();
|
||||
}
|
||||
set_new_overload(array($this, 'newCallback'));
|
||||
}
|
||||
|
||||
public function testModel()
|
||||
{
|
||||
$this->assertInstanceOf('SqlModel', $this->model);
|
||||
}
|
||||
|
||||
public function testGetInsertId()
|
||||
{
|
||||
$this->assertTrue($this->model->getInsertId());
|
||||
}
|
||||
|
||||
public function testIdentify()
|
||||
{
|
||||
$param = 'param';
|
||||
$this->assertSame($param, $this->model->identify($param));
|
||||
}
|
||||
|
||||
public function testQuote()
|
||||
{
|
||||
$param = 'param';
|
||||
$this->assertSame($param, $this->model->quote($param));
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$this->assertTrue($this->model->get(1));
|
||||
}
|
||||
|
||||
public function testInsert()
|
||||
{
|
||||
$this->assertTrue($this->model->insert(array('data')));
|
||||
}
|
||||
|
||||
public function testUpdate()
|
||||
{
|
||||
$this->assertSame('mock', $this->model->update(array('var' => 'val'), 1));
|
||||
}
|
||||
|
||||
public function testDelete()
|
||||
{
|
||||
$this->assertSame('mock', $this->model->delete(1));
|
||||
}
|
||||
|
||||
public function testOrder()
|
||||
{
|
||||
$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()));
|
||||
|
||||
$this->assertSame(' ORDER BY name ASC', $method->invoke($this->model, array('sort' => 'name', 'order' => 'DESC'), array('id', 'name')));
|
||||
}
|
||||
|
||||
public function testSearch()
|
||||
{
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('search');
|
||||
$method->setAccessible(true);
|
||||
$this->assertEmpty($method->invoke($this->model, array()));
|
||||
$this->assertEmpty($method->invoke($this->model, array('q' => 'in', 'qt' => 'name')));
|
||||
$this->assertSame('test.name LIKE %on%', $method->invoke($this->model, array('qt' => 'name', 'q' => 'on'), array('name'), 'test'));
|
||||
}
|
||||
|
||||
public function testFetch()
|
||||
{
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('fetch');
|
||||
$method->setAccessible(true);
|
||||
$key = $this->getCacheKeyMockGetSet();
|
||||
$this->assertTrue(true, $method->invoke($this->model, 'SELECT', array(), $key));
|
||||
}
|
||||
|
||||
public function testFetchField()
|
||||
{
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('fetchField');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$key = $this->getCacheKeyMockGetSet();
|
||||
$this->assertSame('field', $method->invoke($this->model, 'SELECT', array(), 'field', $key));
|
||||
}
|
||||
|
||||
public function testFetchAll()
|
||||
{
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('fetchAll');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$key = $this->getCacheKeyMockGetSet();
|
||||
$this->assertTrue(true, $method->invoke($this->model, 'SELECT', array(), $key));
|
||||
}
|
||||
|
||||
public function testGetCache()
|
||||
{
|
||||
$model = new ReflectionClass('MockModel');
|
||||
$method = $model->getMethod('getCache');
|
||||
$method->setAccessible(true);
|
||||
|
||||
Config::set('Model', 'MockCache');
|
||||
if (!class_exists('MockCache')) {
|
||||
$this->getMock('Cache', array(), array(), 'MockCache');
|
||||
}
|
||||
$this->assertInstanceOf('MockCache', $method->invoke($this->model));
|
||||
}
|
||||
|
||||
public function testCacheKey()
|
||||
{
|
||||
$model = new ReflectionClass('MockModel');
|
||||
$method = $model->getMethod('cacheKey');
|
||||
$method->setAccessible(true);
|
||||
|
||||
Config::set('Model', 'MockCache');
|
||||
if (!class_exists('MockCache')) {
|
||||
$this->getMock('Cache', array(), array(), 'MockCache', false);
|
||||
}
|
||||
if (!class_exists('MockCacheKey')) {
|
||||
$this->getMock('CacheKey', array(), array(), 'MockCacheKey', false);
|
||||
}
|
||||
$this->assertInstanceOf('MockCacheKey', $method->invoke($this->model, 'name'));
|
||||
}
|
||||
|
||||
public function testAddCleanCache()
|
||||
{
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('addCleanCache');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$key = $this->getMock('Key', array('set', 'get'));
|
||||
$method->invoke($this->model, $key);
|
||||
$method->invoke($this->model, $key);
|
||||
$method->invoke($this->model, $key);
|
||||
$this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model);
|
||||
}
|
||||
|
||||
public function testCleanCaches()
|
||||
{
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('addCleanCache');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$key = $this->getCacheKeyMockDel(3);
|
||||
$method->invoke($this->model, $key);
|
||||
$method->invoke($this->model, $key);
|
||||
$method->invoke($this->model, $key);
|
||||
$this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model);
|
||||
|
||||
$model = new ReflectionClass('SqlModel');
|
||||
$method = $model->getMethod('cleanCaches');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$method->invoke($this->model, $key);
|
||||
$this->assertAttributeEquals(array(), 'caches_clean', $this->model);
|
||||
}
|
||||
|
||||
protected function getCacheKeyMockGetSet()
|
||||
{
|
||||
$key = $this->getMock('Key', array('set', 'get'));
|
||||
$key
|
||||
->expects($this->any())
|
||||
->method('set')
|
||||
->with($this->anything())
|
||||
->will($this->returnValue(true));
|
||||
$key
|
||||
->expects($this->any())
|
||||
->method('get')
|
||||
->will($this->returnValue(false));
|
||||
return $key;
|
||||
}
|
||||
|
||||
protected function getCacheKeyMockDel($count)
|
||||
{
|
||||
$key = $this->getMock('Key', array('del'));
|
||||
$key
|
||||
->expects($this->exactly($count))
|
||||
->method('del')
|
||||
->will($this->returnValue(true));
|
||||
return $key;
|
||||
}
|
||||
|
||||
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 'MockDbDriver':
|
||||
return 'MockDbDriver';
|
||||
case 'CacheKey':
|
||||
return 'MockCacheKey';
|
||||
default:
|
||||
return $className;
|
||||
}
|
||||
}
|
||||
}
|
5
Tests/model/testData.xml
Normal file
5
Tests/model/testData.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" ?>
|
||||
<dataset>
|
||||
<table id="1" content="Hello buddy!" user="joe" created="2010-04-24 17:15:23" />
|
||||
<table id="2" content="I like it!" user="nancy" created="2010-04-26 12:14:20" />
|
||||
</dataset>
|
9
Tests/model/testDataAfterCommit.xml
Normal file
9
Tests/model/testDataAfterCommit.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" ?>
|
||||
<dataset>
|
||||
<table id="1" content="Hello buddy!" user="joe" created="2010-04-24 17:15:23" />
|
||||
<table id="2" content="I like it!" user="nancy" created="2010-04-26 12:14:20" />
|
||||
<table id="3" content="some test content" user="tony" created="2011-11-07 11:35:20" />
|
||||
<table id="4" content="some test content" user="tony" created="2011-11-07 11:35:20" />
|
||||
<table id="5" content="some test content" user="tony" created="2011-11-07 11:35:20" />
|
||||
<table id="6" content="some test content" user="tony" created="2011-11-07 11:35:20" />
|
||||
</dataset>
|
19
Tests/model/testdb.sql
Normal file
19
Tests/model/testdb.sql
Normal file
@ -0,0 +1,19 @@
|
||||
--
|
||||
-- База данных: `testdb`
|
||||
--
|
||||
CREATE DATABASE `testdb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
|
||||
USE `testdb`;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Структура таблицы `table`
|
||||
--
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `table` (
|
||||
`id` int(3) NOT NULL AUTO_INCREMENT,
|
||||
`content` varchar(255) NOT NULL,
|
||||
`user` varchar(255) NOT NULL,
|
||||
`created` datetime NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
|
Reference in New Issue
Block a user