Should cover models.

This commit is contained in:
James Cole 2015-05-24 11:13:46 +02:00
parent 8f2f912cdf
commit 6c71f68ed8
9 changed files with 555 additions and 116 deletions

View File

@ -1,6 +1,5 @@
<?php namespace FireflyIII\Models;
use App;
use Crypt;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
@ -51,12 +50,6 @@ class Account extends Model
}
// create it!
$account = Account::create($fields);
if (is_null($account->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;

View File

@ -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;

View File

@ -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;

View File

@ -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;
}
}

View File

@ -1,5 +1,8 @@
<?php
use FireflyIII\Models\Account;
use League\FactoryMuffin\Facade as FactoryMuffin;
/**
* Class AccountModelTest
*/
@ -40,7 +43,49 @@ class AccountModelTest extends TestCase
*/
public function testFirstOrCreateEncrypted()
{
$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::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);
}
}

View File

@ -1,5 +1,9 @@
<?php
use FireflyIII\Models\Category;
use League\FactoryMuffin\Facade as FactoryMuffin;
/**
* Class CategoryModelTest
*/
@ -40,7 +44,33 @@ class CategoryModelTest extends TestCase
*/
public function testFirstOrCreateEncrypted()
{
$this->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);
}
}

View File

@ -1,5 +1,8 @@
<?php
use FireflyIII\Models\Tag;
use League\FactoryMuffin\Facade as FactoryMuffin;
/**
* Class TagModelTest
*/
@ -35,12 +38,40 @@ class TagModelTest extends TestCase
parent::tearDown();
}
/**
* @covers FireflyIII\Models\Tag::firstOrCreateEncrypted
*/
public function testFirstOrCreateEncryptedNew()
{
$tag = FactoryMuffin::create('FireflyIII\Models\Tag');
$search = [
'tagMode' => '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);
}
}

View File

@ -1,78 +0,0 @@
<?php
/**
* Class TransactionJournalModelTest
*/
class TransactionJournalModelTest extends TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
public function setUp()
{
parent::setUp();
}
/**
* This method is called before the first test of this test class is run.
*
* @since Method available since Release 3.4.0
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
public function tearDown()
{
parent::tearDown();
}
/**
* @covers FireflyIII\Models\TransactionJournal::getActualAmountAttribute
*/
public function testGetActualAmountAttribute()
{
$this->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();
}
}

View File

@ -0,0 +1,388 @@
<?php
use FireflyIII\Models\Transaction;
use League\FactoryMuffin\Facade as FactoryMuffin;
/**
* Class TransactionJournalModelTest
*/
class TransactionJournalModelTest extends TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
public function setUp()
{
parent::setUp();
}
/**
* This method is called before the first test of this test class is run.
*
* @since Method available since Release 3.4.0
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
public function tearDown()
{
parent::tearDown();
}
/**
* @covers FireflyIII\Models\TransactionJournal::getActualAmountAttribute
*/
public function testGetActualAmountAttribute()
{
$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->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);
}
}