Skip to content

Commit

Permalink
fix a bunch of boo boo tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 1, 2015
1 parent 112494c commit eb762a1
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 44 deletions.
93 changes: 57 additions & 36 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use Illuminate\Database\Connection;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Pagination\AbstractPaginator as Paginator;
use Illuminate\Database\Capsule\Manager as DB;

class DatabaseEloquentIntegrationTest extends PHPUnit_Framework_TestCase
{
Expand All @@ -16,38 +16,52 @@ class DatabaseEloquentIntegrationTest extends PHPUnit_Framework_TestCase
public function setUp()
{
$db = new DB;

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
], 'second_connection');

$db->bootEloquent();
$db->setAsGlobal();

$this->schema()->create('users', function ($table) {
$table->increments('id');
$table->string('email')->unique();
$table->timestamps();
});
$this->createSchema();
}

$this->schema()->create('friends', function ($table) {
$table->integer('user_id');
$table->integer('friend_id');
});
protected function createSchema()
{
foreach (['default', 'second_connection'] as $connection) {
$this->schema($connection)->create('users', function ($table) {
$table->increments('id');
$table->string('email');
$table->timestamps();
});

$this->schema()->create('posts', function ($table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('parent_id')->nullable();
$table->string('name');
$table->timestamps();
});
$this->schema($connection)->create('friends', function ($table) {
$table->integer('user_id');
$table->integer('friend_id');
});

$this->schema()->create('photos', function ($table) {
$table->increments('id');
$table->morphs('imageable');
$table->string('name');
$table->timestamps();
});
$this->schema($connection)->create('posts', function ($table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('parent_id')->nullable();
$table->string('name');
$table->timestamps();
});

$this->schema($connection)->create('photos', function ($table) {
$table->increments('id');
$table->morphs('imageable');
$table->string('name');
$table->timestamps();
});
}
}

/**
Expand All @@ -57,10 +71,12 @@ public function setUp()
*/
public function tearDown()
{
$this->schema()->drop('users');
$this->schema()->drop('friends');
$this->schema()->drop('posts');
$this->schema()->drop('photos');
foreach (['default', 'second_connection'] as $connection) {
$this->schema($connection)->drop('users');
$this->schema($connection)->drop('friends');
$this->schema($connection)->drop('posts');
$this->schema($connection)->drop('photos');
}

Relation::morphMap([], false);
}
Expand Down Expand Up @@ -227,15 +243,20 @@ public function testOneToManyRelationship()

public function testBasicModelHydration()
{
EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
EloquentTestUser::create(['email' => 'abigailotwell@gmail.com']);
$user = new EloquentTestUser(['email' => 'taylorotwell@gmail.com']);
$user->setConnection('second_connection');
$user->save();

$user = new EloquentTestUser(['email' => 'abigailotwell@gmail.com']);
$user->setConnection('second_connection');
$user->save();

$models = EloquentTestUser::hydrateRaw('SELECT * FROM users WHERE email = ?', ['abigailotwell@gmail.com'], 'foo_connection');
$models = EloquentTestUser::hydrateRaw('SELECT * FROM users WHERE email = ?', ['abigailotwell@gmail.com'], 'second_connection');

$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $models);
$this->assertInstanceOf('EloquentTestUser', $models[0]);
$this->assertEquals('abigailotwell@gmail.com', $models[0]->email);
$this->assertEquals('foo_connection', $models[0]->getConnectionName());
$this->assertEquals('second_connection', $models[0]->getConnectionName());
$this->assertEquals(1, $models->count());
}

Expand Down Expand Up @@ -405,7 +426,7 @@ public function testMorphMapOverwritesCurrentMap()

public function testEmptyMorphToRelationship()
{
$photo = EloquentTestPhoto::create(['name' => 'Avatar 1']);
$photo = new EloquentTestPhoto;

$this->assertNull($photo->imageable);
}
Expand Down Expand Up @@ -492,19 +513,19 @@ public function testToArrayIncludesCustomFormattedTimestamps()
*
* @return Connection
*/
protected function connection()
protected function connection($connection = 'default')
{
return Eloquent::getConnectionResolver()->connection();
return Eloquent::getConnectionResolver()->connection($connection);
}

/**
* Get a schema builder instance.
*
* @return Schema\Builder
*/
protected function schema()
protected function schema($connection = 'default')
{
return $this->connection()->getSchemaBuilder();
return $this->connection($connection)->getSchemaBuilder();
}
}

Expand Down
71 changes: 63 additions & 8 deletions tests/Database/DatabaseEloquentIntegrationWithTablePrefixTest.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,78 @@
<?php

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;

class DatabaseEloquentIntegrationWithTablePrefixTest extends DatabaseEloquentIntegrationTest
class DatabaseEloquentIntegrationWithTablePrefixTest
{
/**
* Bootstrap Eloquent.
*
* @return void
*/
public static function setUpBeforeClass()
public function setUp()
{
$resolver = new DatabaseIntegrationTestConnectionResolver;
$resolver->connection()->setTablePrefix('prefix_');
Eloquent::setConnectionResolver($resolver);
$db = new DB;

Eloquent::setEventDispatcher(
new Illuminate\Events\Dispatcher
);
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);

$db->bootEloquent();
$db->setAsGlobal();

Eloquent::getConnectionResolver()->connection()->setTablePrefix('prefix_');

$this->createSchema();
}

protected function createSchema()
{
foreach (['default'] as $connection) {
$this->schema($connection)->create('users', function ($table) {
$table->increments('id');
$table->string('email');
$table->timestamps();
});

$this->schema($connection)->create('friends', function ($table) {
$table->integer('user_id');
$table->integer('friend_id');
});

$this->schema($connection)->create('posts', function ($table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('parent_id')->nullable();
$table->string('name');
$table->timestamps();
});

$this->schema($connection)->create('photos', function ($table) {
$table->increments('id');
$table->morphs('imageable');
$table->string('name');
$table->timestamps();
});
}
}

/**
* Tear down the database schema.
*
* @return void
*/
public function tearDown()
{
foreach (['default'] as $connection) {
$this->schema($connection)->drop('users');
$this->schema($connection)->drop('friends');
$this->schema($connection)->drop('posts');
$this->schema($connection)->drop('photos');
}

Relation::morphMap([], false);
}

public function testBasicModelHydration()
Expand Down

0 comments on commit eb762a1

Please sign in to comment.