From c06e2380a521bb305fcac3c301d2b5aa0fdd6031 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 9 Aug 2014 10:01:37 +0200 Subject: [PATCH] Wrote tests for Limit Controller --- app/controllers/LimitController.php | 59 ++--- .../Storage/Limit/EloquentLimitRepository.php | 37 +-- .../Limit/LimitRepositoryInterface.php | 2 + app/models/Limit.php | 2 +- app/routes.php | 4 +- app/tests/controllers/LimitControllerTest.php | 232 ++++++++++++++++++ 6 files changed, 285 insertions(+), 51 deletions(-) create mode 100644 app/tests/controllers/LimitControllerTest.php diff --git a/app/controllers/LimitController.php b/app/controllers/LimitController.php index 2d85bd8b3c..afe2cb66d9 100644 --- a/app/controllers/LimitController.php +++ b/app/controllers/LimitController.php @@ -50,21 +50,19 @@ class LimitController extends BaseController return View::make('limits.delete')->with('limit', $limit); } - public function destroy($limitId) + public function destroy(\Limit $limit) { - $limit = $this->_limits->find($limitId); + $success = $this->_limits->destroy($limit); - - if ($limit) { - $limit->delete(); - - if (Input::get('from') == 'date') { - return Redirect::route('budgets.index'); - } else { - return Redirect::route('budgets.index.budget'); - } + if ($success) { + Session::flash('success', 'The envelope was deleted.'); } else { - return View::make('error')->with('message', 'No such limit!'); + Session::flash('error', 'Could not delete the envelope. Check the logs to be sure.'); + } + if (Input::get('from') == 'date') { + return Redirect::route('budgets.index'); + } else { + return Redirect::route('budgets.index.budget'); } } @@ -106,34 +104,29 @@ class LimitController extends BaseController * * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\View\View */ - public function update($limitId = null) + public function update(\Limit $limit) { /** @var \Limit $limit */ - $limit = $this->_limits->find($limitId); - if ($limit) { - $limit->startdate = new \Carbon\Carbon(Input::get('date')); - $limit->repeat_freq = Input::get('period'); - $limit->repeats = !is_null(Input::get('repeats')) && Input::get('repeats') == '1' ? 1 : 0; - $limit->amount = floatval(Input::get('amount')); - if (!$limit->save()) { - Session::flash('error', 'Could not save new limit: ' . $limit->errors()->first()); + $limit->startdate = new \Carbon\Carbon(Input::get('date')); + $limit->repeat_freq = Input::get('period'); + $limit->repeats = !is_null(Input::get('repeats')) && Input::get('repeats') == '1' ? 1 : 0; + $limit->amount = floatval(Input::get('amount')); + if (!$limit->save()) { + Session::flash('error', 'Could not save new limit: ' . $limit->errors()->first()); - return Redirect::route('budgets.limits.edit', [$limit->id, 'from' => Input::get('from')])->withInput(); + return Redirect::route('budgets.limits.edit', [$limit->id, 'from' => Input::get('from')])->withInput(); + } else { + Session::flash('success', 'Limit saved!'); + foreach ($limit->limitrepetitions()->get() as $rep) { + $rep->delete(); + } + if (Input::get('from') == 'date') { + return Redirect::route('budgets.index'); } else { - Session::flash('success', 'Limit saved!'); - foreach ($limit->limitrepetitions()->get() as $rep) { - $rep->delete(); - } - if (Input::get('from') == 'date') { - return Redirect::route('budgets.index'); - } else { - return Redirect::route('budgets.index.budget'); - } + return Redirect::route('budgets.index.budget'); } } - return View::make('error')->with('message', 'No limit!'); - } diff --git a/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php b/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php index 77b4fb34ea..d244007c13 100644 --- a/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php +++ b/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php @@ -14,6 +14,13 @@ class EloquentLimitRepository implements LimitRepositoryInterface { + public function destroy(\Limit $limit) + { + $limit->delete(); + + return true; + } + /** * @param $limitId * @@ -27,6 +34,21 @@ class EloquentLimitRepository implements LimitRepositoryInterface ->where('components.user_id', \Auth::user()->id)->first(['limits.*']); } + /** + * @param \Budget $budget + * @param Carbon $start + * @param Carbon $end + * + * @return mixed + */ + public function getTJByBudgetAndDateRange(\Budget $budget, Carbon $start, Carbon $end) + { + $result = $budget->transactionjournals()->with('transactions')->after($start)->before($end)->get(); + + return $result; + + } + /** * @param $data * @@ -94,19 +116,4 @@ class EloquentLimitRepository implements LimitRepositoryInterface return $limit; } - /** - * @param \Budget $budget - * @param Carbon $start - * @param Carbon $end - * - * @return mixed - */ - public function getTJByBudgetAndDateRange(\Budget $budget, Carbon $start, Carbon $end) - { - $result = $budget->transactionjournals()->with('transactions')->after($start)->before($end)->get(); - - return $result; - - } - } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php b/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php index 511a276a2b..4502148fe5 100644 --- a/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php @@ -34,4 +34,6 @@ interface LimitRepositoryInterface * @return mixed */ public function find($limitId); + + public function destroy(\Limit $limit); } \ No newline at end of file diff --git a/app/models/Limit.php b/app/models/Limit.php index 9e4d1b34e1..b6fa93871e 100644 --- a/app/models/Limit.php +++ b/app/models/Limit.php @@ -42,7 +42,7 @@ class Limit extends Ardent public static function factory() { - $start = new Carbon\Carbon; + $start = new Carbon; $start->startOfMonth(); return [ diff --git a/app/routes.php b/app/routes.php index 3e7d8062b4..a725884e4e 100644 --- a/app/routes.php +++ b/app/routes.php @@ -190,8 +190,8 @@ Route::group(['before' => 'csrf|auth'], function () { // limit controller: Route::post('/budgets/limits/store/{budget?}', ['uses' => 'LimitController@store', 'as' => 'budgets.limits.store']); - Route::post('/budgets/limits/destroy/{id?}',['uses' => 'LimitController@destroy','as' => 'budgets.limits.destroy']); - Route::post('/budgets/limits/update/{id?}',['uses' => 'LimitController@update','as' => 'budgets.limits.update']); + Route::post('/budgets/limits/destroy/{limit}',['uses' => 'LimitController@destroy','as' => 'budgets.limits.destroy']); + Route::post('/budgets/limits/update/{limit}',['uses' => 'LimitController@update','as' => 'budgets.limits.update']); // piggy bank controller diff --git a/app/tests/controllers/LimitControllerTest.php b/app/tests/controllers/LimitControllerTest.php new file mode 100644 index 0000000000..300268da7b --- /dev/null +++ b/app/tests/controllers/LimitControllerTest.php @@ -0,0 +1,232 @@ +_user = m::mock('User', 'Eloquent'); + $this->_budgets = $this->mock('Firefly\Storage\Budget\BudgetRepositoryInterface'); + $this->_limits = $this->mock('Firefly\Storage\Limit\LimitRepositoryInterface'); + } + + public function tearDown() + { + Mockery::close(); + } + + public function testCreate() + { + $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); + $this->action('GET', 'LimitController@create'); + $this->assertResponseOk(); + } + + public function testDelete() + { + $limit = f::create('Limit'); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($limit->budget()->first()->user_id); + $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email'); + + $this->action('GET', 'LimitController@delete', $limit->id); + $this->assertResponseOk(); + } + + public function testDestroy() + { + $limit = f::create('Limit'); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id); + $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + + $this->_limits->shouldReceive('destroy')->once()->andReturn(true); + + $this->action('POST', 'LimitController@destroy', $limit->id); + $this->assertResponseStatus(302); + } + + public function testDestroyFails() + { + $limit = f::create('Limit'); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id); + $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + + $this->_limits->shouldReceive('destroy')->once()->andReturn(false); + + $this->action('POST', 'LimitController@destroy', $limit->id); + $this->assertResponseStatus(302); + } + + public function testDestroyRedirect() + { + $limit = f::create('Limit'); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id); + $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + + $this->_limits->shouldReceive('destroy')->once()->andReturn(true); + + $this->action('POST', 'LimitController@destroy', [$limit->id, 'from' => 'date']); + $this->assertResponseStatus(302); + } + + public function testEdit() + { + $limit = f::create('Limit'); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + + $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); + + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id); + $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + + $this->action('GET', 'LimitController@edit', $limit->id); + $this->assertResponseOk(); + } + + public function testStore() + { + $limit = f::create('Limit'); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + + $this->_limits->shouldReceive('store')->once()->andReturn($limit); + $this->action('POST', 'LimitController@store'); + $this->assertResponseStatus(302); + } + + public function testStoreFails() + { + $budget = f::create('Budget'); + $limit = f::create('Limit'); + $limit->budget()->associate($budget); + $limit->save(); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + unset($limit->id); + + $this->_limits->shouldReceive('store')->once()->andReturn($limit); + $this->action('POST', 'LimitController@store', [$budget->id, 'from' => 'date']); + $this->assertResponseStatus(302); + } + + public function testStoreRedirect() + { + $budget = f::create('Budget'); + $limit = f::create('Limit'); + $limit->budget()->associate($budget); + $limit->save(); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + + $this->_limits->shouldReceive('store')->once()->andReturn($limit); + $this->action('POST', 'LimitController@store', [$budget->id, 'from' => 'date']); + $this->assertResponseStatus(302); + } + + public function testUpdate() + { + $limit = f::create('Limit'); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id); + $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + + + $this->action( + 'POST', 'LimitController@update', + [$limit->id, + 'date' => '02-02-2012', + 'period' => 'monthly', + 'repeats' => 0, + 'amount' => '0.01' + + ] + ); + $this->assertResponseStatus(302); + } + + public function testUpdateFails() + { + $limit = f::create('Limit'); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id); + $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + + + $this->action( + 'POST', 'LimitController@update', + $limit->id + ); + $this->assertResponseStatus(302); + } + + public function testUpdateRedirect() + { + $limit = f::create('Limit'); + $limitrepetition = f::create('LimitRepetition'); + $limit->limitrepetitions()->save($limitrepetition); + + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id); + $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + + + $this->action( + 'POST', 'LimitController@update', + [$limit->id, + 'date' => '02-02-2012', + 'period' => 'monthly', + 'repeats' => 0, + 'amount' => '0.01', + 'from' => 'date' + + ] + ); + $this->assertResponseStatus(302); + } + + +} \ No newline at end of file