Commit 69517eec authored by Jens Segers's avatar Jens Segers

Unit testing and bug fixes

parent 83f9b341
.DS_Store
phpunit.phar
/vendor
composer.phar
composer.lock
\ No newline at end of file
language: php
branches:
only:
- master
php:
- 5.3
- 5.4
services: mongodb
before_script:
- echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- composer install
script: phpunit
\ No newline at end of file
......@@ -11,7 +11,8 @@
"license" : "MIT",
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.0.x"
"illuminate/support": "4.0.x",
"illuminate/database": "4.0.x"
},
"autoload": {
"psr-0": {
......
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
\ No newline at end of file
......@@ -52,11 +52,21 @@ class Connection {
* Get a mongodb collection.
*
* @param string $name
* @return MongoDB
*/
public function getCollection($name) {
return $this->db->{$name};
}
/**
* Get the mongodb database object.
*
* @return MongoDB
*/
public function getDb() {
return $this->db;
}
/**
* Create a DSN string from a configuration.
*
......
......@@ -129,8 +129,15 @@ class Query extends \Illuminate\Database\Query\Builder {
return $this->collection->distinct($column, $wheres);
}
// Columns
$columns = array();
foreach ($this->columns as $column)
{
$columns[$column] = true;
}
// Get the MongoCursor
$cursor = $this->collection->find($wheres, $this->columns);
$cursor = $this->collection->find($wheres, $columns);
// Apply order, offset and limit
if ($this->orders) $cursor->sort($this->orders);
......@@ -138,7 +145,7 @@ class Query extends \Illuminate\Database\Query\Builder {
if ($this->limit) $cursor->limit($this->limit);
// Return results
return $cursor;
return iterator_to_array($cursor);
}
}
......@@ -340,7 +347,17 @@ class Query extends \Illuminate\Database\Query\Builder {
// Convert id's
if (isset($where['column']) && $where['column'] == '_id')
{
$where['value'] = ($where['value'] instanceof MongoID) ? $where['value'] : new MongoID($where['value']);
if (isset($where['values']))
{
foreach ($where['values'] as &$value)
{
$value = ($value instanceof MongoID) ? $value : new MongoID($value);
}
}
else
{
$where['value'] = ($where['value'] instanceof MongoID) ? $where['value'] : new MongoID($where['value']);
}
}
// First item of chain
......
<?php
use Jenssegers\Mongodb\Connection;
class ConnectionTest extends PHPUnit_Framework_TestCase {
private $collection;
private $connection;
public function setUp()
{
$config = array(
'host' => 'localhost',
'database' => 'unittest'
);
$this->connection = new Connection($config);
$this->collection = $this->connection->getCollection('unittest');
}
public function tearDown()
{
if ($this->collection)
{
$this->collection->drop();
}
}
public function testDb()
{
$db = $this->connection->getDb();
$this->assertInstanceOf('MongoDB', $db);
}
public function testCollection()
{
$this->assertInstanceOf('MongoCollection', $this->collection);
$this->assertEquals('unittest', $this->collection->getName());
}
}
\ No newline at end of file
<?php
require_once('models/User.php');
use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Model;
use Jenssegers\Mongodb\ConnectionResolver;
class ModelTest extends PHPUnit_Framework_TestCase {
public function setUp()
{
$app = array();
$app['config']['database']['connections']['mongodb'] = array(
'host' => 'localhost',
'database' => 'unittest'
);
Model::setConnectionResolver(new ConnectionResolver($app));
}
public function tearDown()
{
User::truncate();
}
public function testNewModel()
{
$user = new User();
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
$this->assertEquals(false, $user->exists);
$this->assertEquals('users', $user->getTable());
$this->assertEquals('_id', $user->getKeyName());
$this->assertEquals('users._id', $user->getQualifiedKeyName());
}
public function testInsert()
{
$user = new User();
$user->name = "John Doe";
$user->title = "admin";
$user->age = 35;
$user->save();
$this->assertEquals(true, $user->exists);
$this->assertEquals(1, User::count());
$this->assertInstanceOf('MongoId', $user->_id);
$this->assertNotEquals('', (string) $user->_id);
$this->assertNotEquals(0, strlen((string) $user->_id));
$this->assertEquals("John Doe", $user->name);
$this->assertEquals(35, $user->age);
}
public function testUpdate()
{
$user = new User();
$user->name = "John Doe";
$user->title = "admin";
$user->age = 35;
$user->save();
$check = User::find($user->_id);
$check->age = 36;
$check->save();
$this->assertEquals(true, $check->exists);
$this->assertEquals(1, User::count());
$this->assertEquals("John Doe", $check->name);
$this->assertEquals(36, $check->age);
}
public function testDelete()
{
$user = new User();
$user->name = "John Doe";
$user->title = "admin";
$user->age = 35;
$user->save();
$this->assertEquals(true, $user->exists);
$this->assertEquals(1, User::count());
$user->delete();
$this->assertEquals(0, User::count());
}
public function testAll()
{
$user = new User();
$user->name = "John Doe";
$user->title = "admin";
$user->age = 35;
$user->save();
$user = new User();
$user->name = "Jane Doe";
$user->title = "user";
$user->age = 32;
$user->save();
$all = User::all();
$this->assertEquals(2, count($all));
$this->assertEquals("John Doe", $all[0]->name);
$this->assertEquals("Jane Doe", $all[1]->name);
}
public function testFind()
{
$user = new User();
$user->name = "John Doe";
$user->title = "admin";
$user->age = 35;
$user->save();
$check = User::find($user->_id);
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $check);
$this->assertEquals(true, $check->exists);
$this->assertEquals("John Doe", $check->name);
$this->assertEquals(35, $check->age);
}
/**
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function testFindOrfail()
{
User::findOrfail('123');
}
public function testCreate()
{
$user = User::create(array('name' => 'Jane Poe'));
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
$this->assertEquals(true, $user->exists);
$this->assertEquals("Jane Poe", $user->name);
}
public function testDestroy()
{
$user = new User();
$user->name = "John Doe";
$user->title = "admin";
$user->age = 35;
$user->save();
User::destroy((string) $user->_id);
$this->assertEquals(0, User::count());
}
public function testTouch()
{
$user = new User();
$user->name = "John Doe";
$user->title = "admin";
$user->age = 35;
$user->save();
$old = $user->updated_at;
$user->touch();
$check = User::find($user->_id);
$this->assertNotEquals($old, $check->updated_at);
}
}
\ No newline at end of file
<?php
require_once('models/User.php');
use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Model;
use Jenssegers\Mongodb\ConnectionResolver;
class QueryTest extends PHPUnit_Framework_TestCase {
public function setUp()
{
$app = array();
$app['config']['database']['connections']['mongodb'] = array(
'host' => 'localhost',
'database' => 'unittest'
);
Model::setConnectionResolver(new ConnectionResolver($app));
// test data
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
User::create(array('name' => 'Robert Roe', 'age' => 37, 'title' => 'user'));
User::create(array('name' => 'Mark Moe', 'age' => 23, 'title' => 'user'));
User::create(array('name' => 'Brett Boe', 'age' => 35, 'title' => 'user'));
User::create(array('name' => 'Tommy Toe', 'age' => 33, 'title' => 'user'));
User::create(array('name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin'));
User::create(array('name' => 'Error', 'age' => null, 'title' => null));
}
public function tearDown()
{
User::truncate();
}
public function testGet()
{
$users = User::get();
$this->assertEquals(9, count($users));
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $users[0]);
}
public function testFirst()
{
$user = User::get()->first();
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
$this->assertEquals('John Doe', $user->name);
}
public function testWhere()
{
$users = User::where('age', 35)->get();
$this->assertEquals(3, count($users));
$users = User::where('age', '>=', 35)->get();
$this->assertEquals(4, count($users));
$users = User::where('age', '<=', 18)->get();
$this->assertEquals(1, count($users));
$users = User::where('age', '!=', 35)->get();
$this->assertEquals(6, count($users));
}
public function testAndWhere()
{
$users = User::where('age', 35)->where('title', 'admin')->get();
$this->assertEquals(2, count($users));
$users = User::where('age', '>=', 35)->where('title', 'user')->get();
$this->assertEquals(2, count($users));
}
public function testLike()
{
$users = User::where('name', 'like', '%doe')->get();
$this->assertEquals(2, count($users));
$users = User::where('name', 'like', '%y%')->get();
$this->assertEquals(3, count($users));
$users = User::where('name', 'like', 't%')->get();
$this->assertEquals(1, count($users));
}
public function testPluck()
{
$name = User::where('name', 'John Doe')->pluck('name');
$this->assertEquals('John Doe', $name);
}
public function testList()
{
$list = User::lists('title');
$this->assertEquals(9, count($list));
$this->assertEquals('admin', $list[0]);
$list = User::lists('title', 'name');
$this->assertEquals(9, count($list));
$this->assertEquals('John Doe', key($list));
$this->assertEquals('admin', $list['John Doe']);
}
public function testSelect()
{
$user = User::select('name')->first();
$this->assertEquals('John Doe', $user->name);
$this->assertEquals(null, $user->age);
$user = User::get(array('name'))->first();
$this->assertEquals('John Doe', $user->name);
$this->assertEquals(null, $user->age);
}
public function testOrWhere()
{
$users = User::where('age', 13)->orWhere('title', 'admin')->get();
$this->assertEquals(4, count($users));
$users = User::where('age', 13)->orWhere('age', 23)->get();
$this->assertEquals(2, count($users));
}
public function testBetween()
{
$users = User::whereBetween('age', array(0, 25))->get();
$this->assertEquals(2, count($users));
$users = User::whereBetween('age', array(13, 23))->get();
$this->assertEquals(2, count($users));
}
public function testIn()
{
$users = User::whereIn('age', array(13, 23))->get();
$this->assertEquals(2, count($users));
$users = User::whereIn('age', array(33, 35, 13))->get();
$this->assertEquals(6, count($users));
}
public function testWhereNull()
{
$users = User::whereNull('age')->get();
$this->assertEquals(1, count($users));
}
public function testOrder()
{
$user = User::whereNotNull('age')->orderBy('age', 'asc')->first();
$this->assertEquals(13, $user->age);
$user = User::whereNotNull('age')->orderBy('age', 'desc')->first();
$this->assertEquals(37, $user->age);
}
public function testTake()
{
$users = User::take(3)->get();
$this->assertEquals(3, count($users));
}
public function testOffset()
{
$users = User::skip(1)->take(2)->get();
$this->assertEquals(2, count($users));
$this->assertEquals('Jane Doe', $users[0]->name);
}
public function testAggregates()
{
$this->assertEquals(9, User::count());
$this->assertEquals(37, User::max('age'));
$this->assertEquals(13, User::min('age'));
$this->assertEquals(30.5, User::avg('age'));
$this->assertEquals(244, User::sum('age'));
$this->assertEquals(35, User::where('title', 'admin')->max('age'));
$this->assertEquals(37, User::where('title', 'user')->max('age'));
}
}
\ No newline at end of file
<?php
use Jenssegers\Mongodb\Model as Eloquent;
class User extends Eloquent {
protected $collection = 'users';
protected static $unguarded = true;
public function phone()
{
return $this->hasOne('Phone');
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment