Added unicity to AccountType model. Fixed some tests because of this.

This commit is contained in:
James Cole 2014-08-31 10:03:24 +02:00
parent 58b3334f05
commit 2e342e47a7
4 changed files with 49 additions and 80 deletions

View File

@ -11,6 +11,16 @@ use Illuminate\Database\Schema\Blueprint;
class CreateAccountTypesTable extends Migration class CreateAccountTypesTable extends Migration
{ {
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('account_types');
}
/** /**
* Run the migrations. * Run the migrations.
* *
@ -24,18 +34,10 @@ class CreateAccountTypesTable extends Migration
$table->timestamps(); $table->timestamps();
$table->string('type', 50); $table->string('type', 50);
$table->boolean('editable'); $table->boolean('editable');
$table->unique('type');
} }
); );
} }
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('account_types');
}
} }

View File

@ -68,21 +68,21 @@ class AccountTest extends TestCase
// user should equal test user: // user should equal test user:
$this->assertEquals($user->id, $account->user()->first()->id); $this->assertEquals($user->id, $account->user()->first()->id);
// whatever the account type of this account, searching for it using the
// scope method should return one account:
$accountTypeType = $account->accounttype->type;
$this->assertCount(1, \Account::AccountTypeIn([$accountTypeType])->get());
// lame test
$this->assertEquals('testing',\App::environment()); $this->assertEquals('testing',\App::environment());
// count the number of accounts the account type has. Should be one: \Log::debug('Hello from test!');
\Log::debug('Number of accounts: ' . \Account::count());
\Log::debug('Number of account types: ' . \AccountType::count());
foreach(\AccountType::get() as $t) {
\Log::debug('AccountType: #'.$t->id.', ' . $t->type);
}
// whatever the account type of this account, searching for it using the
// scope method should return one account:
$accountType = $account->accounttype()->first(); $accountType = $account->accounttype()->first();
$this->assertCount(1,$accountType->accounts()->get()); $accounts = $accountType->accounts()->count();
$this->assertCount($accounts, \Account::AccountTypeIn([$accountType->type])->get());
} }
@ -111,9 +111,7 @@ class AccountTest extends TestCase
$account = f::create('Account'); $account = f::create('Account');
/** @var \AccountType $accountType */ /** @var \AccountType $accountType */
$accountType = f::create('AccountType'); $accountType = \AccountType::whereType('Default account')->first();
$accountType->type = 'Default account';
$accountType->save();
$account->accountType()->associate($accountType); $account->accountType()->associate($accountType);
$account->save(); $account->save();
@ -142,9 +140,7 @@ class AccountTest extends TestCase
$account = f::create('Account'); $account = f::create('Account');
/** @var \AccountType $accountType */ /** @var \AccountType $accountType */
$accountType = f::create('AccountType'); $accountType = \AccountType::whereType('Default account')->first();
$accountType->type = 'Default account';
$accountType->save();
$account->accountType()->associate($accountType); $account->accountType()->associate($accountType);
$account->save(); $account->save();
@ -173,9 +169,7 @@ class AccountTest extends TestCase
$account = f::create('Account'); $account = f::create('Account');
/** @var \AccountType $accountType */ /** @var \AccountType $accountType */
$accountType = f::create('AccountType'); $accountType = \AccountType::whereType('Default account')->first();
$accountType->type = 'Default account';
$accountType->save();
$account->accountType()->associate($accountType); $account->accountType()->associate($accountType);
$account->save(); $account->save();
@ -204,12 +198,8 @@ class AccountTest extends TestCase
public function testIndex() public function testIndex()
{ {
// two account types: // two account types:
$personalType = f::create('AccountType'); $personalType = \AccountType::whereType('Default account')->first();
$personalType->type = 'Default account'; $benType = \AccountType::whereType('Beneficiary account')->first();
$personalType->save();
$benType = f::create('AccountType');
$benType->type = 'Beneficiary account';
$benType->save();
// create two accounts: // create two accounts:
/** @var \Account $account */ /** @var \Account $account */
@ -251,9 +241,7 @@ class AccountTest extends TestCase
$account = f::create('Account'); $account = f::create('Account');
/** @var \AccountType $accountType */ /** @var \AccountType $accountType */
$accountType = f::create('AccountType'); $accountType = \AccountType::whereType('Default account')->first();
$accountType->type = 'Default account';
$accountType->save();
$account->accountType()->associate($accountType); $account->accountType()->associate($accountType);
$account->save(); $account->save();
@ -285,9 +273,7 @@ class AccountTest extends TestCase
$account = f::create('Account'); $account = f::create('Account');
/** @var \AccountType $accountType */ /** @var \AccountType $accountType */
$accountType = f::create('AccountType'); $accountType = \AccountType::whereType('Default account')->first();
$accountType->type = 'Default account';
$accountType->save();
$account->accountType()->associate($accountType); $account->accountType()->associate($accountType);
$account->save(); $account->save();
@ -306,9 +292,7 @@ class AccountTest extends TestCase
$account = f::create('Account'); $account = f::create('Account');
/** @var \AccountType $accountType */ /** @var \AccountType $accountType */
$accountType = f::create('AccountType'); $accountType = \AccountType::whereType('Default account')->first();
$accountType->type = 'Default account';
$accountType->save();
$account->accountType()->associate($accountType); $account->accountType()->associate($accountType);
$account->save(); $account->save();
@ -325,9 +309,7 @@ class AccountTest extends TestCase
$account = f::create('Account'); $account = f::create('Account');
/** @var \AccountType $accountType */ /** @var \AccountType $accountType */
$accountType = f::create('AccountType'); $accountType = \AccountType::whereType('Default account')->first();
$accountType->type = 'Default account';
$accountType->save();
$account->accountType()->associate($accountType); $account->accountType()->associate($accountType);
$account->save(); $account->save();
@ -343,9 +325,7 @@ class AccountTest extends TestCase
$account = f::create('Account'); $account = f::create('Account');
/** @var \AccountType $accountType */ /** @var \AccountType $accountType */
$accountType = f::create('AccountType'); $accountType = \AccountType::whereType('Default account')->first();
$accountType->type = 'Default account';
$accountType->save();
$account->accountType()->associate($accountType); $account->accountType()->associate($accountType);
$account->save(); $account->save();
@ -370,9 +350,7 @@ class AccountTest extends TestCase
$account = f::create('Account'); $account = f::create('Account');
/** @var \AccountType $accountType */ /** @var \AccountType $accountType */
$accountType = f::create('AccountType'); $accountType = \AccountType::whereType('Default account')->first();
$accountType->type = 'Default account';
$accountType->save();
$account->accountType()->associate($accountType); $account->accountType()->associate($accountType);
$account->save(); $account->save();

View File

@ -77,10 +77,8 @@ class ChartControllerTest extends TestCase
public function testHomeAccountInfo() public function testHomeAccountInfo()
{ {
$account = f::create('Account'); $account = f::create('Account');
$type = f::create('AccountType'); $accountType = \AccountType::whereType('Default account')->first();
$type->type = 'Default account'; $account->accounttype()->associate($accountType);
$type->save();
$account->accounttype()->associate($type);
$account->save(); $account->save();
// for successful binding: // for successful binding:
Auth::shouldReceive('user')->andReturn($account->user()->first()); Auth::shouldReceive('user')->andReturn($account->user()->first());

View File

@ -4,16 +4,7 @@ use League\FactoryMuffin\Facade;
Facade::define( Facade::define(
'AccountType', 'AccountType',
[ [
'type' => function () { 'type' => 'unique:word',
$types = [
'Default account',
'Cash account',
'Initial balance account',
'Beneficiary account'
];
return $types[rand(0, 3)];
},
'editable' => 1 'editable' => 1
] ]
); );