From e3b11a9eb2a69d582f0ce77e157e9a7df247123a Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 3 Apr 2015 09:30:44 +0200 Subject: [PATCH] New test for edit form that is more inclusive. --- app/Http/Controllers/AccountController.php | 3 +- .../Account/AccountRepository.php | 14 +++- .../Account/AccountRepositoryInterface.php | 10 +++ .../Journal/JournalRepository.php | 4 +- .../Journal/JournalRepositoryInterface.php | 1 + tests/controllers/AccountControllerTest.php | 9 +++ tests/factories/all.php | 65 +++++++++++++++++-- 7 files changed, 99 insertions(+), 7 deletions(-) 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 @@ '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