Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Wrote tests for Limit Controller
James Colec06e2380a521
diff --git a/app/controllers/LimitController.php b/app/controllers/LimitController.phpindex 2d85bd8b3c..afe2cb66d9 100644--- a/app/controllers/LimitController.php+++ b/app/controllers/LimitController.php@@ -50,21 +50,19 @@ class LimitController extends BaseControllerreturn 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());-- return Redirect::route('budgets.limits.edit', [$limit->id, 'from' => Input::get('from')])->withInput();+ $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();+ } 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.phpindex 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 LimitRepositoryInterfacereturn $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 filediff --git a/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php b/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.phpindex 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 filediff --git a/app/models/Limit.php b/app/models/Limit.phpindex 9e4d1b34e1..b6fa93871e 100644--- a/app/models/Limit.php+++ b/app/models/Limit.php@@ -42,7 +42,7 @@ class Limit extends Ardentpublic static function factory(){- $start = new Carbon\Carbon;+ $start = new Carbon;$start->startOfMonth();return [diff --git a/app/routes.php b/app/routes.phpindex 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 controllerdiff --git a/app/tests/controllers/LimitControllerTest.php b/app/tests/controllers/LimitControllerTest.phpnew file mode 100644index 0000000000..300268da7b--- /dev/null+++ b/app/tests/controllers/LimitControllerTest.php@@ -0,0 +1,232 @@+<?php+use Mockery as m;+use Zizaco\FactoryMuff\Facade\FactoryMuff as f;++/**+ * Class LimitControllerTest+ */+class LimitControllerTest extends TestCase+{++ protected $_budgets;+ protected $_limits;+ protected $_user;++ public function setUp()+ {+ parent::setUp();+ Artisan::call('migrate');+ Artisan::call('db:seed');+ $this->_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