From e9f391b2eb7fe43a4441a2f085f6b6e38c3314f5 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 22 Dec 2014 07:10:18 +0100 Subject: [PATCH] First tests for piggy bank controller. --- app/controllers/PiggybankController.php | 60 ++++---- app/database/seeds/TestContentSeeder.php | 14 +- app/views/piggybanks/show.blade.php | 6 +- tests/functional/PiggyBankControllerCest.php | 144 +++++++++++++++++++ 4 files changed, 190 insertions(+), 34 deletions(-) create mode 100644 tests/functional/PiggyBankControllerCest.php diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index f6983dca05..1c268f9c4f 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -1,5 +1,6 @@ _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); $savedSoFar = $piggybank->currentRelevantRep()->currentamount; $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')); } /** @@ -59,14 +62,12 @@ class PiggybankController extends BaseController /** @var \FireflyIII\Database\Account\Account $acct */ $acct = App::make('FireflyIII\Database\Account\Account'); - $periods = Config::get('firefly.piggybank_periods'); + $periods = Config::get('firefly.piggybank_periods'); + $accounts = FFForm::makeSelectList($acct->getAssetAccounts()); + $subTitle = 'Create new piggy bank'; + $subTitleIcon = 'fa-plus'; - - $accounts = FFForm::makeSelectList($acct->getAssetAccounts()); - - return View::make('piggybanks.create', compact('accounts', 'periods'))->with('title', 'Piggy banks')->with('mainTitleIcon', 'fa-sort-amount-asc')->with( - 'subTitle', 'Create new piggy bank' - )->with('subTitleIcon', 'fa-plus'); + return View::make('piggybanks.create', compact('accounts', 'periods', 'subTitle', 'subTitleIcon')); } /** @@ -76,9 +77,9 @@ class PiggybankController extends BaseController */ public function delete(Piggybank $piggybank) { - return View::make('piggybanks.delete')->with('piggybank', $piggybank)->with('subTitle', 'Delete "' . $piggybank->name . '"')->with( - 'title', 'Piggy banks' - )->with('mainTitleIcon', 'fa-sort-amount-asc'); + $subTitle = 'Delete "' . e($piggybank->name) . '"'; + + return View::make('piggybanks.delete', compact('piggybank', 'subTitle')); } /** @@ -88,10 +89,9 @@ class PiggybankController extends BaseController */ public function destroy(Piggybank $piggyBank) { - /** @var \FireflyIII\Database\PiggyBank\PiggyBank $acct */ - $repos = App::make('FireflyIII\Database\PiggyBank\PiggyBank'); - $repos->destroy($piggyBank); - Session::flash('success', 'Piggy bank deleted.'); + + Session::flash('success', 'Piggy bank "' . e($piggyBank->name) . '" deleted.'); + $this->_repository->destroy($piggyBank); return Redirect::route('piggybanks.index'); } @@ -107,25 +107,30 @@ class PiggybankController extends BaseController /** @var \FireflyIII\Database\Account\Account $acct */ $acct = App::make('FireflyIII\Database\Account\Account'); - $periods = Config::get('firefly.piggybank_periods'); - - $accounts = FFForm::makeSelectList($acct->getAssetAccounts()); + $periods = Config::get('firefly.piggybank_periods'); + $accounts = FFForm::makeSelectList($acct->getAssetAccounts()); + $subTitle = 'Edit piggy bank "' . e($piggybank->name) . '"'; + $subTitleIcon = 'fa-pencil'; /* * 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, 'account_id' => $piggybank->account_id, 'targetamount' => $piggybank->targetamount, - 'targetdate' => !is_null($piggybank->targetdate) ? $piggybank->targetdate->format('Y-m-d') : null, + 'targetdate' => $targetDate, 'reminder' => $piggybank->reminder, 'remind_me' => intval($piggybank->remind_me) == 1 || !is_null($piggybank->reminder) ? true : false ]; Session::flash('preFilled', $preFilled); - return View::make('piggybanks.edit', compact('piggybank', 'accounts', 'periods', 'preFilled'))->with('title', 'Piggybanks')->with( - 'mainTitleIcon', 'fa-sort-amount-asc' - )->with('subTitle', 'Edit piggy bank "' . e($piggybank->name) . '"')->with('subTitleIcon', 'fa-pencil'); + return View::make('piggybanks.edit', compact('subTitle', 'subTitleIcon', 'piggybank', 'accounts', 'periods', 'preFilled')); } /** @@ -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(); $remindersCount = $piggybank->countFutureReminders(); + $subTitle = e($piggybank->name); - return View::make('piggybanks.show', compact('amountPerReminder', 'remindersCount', 'piggybank', 'events'))->with('title', 'Piggy banks')->with( - 'mainTitleIcon', 'fa-sort-amount-asc' - )->with( - 'subTitle', $piggybank->name - ); + return View::make('piggybanks.show', compact('amountPerReminder', 'remindersCount', 'piggybank', 'events', 'subTitle')); } diff --git a/app/database/seeds/TestContentSeeder.php b/app/database/seeds/TestContentSeeder.php index ccdb3a15b3..0d1a8dc8c1 100644 --- a/app/database/seeds/TestContentSeeder.php +++ b/app/database/seeds/TestContentSeeder.php @@ -76,13 +76,13 @@ class TestContentSeeder extends Seeder Component::create(['user_id' => $user->id, 'name' => 'Some Component 7', 'class' => 'Category']); // piggy bank - $piggy = Piggybank::create( + $piggy = Piggybank::create( [ 'account_id' => $savings->id, 'name' => 'New camera', 'targetamount' => 2000, 'startdate' => Carbon::now()->format('Y-m-d'), - 'targetdate' => '', + 'targetdate' => null, 'repeats' => 0, 'rep_length' => null, 'rep_every' => 0, @@ -93,7 +93,15 @@ class TestContentSeeder extends Seeder 'order' => 0, ] ); - $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 = \RecurringTransaction::create( diff --git a/app/views/piggybanks/show.blade.php b/app/views/piggybanks/show.blade.php index 3965d54f19..ea3cef6d53 100644 --- a/app/views/piggybanks/show.blade.php +++ b/app/views/piggybanks/show.blade.php @@ -55,7 +55,8 @@ @if(is_null($piggybank->startdate)) No start date - @else + @endif + @if(!is_string($piggybank->startdate)) {{$piggybank->startdate->format('jS F Y')}} @endif @@ -65,7 +66,8 @@ @if(is_null($piggybank->targetdate)) No target date - @else + @endif + @if(!is_string($piggybank->targetdate)) {{$piggybank->targetdate->format('jS F Y')}} @endif diff --git a/tests/functional/PiggyBankControllerCest.php b/tests/functional/PiggyBankControllerCest.php new file mode 100644 index 0000000000..c3163609d3 --- /dev/null +++ b/tests/functional/PiggyBankControllerCest.php @@ -0,0 +1,144 @@ +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 "New camera"'); + } + + /** + * @param FunctionalTester $I + */ + public function destroy(FunctionalTester $I) + { + $I->wantTo('destroy a piggy bank'); + $I->amOnPage('/piggybanks/delete/1'); + $I->see('Delete "New camera"'); + } + + /** + * @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"'); + } + +} \ No newline at end of file