Fixed more tests but the factories are not perfect yet.

This commit is contained in:
James Cole 2015-04-04 21:23:37 +02:00
parent 0c02a08954
commit 5d505f4ed0
6 changed files with 144 additions and 80 deletions

View File

@ -1,5 +1,6 @@
<?php namespace FireflyIII\Http\Controllers; <?php namespace FireflyIII\Http\Controllers;
use Amount;
use Auth; use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use Config; use Config;
@ -7,7 +8,6 @@ use FireflyIII\Http\Requests;
use FireflyIII\Http\Requests\AccountFormRequest; use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use Input; use Input;
use Redirect; use Redirect;
@ -114,43 +114,32 @@ class AccountController extends Controller
} }
/** /**
* @param string $what * @param $what
* @param AccountRepositoryInterface $repository
* *
* @return View * @return View
*/ */
public function index($what = 'default') public function index($what, AccountRepositoryInterface $repository)
{ {
$subTitle = Config::get('firefly.subTitlesByIdentifier.' . $what); $subTitle = Config::get('firefly.subTitlesByIdentifier.' . $what);
$subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what); $subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what);
$types = Config::get('firefly.accountTypesByIdentifier.' . $what); $types = Config::get('firefly.accountTypesByIdentifier.' . $what);
$size = 50; $size = 50;
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
$offset = ($page - 1) * $size; $set = $repository->getAccounts($types, $page);
$total = $repository->countAccounts($types);
// 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();
// last activity: // last activity:
/**
* HERE WE ARE
*/
$start = clone Session::get('start', Carbon::now()->startOfMonth()); $start = clone Session::get('start', Carbon::now()->startOfMonth());
$start->subDay(); $start->subDay();
$set->each( $set->each(
function (Account $account) use ($start) { function (Account $account) use ($start, $repository) {
$lastTransaction = $account->transactions()->leftJoin( $account->lastActivityDate = $repository->getLastActivity($account);
'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->startBalance = Steam::balance($account, $start);
$account->endBalance = Steam::balance($account, clone Session::get('end')); $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) . '"'; $subTitle = 'Details for ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"';
$journals->setPath('accounts/show/' . $account->id); $journals->setPath('accounts/show/' . $account->id);
return view('accounts.show', compact('account', 'what', 'subTitleIcon', 'journals', 'subTitle')); return view('accounts.show', compact('account', 'what', 'subTitleIcon', 'journals', 'subTitle'));
} }

View File

@ -7,6 +7,7 @@ use Input;
use Preferences; use Preferences;
use Session; use Session;
use Redirect; use Redirect;
use Config;
/** /**
* Class HomeController * Class HomeController
* *
@ -41,7 +42,8 @@ class HomeController extends Controller
public function index(AccountRepositoryInterface $repository) public function index(AccountRepositoryInterface $repository)
{ {
$count = $repository->countAssetAccounts(); $types = Config::get('firefly.accountTypesByIdentifier.asset');
$count = $repository->countAccounts($types);
$title = 'Firefly'; $title = 'Firefly';
$subTitle = 'What\'s playing?'; $subTitle = 'What\'s playing?';
$mainTitleIcon = 'fa-fire'; $mainTitleIcon = 'fa-fire';

View File

@ -14,6 +14,7 @@ use FireflyIII\Models\Preference;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@ -29,11 +30,13 @@ class AccountRepository implements AccountRepositoryInterface
{ {
/** /**
* @param array $types
*
* @return int * @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; 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 TransactionJournal $journal
* @param Account $account * @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. * Get savings accounts and the balance difference in the period.
* *

View File

@ -16,6 +16,13 @@ use Illuminate\Support\Collection;
*/ */
interface AccountRepositoryInterface interface AccountRepositoryInterface
{ {
/**
* @param array $types
*
* @return int
*/
public function countAccounts(array $types);
/** /**
* @param Account $account * @param Account $account
* *
@ -24,9 +31,20 @@ interface AccountRepositoryInterface
public function destroy(Account $account); 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 * @param Preference $preference
@ -52,6 +70,27 @@ interface AccountRepositoryInterface
*/ */
public function getJournals(Account $account, $page); 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 * @param Account $account
* *
@ -73,27 +112,4 @@ interface AccountRepositoryInterface
* @return Account * @return Account
*/ */
public function update(Account $account, array $data); 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);
} }

View File

@ -1,7 +1,10 @@
<?php <?php
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\Preference; use FireflyIII\Models\Preference;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use Illuminate\Support\Collection;
use League\FactoryMuffin\Facade as FactoryMuffin; use League\FactoryMuffin\Facade as FactoryMuffin;
/** /**
@ -17,6 +20,7 @@ class AccountControllerTest extends TestCase
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->account = FactoryMuffin::create('FireflyIII\Models\Account');
} }
@ -28,6 +32,7 @@ class AccountControllerTest extends TestCase
public function tearDown() public function tearDown()
{ {
parent::tearDown(); parent::tearDown();
} }
@ -58,42 +63,34 @@ class AccountControllerTest extends TestCase
public function testDelete() public function testDelete()
{ {
// fake an account. // fake an account.
$account = FactoryMuffin::create('FireflyIII\Models\Account'); $this->account->accountType->type = 'Asset account';
$account->accountType->type = 'Asset account'; $this->account->accountType->save();
$account->accountType->save();
$this->be($account->user); $this->be($this->account->user);
$this->call('GET', '/accounts/delete/' . $account->id); $this->call('GET', '/accounts/delete/' . $this->account->id);
$this->assertResponseOk(); $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() public function testDestroy()
{ {
// fake an account. // fake an account.
$account = FactoryMuffin::create('FireflyIII\Models\Account'); $this->be($this->account->user);
$account->accountType->type = 'Asset account'; $this->assertCount(1, DB::table('accounts')->where('id', $this->account->id)->whereNull('deleted_at')->get());
$account->accountType->save();
$this->be($account->user);
$this->assertCount(1, DB::table('accounts')->where('id', $account->id)->whereNull('deleted_at')->get());
// post it! // 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->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() public function testEdit()
{ {
// fake an account. // fake an account.
$account = FactoryMuffin::create('FireflyIII\Models\Account');
$account->accountType->type = 'Asset account';
$account->accountType->save();
$this->be($account->user); $this->be($this->account->user);
$this->assertCount(1, DB::table('accounts')->where('id', $account->id)->whereNull('deleted_at')->get()); $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: // create a transaction journal that will act as opening balance:
$openingBalance = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); $openingBalance = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
@ -110,24 +107,32 @@ class AccountControllerTest extends TestCase
Amount::shouldReceive('getAllCurrencies')->once()->andReturn([$currency]); Amount::shouldReceive('getAllCurrencies')->once()->andReturn([$currency]);
// get edit page: // get edit page:
$this->call('GET', '/accounts/edit/' . $account->id); $this->call('GET', '/accounts/edit/' . $this->account->id);
// assert stuff: // assert stuff:
$this->assertResponseOk(); $this->assertResponseOk();
$this->assertSessionHas('preFilled'); $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() public function testIndex()
{ {
$user = FactoryMuffin::create('FireflyIII\User'); // an account:
$this->be($user); $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: // put stuff in session:
$this->session(['start' => new Carbon, 'end' => new Carbon]); $this->session(['start' => new Carbon, 'end' => new Carbon]);
@ -141,7 +146,15 @@ class AccountControllerTest extends TestCase
public function testShow() 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() public function testStore()

View File

@ -48,7 +48,11 @@ FactoryMuffin::define(
FactoryMuffin::define( FactoryMuffin::define(
'FireflyIII\Models\AccountType', [ 'FireflyIII\Models\AccountType', [
'type' => 'word', 'type' => function () {
$types = ['Expense account', 'Revenue account', 'Asset account'];
return $types[rand(0, 2)];
},
'editable' => 1, 'editable' => 1,
] ]
); );