First tests for piggy bank controller.

This commit is contained in:
James Cole 2014-12-22 07:10:18 +01:00
parent 50be39b054
commit e9f391b2eb
4 changed files with 190 additions and 34 deletions

View File

@ -1,5 +1,6 @@
<?php <?php
use Carbon\Carbon;
use FireflyIII\Database\PiggyBank\PiggyBank as Repository; use FireflyIII\Database\PiggyBank\PiggyBank as Repository;
use FireflyIII\Exception\FireflyException; use FireflyIII\Exception\FireflyException;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -27,6 +28,8 @@ class PiggybankController extends BaseController
public function __construct(Repository $repository) public function __construct(Repository $repository)
{ {
$this->_repository = $repository; $this->_repository = $repository;
View::share('title', 'Piggy banks');
View::share('mainTitleIcon', 'fa-sort-amount-asc');
} }
/** /**
@ -44,10 +47,10 @@ class PiggybankController extends BaseController
$leftOnAccount = $repos->leftOnAccount($piggybank->account); $leftOnAccount = $repos->leftOnAccount($piggybank->account);
$savedSoFar = $piggybank->currentRelevantRep()->currentamount; $savedSoFar = $piggybank->currentRelevantRep()->currentamount;
$leftToSave = $piggybank->targetamount - $savedSoFar; $leftToSave = $piggybank->targetamount - $savedSoFar;
$amount = min($leftOnAccount, $leftToSave); $maxAmount = min($leftOnAccount, $leftToSave);
return View::make('piggybanks.add', compact('piggybank'))->with('maxAmount', $amount); return View::make('piggybanks.add', compact('piggybank', 'maxAmount'));
} }
/** /**
@ -60,13 +63,11 @@ class PiggybankController extends BaseController
$acct = App::make('FireflyIII\Database\Account\Account'); $acct = App::make('FireflyIII\Database\Account\Account');
$periods = Config::get('firefly.piggybank_periods'); $periods = Config::get('firefly.piggybank_periods');
$accounts = FFForm::makeSelectList($acct->getAssetAccounts()); $accounts = FFForm::makeSelectList($acct->getAssetAccounts());
$subTitle = 'Create new piggy bank';
$subTitleIcon = 'fa-plus';
return View::make('piggybanks.create', compact('accounts', 'periods'))->with('title', 'Piggy banks')->with('mainTitleIcon', 'fa-sort-amount-asc')->with( return View::make('piggybanks.create', compact('accounts', 'periods', 'subTitle', 'subTitleIcon'));
'subTitle', 'Create new piggy bank'
)->with('subTitleIcon', 'fa-plus');
} }
/** /**
@ -76,9 +77,9 @@ class PiggybankController extends BaseController
*/ */
public function delete(Piggybank $piggybank) public function delete(Piggybank $piggybank)
{ {
return View::make('piggybanks.delete')->with('piggybank', $piggybank)->with('subTitle', 'Delete "' . $piggybank->name . '"')->with( $subTitle = 'Delete "' . e($piggybank->name) . '"';
'title', 'Piggy banks'
)->with('mainTitleIcon', 'fa-sort-amount-asc'); return View::make('piggybanks.delete', compact('piggybank', 'subTitle'));
} }
/** /**
@ -88,10 +89,9 @@ class PiggybankController extends BaseController
*/ */
public function destroy(Piggybank $piggyBank) public function destroy(Piggybank $piggyBank)
{ {
/** @var \FireflyIII\Database\PiggyBank\PiggyBank $acct */
$repos = App::make('FireflyIII\Database\PiggyBank\PiggyBank'); Session::flash('success', 'Piggy bank "' . e($piggyBank->name) . '" deleted.');
$repos->destroy($piggyBank); $this->_repository->destroy($piggyBank);
Session::flash('success', 'Piggy bank deleted.');
return Redirect::route('piggybanks.index'); return Redirect::route('piggybanks.index');
} }
@ -108,24 +108,29 @@ class PiggybankController extends BaseController
$acct = App::make('FireflyIII\Database\Account\Account'); $acct = App::make('FireflyIII\Database\Account\Account');
$periods = Config::get('firefly.piggybank_periods'); $periods = Config::get('firefly.piggybank_periods');
$accounts = FFForm::makeSelectList($acct->getAssetAccounts()); $accounts = FFForm::makeSelectList($acct->getAssetAccounts());
$subTitle = 'Edit piggy bank "' . e($piggybank->name) . '"';
$subTitleIcon = 'fa-pencil';
/* /*
* Flash some data to fill the form. * Flash some data to fill the form.
*/ */
if (is_null($piggybank->targetdate)) {
$targetDate = null;
} else {
$targetDate = new Carbon($piggybank->targetdate);
$targetDate = $targetDate->format('Y-m-d');
}
$preFilled = ['name' => $piggybank->name, $preFilled = ['name' => $piggybank->name,
'account_id' => $piggybank->account_id, 'account_id' => $piggybank->account_id,
'targetamount' => $piggybank->targetamount, 'targetamount' => $piggybank->targetamount,
'targetdate' => !is_null($piggybank->targetdate) ? $piggybank->targetdate->format('Y-m-d') : null, 'targetdate' => $targetDate,
'reminder' => $piggybank->reminder, 'reminder' => $piggybank->reminder,
'remind_me' => intval($piggybank->remind_me) == 1 || !is_null($piggybank->reminder) ? true : false 'remind_me' => intval($piggybank->remind_me) == 1 || !is_null($piggybank->reminder) ? true : false
]; ];
Session::flash('preFilled', $preFilled); Session::flash('preFilled', $preFilled);
return View::make('piggybanks.edit', compact('piggybank', 'accounts', 'periods', 'preFilled'))->with('title', 'Piggybanks')->with( return View::make('piggybanks.edit', compact('subTitle', 'subTitleIcon', 'piggybank', 'accounts', 'periods', 'preFilled'));
'mainTitleIcon', 'fa-sort-amount-asc'
)->with('subTitle', 'Edit piggy bank "' . e($piggybank->name) . '"')->with('subTitleIcon', 'fa-pencil');
} }
/** /**
@ -161,7 +166,7 @@ class PiggybankController extends BaseController
} }
} }
return View::make('piggybanks.index', compact('piggybanks', 'accounts'))->with('title', 'Piggy banks')->with('mainTitleIcon', 'fa-sort-amount-asc'); return View::make('piggybanks.index', compact('piggybanks', 'accounts'));
} }
/** /**
@ -256,12 +261,9 @@ class PiggybankController extends BaseController
$amountPerReminder = $piggybank->amountPerReminder(); $amountPerReminder = $piggybank->amountPerReminder();
$remindersCount = $piggybank->countFutureReminders(); $remindersCount = $piggybank->countFutureReminders();
$subTitle = e($piggybank->name);
return View::make('piggybanks.show', compact('amountPerReminder', 'remindersCount', 'piggybank', 'events'))->with('title', 'Piggy banks')->with( return View::make('piggybanks.show', compact('amountPerReminder', 'remindersCount', 'piggybank', 'events', 'subTitle'));
'mainTitleIcon', 'fa-sort-amount-asc'
)->with(
'subTitle', $piggybank->name
);
} }

View File

@ -82,7 +82,7 @@ class TestContentSeeder extends Seeder
'name' => 'New camera', 'name' => 'New camera',
'targetamount' => 2000, 'targetamount' => 2000,
'startdate' => Carbon::now()->format('Y-m-d'), 'startdate' => Carbon::now()->format('Y-m-d'),
'targetdate' => '', 'targetdate' => null,
'repeats' => 0, 'repeats' => 0,
'rep_length' => null, 'rep_length' => null,
'rep_every' => 0, 'rep_every' => 0,
@ -94,6 +94,14 @@ class TestContentSeeder extends Seeder
] ]
); );
$piggyBankEvent = PiggyBankEvent::create(['piggybank_id' => 1, 'date' => $startDate->format('Y-m-d'), 'amount' => 100]); $piggyBankEvent = PiggyBankEvent::create(['piggybank_id' => 1, 'date' => $startDate->format('Y-m-d'), 'amount' => 100]);
$piggyBankRepetition = PiggybankRepetition::create(
[
'piggybank_id' => $piggy->id,
'startdate' => Carbon::now()->format('Y-m-d'),
'targetdate' => null,
'currentamount' => 0
]
);
// recurring transaction // recurring transaction
$recurring = \RecurringTransaction::create( $recurring = \RecurringTransaction::create(

View File

@ -55,7 +55,8 @@
<td> <td>
@if(is_null($piggybank->startdate)) @if(is_null($piggybank->startdate))
<em>No start date</em> <em>No start date</em>
@else @endif
@if(!is_string($piggybank->startdate))
{{$piggybank->startdate->format('jS F Y')}} {{$piggybank->startdate->format('jS F Y')}}
@endif @endif
</td> </td>
@ -65,7 +66,8 @@
<td> <td>
@if(is_null($piggybank->targetdate)) @if(is_null($piggybank->targetdate))
<em>No target date</em> <em>No target date</em>
@else @endif
@if(!is_string($piggybank->targetdate))
{{$piggybank->targetdate->format('jS F Y')}} {{$piggybank->targetdate->format('jS F Y')}}
@endif @endif
</td> </td>

View File

@ -0,0 +1,144 @@
<?php
/**
* Class PiggybankControllerCest
*/
class PiggyBankControllerCest
{
/**
* @param FunctionalTester $I
*/
public function _after(FunctionalTester $I)
{
}
/**
* @param FunctionalTester $I
*/
public function _before(FunctionalTester $I)
{
$I->amLoggedAs(['email' => 'thegrumpydictator@gmail.com', 'password' => 'james']);
}
/**
* @param FunctionalTester $I
*/
public function add(FunctionalTester $I)
{
$I->wantTo('add money to a piggy bank');
$I->amOnPage('/piggybanks/add/1');
$I->see('Add money to New camera');
}
/**
* @param FunctionalTester $I
*/
public function create(FunctionalTester $I)
{
$I->wantTo('create a piggy bank');
$I->amOnPage('/piggybanks/create');
$I->see('Create new piggy bank');
}
/**
* @param FunctionalTester $I
*/
public function delete(FunctionalTester $I)
{
$I->wantTo('delete a piggy bank');
$I->amOnPage('/piggybanks/delete/1');
$I->see('Delete &quot;New camera&quot;');
}
/**
* @param FunctionalTester $I
*/
public function destroy(FunctionalTester $I)
{
$I->wantTo('destroy a piggy bank');
$I->amOnPage('/piggybanks/delete/1');
$I->see('Delete &quot;New camera&quot;');
}
/**
* @param FunctionalTester $I
*/
public function edit(FunctionalTester $I)
{
$I->wantTo('edit a piggy bank');
$I->amOnPage('/piggybanks/edit/1');
$I->see('Edit piggy bank "New camera"');
}
/**
* @param FunctionalTester $I
*/
public function index(FunctionalTester $I)
{
$I->wantTo('view all piggy banks');
$I->amOnPage('/piggybanks');
$I->see('Piggy banks');
$I->see('New camera');
}
/**
* @param FunctionalTester $I
*/
public function postAdd(FunctionalTester $I)
{
$I->wantTo('process adding money to a piggy bank');
$I->amOnPage('/piggybanks/add/1');
$I->see('Add money to New camera');
}
/**
* @param FunctionalTester $I
*/
public function postRemove(FunctionalTester $I)
{
$I->wantTo('process removing money from a piggy bank');
$I->amOnPage('/piggybanks/remove/1');
$I->see('Remove money from New camera');
}
/**
* @param FunctionalTester $I
*/
public function remove(FunctionalTester $I)
{
$I->wantTo('removing money from a piggy bank');
$I->amOnPage('/piggybanks/remove/1');
$I->see('Remove money from New camera');
}
/**
* @param FunctionalTester $I
*/
public function show(FunctionalTester $I)
{
$I->wantTo('view a piggy bank');
$I->amOnPage('/piggybanks/show/1');
$I->see('New camera');
}
/**
* @param FunctionalTester $I
*/
public function store(FunctionalTester $I)
{
$I->wantTo('store a new piggy bank');
$I->amOnPage('/piggybanks/create');
$I->see('Create new piggy bank');
}
/**
* @param FunctionalTester $I
*/
public function update(FunctionalTester $I)
{
$I->wantTo('update a piggy bank');
$I->amOnPage('/piggybanks/edit/1');
$I->see('Edit piggy bank "New camera"');
}
}