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;
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'));
}

View File

@ -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';

View File

@ -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.
*

View File

@ -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);
}

View File

@ -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()

View File

@ -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,
]
);