Skip to content

Firefly III: a personal finances manager

New test for edit form that is more inclusive.

James Colee3b11a9eb2a6
diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php
index daa6ff16ed..1674601f81 100644
--- a/app/Http/Controllers/AccountController.php
+++ b/app/Http/Controllers/AccountController.php
@@ -93,8 +93,9 @@ class AccountController extends Controller
// the opening balance is tricky:
$openingBalanceAmount = null;
+
if ($openingBalance) {
- $transaction = $openingBalance->transactions()->where('account_id', $account->id)->first();
+ $transaction = $repository->getFirstTransaction($openingBalance, $account);
$openingBalanceAmount = $transaction->amount;
}
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index b5a5617fb3..8b15de67e4 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -48,6 +48,18 @@ class AccountRepository implements AccountRepositoryInterface
return true;
}
+ /**
+ * @param TransactionJournal $journal
+ * @param Account $account
+ *
+ * @return Transaction
+ */
+ public function getFirstTransaction(TransactionJournal $journal, Account $account)
+ {
+
+ return $journal->transactions()->where('account_id', $account->id)->first();
+ }
+
/**
* @param Preference $preference
*
@@ -170,7 +182,7 @@ class AccountRepository implements AccountRepositoryInterface
*/
public function leftOnAccount(Account $account)
{
- $balance = \Steam::balance($account,null,true);
+ $balance = \Steam::balance($account, null, true);
/** @var PiggyBank $p */
foreach ($account->piggybanks()->get() as $p) {
$balance -= $p->currentRelevantRep()->currentamount;
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index 20d96fbeb0..8d00441a22 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -5,6 +5,7 @@ namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\Preference;
+use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
@@ -86,4 +87,13 @@ interface AccountRepositoryInterface
* @return Collection
*/
public function getSavingsAccounts();
+
+
+ /**
+ * @param TransactionJournal $journal
+ * @param Account $account
+ *
+ * @return Transaction
+ */
+ public function getFirstTransaction(TransactionJournal $journal, Account $account);
}
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 3c4f13eaf3..851c879ed3 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -274,4 +274,6 @@ class JournalRepository implements JournalRepositoryInterface
public function first()
{
return Auth::user()->transactionjournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']);
- }}
+ }
+
+}
diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php
index 5e89501790..2bd13b2870 100644
--- a/app/Repositories/Journal/JournalRepositoryInterface.php
+++ b/app/Repositories/Journal/JournalRepositoryInterface.php
@@ -3,6 +3,7 @@
namespace FireflyIII\Repositories\Journal;
use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Models\Transaction;
use Illuminate\Support\Collection;
/**
diff --git a/tests/controllers/AccountControllerTest.php b/tests/controllers/AccountControllerTest.php
index 59141f30f7..8734f10f5a 100644
--- a/tests/controllers/AccountControllerTest.php
+++ b/tests/controllers/AccountControllerTest.php
@@ -94,6 +94,15 @@ class AccountControllerTest extends TestCase
$this->be($account->user);
$this->assertCount(1, DB::table('accounts')->where('id', $account->id)->whereNull('deleted_at')->get());
+ // create a transaction journal that will act as opening balance:
+ $openingBalance = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
+ $repository->shouldReceive('openingBalanceTransaction')->andReturn($openingBalance);
+
+ // create a transaction that will be returned for the opening balance transaction:
+ $openingBalanceTransaction = FactoryMuffin::create('FireflyIII\Models\Transaction');
+ $repository->shouldReceive('getFirstTransaction')->andReturn($openingBalanceTransaction);
+
// CURRENCY:
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
Amount::shouldReceive('getDefaultCurrency')->once()->andReturn($currency);
diff --git a/tests/factories/all.php b/tests/factories/all.php
index 022018297f..8ddab42910 100644
--- a/tests/factories/all.php
+++ b/tests/factories/all.php
@@ -1,6 +1,32 @@
<?php
use League\FactoryMuffin\Facade as FactoryMuffin;
+
+if (!class_exists('RandomString')) {
+ /**
+ * Class RandomString
+ */
+ class RandomString
+ {
+ /**
+ * @param int $length
+ *
+ * @return string
+ */
+ public static function generateRandomString($length = 10)
+ {
+ $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+ $charactersLength = strlen($characters);
+ $randomString = '';
+ for ($i = 0; $i < $length; $i++) {
+ $randomString .= $characters[rand(0, $charactersLength - 1)];
+ }
+
+ return $randomString;
+ }
+
+ }
+}
FactoryMuffin::define(
'FireflyIII\Models\Account', [
'user_id' => 'factory|FireflyIII\User',
@@ -29,10 +55,14 @@ FactoryMuffin::define(
FactoryMuffin::define(
'FireflyIII\Models\TransactionCurrency', [
- 'code' => 'EUR',
- 'symbol' => 'x',
- 'name' => 'word'
-]
+ 'code' => function () {
+ return RandomString::generateRandomString(3);
+ },
+ 'symbol' => function () {
+ return RandomString::generateRandomString(1);
+ },
+ 'name' => 'word'
+ ]
);
FactoryMuffin::define(
@@ -40,4 +70,31 @@ FactoryMuffin::define(
'email' => 'email',
'password' => bcrypt('james'),
]
+);
+
+FactoryMuffin::define(
+ 'FireflyIII\Models\Transaction', [
+ 'transaction_journal_id' => 'factory|FireflyIII\Models\TransactionJournal',
+ 'amount' => 'integer',
+ 'account_id' => 'factory|FireflyIII\Models\Account'
+ ]
+);
+
+FactoryMuffin::define(
+ 'FireflyIII\Models\TransactionType', [
+ 'type' => 'word',
+ ]
+);
+
+FactoryMuffin::define(
+ 'FireflyIII\Models\TransactionJournal', [
+ 'user_id' => 'factory|FireflyIII\User',
+ 'transaction_type_id' => 'factory|FireflyIII\Models\TransactionType',
+ 'transaction_currency_id' => 'factory|FireflyIII\Models\TransactionCurrency',
+ 'description' => 'sentence',
+ 'completed' => '1',
+ 'date' => 'date',
+ 'encrypted' => '1',
+ 'order' => '0',
+ ]
);
\ No newline at end of file