Skip to content

Firefly III: a personal finances manager

Fixed more tests but the factories are not perfect yet.

James Cole5d505f4ed093
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 @@
<?php namespace FireflyIII\Http\Controllers;
+use Amount;
use Auth;
use Carbon\Carbon;
use Config;
@@ -7,7 +8,6 @@ use FireflyIII\Http\Requests;
use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
-use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Pagination\LengthAwarePaginator;
use Input;
use Redirect;
@@ -114,43 +114,32 @@ class AccountController extends Controller
}
/**
- * @param string $what
+ * @param $what
+ * @param AccountRepositoryInterface $repository
*
* @return View
*/
- public function index($what = 'default')
+ public function index($what, AccountRepositoryInterface $repository)
{
$subTitle = Config::get('firefly.subTitlesByIdentifier.' . $what);
$subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what);
$types = Config::get('firefly.accountTypesByIdentifier.' . $what);
$size = 50;
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
- $offset = ($page - 1) * $size;
-
-
- // move to repository:
- $set = Auth::user()->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 getAccounts(array $types, $page);
+
+ /**
+ * @param TransactionJournal $journal
+ * @param Account $account
+ *
+ * @return Transaction
*/
- public function countAssetAccounts();
+ public function getFirstTransaction(TransactionJournal $journal, Account $account);
/**
* @param Preference $preference
@@ -55,45 +73,43 @@ interface AccountRepositoryInterface
/**
* @param Account $account
*
- * @return TransactionJournal|null
+ * @return Carbon|null
*/
- public function openingBalanceTransaction(Account $account);
+ public function getLastActivity(Account $account);
/**
- * @param array $data
+ * Get savings accounts and the balance difference in the period.
*
- * @return Account
+ * @return Collection
*/
- public function store(array $data);
+ public function getSavingsAccounts();
/**
* @param Account $account
- * @param array $data
*
- * @return Account
+ * @return float
*/
- public function update(Account $account, array $data);
+ public function leftOnAccount(Account $account);
/**
* @param Account $account
*
- * @return float
+ * @return TransactionJournal|null
*/
- public function leftOnAccount(Account $account);
+ public function openingBalanceTransaction(Account $account);
/**
- * Get savings accounts and the balance difference in the period.
+ * @param array $data
*
- * @return Collection
+ * @return Account
*/
- public function getSavingsAccounts();
-
+ public function store(array $data);
/**
- * @param TransactionJournal $journal
- * @param Account $account
+ * @param Account $account
+ * @param array $data
*
- * @return Transaction
+ * @return Account
*/
- public function getFirstTransaction(TransactionJournal $journal, Account $account);
+ public function update(Account $account, array $data);
}
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 @@
<?php
use Carbon\Carbon;
use FireflyIII\Models\Preference;
+use FireflyIII\Models\Account;
+use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
+use Illuminate\Support\Collection;
use League\FactoryMuffin\Facade as FactoryMuffin;
/**
@@ -17,6 +20,7 @@ class AccountControllerTest extends TestCase
public function setUp()
{
parent::setUp();
+ $this->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,
]
);