Commit 72c64442 authored by Jens Segers's avatar Jens Segers

Merge master

parents e51ea71e 11f0f83f
...@@ -16,8 +16,7 @@ ...@@ -16,8 +16,7 @@
"illuminate/events": "4.2.x" "illuminate/events": "4.2.x"
}, },
"require-dev": { "require-dev": {
"illuminate/cache": "4.2.x", "orchestra/testbench": "dev-master",
"illuminate/auth": "4.2.x",
"mockery/mockery": "*" "mockery/mockery": "*"
}, },
"autoload": { "autoload": {
......
...@@ -39,5 +39,8 @@ ...@@ -39,5 +39,8 @@
<directory>tests/RelationsTest.php</directory> <directory>tests/RelationsTest.php</directory>
<directory>tests/MysqlRelationsTest.php</directory> <directory>tests/MysqlRelationsTest.php</directory>
</testsuite> </testsuite>
<testsuite name="validation">
<directory>tests/ValidationTest.php</directory>
</testsuite>
</testsuites> </testsuites>
</phpunit> </phpunit>
...@@ -10,16 +10,16 @@ class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseRemi ...@@ -10,16 +10,16 @@ class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseRemi
*/ */
protected function reminderExpired($reminder) protected function reminderExpired($reminder)
{ {
// Convert to object so that we can pass it to the parent method // Convert to array so that we can pass it to the parent method
if (is_array($reminder)) if (is_object($reminder))
{ {
$reminder = (object) $reminder; $reminder = (array) $reminder;
} }
// Convert the DateTime object that got saved to MongoDB // Convert the DateTime object that got saved to MongoDB
if (is_array($reminder->created_at)) if (is_array($reminder['created_at']))
{ {
$reminder->created_at = $reminder->created_at['date'] + $reminder->created_at['timezone']; $reminder['created_at'] = $reminder['created_at']['date'] + $reminder['created_at']['timezone'];
} }
return parent::reminderExpired($reminder); return parent::reminderExpired($reminder);
......
...@@ -487,6 +487,7 @@ class EmbedsMany extends Relation { ...@@ -487,6 +487,7 @@ class EmbedsMany extends Relation {
// Attatch the parent relation to the embedded model. // Attatch the parent relation to the embedded model.
$model->setRelation($this->foreignKey, $this->parent); $model->setRelation($this->foreignKey, $this->parent);
$model->setHidden(array_merge($model->getHidden(), array($this->foreignKey)));
$models[] = $model; $models[] = $model;
} }
......
<?php <?php
use Illuminate\Support\Facades\DB; class CacheTest extends TestCase {
class CacheTest extends PHPUnit_Framework_TestCase {
protected $cache;
public function setUp()
{
global $app;
$this->cache = $app['cache'];
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'));
}
public function tearDown() public function tearDown()
{ {
User::truncate(); User::truncate();
$this->cache->forget('db.users'); Cache::forget('db.users');
} }
public function testCache() public function testCache()
{ {
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'));
$users = DB::collection('users')->where('age', '>', 10)->remember(10)->get(); $users = DB::collection('users')->where('age', '>', 10)->remember(10)->get();
$this->assertEquals(3, count($users)); $this->assertEquals(3, count($users));
...@@ -33,7 +23,7 @@ class CacheTest extends PHPUnit_Framework_TestCase { ...@@ -33,7 +23,7 @@ class CacheTest extends PHPUnit_Framework_TestCase {
$users = User::where('age', '>', 10)->remember(10, 'db.users')->get(); $users = User::where('age', '>', 10)->remember(10, 'db.users')->get();
$this->assertEquals(3, count($users)); $this->assertEquals(3, count($users));
$users = $this->cache->get('db.users'); $users = Cache::get('db.users');
$this->assertEquals(3, count($users)); $this->assertEquals(3, count($users));
} }
......
<?php <?php
use Illuminate\Support\Facades\DB;
use Jenssegers\Mongodb\Connection;
class ConnectionTest extends PHPUnit_Framework_TestCase { class ConnectionTest extends TestCase {
public function setUp() {}
public function tearDown() {}
public function testConnection() public function testConnection()
{ {
...@@ -15,11 +9,11 @@ class ConnectionTest extends PHPUnit_Framework_TestCase { ...@@ -15,11 +9,11 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$c1 = DB::connection('mongodb'); $c1 = DB::connection('mongodb');
$c2 = DB::connection('mongodb'); $c2 = DB::connection('mongodb');
$this->assertEquals($c1, $c2); $this->assertEquals(spl_object_hash($c1), spl_object_hash($c2));
$c1 = DB::connection('mongodb'); $c1 = DB::connection('mongodb');
$c2 = DB::reconnect('mongodb'); $c2 = DB::reconnect('mongodb');
$this->assertNotEquals($c1, $c2); $this->assertNotEquals(spl_object_hash($c1), spl_object_hash($c2));
} }
public function testDb() public function testDb()
......
<?php <?php
class ModelTest extends PHPUnit_Framework_TestCase { class ModelTest extends TestCase {
public function setUp() {}
public function tearDown() public function tearDown()
{ {
......
<?php <?php
class MysqlRelationsTest extends PHPUnit_Framework_TestCase { class MysqlRelationsTest extends TestCase {
public function setUp() public function setUp()
{ {
parent::setUp();
MysqlUser::executeSchema(); MysqlUser::executeSchema();
MysqlBook::executeSchema(); MysqlBook::executeSchema();
MysqlRole::executeSchema(); MysqlRole::executeSchema();
......
<?php <?php
use Illuminate\Support\Facades\DB; class QueryBuilderTest extends TestCase {
class QueryBuilderTest extends PHPUnit_Framework_TestCase {
public function setUp() {}
public function tearDown() public function tearDown()
{ {
...@@ -238,17 +234,25 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -238,17 +234,25 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
DB::collection('users')->where('_id', $id)->push('messages', $message); DB::collection('users')->where('_id', $id)->push('messages', $message);
$user = DB::collection('users')->find($id); $user = DB::collection('users')->find($id);
$this->assertTrue(is_array($user['messages'])); $this->assertTrue(is_array($user['messages']));
$this->assertEquals(1, count($user['messages']));
$this->assertEquals($message, $user['messages'][0]); $this->assertEquals($message, $user['messages'][0]);
// Raw
DB::collection('users')->where('_id', $id)->push(array('tags' => 'tag3', 'messages' => array('from' => 'Mark', 'body' => 'Hi John')));
$user = DB::collection('users')->find($id);
$this->assertEquals(4, count($user['tags']));
$this->assertEquals(2, count($user['messages']));
} }
public function testPull() public function testPull()
{ {
$message = array('from' => 'Jane', 'body' => 'Hi John'); $message1 = array('from' => 'Jane', 'body' => 'Hi John');
$message2 = array('from' => 'Mark', 'body' => 'Hi John');
$id = DB::collection('users')->insertGetId(array( $id = DB::collection('users')->insertGetId(array(
'name' => 'John Doe', 'name' => 'John Doe',
'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'),
'messages' => array($message) 'messages' => array($message1, $message2)
)); ));
DB::collection('users')->where('_id', $id)->pull('tags', 'tag3'); DB::collection('users')->where('_id', $id)->pull('tags', 'tag3');
...@@ -258,10 +262,16 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -258,10 +262,16 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(3, count($user['tags'])); $this->assertEquals(3, count($user['tags']));
$this->assertEquals('tag4', $user['tags'][2]); $this->assertEquals('tag4', $user['tags'][2]);
DB::collection('users')->where('_id', $id)->pull('messages', $message); DB::collection('users')->where('_id', $id)->pull('messages', $message1);
$user = DB::collection('users')->find($id); $user = DB::collection('users')->find($id);
$this->assertTrue(is_array($user['messages'])); $this->assertTrue(is_array($user['messages']));
$this->assertEquals(1, count($user['messages']));
// Raw
DB::collection('users')->where('_id', $id)->pull(array('tags' => 'tag2', 'messages' => $message2));
$user = DB::collection('users')->find($id);
$this->assertEquals(2, count($user['tags']));
$this->assertEquals(0, count($user['messages'])); $this->assertEquals(0, count($user['messages']));
} }
......
<?php <?php
class QueryTest extends PHPUnit_Framework_TestCase { class QueryTest extends TestCase {
protected static $started = false; protected static $started = false;
public function setUp() public function setUp()
{ {
parent::setUp();
// only run this stuff once
if (self::$started) return; if (self::$started) return;
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin')); User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
...@@ -188,8 +191,14 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -188,8 +191,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(33, $users[2]->age); $this->assertEquals(33, $users[2]->age);
$users = User::groupBy('age')->skip(1)->take(2)->orderBy('age', 'desc')->get(); $users = User::groupBy('age')->skip(1)->take(2)->orderBy('age', 'desc')->get();
$this->assertEquals(2, count($users));
$this->assertEquals(35, $users[0]->age); $this->assertEquals(35, $users[0]->age);
$this->assertEquals(33, $users[1]->age); $this->assertEquals(33, $users[1]->age);
$this->assertNull($users[0]->name);
$users = User::select('name')->groupBy('age')->skip(1)->take(2)->orderBy('age', 'desc')->get();
$this->assertEquals(2, count($users));
$this->assertNotNull($users[0]->name);
} }
public function testCount() public function testCount()
......
<?php <?php
use Illuminate\Database\Eloquent\Collection; class RelationsTest extends TestCase {
class RelationsTest extends PHPUnit_Framework_TestCase {
public function setUp() {
}
public function tearDown() public function tearDown()
{ {
...@@ -148,8 +143,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -148,8 +143,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(array_key_exists('user_ids', $client->getAttributes())); $this->assertTrue(array_key_exists('user_ids', $client->getAttributes()));
$this->assertTrue(array_key_exists('client_ids', $user->getAttributes())); $this->assertTrue(array_key_exists('client_ids', $user->getAttributes()));
$users = $client->getRelation('users');
$clients = $user->getRelation('clients'); $clients = $user->getRelation('clients');
$users = $client->getRelation('users');
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users); $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients); $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients);
...@@ -280,15 +275,15 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -280,15 +275,15 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(1, $user->photos->count()); $this->assertEquals(1, $user->photos->count());
$this->assertEquals($photo->id, $user->photos->first()->id); $this->assertEquals($photo->id, $user->photos->first()->id);
$photo = Photo::create(array('url' => 'http://graph.facebook.com/john.doe/picture')); $photo = Photo::create(array('url' => 'http://graph.facebook.com/jane.doe/picture'));
$client->photos()->save($photo); $client->photo()->save($photo);
$this->assertEquals(1, $client->photos->count()); $this->assertNotNull($client->photo);
$this->assertEquals($photo->id, $client->photos->first()->id); $this->assertEquals($photo->id, $client->photo->id);
$client = Client::find($client->_id); $client = Client::find($client->_id);
$this->assertEquals(1, $client->photos->count()); $this->assertNotNull($client->photo);
$this->assertEquals($photo->id, $client->photos->first()->id); $this->assertEquals($photo->id, $client->photo->id);
$photo = Photo::first(); $photo = Photo::first();
$this->assertEquals($photo->imageable->name, $user->name); $this->assertEquals($photo->imageable->name, $user->name);
...@@ -342,6 +337,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -342,6 +337,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertInstanceOf('DateTime', $address->created_at); $this->assertInstanceOf('DateTime', $address->created_at);
$this->assertInstanceOf('DateTime', $address->updated_at); $this->assertInstanceOf('DateTime', $address->updated_at);
$this->assertInstanceOf('User', $address->user); $this->assertInstanceOf('User', $address->user);
$this->assertEmpty($address->relationsToArray()); // prevent infinite loop
$user = User::find($user->_id); $user = User::find($user->_id);
$user->addresses()->save(new Address(array('city' => 'Bruxelles'))); $user->addresses()->save(new Address(array('city' => 'Bruxelles')));
......
<?php <?php
use Illuminate\Support\Facades\DB; class SchemaTest extends TestCase {
use Illuminate\Support\Facades\Schema;
class SchemaTest extends PHPUnit_Framework_TestCase {
public function setUp() {}
public function tearDown() public function tearDown()
{ {
...@@ -19,6 +14,18 @@ class SchemaTest extends PHPUnit_Framework_TestCase { ...@@ -19,6 +14,18 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(Schema::hasTable('newcollection')); $this->assertTrue(Schema::hasTable('newcollection'));
} }
public function testCreateWithCallback()
{
$instance = $this;
Schema::create('newcollection', function($collection) use ($instance)
{
$instance->assertInstanceOf('Jenssegers\Mongodb\Schema\Blueprint', $collection);
});
$this->assertTrue(Schema::hasCollection('newcollection'));
}
public function testDrop() public function testDrop()
{ {
Schema::create('newcollection'); Schema::create('newcollection');
......
<?php <?php
use Illuminate\Support\Facades\DB; class SeederTest extends TestCase {
class SeederTest extends PHPUnit_Framework_TestCase {
public function setUp() {}
public function tearDown() public function tearDown()
{ {
......
<?php
class TestCase extends Orchestra\Testbench\TestCase {
/**
* Get package providers.
*
* @return array
*/
protected function getPackageProviders()
{
return array('Jenssegers\Mongodb\MongodbServiceProvider');
}
/**
* Define environment setup.
*
* @param Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
// reset base path to point to our package's src directory
//$app['path.base'] = __DIR__ . '/../src';
// load custom config
$config = require 'config/database.php';
// set mongodb as default connection
$app['config']->set('database.default', 'mongodb');
// overwrite database configuration
$app['config']->set('database.connections.mysql', $config['connections']['mysql']);
$app['config']->set('database.connections.mongodb', $config['connections']['mongodb']);
}
}
<?php
class ValidationTest extends TestCase {
public function tearDown()
{
User::truncate();
}
public function testUnique()
{
$validator = Validator::make(
array('name' => 'John Doe'),
array('name' => 'required|unique:users')
);
$this->assertFalse($validator->fails());
User::create(array('name' => 'John Doe'));
$validator = Validator::make(
array('name' => 'John Doe'),
array('name' => 'required|unique:users')
);
$this->assertTrue($validator->fails());
}
}
<?php <?php
$loader = require 'vendor/autoload.php';
$loader->add('', 'tests/models');
$loader->add('', 'tests/seeds');
use Jenssegers\Mongodb\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Container\Container;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Events\Dispatcher;
use Illuminate\Cache\CacheManager;
# Fake app class
class App extends ArrayObject {
function bound() {}
}
# Fake app
$app = new App;
# Load database configuration $loader = require 'vendor/autoload.php';
$config = require 'config/database.php';
foreach ($config as $key => $value)
{
$app['config']["database.$key"] = $value;
}
# Event dispatcher
$app['events'] = new Dispatcher;
# Cache driver
$app['config']['cache.driver'] = 'array';
$app['cache'] = new CacheManager($app);
# Initialize database manager
$app['db.factory'] = new ConnectionFactory(new Container);
$app['db'] = new DatabaseManager($app, $app['db.factory']);
# Extend database manager with reflection hack // Could not figure out how to add this to the loader
$reflection = new ReflectionClass('Jenssegers\Mongodb\Connection'); require 'TestCase.php';
$app['db']->extend('mongodb', array($reflection, 'newInstance'));
# Static setup // Add stuff to autoload
\Jenssegers\Mongodb\Model::setConnectionResolver($app['db']); $loader->add('', 'tests/models');
\Jenssegers\Eloquent\Model::setConnectionResolver($app['db']); $loader->add('', 'tests/seeds');
DB::setFacadeApplication($app);
...@@ -2,10 +2,8 @@ ...@@ -2,10 +2,8 @@
return array( return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mongodb',
'connections' => array( 'connections' => array(
'mongodb' => array( 'mongodb' => array(
'name' => 'mongodb', 'name' => 'mongodb',
'driver' => 'mongodb', 'driver' => 'mongodb',
......
...@@ -12,8 +12,8 @@ class Client extends Eloquent { ...@@ -12,8 +12,8 @@ class Client extends Eloquent {
return $this->belongsToMany('User'); return $this->belongsToMany('User');
} }
public function photos() public function photo()
{ {
return $this->morphMany('Photo', 'imageable'); return $this->morphOne('Photo', 'imageable');
} }
} }
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