mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Wrote tests for Limit Controller
This commit is contained in:
		| @@ -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!'); | ||||
|  | ||||
|     } | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -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; | ||||
|  | ||||
|     } | ||||
|  | ||||
| }  | ||||
| @@ -34,4 +34,6 @@ interface LimitRepositoryInterface | ||||
|      * @return mixed | ||||
|      */ | ||||
|     public function find($limitId); | ||||
|  | ||||
|     public function destroy(\Limit $limit); | ||||
| }  | ||||
| @@ -42,7 +42,7 @@ class Limit extends Ardent | ||||
|  | ||||
|     public static function factory() | ||||
|     { | ||||
|         $start = new Carbon\Carbon; | ||||
|         $start = new Carbon; | ||||
|         $start->startOfMonth(); | ||||
|  | ||||
|         return [ | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
							
								
								
									
										232
									
								
								app/tests/controllers/LimitControllerTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										232
									
								
								app/tests/controllers/LimitControllerTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -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); | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user