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

Merge master

parents e51ea71e 11f0f83f
......@@ -16,8 +16,7 @@
"illuminate/events": "4.2.x"
},
"require-dev": {
"illuminate/cache": "4.2.x",
"illuminate/auth": "4.2.x",
"orchestra/testbench": "dev-master",
"mockery/mockery": "*"
},
"autoload": {
......
......@@ -39,5 +39,8 @@
<directory>tests/RelationsTest.php</directory>
<directory>tests/MysqlRelationsTest.php</directory>
</testsuite>
<testsuite name="validation">
<directory>tests/ValidationTest.php</directory>
</testsuite>
</testsuites>
</phpunit>
......@@ -10,16 +10,16 @@ class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseRemi
*/
protected function reminderExpired($reminder)
{
// Convert to object so that we can pass it to the parent method
if (is_array($reminder))
// Convert to array so that we can pass it to the parent method
if (is_object($reminder))
{
$reminder = (object) $reminder;
$reminder = (array) $reminder;
}
// 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);
......
......@@ -487,6 +487,7 @@ class EmbedsMany extends Relation {
// Attatch the parent relation to the embedded model.
$model->setRelation($this->foreignKey, $this->parent);
$model->setHidden(array_merge($model->getHidden(), array($this->foreignKey)));
$models[] = $model;
}
......
<?php
use Illuminate\Support\Facades\DB;
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'));
}
class CacheTest extends TestCase {
public function tearDown()
{
User::truncate();
$this->cache->forget('db.users');
Cache::forget('db.users');
}
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();
$this->assertEquals(3, count($users));
......@@ -33,7 +23,7 @@ class CacheTest extends PHPUnit_Framework_TestCase {
$users = User::where('age', '>', 10)->remember(10, 'db.users')->get();
$this->assertEquals(3, count($users));
$users = $this->cache->get('db.users');
$users = Cache::get('db.users');
$this->assertEquals(3, count($users));
}
......
<?php
use Illuminate\Support\Facades\DB;
use Jenssegers\Mongodb\Connection;
class ConnectionTest extends PHPUnit_Framework_TestCase {
public function setUp() {}
public function tearDown() {}
class ConnectionTest extends TestCase {
public function testConnection()
{
......@@ -15,11 +9,11 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$c1 = 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');
$c2 = DB::reconnect('mongodb');
$this->assertNotEquals($c1, $c2);
$this->assertNotEquals(spl_object_hash($c1), spl_object_hash($c2));
}
public function testDb()
......
<?php
class ModelTest extends PHPUnit_Framework_TestCase {
public function setUp() {}
class ModelTest extends TestCase {
public function tearDown()
{
......
<?php
class MysqlRelationsTest extends PHPUnit_Framework_TestCase {
class MysqlRelationsTest extends TestCase {
public function setUp()
{
parent::setUp();
MysqlUser::executeSchema();
MysqlBook::executeSchema();
MysqlRole::executeSchema();
......
<?php
use Illuminate\Support\Facades\DB;
class QueryBuilderTest extends PHPUnit_Framework_TestCase {
public function setUp() {}
class QueryBuilderTest extends TestCase {
public function tearDown()
{
......@@ -238,17 +234,25 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
DB::collection('users')->where('_id', $id)->push('messages', $message);
$user = DB::collection('users')->find($id);
$this->assertTrue(is_array($user['messages']));
$this->assertEquals(1, count($user['messages']));
$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()
{
$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(
'name' => 'John Doe',
'tags' => array('tag1', 'tag2', 'tag3', 'tag4'),
'messages' => array($message)
'messages' => array($message1, $message2)
));
DB::collection('users')->where('_id', $id)->pull('tags', 'tag3');
......@@ -258,10 +262,16 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(3, count($user['tags']));
$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);
$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']));
}
......
<?php
class QueryTest extends PHPUnit_Framework_TestCase {
class QueryTest extends TestCase {
protected static $started = false;
public function setUp()
{
parent::setUp();
// only run this stuff once
if (self::$started) return;
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
......@@ -188,8 +191,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(33, $users[2]->age);
$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(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()
......
<?php
use Illuminate\Database\Eloquent\Collection;
class RelationsTest extends PHPUnit_Framework_TestCase {
public function setUp() {
}
class RelationsTest extends TestCase {
public function tearDown()
{
......@@ -148,8 +143,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(array_key_exists('user_ids', $client->getAttributes()));
$this->assertTrue(array_key_exists('client_ids', $user->getAttributes()));
$users = $client->getRelation('users');
$clients = $user->getRelation('clients');
$users = $client->getRelation('users');
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients);
......@@ -280,15 +275,15 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(1, $user->photos->count());
$this->assertEquals($photo->id, $user->photos->first()->id);
$photo = Photo::create(array('url' => 'http://graph.facebook.com/john.doe/picture'));
$client->photos()->save($photo);
$photo = Photo::create(array('url' => 'http://graph.facebook.com/jane.doe/picture'));
$client->photo()->save($photo);
$this->assertEquals(1, $client->photos->count());
$this->assertEquals($photo->id, $client->photos->first()->id);
$this->assertNotNull($client->photo);
$this->assertEquals($photo->id, $client->photo->id);
$client = Client::find($client->_id);
$this->assertEquals(1, $client->photos->count());
$this->assertEquals($photo->id, $client->photos->first()->id);
$this->assertNotNull($client->photo);
$this->assertEquals($photo->id, $client->photo->id);
$photo = Photo::first();
$this->assertEquals($photo->imageable->name, $user->name);
......@@ -342,6 +337,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertInstanceOf('DateTime', $address->created_at);
$this->assertInstanceOf('DateTime', $address->updated_at);
$this->assertInstanceOf('User', $address->user);
$this->assertEmpty($address->relationsToArray()); // prevent infinite loop
$user = User::find($user->_id);
$user->addresses()->save(new Address(array('city' => 'Bruxelles')));
......
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class SchemaTest extends PHPUnit_Framework_TestCase {
public function setUp() {}
class SchemaTest extends TestCase {
public function tearDown()
{
......@@ -19,6 +14,18 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
$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()
{
Schema::create('newcollection');
......
<?php
use Illuminate\Support\Facades\DB;
class SeederTest extends PHPUnit_Framework_TestCase {
public function setUp() {}
class SeederTest extends TestCase {
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
$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
$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']);
$loader = require 'vendor/autoload.php';
# Extend database manager with reflection hack
$reflection = new ReflectionClass('Jenssegers\Mongodb\Connection');
$app['db']->extend('mongodb', array($reflection, 'newInstance'));
// Could not figure out how to add this to the loader
require 'TestCase.php';
# Static setup
\Jenssegers\Mongodb\Model::setConnectionResolver($app['db']);
\Jenssegers\Eloquent\Model::setConnectionResolver($app['db']);
DB::setFacadeApplication($app);
// Add stuff to autoload
$loader->add('', 'tests/models');
$loader->add('', 'tests/seeds');
......@@ -2,10 +2,8 @@
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mongodb',
'connections' => array(
'mongodb' => array(
'name' => 'mongodb',
'driver' => 'mongodb',
......
......@@ -12,8 +12,8 @@ class Client extends Eloquent {
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