|
|
@ -10,8 +10,12 @@ |
|
|
|
* 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'; |
|
|
|
|
|
|
|
class MongoDriverTest extends PHPUnit_Framework_TestCase |
|
|
@ -101,66 +105,121 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase |
|
|
|
$this->assertFalse($mongo->isConnected()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @runInSeparateProcess |
|
|
|
*/ |
|
|
|
public function testFind() |
|
|
|
{ |
|
|
|
if (!defined('DEBUG')) { |
|
|
|
define('DEBUG', false); |
|
|
|
} |
|
|
|
|
|
|
|
$mongo = new MongoDriver($this->conf); |
|
|
|
$mongo->getConnection(); |
|
|
|
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count()); |
|
|
|
$this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->count()); |
|
|
|
$this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->count()); |
|
|
|
$this->assertEquals(5, $mongo->find('items', array())->count()); |
|
|
|
$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 |
|
|
|
*/ |
|
|
|
public function testGet() |
|
|
|
{ |
|
|
|
if (!defined('DEBUG')) { |
|
|
|
define('DEBUG', false); |
|
|
|
} |
|
|
|
|
|
|
|
$mongo = new MongoDriver($this->conf); |
|
|
|
$mongo->getConnection(); |
|
|
|
$bread = $mongo->get('items', array('name' => 'bread')); |
|
|
|
$this->assertEquals(3.2, $bread['price']); |
|
|
|
$eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject(); |
|
|
|
$this->assertEquals(20, $eggs->quantity); |
|
|
|
$eggs = $mongo->get('items', array('name' => 'eggs'))->fetch(); |
|
|
|
$this->assertEquals('eggs', $eggs->name); |
|
|
|
$this->assertInstanceOf('MongoId', $eggs->_id); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @runInSeparateProcess |
|
|
|
*/ |
|
|
|
public function testRemove() |
|
|
|
{ |
|
|
|
if (!defined('DEBUG')) { |
|
|
|
define('DEBUG', false); |
|
|
|
} |
|
|
|
|
|
|
|
$mongo = new MongoDriver($this->conf); |
|
|
|
$mongo->getConnection(); |
|
|
|
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count()); |
|
|
|
$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'))->count()); |
|
|
|
$this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->numRows()); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @runInSeparateProcess |
|
|
|
*/ |
|
|
|
public function testInsert() |
|
|
|
{ |
|
|
|
if (!defined('DEBUG')) { |
|
|
|
define('DEBUG', false); |
|
|
|
} |
|
|
|
|
|
|
|
$mongo = new MongoDriver($this->conf); |
|
|
|
$mongo->getConnection(); |
|
|
|
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count()); |
|
|
|
$this->assertTrue($mongo->insert('items', array('name' => 'bread'))); |
|
|
|
$this->assertNotEmpty($mongo->getInsertId()); |
|
|
|
$this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->count()); |
|
|
|
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); |
|
|
|
$this->assertEquals(0, $mongo->insert('items', array('name' => 'bread'))); |
|
|
|
//$this->assertNotEmpty($mongo->getInsertId());
|
|
|
|
$this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows()); |
|
|
|
$this->assertEquals(0, $mongo->insert('items', array('name' => 'meat', 'weight' => 230))); |
|
|
|
$this->assertEquals(230, $mongo->get('items', array('name' => 'meat'))->fetch()->weight); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @runInSeparateProcess |
|
|
|
*/ |
|
|
|
public function testUpdate() |
|
|
|
{ |
|
|
|
if (!defined('DEBUG')) { |
|
|
|
define('DEBUG', false); |
|
|
|
} |
|
|
|
|
|
|
|
$mongo = new MongoDriver($this->conf); |
|
|
|
$mongo->getConnection(); |
|
|
|
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count()); |
|
|
|
$mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')); |
|
|
|
$fish = $mongo->get('items', array('name' => 'fish')); |
|
|
|
$this->assertEquals(200, $fish['price']); |
|
|
|
$this->assertEquals('today', $fish['date']); |
|
|
|
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); |
|
|
|
$this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish'))); |
|
|
|
$this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs'))); |
|
|
|
$fish = $mongo->get('items', array('name' => 'fish'))->fetch(); |
|
|
|
$this->assertEquals(200, $fish->price); |
|
|
|
$this->assertEquals('today', $fish->date); |
|
|
|
$this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'))); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @runInSeparateProcess |
|
|
|
*/ |
|
|
|
public function testUpsert() |
|
|
|
{ |
|
|
|
if (!defined('DEBUG')) { |
|
|
|
define('DEBUG', false); |
|
|
|
} |
|
|
|
|
|
|
|
$mongo = new MongoDriver($this->conf); |
|
|
|
$mongo->getConnection(); |
|
|
|
|
|
|
|
$this->assertTrue($mongo->insert('items', array('name' => 'bread'))); |
|
|
|
$id = $mongo->getInsertId(); |
|
|
|
$this->assertNotEmpty($id); |
|
|
|
$this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true)); |
|
|
|
$this->assertNotEquals($id, $mongo->getInsertId()); |
|
|
|
$mongo->insert('items', array('name' => 'bread')); |
|
|
|
$this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true)); |
|
|
|
$this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date); |
|
|
|
} |
|
|
|
} |