Skip to content

Firefly III: a personal finances manager

Covered account repository.

James Cole58241ed39de4
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index c026a037d0..7c3f3e94ff 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -510,9 +510,6 @@ class AccountRepository implements AccountRepositoryInterface
'encrypted' => true
]
);
- if (!$journal->isValid()) {
- App::abort(500);
- }
$journal->save();
@@ -536,9 +533,6 @@ class AccountRepository implements AccountRepositoryInterface
'amount' => $firstAmount
]
);
- if (!$one->isValid()) {
- App::abort(500);
- }
$one->save();
// second transaction: to
@@ -549,9 +543,6 @@ class AccountRepository implements AccountRepositoryInterface
'amount' => $secondAmount
]
);
- if (!$two->isValid()) {
- App::abort(500);
- }
$two->save();
return $journal;
diff --git a/tests/repositories/AccountRepositoryTest.php b/tests/repositories/AccountRepositoryTest.php
index d635463355..5f57fd4a03 100644
--- a/tests/repositories/AccountRepositoryTest.php
+++ b/tests/repositories/AccountRepositoryTest.php
@@ -647,34 +647,35 @@ class AccountRepositoryTest extends TestCase
* @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata
* @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance
*/
- public function testStoreWithNegativeInitialBalance()
+ public function testStoreWithExistingAccount()
{
- $user = FactoryMuffin::create('FireflyIII\User');
- FactoryMuffin::create('FireflyIII\Models\AccountType');
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
FactoryMuffin::create('FireflyIII\Models\AccountType');
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');
- $this->be($user);
+ $this->be($account->user);
+
$data = [
'accountType' => 'expense',
- 'user' => $user->id,
- 'name' => 'Test account #' . rand(1, 100),
- 'active' => true,
+ 'user' => $account->user->id,
+ 'name' => $account->name,
+ 'active' => $account->active,
'accountRole' => 'testAccount',
- 'openingBalance' => -100,
+ 'openingBalance' => 0,
'virtualBalance' => 0,
'openingBalanceCurrency' => $currency->id,
'openingBalanceDate' => '2015-01-01',
];
- $account = $this->object->store($data);
+ $newAccount = $this->object->store($data);
- $this->assertEquals($data['name'], $account->name);
+ $this->assertEquals($account->name, $newAccount->name);
+ $this->assertEquals($account->id, $newAccount->id);
}
@@ -683,8 +684,9 @@ class AccountRepositoryTest extends TestCase
* @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount
* @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata
* @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance
+ * @expectedException Symfony\Component\HttpKernel\Exception\HttpException
*/
- public function testStoreWithExistingAccount()
+ public function testStoreWithInvalidAccountData()
{
$account = FactoryMuffin::create('FireflyIII\Models\Account');
FactoryMuffin::create('FireflyIII\Models\AccountType');
@@ -698,7 +700,7 @@ class AccountRepositoryTest extends TestCase
$data = [
'accountType' => 'expense',
- 'user' => $account->user->id,
+ 'user' => $account->user->id + 12,
'name' => $account->name,
'active' => $account->active,
'accountRole' => 'testAccount',
@@ -708,11 +710,7 @@ class AccountRepositoryTest extends TestCase
'openingBalanceDate' => '2015-01-01',
];
-
- $newAccount = $this->object->store($data);
-
- $this->assertEquals($account->name, $newAccount->name);
- $this->assertEquals($account->id, $newAccount->id);
+ $this->object->store($data);
}
@@ -721,33 +719,36 @@ class AccountRepositoryTest extends TestCase
* @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount
* @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata
* @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance
- * @expectedException Symfony\Component\HttpKernel\Exception\HttpException
*/
- public function testStoreWithInvalidAccountData()
+ public function testStoreWithNegativeInitialBalance()
{
- $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
FactoryMuffin::create('FireflyIII\Models\AccountType');
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');
- $this->be($account->user);
-
+ $this->be($user);
$data = [
'accountType' => 'expense',
- 'user' => $account->user->id + 12,
- 'name' => $account->name,
- 'active' => $account->active,
+ 'user' => $user->id,
+ 'name' => 'Test account #' . rand(1, 100),
+ 'active' => true,
'accountRole' => 'testAccount',
- 'openingBalance' => 0,
+ 'openingBalance' => -100,
'virtualBalance' => 0,
'openingBalanceCurrency' => $currency->id,
'openingBalanceDate' => '2015-01-01',
];
- $this->object->store($data);
+
+ $account = $this->object->store($data);
+
+ $this->assertEquals($data['name'], $account->name);
+ $this->assertEquals(-100, $account->transactions()->first()->amount);
}
@@ -764,11 +765,152 @@ class AccountRepositoryTest extends TestCase
/**
* @covers FireflyIII\Repositories\Account\AccountRepository::update
- * @todo Implement testUpdate().
+ * @covers FireflyIII\Repositories\Account\AccountRepository::updateMetadata
+ * @covers FireflyIII\Repositories\Account\AccountRepository::updateInitialBalance
*/
public function testUpdate()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete('This test has not been implemented yet.');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ 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');
+ $this->be($user);
+
+ $data = [
+ 'accountType' => 'expense',
+ 'user' => $user->id,
+ 'name' => 'Test account #' . rand(1, 100),
+ 'active' => true,
+ 'accountRole' => 'testAccount',
+ 'openingBalance' => 100,
+ 'virtualBalance' => 0,
+ 'openingBalanceCurrency' => $currency->id,
+ 'openingBalanceDate' => '2015-01-01',
+ ];
+
+
+ $account = $this->object->store($data);
+
+ // now update that same account:
+ $data = [
+ 'name' => 'New account name' . rand(0, 100),
+ 'active' => 1,
+ 'virtualBalance' => 0,
+ 'openingBalance' => 50,
+ 'openingBalanceCurrency' => $currency->id,
+ 'openingBalanceDate' => '2015-02-02',
+ ];
+
+ $newAccount = $this->object->update($account, $data);
+
+ $this->assertEquals($data['name'], $newAccount->name);
+ $this->assertEquals(50, $account->transactions()->first()->amount);
+
+
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Account\AccountRepository::update
+ * @covers FireflyIII\Repositories\Account\AccountRepository::updateMetadata
+ * @covers FireflyIII\Repositories\Account\AccountRepository::updateInitialBalance
+ */
+ public function testUpdateDeleteOpeningBalance()
+ {
+ $user = FactoryMuffin::create('FireflyIII\User');
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ 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');
+ $this->be($user);
+
+ $data = [
+ 'accountType' => 'expense',
+ 'user' => $user->id,
+ 'name' => 'Test account #' . rand(1, 100),
+ 'active' => true,
+ 'accountRole' => 'testAccount',
+ 'openingBalance' => 100,
+ 'virtualBalance' => 0,
+ 'openingBalanceCurrency' => $currency->id,
+ 'openingBalanceDate' => '2015-01-01',
+ ];
+
+
+ $account = $this->object->store($data);
+
+ // now update that same account:
+ $data = [
+ 'name' => 'New account name' . rand(0, 100),
+ 'active' => 1,
+ 'user' => $user->id,
+ 'accountRole' => 'testAccount',
+ 'virtualBalance' => 0,
+ 'openingBalance' => 0,
+ ];
+
+ $newAccount = $this->object->update($account, $data);
+
+ $this->assertEquals($data['name'], $newAccount->name);
+ $this->assertEquals(0, $newAccount->transactions()->whereNull('deleted_at')->count());
+
+
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Account\AccountRepository::update
+ * @covers FireflyIII\Repositories\Account\AccountRepository::updateMetadata
+ * @covers FireflyIII\Repositories\Account\AccountRepository::updateInitialBalance
+ */
+ public function testUpdateNewOpeningBalance()
+ {
+ $user = FactoryMuffin::create('FireflyIII\User');
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ 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');
+ $this->be($user);
+
+ $data = [
+ 'accountType' => 'expense',
+ 'user' => $user->id,
+ 'name' => 'Test account #' . rand(1, 100),
+ 'active' => true,
+ 'accountRole' => 'testAccount',
+ 'openingBalance' => 0,
+ 'virtualBalance' => 0,
+ ];
+
+
+ $account = $this->object->store($data);
+
+ // now update that same account:
+ $data = [
+ 'name' => 'New account name' . rand(0, 100),
+ 'active' => 1,
+ 'user' => $user->id,
+ 'virtualBalance' => 0,
+ 'accountRole' => 'testAccount',
+ 'ccMonthlyPaymentDate' => '2015-01-01',
+ 'openingBalance' => 51,
+ 'openingBalanceCurrency' => $currency->id,
+ 'openingBalanceDate' => '2015-02-02',
+ ];
+
+ $newAccount = $this->object->update($account, $data);
+
+ $this->assertEquals($data['name'], $newAccount->name);
+ $this->assertEquals(51, $account->transactions()->first()->amount);
+
+
}
}