Skip to content

Commit

Permalink
Use migrations file in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Nov 22, 2015
1 parent 5beabf9 commit a11bf79
Showing 1 changed file with 26 additions and 60 deletions.
86 changes: 26 additions & 60 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require __DIR__.'/../migrations/create_bouncer_tables.php';

use Silber\Bouncer\Bouncer;
use Silber\Bouncer\CachedClipboard;
use Silber\Bouncer\Database\Models;
Expand All @@ -11,7 +13,6 @@
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;


abstract class BaseTestCase extends PHPUnit_Framework_TestCase
{
/**
Expand All @@ -37,58 +38,21 @@ public function setUp()
{
Models::setUsersModel(User::class);

$this->schema()->create('users', function ($table) {
$table->increments('id');
$table->timestamps();
});
$this->clipboard = new CachedClipboard(new ArrayStore);

$this->schema()->create('abilities', function ($table) {
$table->increments('id');
$table->string('name');
$table->integer('entity_id')->unsigned()->nullable();
$table->string('entity_type')->nullable();
$table->timestamps();
$this->migrate();
}

$table->unique(['name', 'entity_id', 'entity_type']);
});
protected function migrate()
{
$this->db();

(new CreateBouncerTables)->up();

$this->schema()->create('roles', function ($table) {
Schema::create('users', function ($table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});

$this->schema()->create('user_roles', function ($table) {
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();

$table->unique(['role_id', 'user_id']);

$table->foreign('role_id')->references('id')->on('roles');
$table->foreign('user_id')->references('id')->on('users');
});

$this->schema()->create('user_abilities', function ($table) {
$table->integer('ability_id')->unsigned();
$table->integer('user_id')->unsigned();

$table->unique(['ability_id', 'user_id']);

$table->foreign('ability_id')->references('id')->on('abilities');
$table->foreign('user_id')->references('id')->on('users');
});

$this->schema()->create('role_abilities', function ($table) {
$table->integer('ability_id')->unsigned();
$table->integer('role_id')->unsigned();

$table->unique(['ability_id', 'role_id']);

$table->foreign('ability_id')->references('id')->on('abilities');
$table->foreign('role_id')->references('id')->on('roles');
});

$this->clipboard = new CachedClipboard(new ArrayStore);
}

/**
Expand All @@ -98,12 +62,9 @@ public function setUp()
*/
public function tearDown()
{
$this->schema()->drop('role_abilities');
$this->schema()->drop('user_abilities');
$this->schema()->drop('user_roles');
$this->schema()->drop('roles');
$this->schema()->drop('abilities');
$this->schema()->drop('users');
Schema::drop('users');

(new CreateBouncerTables)->down();

$this->clipboard = $this->db = null;
}
Expand Down Expand Up @@ -137,15 +98,10 @@ protected function gate(User $user)
}

/**
* Get a schema builder instance.
* Get an instance of the database capsule manager.
*
* @return \Schema\Builder
* @return \Illuminate\Database\Capsule\Manager
*/
protected function schema()
{
return $this->db()->connection()->getSchemaBuilder();
}

protected function db()
{
if ($this->db) {
Expand Down Expand Up @@ -173,3 +129,13 @@ class User extends Eloquent

protected $table = 'users';
}

class Schema
{
public static function __callStatic($method, array $parameters)
{
$schema = DB::connection()->getSchemaBuilder();

return call_user_func_array([$schema, $method], $parameters);
}
}

0 comments on commit a11bf79

Please sign in to comment.