Skip to content

Firefly III: a personal finances manager

Finished journal repository.

James Cole96ed9a4256f9
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 81fadc59dc..012a144bdb 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -379,14 +379,19 @@ class JournalRepository implements JournalRepositoryInterface
$to = Account::find($data['account_to_id']);
break;
}
- if (is_null($to->id)) {
+ if (is_null($to) || (!is_null($to) && is_null($to->id))) {
Log::error('"to"-account is null, so we cannot continue!');
App::abort(500, '"to"-account is null, so we cannot continue!');
+ // @codeCoverageIgnoreStart
}
- if (is_null($from->id)) {
+ // @codeCoverageIgnoreEnd
+
+ if (is_null($from) || (!is_null($from) && is_null($from->id))) {
Log::error('"from"-account is null, so we cannot continue!');
App::abort(500, '"from"-account is null, so we cannot continue!');
+ // @codeCoverageIgnoreStart
}
+ // @codeCoverageIgnoreEnd
return [$from, $to];
}
diff --git a/tests/factories/all.php b/tests/factories/all.php
index a060efcc07..572cf25ee1 100644
--- a/tests/factories/all.php
+++ b/tests/factories/all.php
@@ -173,9 +173,9 @@ FactoryMuffin::define(
'FireflyIII\Models\AccountType',
[
'type' => function () {
- $types = ['Expense account', 'Revenue account', 'Asset account'];
+ $types = ['Expense account', 'Revenue account', 'Asset account','Cash account'];
$count = DB::table('account_types')->count();
- if ($count < 3) {
+ if ($count < 4) {
return $types[$count];
} else {
return RandomString::generateRandomString(10);
diff --git a/tests/repositories/JournalRepositoryTest.php b/tests/repositories/JournalRepositoryTest.php
index 6b99313a2b..e4d0e2a68a 100644
--- a/tests/repositories/JournalRepositoryTest.php
+++ b/tests/repositories/JournalRepositoryTest.php
@@ -195,27 +195,344 @@ class JournalRepositoryTest extends TestCase
$this->assertEquals($data['description'], $journal->description);
}
+ /**
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::store
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts
+ */
+ public function testStoreDeposit()
+ {
+ for ($i = 0; $i < 4; $i++) {
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ }
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+
+ $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $data = [
+ 'description' => 'Some journal ' . rand(1, 100),
+ 'user' => $user->id,
+ 'what' => 'deposit',
+ 'amount_currency_id' => $currency->id,
+ 'account_id' => $account1->id,
+ 'revenue_account' => 'Some revenue account',
+ 'date' => '2014-01-01',
+ 'category' => 'Some category',
+ 'budget_id' => $budget->id,
+ 'amount' => 100,
+ 'tags' => ['one', 'two', 'three']
+
+
+ ];
+
+ $journal = $this->object->store($data);
+
+ $this->assertEquals($data['description'], $journal->description);
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::store
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts
+ */
+ public function testStoreExpenseWithCash()
+ {
+ for ($i = 0; $i < 4; $i++) {
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ }
+
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $data = [
+ 'description' => 'Some journal ' . rand(1, 100),
+ 'user' => $user->id,
+ 'what' => 'withdrawal',
+ 'amount_currency_id' => $currency->id,
+ 'account_id' => $account1->id,
+ 'expense_account' => '',
+ 'date' => '2014-01-01',
+ 'category' => 'Some other category',
+ 'budget_id' => $budget->id,
+ 'amount' => 100,
+ 'tags' => ['one', 'two', 'three']
+
+
+ ];
+
+ $journal = $this->object->store($data);
+
+ $this->assertEquals($data['description'], $journal->description);
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::store
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts
+ * @expectedException Symfony\Component\HttpKernel\Exception\HttpException
+ */
+ public function testStoreInvalidFromAccount()
+ {
+ for ($i = 0; $i < 4; $i++) {
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ }
+
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $data = [
+ 'description' => 'Some journal ' . rand(1, 100),
+ 'user' => $user->id,
+ 'what' => 'transfer',
+ 'amount_currency_id' => $currency->id,
+ 'account_from_id' => $account1->id,
+ 'account_to_id' => 17,
+ 'date' => '2014-01-01',
+ 'category' => 'Some other category',
+ 'budget_id' => $budget->id,
+ 'amount' => 100,
+ 'tags' => ['one', 'two', 'three']
+
+
+ ];
+
+ $journal = $this->object->store($data);
+
+ $this->assertEquals($data['description'], $journal->description);
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::store
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts
+ * @expectedException Symfony\Component\HttpKernel\Exception\HttpException
+ */
+ public function testStoreInvalidToAccount()
+ {
+ for ($i = 0; $i < 4; $i++) {
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ }
+
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $data = [
+ 'description' => 'Some journal ' . rand(1, 100),
+ 'user' => $user->id,
+ 'what' => 'transfer',
+ 'amount_currency_id' => $currency->id,
+ 'account_from_id' => 17,
+ 'account_to_id' => $account1->id,
+ 'date' => '2014-01-01',
+ 'category' => 'Some other category',
+ 'budget_id' => $budget->id,
+ 'amount' => 100,
+ 'tags' => ['one', 'two', 'three']
+
+
+ ];
+
+ $journal = $this->object->store($data);
+
+ $this->assertEquals($data['description'], $journal->description);
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::store
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts
+ */
+ public function testStoreRevenueWithCash()
+ {
+ for ($i = 0; $i < 4; $i++) {
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ }
+
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $data = [
+ 'description' => 'Some journal ' . rand(1, 100),
+ 'user' => $user->id,
+ 'what' => 'deposit',
+ 'amount_currency_id' => $currency->id,
+ 'account_id' => $account1->id,
+ 'revenue_account' => '',
+ 'date' => '2014-01-01',
+ 'category' => 'Some other category',
+ 'budget_id' => $budget->id,
+ 'amount' => 100,
+ 'tags' => ['one', 'two', 'three']
+
+
+ ];
+
+ $journal = $this->object->store($data);
+
+ $this->assertEquals($data['description'], $journal->description);
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::store
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts
+ */
+ public function testStoreTransfer()
+ {
+ for ($i = 0; $i < 4; $i++) {
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ }
+
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $account2 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $data = [
+ 'description' => 'Some journal ' . rand(1, 100),
+ 'user' => $user->id,
+ 'what' => 'transfer',
+ 'amount_currency_id' => $currency->id,
+ 'account_from_id' => $account1->id,
+ 'account_to_id' => $account2->id,
+ 'date' => '2014-01-01',
+ 'category' => 'Some other category',
+ 'budget_id' => $budget->id,
+ 'amount' => 100,
+ 'tags' => ['one', 'two', 'three']
+
+
+ ];
+
+ $journal = $this->object->store($data);
+
+ $this->assertEquals($data['description'], $journal->description);
+ }
+
/**
* @covers FireflyIII\Repositories\Journal\JournalRepository::update
- * @todo Implement testUpdate().
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::updateTags
*/
public function testUpdate()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
+ for ($i = 0; $i < 4; $i++) {
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ }
+
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $account2 = FactoryMuffin::create('FireflyIII\Models\Account');
+
+ // two transactions
+ Transaction::create(
+ [
+ 'account_id' => $account1->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => 100,
+ ]
);
- }
+ Transaction::create(
+ [
+ 'account_id' => $account1->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => -100,
+ ]
+ );
+
+
+ $data = [
+ 'amount_currency_id' => $currency->id,
+ 'description' => 'New description ' . rand(1, 100),
+ 'date' => '2015-01-01',
+ 'category' => 'SomenewCat',
+ 'amount' => 50,
+ 'user' => $journal->user_id,
+ 'budget_id' => $budget->id,
+ 'account_from_id' => $account1->id,
+ 'account_to_id' => $account2->id,
+ 'revenue_account' => 'Some revenue account',
+ 'expense_account' => 'Some expense account',
+ 'tags' => ['a', 'b', 'c']
+
+ ];
+
+ $result = $this->object->update($journal, $data);
+ $this->assertEquals($result->description, $data['description']);
+ $this->assertEquals($result->amount, 50);
+ }
/**
+ * @covers FireflyIII\Repositories\Journal\JournalRepository::update
* @covers FireflyIII\Repositories\Journal\JournalRepository::updateTags
- * @todo Implement testUpdateTags().
*/
- public function testUpdateTags()
+ public function testUpdateNoTags()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
+ for ($i = 0; $i < 4; $i++) {
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ }
+
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $account2 = FactoryMuffin::create('FireflyIII\Models\Account');
+
+ // two transactions
+ Transaction::create(
+ [
+ 'account_id' => $account1->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => 100,
+ ]
);
+ Transaction::create(
+ [
+ 'account_id' => $account1->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => -100,
+ ]
+ );
+
+
+ $data = [
+ 'amount_currency_id' => $currency->id,
+ 'description' => 'New description ' . rand(1, 100),
+ 'date' => '2015-01-01',
+ 'category' => 'SomenewCat',
+ 'amount' => 50,
+ 'user' => $journal->user_id,
+ 'budget_id' => $budget->id,
+ 'account_from_id' => $account1->id,
+ 'account_to_id' => $account2->id,
+ 'revenue_account' => 'Some revenue account',
+ 'expense_account' => 'Some expense account',
+ 'tags' => []
+
+ ];
+
+ $result = $this->object->update($journal, $data);
+
+ $this->assertEquals($result->description, $data['description']);
+ $this->assertEquals($result->amount, 50);
}
+
}