firefly-iii/tests/models/TransactionJournalModelTest.php

606 lines
22 KiB
PHP
Raw Normal View History

2015-05-24 04:13:46 -05:00
<?php
use League\FactoryMuffin\Facade as FactoryMuffin;
/**
* Class TransactionJournalModelTest
2015-05-26 01:17:58 -05:00
* @SuppressWarnings(PHPMD.TooManyMethods)
2015-05-24 04:13:46 -05:00
*/
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');
2015-05-26 12:38:52 -05:00
$journal->transactions[0]->amount = '123.45';
$journal->transactions[0]->save();
$journal->transactions[1]->amount = '-123.45';
$journal->transactions[1]->save();
2015-05-24 04:13:46 -05:00
$amount = $journal->actual_amount;
$this->assertEquals('123.45', $amount);
}
/**
* @covers FireflyIII\Models\TransactionJournal::getAmountAttribute
2015-06-07 03:28:26 -05:00
* @covers FireflyIII\Models\TransactionJournal::amountByTag
* @covers FireflyIII\Models\TransactionJournal::amountByTags
2015-05-24 04:13:46 -05:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testGetAmountAttributeAdvancePayment()
{
2015-06-03 14:15:52 -05:00
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
2015-05-24 04:13:46 -05:00
// 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');
2015-05-26 12:38:52 -05:00
// transactions are already in place, update them:
$withdrawal->transactions[0]->account_id = $asset->id;
2015-06-04 16:08:44 -05:00
$withdrawal->transactions[0]->amount = -300;
2015-05-26 12:38:52 -05:00
$withdrawal->transactions[0]->save();
$withdrawal->transactions[1]->account_id = $expense->id;
2015-06-04 16:08:44 -05:00
$withdrawal->transactions[1]->amount = 300;
2015-05-26 12:38:52 -05:00
$withdrawal->transactions[1]->save();
2015-05-24 04:13:46 -05:00
2015-05-26 12:38:52 -05:00
$deposit->transactions[0]->account_id = $revenue->id;
2015-06-04 16:08:44 -05:00
$deposit->transactions[0]->amount = -89.88;
2015-05-26 12:38:52 -05:00
$deposit->transactions[0]->save();
2015-05-24 04:13:46 -05:00
2015-05-26 12:38:52 -05:00
$deposit->transactions[1]->account_id = $asset->id;
2015-06-04 16:08:44 -05:00
$deposit->transactions[1]->amount = 89.88;
2015-05-26 12:38:52 -05:00
$deposit->transactions[1]->save();
2015-05-24 04:13:46 -05:00
// connect to tag:
$tag->transactionJournals()->save($withdrawal);
$tag->transactionJournals()->save($deposit);
2015-06-15 12:25:54 -05:00
$withdrawal->save();
$deposit->save();
2015-05-24 04:13:46 -05:00
// amount should be 210.12:
$this->assertEquals('210.12', $withdrawal->amount);
$this->assertEquals('0', $deposit->amount);
}
2015-06-07 03:28:26 -05:00
2015-05-24 04:13:46 -05:00
/**
* @covers FireflyIII\Models\TransactionJournal::getAmountAttribute
2015-06-07 03:28:26 -05:00
* @covers FireflyIII\Models\TransactionJournal::amountByTag
* @covers FireflyIII\Models\TransactionJournal::amountByTags
2015-05-24 04:13:46 -05:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testGetAmountAttributeBalancingAct()
{
2015-06-03 14:15:52 -05:00
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
2015-05-24 04:13:46 -05:00
// 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:
2015-06-04 16:08:44 -05:00
$expense = FactoryMuffin::create('FireflyIII\Models\Account');
2015-05-26 12:38:52 -05:00
$revenue = FactoryMuffin::create('FireflyIII\Models\Account');
2015-06-04 16:08:44 -05:00
$asset = FactoryMuffin::create('FireflyIII\Models\Account');
2015-05-26 12:38:52 -05:00
$revenue->account_type_id = $asset->account_type_id;
$revenue->save();
// transactions are already in place, update them:
$withdrawal->transactions[0]->account_id = $asset->id;
2015-06-04 16:08:44 -05:00
$withdrawal->transactions[0]->amount = -123.45;
2015-05-26 12:38:52 -05:00
$withdrawal->transactions[0]->save();
2015-05-24 04:13:46 -05:00
2015-05-26 12:38:52 -05:00
$withdrawal->transactions[1]->account_id = $expense->id;
2015-06-04 16:08:44 -05:00
$withdrawal->transactions[1]->amount = 123.45;
2015-05-26 12:38:52 -05:00
$withdrawal->transactions[1]->save();
2015-05-24 04:13:46 -05:00
2015-05-26 12:38:52 -05:00
$transfer->transactions[0]->account_id = $revenue->id;
2015-06-04 16:08:44 -05:00
$transfer->transactions[0]->amount = -123.45;
2015-05-26 12:38:52 -05:00
$transfer->transactions[0]->save();
$transfer->transactions[1]->account_id = $asset->id;
2015-06-04 16:08:44 -05:00
$transfer->transactions[1]->amount = 123.45;
2015-05-26 12:38:52 -05:00
$transfer->transactions[1]->save();
2015-05-24 04:13:46 -05:00
2015-06-15 12:25:54 -05:00
$withdrawal->save();
2015-05-24 04:13:46 -05:00
$amount = $withdrawal->amount;
2015-06-15 12:25:54 -05:00
2015-05-24 04:13:46 -05:00
$this->assertEquals('0', $amount);
}
/**
* @covers FireflyIII\Models\TransactionJournal::getAmountAttribute
2015-06-07 03:28:26 -05:00
* @covers FireflyIII\Models\TransactionJournal::amountByTag
* @covers FireflyIII\Models\TransactionJournal::amountByTags
2015-05-24 04:13:46 -05:00
*/
public function testGetAmountAttributeNoTags()
{
2015-06-03 14:15:52 -05:00
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
2015-05-24 04:13:46 -05:00
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
2015-05-26 12:38:52 -05:00
$journal->transactions[0]->amount = 123.45;
$journal->transactions[0]->save();
$journal->transactions[1]->amount = -123.45;
$journal->transactions[1]->save();
2015-05-24 04:13:46 -05:00
$amount = $journal->amount;
$this->assertEquals('123.45', $amount);
}
/**
* @covers FireflyIII\Models\TransactionJournal::getAmountAttribute
2015-06-07 03:28:26 -05:00
* @covers FireflyIII\Models\TransactionJournal::amountByTag
* @covers FireflyIII\Models\TransactionJournal::amountByTags
2015-05-24 04:13:46 -05:00
*/
public function testGetAmountAttributeTag()
{
2015-06-03 14:15:52 -05:00
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
2015-05-24 04:13:46 -05:00
// 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');
2015-06-04 16:08:44 -05:00
$withdrawal->transactions[0]->amount = -300;
2015-05-26 12:38:52 -05:00
$withdrawal->transactions[0]->account_id = $asset->id;
$withdrawal->transactions[0]->save();
2015-06-04 16:08:44 -05:00
$withdrawal->transactions[1]->amount = 300;
2015-05-26 12:38:52 -05:00
$withdrawal->transactions[1]->account_id = $expense->id;
$withdrawal->transactions[1]->save();
2015-05-24 04:13:46 -05:00
// connect to tag:
$tag->transactionJournals()->save($withdrawal);
$this->assertEquals('300', $withdrawal->amount);
2015-06-07 03:28:26 -05:00
}
/**
2015-06-13 01:17:38 -05:00
* Multiple tags, withdrawal.
*
2015-06-07 03:28:26 -05:00
* @covers FireflyIII\Models\TransactionJournal::getAmountAttribute
* @covers FireflyIII\Models\TransactionJournal::amountByTag
* @covers FireflyIII\Models\TransactionJournal::amountByTags
*/
public function testGetAmountAttributeTags()
{
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
// has two normal tags:
$tag = FactoryMuffin::create('FireflyIII\Models\Tag');
$tag->tagMode = 'nothing';
$tag->save();
$tag2 = FactoryMuffin::create('FireflyIII\Models\Tag');
$tag2->tagMode = 'nothing';
$tag2->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');
$withdrawal->transactions[0]->amount = -300;
$withdrawal->transactions[0]->account_id = $asset->id;
$withdrawal->transactions[0]->save();
$withdrawal->transactions[1]->amount = 300;
$withdrawal->transactions[1]->account_id = $expense->id;
$withdrawal->transactions[1]->save();
// connect to tag:
$tag->transactionJournals()->save($withdrawal);
$tag2->transactionJournals()->save($withdrawal);
$this->assertEquals('300', $withdrawal->amount);
2015-06-13 01:17:38 -05:00
}
/**
* Multiple tags, transfer, and one is a balancing act
*
* @covers FireflyIII\Models\TransactionJournal::getAmountAttribute
* @covers FireflyIII\Models\TransactionJournal::amountByTag
* @covers FireflyIII\Models\TransactionJournal::amountByTags
*/
public function testGetAmountAttributeTagsTransfer()
{
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
// has two normal tags:
$tag = FactoryMuffin::create('FireflyIII\Models\Tag');
$tag->tagMode = 'balancingAct';
$tag->save();
$tag2 = FactoryMuffin::create('FireflyIII\Models\Tag');
$tag2->tagMode = 'nothing';
$tag2->save();
// make withdrawal
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
$transferType = FactoryMuffin::create('FireflyIII\Models\TransactionType');
$transfer = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
$transfer->transaction_type_id = $transferType->id;
$transfer->save();
// make accounts
$expense = FactoryMuffin::create('FireflyIII\Models\Account');
$asset = FactoryMuffin::create('FireflyIII\Models\Account');
$transfer->transactions[0]->amount = -300;
$transfer->transactions[0]->account_id = $asset->id;
$transfer->transactions[0]->save();
$transfer->transactions[1]->amount = 300;
$transfer->transactions[1]->account_id = $expense->id;
$transfer->transactions[1]->save();
// connect to tag:
$tag->transactionJournals()->save($transfer);
$tag2->transactionJournals()->save($transfer);
$this->assertEquals('300', $transfer->amount);
}
/**
* Multiple tags, transfer, and one is a advance payment.
*
* @covers FireflyIII\Models\TransactionJournal::getAmountAttribute
* @covers FireflyIII\Models\TransactionJournal::amountByTag
* @covers FireflyIII\Models\TransactionJournal::amountByTags
*/
public function testGetAmountAttributeTagsTransferAdvance()
{
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
// has two normal tags:
$tag = FactoryMuffin::create('FireflyIII\Models\Tag');
$tag->tagMode = 'advancePayment';
$tag->save();
$tag2 = FactoryMuffin::create('FireflyIII\Models\Tag');
$tag2->tagMode = 'nothing';
$tag2->save();
// make withdrawal
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
$transferType = FactoryMuffin::create('FireflyIII\Models\TransactionType');
$transfer = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
$transfer->transaction_type_id = $transferType->id;
$transfer->save();
// make accounts
$expense = FactoryMuffin::create('FireflyIII\Models\Account');
$asset = FactoryMuffin::create('FireflyIII\Models\Account');
$transfer->transactions[0]->amount = -300;
$transfer->transactions[0]->account_id = $asset->id;
$transfer->transactions[0]->save();
$transfer->transactions[1]->amount = 300;
$transfer->transactions[1]->account_id = $expense->id;
$transfer->transactions[1]->save();
// connect to tag:
$tag->transactionJournals()->save($transfer);
$tag2->transactionJournals()->save($transfer);
$this->assertEquals('300', $transfer->amount);
}
/**
* Multiple tags, withdrawal, and one is a balancingAct.
*
* @covers FireflyIII\Models\TransactionJournal::getAmountAttribute
* @covers FireflyIII\Models\TransactionJournal::amountByTag
* @covers FireflyIII\Models\TransactionJournal::amountByTags
*/
public function testGetAmountAttributeTagsWithdrawalAdvance()
{
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
// has two normal tags:
$tag = FactoryMuffin::create('FireflyIII\Models\Tag');
$tag->tagMode = 'balancingAct';
$tag->save();
$tag2 = FactoryMuffin::create('FireflyIII\Models\Tag');
$tag2->tagMode = 'nothing';
$tag2->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');
$withdrawal->transactions[0]->amount = -300;
$withdrawal->transactions[0]->account_id = $asset->id;
$withdrawal->transactions[0]->save();
$withdrawal->transactions[1]->amount = 300;
$withdrawal->transactions[1]->account_id = $expense->id;
$withdrawal->transactions[1]->save();
// connect to tag:
$tag->transactionJournals()->save($withdrawal);
$tag2->transactionJournals()->save($withdrawal);
$this->assertEquals('300', $withdrawal->amount);
2015-05-24 04:13:46 -05:00
}
2015-06-04 16:08:44 -05:00
/**
* @covers FireflyIII\Models\TransactionJournal::getCorrectAmountAttribute
*/
public function testGetCorrectAmountAttribute()
{
$withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal
// make accounts
FactoryMuffin::create('FireflyIII\Models\Account');
$revenue = FactoryMuffin::create('FireflyIII\Models\Account');
$asset = FactoryMuffin::create('FireflyIII\Models\Account');
// make withdrawal
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
$journal->transaction_type_id = $withdrawal->id;
$journal->save();
$journal->transactions[0]->account_id = $asset->id;
$journal->transactions[0]->amount = 300;
$journal->transactions[0]->save();
$journal->transactions[1]->account_id = $revenue->id;
$journal->transactions[1]->amount = -300;
$journal->transactions[1]->save();
// get asset account:
$result = $journal->correct_amount;
$this->assertEquals(-300, $result);
}
/**
* @covers FireflyIII\Models\TransactionJournal::getCorrectAmountAttribute
*/
public function testGetCorrectAmountAttributeDeposit()
{
FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal
$deposit = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit
// make accounts
FactoryMuffin::create('FireflyIII\Models\Account');
$revenue = FactoryMuffin::create('FireflyIII\Models\Account');
$asset = FactoryMuffin::create('FireflyIII\Models\Account');
// make withdrawal
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
$journal->transaction_type_id = $deposit->id;
$journal->save();
$journal->transactions[0]->account_id = $asset->id;
$journal->transactions[0]->amount = 300;
$journal->transactions[0]->save();
$journal->transactions[1]->account_id = $revenue->id;
$journal->transactions[1]->amount = -300;
$journal->transactions[1]->save();
// get asset account:
$result = $journal->correct_amount;
$this->assertEquals(300, $result);
}
/**
* @covers FireflyIII\Models\TransactionJournal::getCorrectAmountAttribute
*/
public function testGetCorrectAmountAttributeTransfer()
{
FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal
FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit
$transfer = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // transfer
// make accounts
FactoryMuffin::create('FireflyIII\Models\Account');
$revenue = FactoryMuffin::create('FireflyIII\Models\Account');
$asset = FactoryMuffin::create('FireflyIII\Models\Account');
// make withdrawal
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
$journal->transaction_type_id = $transfer->id;
$journal->save();
$journal->transactions[0]->account_id = $asset->id;
$journal->transactions[0]->amount = 300;
$journal->transactions[0]->save();
$journal->transactions[1]->account_id = $revenue->id;
$journal->transactions[1]->amount = -300;
$journal->transactions[1]->save();
// get asset account:
$result = $journal->correct_amount;
$this->assertEquals('300', $result);
2015-06-04 16:08:44 -05:00
}
2015-05-24 04:13:46 -05:00
/**
* @covers FireflyIII\Models\TransactionJournal::getDestinationAccountAttribute
*/
public function testGetDestinationAccountAttribute()
{
2015-06-06 08:36:12 -05:00
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
2015-05-24 04:13:46 -05:00
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');
2015-05-26 12:38:52 -05:00
$deposit->transactions[0]->account_id = $asset->id;
2015-06-04 16:08:44 -05:00
$deposit->transactions[0]->amount = 300;
2015-05-26 12:38:52 -05:00
$deposit->transactions[0]->save();
$deposit->transactions[1]->account_id = $revenue->id;
2015-06-04 16:08:44 -05:00
$deposit->transactions[1]->amount = -300;
2015-05-26 12:38:52 -05:00
$deposit->transactions[1]->save();
2015-05-24 04:13:46 -05:00
// get asset account:
$result = $deposit->destination_account;
$this->assertEquals($asset->id, $result->id);
}
/**
2015-06-07 03:28:26 -05:00
* @covers FireflyIII\Models\TransactionJournal::getSourceAccountAttribute
2015-05-24 04:13:46 -05:00
*/
2015-06-06 08:36:12 -05:00
public function testGetSourceAccountAttribute()
2015-05-24 04:13:46 -05:00
{
2015-06-06 08:36:12 -05:00
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
2015-05-24 04:13:46 -05:00
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');
2015-06-04 16:08:44 -05:00
$deposit->transactions[0]->account_id = $asset->id;
$deposit->transactions[0]->amount = 300;
$deposit->transactions[0]->save();
$deposit->transactions[1]->account_id = $revenue->id;
$deposit->transactions[1]->amount = -300;
$deposit->transactions[1]->save();
// get asset account:
2015-06-06 08:36:12 -05:00
$result = $deposit->source_account;
2015-06-04 16:08:44 -05:00
$this->assertEquals($revenue->id, $result->id);
}
2015-05-24 08:18:19 -05:00
}