From 941ff99373d155a98211c63019ede62d1e735a93 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 10 Aug 2014 09:57:30 +0200 Subject: [PATCH] I think this covers everything. --- app/models/RecurringTransaction.php | 2 +- app/models/TransactionType.php | 13 +- app/models/User.php | 5 - app/tests/models/ModelTest.php | 228 +++++++++++++++++++++++++++- 4 files changed, 232 insertions(+), 16 deletions(-) diff --git a/app/models/RecurringTransaction.php b/app/models/RecurringTransaction.php index 9dafdccac3..024b664aea 100644 --- a/app/models/RecurringTransaction.php +++ b/app/models/RecurringTransaction.php @@ -96,7 +96,7 @@ class RecurringTransaction extends Ardent $start->addMonths($skip * 6); break; case 'yearly': - $this->addYears($skip); + $start->addYears($skip); break; } diff --git a/app/models/TransactionType.php b/app/models/TransactionType.php index 95893dd6d0..f6e3e2f881 100644 --- a/app/models/TransactionType.php +++ b/app/models/TransactionType.php @@ -1,5 +1,6 @@ hasMany('TransactionJournal'); - } - public static $factory = [ 'type' => 'string' ]; + public function transactionJournals() + { + return $this->hasMany('TransactionJournal'); + } + } \ No newline at end of file diff --git a/app/models/User.php b/app/models/User.php index 020484d3c4..07b8b6b1e8 100644 --- a/app/models/User.php +++ b/app/models/User.php @@ -91,11 +91,6 @@ class User extends Ardent implements UserInterface, RemindableInterface return $this->hasMany('Component'); } - public function piggybanks() - { - return $this->hasMany('Piggybank'); - } - public function preferences() { return $this->hasMany('Preference'); diff --git a/app/tests/models/ModelTest.php b/app/tests/models/ModelTest.php index 3e24d91e35..31170fbc2b 100644 --- a/app/tests/models/ModelTest.php +++ b/app/tests/models/ModelTest.php @@ -97,20 +97,240 @@ class ModelTest extends TestCase $rep->save(); $limit->save(); - $this->assertEquals($rep->limit_id,$limit->id); + $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) { + $list = ['daily', 'weekly', 'monthly', 'quarterly', 'half-year', 'yearly']; + foreach ($list as $entry) { $limit->repeat_freq = $entry; $limit->createRepetition($start); } - // try and fail: + } + /** + * @expectedException \Firefly\Exception\FireflyException + */ + public function testLimitrepetition() + { + $limit = f::create('Limit'); + $rep = f::create('LimitRepetition'); + $budget = f::create('Budget'); + $journal = f::create('TransactionJournal'); + $one = f::create('Transaction'); + $two = f::create('Transaction'); + $one->amount = 300; + $two->amount = -300; + + $rep->limit()->associate($limit); + $limit->budget()->associate($budget); + $journal->transactions()->save($one); + $journal->transactions()->save($two); + $journal->budgets()->save($budget); + + $this->assertEquals(($rep->amount - 300), $rep->left()); + + // repeat frequency (not present) for periodOrder + $testDate = new Carbon; + $testDate->startOfMonth(); + $rep->repeat_freq = null; + $this->assertEquals($testDate->format('Ymd') . '-3', $rep->periodOrder()); + + // repeat frequency (present) for periodOrder + $list = ['yearly', 'half-year', 'quarterly', 'monthly', 'weekly', 'daily']; + foreach ($list as $index => $entry) { + $rep->repeat_freq = $entry; + $this->assertEquals($testDate->format('Ymd') . '-' . $index, $rep->periodOrder()); + } + + // repeat freq (invalid) for periodOrder + $rep->repeat_freq = 'bad'; + $rep->periodOrder(); } + /** + * @expectedException \Firefly\Exception\FireflyException + */ + public function testLimitrepetitionContinued() + { + $limit = f::create('Limit'); + $rep = f::create('LimitRepetition'); + $budget = f::create('Budget'); + $journal = f::create('TransactionJournal'); + $one = f::create('Transaction'); + $two = f::create('Transaction'); + $one->amount = 300; + $two->amount = -300; + + $rep->limit()->associate($limit); + $limit->budget()->associate($budget); + $journal->transactions()->save($one); + $journal->transactions()->save($two); + $journal->budgets()->save($budget); + + // repeat frequency (not present) for periodShow + $testDate = new Carbon; + $testDate->startOfMonth(); + $rep->repeat_freq = null; + $this->assertEquals($testDate->format('F Y'), $rep->periodShow()); + + // repeat frequency (present) for periodOrder + $list = ['yearly', 'half-year', 'quarterly', 'monthly', 'weekly', 'daily']; + foreach ($list as $index => $entry) { + $rep->repeat_freq = $entry; + $this->assertGreaterThan(0, strlen($rep->periodShow())); + } + + // repeat freq (invalid) for periodOrder + $rep->repeat_freq = 'bad'; + $rep->periodShow(); + } + + public function testPiggybank() + { + $piggy = f::create('Piggybank'); + $account = f::create('Account'); + $piggy->account()->associate($account); + + $this->assertEquals($account->id, $piggy->account_id); + } + + public function testPreference() + { + $pref = f::create('Preference'); + $user = f::create('User'); + $pref->user()->associate($user); + $this->assertEquals($pref->user_id, $user->id); + $pref->data = 'Hello'; + $this->assertEquals($pref->data, 'Hello'); + } + + public function testRecurringtransaction() + { + $rec = f::create('RecurringTransaction'); + $user = f::create('User'); + $rec->user()->associate($user); + $this->assertEquals($rec->user_id, $user->id); + + $list = ['yearly', 'half-year', 'quarterly', 'monthly', 'weekly', 'daily']; + foreach ($list as $index => $entry) { + $start = clone $rec->date; + $rec->repeat_freq = $entry; + $end = $rec->next(); + $this->assertTrue($end > $start); + } + } + + public function testTransaction() + { + $transaction = f::create('Transaction'); + $journal = f::create('TransactionJournal'); + $component = f::create('Component'); + $budget = f::create('Budget'); + $category = f::create('Category'); + $account = f::create('Account'); + + $transaction->transactionJournal()->associate($journal); + $this->assertEquals($transaction->transaction_journal_id, $journal->id); + $transaction->components()->save($component); + $this->assertEquals($transaction->components()->first()->id, $component->id); + $transaction->budgets()->save($budget); + $this->assertEquals($transaction->budgets()->first()->id, $budget->id); + $transaction->categories()->save($category); + $this->assertEquals($transaction->categories()->first()->id, $category->id); + $transaction->account()->associate($account); + $this->assertEquals($transaction->account_id, $account->id); + } + + public function testTransactionCurrency() + { + $cur = f::create('TransactionCurrency'); + $journal = f::create('TransactionJournal'); + $cur->transactionjournals()->save($journal); + $journal->save(); + $cur->save(); + $this->assertEquals($cur->id, $journal->transaction_currency_id); + } + + public function testTransactionJournal() + { + $journal = f::create('TransactionJournal'); + $type = f::create('TransactionType'); + $user = f::create('User'); + $cur = f::create('TransactionCurrency'); + $transaction = f::create('Transaction'); + $component = f::create('Component'); + $budget = f::create('Budget'); + $category = f::create('Category'); + + $journal->transactionType()->associate($type); + $this->assertEquals($type->id, $journal->transaction_type_id); + + $journal->user()->associate($user); + $this->assertEquals($user->id, $journal->user_id); + + $journal->transactionCurrency()->associate($cur); + $this->assertEquals($cur->id, $journal->transaction_currency_id); + + $journal->transactions()->save($transaction); + $this->assertEquals($journal->transactions()->first()->id, $transaction->id); + + $journal->components()->save($component); + $this->assertEquals($journal->components()->first()->id, $component->id); + + $journal->budgets()->save($budget); + $this->assertEquals($journal->budgets()->first()->id, $budget->id); + + $journal->categories()->save($category); + $this->assertEquals($journal->categories()->first()->id, $category->id); + } + + public function testTransactionType() + { + $journal = f::create('TransactionJournal'); + $type = f::create('TransactionType'); + + $type->transactionjournals()->save($journal); + $this->assertEquals($type->transactionJournals()->first()->id, $journal->id); + + } + + public function testUser() + { + $user = f::create('User'); + + $account = f::create('Account'); + $bud = f::create('Budget'); + $cat = f::create('Category'); + $comp = f::create('Component'); + $pref = f::create('Preference'); + $rec = f::create('RecurringTransaction'); + $journal = f::create('TransactionJournal'); + + $user->accounts()->save($account); + $this->assertEquals($account->id,$user->accounts()->first()->id); + + $user->components()->save($comp); + $this->assertEquals($comp->id,$user->components()->first()->id); + + $user->budgets()->save($bud); + $this->assertEquals($bud->id,$user->budgets()->first()->id); + + $user->categories()->save($cat); + $this->assertEquals($cat->id,$user->categories()->first()->id); + + $user->preferences()->save($pref); + $this->assertEquals($pref->id,$user->preferences()->first()->id); + + $user->recurringtransactions()->save($rec); + $this->assertEquals($rec->id,$user->recurringtransactions()->first()->id); + + $user->transactionjournals()->save($journal); + $this->assertEquals($journal->id,$user->transactionjournals()->first()->id); + } + + } \ No newline at end of file