From 5d505f4ed09356dac548ad50af2ba0c0dd4d95e3 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 4 Apr 2015 21:23:37 +0200 Subject: [PATCH] Fixed more tests but the factories are not perfect yet. --- app/Http/Controllers/AccountController.php | 39 +++++------ app/Http/Controllers/HomeController.php | 4 +- .../Account/AccountRepository.php | 42 +++++++++++- .../Account/AccountRepositoryInterface.php | 66 +++++++++++------- tests/controllers/AccountControllerTest.php | 67 +++++++++++-------- tests/factories/all.php | 6 +- 6 files changed, 144 insertions(+), 80 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index b671775f0c..bd84774cd0 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -1,5 +1,6 @@ accounts()->with( - ['accountmeta' => function (HasMany $query) { - $query->where('name', 'accountRole'); - }] - )->accountTypeIn($types)->take($size)->offset($offset)->orderBy('accounts.name', 'ASC')->get(['accounts.*']); - $total = Auth::user()->accounts()->accountTypeIn($types)->count(); + $set = $repository->getAccounts($types, $page); + $total = $repository->countAccounts($types); // last activity: + /** + * HERE WE ARE + */ $start = clone Session::get('start', Carbon::now()->startOfMonth()); $start->subDay(); $set->each( - function (Account $account) use ($start) { - $lastTransaction = $account->transactions()->leftJoin( - 'transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id' - )->orderBy('transaction_journals.date', 'DESC')->first(['transactions.*', 'transaction_journals.date']); - if ($lastTransaction) { - $account->lastActivityDate = $lastTransaction->transactionjournal->date; - } else { - $account->lastActivityDate = null; - } - $account->startBalance = Steam::balance($account, $start); - $account->endBalance = Steam::balance($account, clone Session::get('end')); + function (Account $account) use ($start, $repository) { + $account->lastActivityDate = $repository->getLastActivity($account); + $account->startBalance = Steam::balance($account, $start); + $account->endBalance = Steam::balance($account, clone Session::get('end', Carbon::now()->endOfMonth())); } ); @@ -176,6 +165,8 @@ class AccountController extends Controller $subTitle = 'Details for ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"'; $journals->setPath('accounts/show/' . $account->id); + + return view('accounts.show', compact('account', 'what', 'subTitleIcon', 'journals', 'subTitle')); } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 76b41d0bc9..2f029fde65 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -7,6 +7,7 @@ use Input; use Preferences; use Session; use Redirect; +use Config; /** * Class HomeController * @@ -41,7 +42,8 @@ class HomeController extends Controller public function index(AccountRepositoryInterface $repository) { - $count = $repository->countAssetAccounts(); + $types = Config::get('firefly.accountTypesByIdentifier.asset'); + $count = $repository->countAccounts($types); $title = 'Firefly'; $subTitle = 'What\'s playing?'; $mainTitleIcon = 'fa-fire'; diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 4d580a848e..d39c9b65b2 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -14,6 +14,7 @@ use FireflyIII\Models\Preference; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Log; @@ -29,11 +30,13 @@ class AccountRepository implements AccountRepositoryInterface { /** + * @param array $types + * * @return int */ - public function countAssetAccounts() + public function countAccounts(array $types) { - return Auth::user()->accounts()->accountTypeIn(['Asset account', 'Default account'])->count(); + return Auth::user()->accounts()->accountTypeIn($types)->count(); } /** @@ -48,6 +51,24 @@ class AccountRepository implements AccountRepositoryInterface return true; } + /** + * @param array $types + * @param int $page + * + * @return Collection + */ + public function getAccounts(array $types, $page) + { + $size = 50; + $offset = ($page - 1) * $size; + + return Auth::user()->accounts()->with( + ['accountmeta' => function (HasMany $query) { + $query->where('name', 'accountRole'); + }] + )->accountTypeIn($types)->take($size)->offset($offset)->orderBy('accounts.name', 'ASC')->get(['accounts.*']); + } + /** * @param TransactionJournal $journal * @param Account $account @@ -133,6 +154,23 @@ class AccountRepository implements AccountRepositoryInterface } + /** + * @param Account $account + * + * @return Carbon|null + */ + public function getLastActivity(Account $account) + { + $lastTransaction = $account->transactions()->leftJoin( + 'transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id' + )->orderBy('transaction_journals.date', 'DESC')->first(['transactions.*', 'transaction_journals.date']); + if ($lastTransaction) { + return $lastTransaction->transactionjournal->date; + } + + return null; + } + /** * Get savings accounts and the balance difference in the period. * diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index 8d00441a22..d064db673a 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -16,6 +16,13 @@ use Illuminate\Support\Collection; */ interface AccountRepositoryInterface { + /** + * @param array $types + * + * @return int + */ + public function countAccounts(array $types); + /** * @param Account $account * @@ -24,9 +31,20 @@ interface AccountRepositoryInterface public function destroy(Account $account); /** - * @return int + * @param array $types + * @param int $page + * + * @return mixed */ - public function countAssetAccounts(); + public function getAccounts(array $types, $page); + + /** + * @param TransactionJournal $journal + * @param Account $account + * + * @return Transaction + */ + public function getFirstTransaction(TransactionJournal $journal, Account $account); /** * @param Preference $preference @@ -52,6 +70,27 @@ interface AccountRepositoryInterface */ public function getJournals(Account $account, $page); + /** + * @param Account $account + * + * @return Carbon|null + */ + public function getLastActivity(Account $account); + + /** + * Get savings accounts and the balance difference in the period. + * + * @return Collection + */ + public function getSavingsAccounts(); + + /** + * @param Account $account + * + * @return float + */ + public function leftOnAccount(Account $account); + /** * @param Account $account * @@ -73,27 +112,4 @@ interface AccountRepositoryInterface * @return Account */ public function update(Account $account, array $data); - - /** - * @param Account $account - * - * @return float - */ - public function leftOnAccount(Account $account); - - /** - * Get savings accounts and the balance difference in the period. - * - * @return Collection - */ - public function getSavingsAccounts(); - - - /** - * @param TransactionJournal $journal - * @param Account $account - * - * @return Transaction - */ - public function getFirstTransaction(TransactionJournal $journal, Account $account); } diff --git a/tests/controllers/AccountControllerTest.php b/tests/controllers/AccountControllerTest.php index 07da123560..3ec57db5b3 100644 --- a/tests/controllers/AccountControllerTest.php +++ b/tests/controllers/AccountControllerTest.php @@ -1,7 +1,10 @@ account = FactoryMuffin::create('FireflyIII\Models\Account'); } @@ -28,6 +32,7 @@ class AccountControllerTest extends TestCase public function tearDown() { parent::tearDown(); + } @@ -58,42 +63,34 @@ class AccountControllerTest extends TestCase public function testDelete() { // fake an account. - $account = FactoryMuffin::create('FireflyIII\Models\Account'); - $account->accountType->type = 'Asset account'; - $account->accountType->save(); + $this->account->accountType->type = 'Asset account'; + $this->account->accountType->save(); - $this->be($account->user); - $this->call('GET', '/accounts/delete/' . $account->id); + $this->be($this->account->user); + $this->call('GET', '/accounts/delete/' . $this->account->id); $this->assertResponseOk(); - $this->assertViewHas('subTitle', 'Delete ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"'); + $this->assertViewHas('subTitle', 'Delete ' . strtolower(e($this->account->accountType->type)) . ' "' . e($this->account->name) . '"'); } public function testDestroy() { // fake an account. - $account = FactoryMuffin::create('FireflyIII\Models\Account'); - $account->accountType->type = 'Asset account'; - $account->accountType->save(); - - $this->be($account->user); - $this->assertCount(1, DB::table('accounts')->where('id', $account->id)->whereNull('deleted_at')->get()); + $this->be($this->account->user); + $this->assertCount(1, DB::table('accounts')->where('id', $this->account->id)->whereNull('deleted_at')->get()); // post it! - $this->call('POST', '/accounts/destroy/' . $account->id, ['_token' => 'replaceme']); + $this->call('POST', '/accounts/destroy/' . $this->account->id, ['_token' => 'replaceme']); $this->assertSessionHas('success'); - $this->assertCount(0, DB::table('accounts')->where('id', $account->id)->whereNull('deleted_at')->get()); + $this->assertCount(0, DB::table('accounts')->where('id', $this->account->id)->whereNull('deleted_at')->get()); } public function testEdit() { // fake an account. - $account = FactoryMuffin::create('FireflyIII\Models\Account'); - $account->accountType->type = 'Asset account'; - $account->accountType->save(); - $this->be($account->user); - $this->assertCount(1, DB::table('accounts')->where('id', $account->id)->whereNull('deleted_at')->get()); + $this->be($this->account->user); + $this->assertCount(1, DB::table('accounts')->where('id', $this->account->id)->whereNull('deleted_at')->get()); // create a transaction journal that will act as opening balance: $openingBalance = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); @@ -110,24 +107,32 @@ class AccountControllerTest extends TestCase Amount::shouldReceive('getAllCurrencies')->once()->andReturn([$currency]); // get edit page: - $this->call('GET', '/accounts/edit/' . $account->id); + $this->call('GET', '/accounts/edit/' . $this->account->id); // assert stuff: $this->assertResponseOk(); $this->assertSessionHas('preFilled'); - $this->assertViewHas('subTitle', 'Edit ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"'); + $this->assertViewHas('subTitle', 'Edit ' . strtolower(e($this->account->accountType->type)) . ' "' . e($this->account->name) . '"'); } public function testIndex() { - $user = FactoryMuffin::create('FireflyIII\User'); - $this->be($user); + // an account: + $this->be($this->account->user); + + $collection = new Collection; + $collection->push($this->account); + + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('getAccounts')->andReturn($collection); + $repository->shouldReceive('countAccounts')->andReturn(1); + $repository->shouldReceive('getLastActivity')->andReturn(null); + + Amount::shouldReceive('format')->andReturn(''); + Amount::shouldReceive('getCurrencyCode')->once()->andReturn('A'); - // get currency code: - $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); - Amount::shouldReceive('getCurrencyCode')->andReturn($currency->code); // put stuff in session: $this->session(['start' => new Carbon, 'end' => new Carbon]); @@ -141,7 +146,15 @@ class AccountControllerTest extends TestCase public function testShow() { - $this->markTestIncomplete(); + // an account: + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $this->be($account->user); + + Amount::shouldReceive('getCurrencyCode')->once()->andReturn('A'); + + // get edit page: + $this->call('GET', '/accounts/show/' . $account->id); + $this->assertResponseOk(); } public function testStore() diff --git a/tests/factories/all.php b/tests/factories/all.php index 8ddab42910..c12158c9cc 100644 --- a/tests/factories/all.php +++ b/tests/factories/all.php @@ -48,7 +48,11 @@ FactoryMuffin::define( FactoryMuffin::define( 'FireflyIII\Models\AccountType', [ - 'type' => 'word', + 'type' => function () { + $types = ['Expense account', 'Revenue account', 'Asset account']; + + return $types[rand(0, 2)]; + }, 'editable' => 1, ] );