You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.7 KiB

<?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';
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);
}
}
/**
* @expectedException Exception
* @expectedExceptionMessage Configuration must have a "hostname".
*/
public function testGetConnectionNoHostname()
{
unset($this->conf['hostname']);
$mongo = new MongoDriver($this->conf);
$mongo->getConnection();
}
/**
* @expectedException MongoConnectionException
* @expectedExceptionMessage Couldn't authenticate with database
*/
public function testGetConnectionWrongPassword()
{
$this->conf['password'] = 'nopass';
$mongo = new MongoDriver($this->conf);
$this->assertInstanceOf('MongoDB', $mongo->getConnection());
}
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());
}
public function testFind()
{
$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());
}
public function testGet()
{
$mongo = new MongoDriver($this->conf);
$mongo->getConnection();
$bread = $mongo->get('items', array('name' => 'bread'));
$this->assertEquals(3.2, $bread['price']);
}
public function testRemove()
{
$mongo = new MongoDriver($this->conf);
$mongo->getConnection();
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count());
$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());
}
public function testInsert()
{
$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());
}
public function testUpdate()
{
$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(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
}
public function testUpsert()
{
$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());
}
}