Model tests.

This commit is contained in:
James Cole 2014-08-09 19:10:31 +02:00
parent 0967ff9c7f
commit 8eeadffdeb
3 changed files with 126 additions and 6 deletions

View File

@ -38,14 +38,9 @@ class Component extends Firefly\Database\SingleTableInheritanceEntity
protected $table = 'components';
protected $subclassField = 'class';
public function transactions()
{
return $this->belongsToMany('Transaction');
}
public function limits()
{
return $this->belongsTo('Limit');
return $this->hasMany('Limit');
}
public function transactionjournals()
@ -53,6 +48,11 @@ class Component extends Firefly\Database\SingleTableInheritanceEntity
return $this->belongsToMany('TransactionJournal');
}
public function transactions()
{
return $this->belongsToMany('Transaction');
}
public function user()
{
return $this->belongsTo('User');

View File

@ -105,11 +105,14 @@ class Limit extends Ardent
try {
$repetition->save();
\Log::debug('Created new repetition with id #' . $repetition->id);
// @codeCoverageIgnoreStart
} catch (QueryException $e) {
// do nothing
\Log::error('Trying to save new Limitrepetition failed!');
\Log::error($e->getMessage());
}
// @codeCoverageIgnoreEnd
}
}
@ -118,6 +121,7 @@ class Limit extends Ardent
return $this->hasMany('LimitRepetition');
}
public function getDates()
{
return ['created_at', 'updated_at', 'startdate', 'enddate'];

View File

@ -0,0 +1,116 @@
<?php
use Carbon\Carbon;
use Zizaco\FactoryMuff\Facade\FactoryMuff as f;
class ModelTest extends TestCase
{
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
Artisan::call('db:seed');
}
public function tearDown()
{
Mockery::close();
}
public function testAccount()
{
$account = f::create('Account');
$user = f::create('User');
$type = f::create('AccountType');
$account->user()->associate($user);
$account->accounttype()->associate($type);
$this->assertEquals($account->predict(new Carbon), null);
$this->assertEquals($account->balance(new Carbon), null);
$this->assertEquals($account->user_id, $user->id);
$this->assertEquals($account->account_type_id, $type->id);
}
public function testAccountType()
{
$account = f::create('Account');
$type = f::create('AccountType');
$type->accounts()->save($account);
$this->assertEquals($account->account_type_id, $type->id);
}
public function testBudget()
{
$budget = f::create('Budget');
$limit = f::create('Limit');
$journal = f::create('TransactionJournal');
$budget->limits()->save($limit);
$budget->transactionjournals()->save($journal);
$this->assertEquals($limit->component_id, $budget->id);
$this->assertEquals($journal->budgets()->first()->id, $budget->id);
}
public function testCategory()
{
$category = f::create('Category');
$journal = f::create('TransactionJournal');
$category->transactionjournals()->save($journal);
$this->assertEquals($journal->categories()->first()->id, $category->id);
}
public function testComponent()
{
$component = f::create('Component');
$limit = f::create('Limit');
$component->limits()->save($limit);
$component->save();
$transaction = f::create('Transaction');
$journal = f::create('TransactionJournal');
$user = f::create('User');
$component->transactions()->save($transaction);
$component->transactionjournals()->save($journal);
$component->user()->associate($user);
$this->assertEquals($transaction->components()->first()->id, $component->id);
$this->assertEquals($journal->components()->first()->id, $component->id);
$this->assertEquals($limit->component()->first()->id, $component->id);
$this->assertEquals($component->user_id, $user->id);
}
public function testLimit()
{
$limit = f::create('Limit');
$budget = f::create('Budget');
$rep = f::create('LimitRepetition');
$limit->budget()->associate($budget);
$limit->limitrepetitions()->save($rep);
$rep->save();
$limit->save();
$this->assertEquals($rep->limit_id,$limit->id);
$this->assertEquals($limit->component_id, $budget->id);
// create repetition:
$start = new Carbon;
$list = ['daily','weekly','monthly','quarterly','half-year','yearly'];
foreach($list as $entry) {
$limit->repeat_freq = $entry;
$limit->createRepetition($start);
}
// try and fail:
}
}