From 6c71f68ed85d29b6aff10bfccce6bed2d7634f82 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 24 May 2015 11:13:46 +0200 Subject: [PATCH] Should cover models. --- app/Models/Account.php | 7 - app/Models/Category.php | 5 - app/Models/Tag.php | 6 - app/Models/TransactionJournal.php | 32 +- tests/models/AccountModelTest.php | 90 ++++- tests/models/CategoryModelTest.php | 32 +- tests/models/TagModelTest.php | 33 +- tests/models/TransactionJournalModelTes.php | 78 ---- tests/models/TransactionJournalModelTest.php | 388 +++++++++++++++++++ 9 files changed, 555 insertions(+), 116 deletions(-) delete mode 100644 tests/models/TransactionJournalModelTes.php create mode 100644 tests/models/TransactionJournalModelTest.php diff --git a/app/Models/Account.php b/app/Models/Account.php index 6d1a51f108..a3fe6796c9 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -1,6 +1,5 @@ id)) { - // could not create account: - App::abort(500, 'Could not create new account with data: ' . json_encode($fields) . ' because ' . json_encode($account->getErrors())); - - - } return $account; diff --git a/app/Models/Category.php b/app/Models/Category.php index 9a8245452e..2cf3aa2fda 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -42,11 +42,6 @@ class Category extends Model } // create it! $category = Category::create($fields); - if (is_null($category->id)) { - // could not create account: - App::abort(500, 'Could not create new category with data: ' . json_encode($fields)); - - } return $category; diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 68955aa201..719641de02 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -57,12 +57,6 @@ class Tag extends Model $fields['tagMode'] = 'nothing'; $fields['description'] = isset($fields['description']) && !is_null($fields['description']) ? $fields['description'] : ''; $tag = Tag::create($fields); - if (is_null($tag->id)) { - // could not create account: - App::abort(500, 'Could not create new tag with data: ' . json_encode($fields) . ' because ' . json_encode($tag->getErrors())); - - - } return $tag; diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 09b3d5d86d..c1c21adbbc 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -59,15 +59,15 @@ class TransactionJournal extends Model } /** - * @return float + * @return string */ public function getActualAmountAttribute() { - $amount = 0; + $amount = '0'; /** @var Transaction $t */ foreach ($this->transactions as $t) { if ($t->amount > 0) { - $amount = floatval($t->amount); + $amount = $t->amount; } } @@ -79,11 +79,12 @@ class TransactionJournal extends Model */ public function getAmountAttribute() { - $amount = 0; + $amount = '0'; + bcscale(2); /** @var Transaction $t */ foreach ($this->transactions as $t) { if ($t->amount > 0) { - $amount = floatval($t->amount); + $amount = $t->amount; } } @@ -93,16 +94,16 @@ class TransactionJournal extends Model if ($this->tags->count() == 0) { return $amount; } + // if journal is part of advancePayment AND journal is a withdrawal, // then journal is being repaid by other journals, so the actual amount will lower: /** @var Tag $advancePayment */ $advancePayment = $this->tags()->where('tagMode', 'advancePayment')->first(); if ($advancePayment && $this->transactionType->type == 'Withdrawal') { - // loop other deposits, remove from our amount. $others = $advancePayment->transactionJournals()->transactionTypes(['Deposit'])->get(); foreach ($others as $other) { - $amount -= $other->actualAmount; + $amount = bcsub($amount, $other->actualAmount); } return $amount; @@ -111,25 +112,24 @@ class TransactionJournal extends Model // if this journal is part of an advancePayment AND the journal is a deposit, // then the journal amount is correcting a withdrawal, and the amount is zero: if ($advancePayment && $this->transactionType->type == 'Deposit') { - return 0; + return '0'; } // is balancing act? $balancingAct = $this->tags()->where('tagMode', 'balancingAct')->first(); - if ($balancingAct) { - // this is the transfer + if ($balancingAct) { // this is the expense: if ($this->transactionType->type == 'Withdrawal') { $transfer = $balancingAct->transactionJournals()->transactionTypes(['Transfer'])->first(); if ($transfer) { - $amount -= $transfer->actualAmount; + $amount = bcsub($amount, $transfer->actualAmount); return $amount; } - } - } + } // @codeCoverageIgnore + } // @codeCoverageIgnore return $amount; } @@ -180,16 +180,16 @@ class TransactionJournal extends Model */ public function getCorrectedActualAmountAttribute() { - $amount = 0; + $amount = '0'; $type = $this->transactionType->type; /** @var Transaction $t */ foreach ($this->transactions as $t) { if ($t->amount > 0 && $type != 'Withdrawal') { - $amount = floatval($t->amount); + $amount = $t->amount; break; } if ($t->amount < 0 && $type == 'Withdrawal') { - $amount = floatval($t->amount); + $amount = $t->amount; break; } } diff --git a/tests/models/AccountModelTest.php b/tests/models/AccountModelTest.php index f42ca9b720..ecb78a2490 100644 --- a/tests/models/AccountModelTest.php +++ b/tests/models/AccountModelTest.php @@ -1,5 +1,8 @@ markTestIncomplete(); + // create account: + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + + // search for account with the same properties: + $search = [ + 'name' => $account->name, + 'account_type_id' => $account->account_type_id, + 'user_id' => $account->user_id + ]; + + $result = Account::firstOrCreateEncrypted($search); + + // should be the same account: + + $this->assertEquals($account->id, $result->id); + + } + + /** + * @covers FireflyIII\Models\Account::firstOrCreateEncrypted + */ + public function testFirstOrCreateEncryptedNew() + { + // create account: + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $user = FactoryMuffin::create('FireflyIII\User'); + + // search for account with the same properties: + $search = [ + 'name' => 'Some new account', + 'account_type_id' => $account->account_type_id, + 'user_id' => $account->user_id, + 'active' => 1, + ]; + + $result = Account::firstOrCreateEncrypted($search); + + // should not be the same account: + + $this->assertNotEquals($account->id, $result->id); + + } /** @@ -48,7 +93,48 @@ class AccountModelTest extends TestCase */ public function testFirstOrNullEncrypted() { - $this->markTestIncomplete(); + // create account: + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + + // search for account with the same properties: + $search = [ + 'name' => $account->name, + 'account_type_id' => $account->account_type_id, + 'user_id' => $account->user_id + ]; + + $result = Account::firstOrNullEncrypted($search); + + // should be the same account: + + $this->assertEquals($account->id, $result->id); + } + + /** + * @covers FireflyIII\Models\Account::firstOrNullEncrypted + */ + public function testFirstOrNullEncryptedNew() + { + // create account: + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $user = FactoryMuffin::create('FireflyIII\User'); + + // search for account with the same properties: + $search = [ + 'name' => 'Some new account', + 'account_type_id' => $account->account_type_id, + 'user_id' => $account->user_id, + 'active' => 1, + ]; + + $result = Account::firstOrNullEncrypted($search); + + // should not be the same account: + + $this->assertNull($result); + + } } \ No newline at end of file diff --git a/tests/models/CategoryModelTest.php b/tests/models/CategoryModelTest.php index 8b8709038b..59f5eeb2c8 100644 --- a/tests/models/CategoryModelTest.php +++ b/tests/models/CategoryModelTest.php @@ -1,5 +1,9 @@ markTestIncomplete(); + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + + $search = [ + 'name' => $category->name, + 'user_id' => $category->user_id + ]; + + $result = Category::firstOrCreateEncrypted($search); + + $this->assertEquals($result->id, $category->id); + } + + /** + * @covers FireflyIII\Models\Category::firstOrCreateEncrypted + */ + public function testFirstOrCreateEncryptedNew() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + + $search = [ + 'name' => 'Some category name', + 'user_id' => $category->user_id + ]; + + $result = Category::firstOrCreateEncrypted($search); + + $this->assertNotEquals($result->id, $category->id); } } \ No newline at end of file diff --git a/tests/models/TagModelTest.php b/tests/models/TagModelTest.php index 54e4b5f0ac..6391e31142 100644 --- a/tests/models/TagModelTest.php +++ b/tests/models/TagModelTest.php @@ -1,5 +1,8 @@ 'something', + 'tag' => 'Something else', + 'user_id' => $tag->user_id, + ]; + + $result = Tag::firstOrCreateEncrypted($search); + + $this->assertNotEquals($tag->id, $result->id); + } + /** * @covers FireflyIII\Models\Tag::firstOrCreateEncrypted */ public function testFirstOrCreateEncrypted() { - $this->markTestIncomplete(); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $search = [ + 'tagMode' => 'something', + 'tag' => $tag->tag, + 'user_id' => $tag->user_id, + ]; + + $result = Tag::firstOrCreateEncrypted($search); + + $this->assertEquals($tag->id, $result->id); } } \ No newline at end of file diff --git a/tests/models/TransactionJournalModelTes.php b/tests/models/TransactionJournalModelTes.php deleted file mode 100644 index b31cef4329..0000000000 --- a/tests/models/TransactionJournalModelTes.php +++ /dev/null @@ -1,78 +0,0 @@ -markTestIncomplete(); - } - - /** - * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute - */ - public function testGetAmountAttribute() - { - $this->markTestIncomplete(); - } - - /** - * @covers FireflyIII\Models\TransactionJournal::getAssetAccountAttribute - */ - public function testGetAssetAccountAttribute() - { - $this->markTestIncomplete(); - } - - /** - * @covers FireflyIII\Models\TransactionJournal::getCorrectedActualAmountAttribute - */ - public function testGetCorrectedActualAmountAttribute() - { - $this->markTestIncomplete(); - } - - /** - * @covers FireflyIII\Models\TransactionJournal::getDestinationAccountAttribute - */ - public function testGetDestinationAccountAttribute() - { - $this->markTestIncomplete(); - } - -} \ No newline at end of file diff --git a/tests/models/TransactionJournalModelTest.php b/tests/models/TransactionJournalModelTest.php new file mode 100644 index 0000000000..20711f10de --- /dev/null +++ b/tests/models/TransactionJournalModelTest.php @@ -0,0 +1,388 @@ + $account->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 123.45 + ] + ); + $amount = $journal->actual_amount; + $this->assertEquals('123.45', $amount); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetAmountAttributeAdvancePayment() + { + // make types: + $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + // make tag + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'advancePayment'; + $tag->save(); + + // make withdrawal + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $withdrawal->transaction_type_id = $withdrawalType->id; + $withdrawal->save(); + + // make deposit + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $deposit->transaction_type_id = $depositType->id; + $deposit->save(); + + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + // for withdrawal, asset to expense account and reversed: //89,88 + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => -300]); + Transaction::create(['account_id' => $expense->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => 300]); + + Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -89.88]); + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => 89.88]); + + + // connect to tag: + $tag->transactionJournals()->save($withdrawal); + $tag->transactionJournals()->save($deposit); + + // amount should be 210.12: + $this->assertEquals('210.12', $withdrawal->amount); + $this->assertEquals('0', $deposit->amount); + + + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetAmountAttributeBalancingAct() + { + // make types: + $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $transferType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + // make a tag + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'balancingAct'; + $tag->save(); + + // make a withdrawal and a transfer + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $withdrawal->transaction_type_id = $withdrawalType->id; + $withdrawal->save(); + + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $transfer->transaction_type_id = $transferType->id; + $transfer->save(); + + // connect to tag: + $tag->transactionJournals()->save($withdrawal); + $tag->transactionJournals()->save($transfer); + + // make accounts: + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset2 = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset2->account_type_id = $asset->account_type_id; + $asset2->save(); + + // make transactions: + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => -123.45]); + Transaction::create(['account_id' => $expense->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => 123.45]); + + Transaction::create(['account_id' => $asset2->id, 'transaction_journal_id' => $transfer->id, 'amount' => -123.45]); + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $transfer->id, 'amount' => 123.45]); + + $amount = $withdrawal->amount; + + $this->assertEquals('0', $amount); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + */ + public function testGetAmountAttributeNoTags() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create( + [ + 'account_id' => $account->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 123.45 + ] + ); + $amount = $journal->amount; + $this->assertEquals('123.45', $amount); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + */ + public function testGetAmountAttributeTag() + { + // has a normal tag, but nothing special. + // make tag + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'nothing'; + $tag->save(); + + // make withdrawal + $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $withdrawal->transaction_type_id = $withdrawalType->id; + $withdrawal->save(); + + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => -300]); + Transaction::create(['account_id' => $expense->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => 300]); + + // connect to tag: + $tag->transactionJournals()->save($withdrawal); + + $this->assertEquals('300', $withdrawal->amount); + + + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getAssetAccountAttribute + */ + public function testGetAssetAccountAttributeDeposit() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + // make withdrawal + $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $deposit->transaction_type_id = $depositType->id; + $deposit->save(); + + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => 300]); + Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + + // get asset account: + $result = $deposit->asset_account; + + $this->assertEquals($asset->id, $result->id); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getAssetAccountAttribute + */ + public function testGetAssetAccountAttributeFallback() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + // make withdrawal + $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $deposit->transaction_type_id = $depositType->id; + $deposit->save(); + + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + + // get asset account: + $result = $deposit->asset_account; + + $this->assertEquals($asset->id, $result->id); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getAssetAccountAttribute + */ + public function testGetAssetAccountAttributeWithdrawal() + { + // make withdrawal + $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $withdrawal->transaction_type_id = $withdrawalType->id; + $withdrawal->save(); + + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => -300]); + Transaction::create(['account_id' => $expense->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => 300]); + + // get asset account: + $result = $withdrawal->asset_account; + + $this->assertEquals($asset->id, $result->id); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getCorrectedActualAmountAttribute + */ + public function testGetCorrectedActualAmountAttributeDeposit() + { + + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $deposit->transaction_type_id = $depositType->id; + $deposit->save(); + + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => 300]); + Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + + // get asset account: + $result = $deposit->corrected_actual_amount; + + $this->assertEquals('300', $result); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getCorrectedActualAmountAttribute + */ + public function testGetCorrectedActualAmountAttributeWithdrawal() + { + + // make withdrawal + $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $withdrawal->transaction_type_id = $withdrawalType->id; + $withdrawal->save(); + + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create(['account_id' => $expense->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => 300]); + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => -300]); + + // get asset account: + $result = $withdrawal->corrected_actual_amount; + + $this->assertEquals('-300', $result); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getDestinationAccountAttribute + */ + public function testGetDestinationAccountAttribute() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $deposit->transaction_type_id = $depositType->id; + $deposit->save(); + + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => 300]); + Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + + // get asset account: + $result = $deposit->destination_account; + + $this->assertEquals($asset->id, $result->id); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getDestinationAccountAttribute + */ + public function testGetDestinationAccountAttributeFallback() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $deposit->transaction_type_id = $depositType->id; + $deposit->save(); + + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + + // get asset account: + $result = $deposit->destination_account; + + $this->assertEquals($asset->id, $result->id); + } + +} \ No newline at end of file