Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Tests for category controller
James Coled01b480c0d15
diff --git a/app/controllers/CategoryController.php b/app/controllers/CategoryController.phpindex 14c0465076..2e6bcbeccc 100644--- a/app/controllers/CategoryController.php+++ b/app/controllers/CategoryController.php@@ -9,6 +9,7 @@ use Firefly\Storage\Category\CategoryRepositoryInterface as CRI;class CategoryController extends BaseController{protected $_repository;+ protected $_category;public function __construct(CRI $repository, CI $category){@@ -27,9 +28,9 @@ class CategoryController extends BaseControllerreturn View::make('categories.delete')->with('category', $category);}- public function destroy()+ public function destroy(Category $category){- $result = $this->_repository->destroy(Input::get('id'));+ $result = $this->_repository->destroy($category);if ($result === true) {Session::flash('success', 'The category was deleted.');} else {@@ -80,12 +81,19 @@ class CategoryController extends BaseController}}- public function update()+ public function update(Category $category){- $category = $this->_repository->update(Input::all());- Session::flash('success', 'Category "' . $category->name . '" updated.');+ $category = $this->_repository->update($category, Input::all());+ if($category->validate()) {+ Session::flash('success', 'Category "' . $category->name . '" updated.');+ return Redirect::route('categories.index');+ } else {+ Session::flash('success', 'Could not update category "' . $category->name . '".');+ return Redirect::route('categories.edit')->withErrors($category->errors())->withInput();+ }++- return Redirect::route('categories.index');}diff --git a/app/lib/Firefly/Storage/Category/CategoryRepositoryInterface.php b/app/lib/Firefly/Storage/Category/CategoryRepositoryInterface.phpindex ca422ca541..bc3dd36751 100644--- a/app/lib/Firefly/Storage/Category/CategoryRepositoryInterface.php+++ b/app/lib/Firefly/Storage/Category/CategoryRepositoryInterface.php@@ -38,13 +38,13 @@ interface CategoryRepositoryInterface*/public function store($data);- public function update($data);+ public function update($category, $data);/*** @param $data** @return mixed*/- public function destroy($categoryId);+ public function destroy($category);}\ No newline at end of filediff --git a/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php b/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.phpindex 0ab79e6a43..03b425b1c5 100644--- a/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php+++ b/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php@@ -9,19 +9,6 @@ namespace Firefly\Storage\Category;*/class EloquentCategoryRepository implements CategoryRepositoryInterface{- /**- * @return mixed- */- public function get()- {- return \Auth::user()->categories()->orderBy('name', 'ASC')->get();- }-- public function find($categoryId)- {- return \Auth::user()->categories()->find($categoryId);- }-/*** @param $name*@@ -39,6 +26,18 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface}+ public function destroy($category)+ {+ $category->delete();++ return true;+ }++ public function find($categoryId)+ {+ return \Auth::user()->categories()->find($categoryId);+ }+/*** @param $name*@@ -54,6 +53,14 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface}+ /**+ * @return mixed+ */+ public function get()+ {+ return \Auth::user()->categories()->orderBy('name', 'ASC')->get();+ }+/*** @param $name*@@ -70,29 +77,14 @@ class EloquentCategoryRepository implements CategoryRepositoryInterfacereturn $category;}- public function update($data)+ public function update($category, $data){- $category = $this->find($data['id']);- if ($category) {- // update account accordingly:- $category->name = $data['name'];- if ($category->validate()) {- $category->save();- }+ // update account accordingly:+ $category->name = $data['name'];+ if ($category->validate()) {+ $category->save();}return $category;}-- public function destroy($categoryId)- {- $category = $this->find($categoryId);- if ($category) {- $category->delete();-- return true;- }-- return false;- }}\ No newline at end of filediff --git a/app/models/TransactionJournal.php b/app/models/TransactionJournal.phpindex 4afdac6c13..13223996e1 100644--- a/app/models/TransactionJournal.php+++ b/app/models/TransactionJournal.php@@ -55,6 +55,10 @@ use LaravelBook\Ardent\Ardent;* 'Budget[] $budgets* @property-read \Illuminate\Database\Eloquent\Collection|\* 'Category[] $categories+ * @property-read \Illuminate\Database\Eloquent\Collection|\+ * 'Budget[] $budgets+ * @property-read \Illuminate\Database\Eloquent\Collection|\+ * 'Category[] $categories*/class TransactionJournal extends Ardent{diff --git a/app/routes.php b/app/routes.phpindex 66498044fa..53da333c74 100644--- a/app/routes.php+++ b/app/routes.php@@ -82,6 +82,13 @@ Route::group(['before' => 'auth'], function () {Route::get('/budgets/edit/{budget}',['uses' => 'BudgetController@edit', 'as' => 'budgets.edit']);Route::get('/budgets/delete/{budget}',['uses' => 'BudgetController@delete', 'as' => 'budgets.delete']);+ // category controller:+ Route::get('/categories',['uses' => 'CategoryController@index','as' => 'categories.index']);+ Route::get('/categories/create',['uses' => 'CategoryController@create','as' => 'categories.create']);+ Route::get('/categories/show/{category}',['uses' => 'CategoryController@show','as' => 'categories.show']);+ Route::get('/categories/edit/{category}',['uses' => 'CategoryController@edit','as' => 'categories.edit']);+ Route::get('/categories/delete/{category}',['uses' => 'CategoryController@delete','as' => 'categories.delete']);+// home controllerRoute::get('/', ['uses' => 'HomeController@index', 'as' => 'index']);Route::get('/flush', ['uses' => 'HomeController@flush', 'as' => 'flush']);@@ -94,11 +101,7 @@ Route::group(['before' => 'auth'], function () {Route::get('/chart/categories/show/{category}', ['uses' => 'ChartController@categoryShowChart','as' => 'chart.showcategory']);// Categories controller:- Route::get('/categories',['uses' => 'CategoryController@index','as' => 'categories.index']);- Route::get('/categories/create',['uses' => 'CategoryController@create','as' => 'categories.create']);- Route::get('/categories/show/{category}',['uses' => 'CategoryController@show','as' => 'categories.show']);- Route::get('/categories/edit/{category}',['uses' => 'CategoryController@edit','as' => 'categories.edit']);- Route::get('/categories/delete/{category}',['uses' => 'CategoryController@delete','as' => 'categories.delete']);+// piggy bank controllerRoute::get('/piggybanks',['uses' => 'PiggybankController@index','as' => 'piggybanks.index']);@@ -157,16 +160,17 @@ Route::group(['before' => 'csrf|auth'], function () {Route::post('/budgets/update/{budget}', ['uses' => 'BudgetController@update', 'as' => 'budgets.update']);Route::post('/budgets/destroy/{budget}', ['uses' => 'BudgetController@destroy', 'as' => 'budgets.destroy']);+ // category controller+ Route::post('/categories/store',['uses' => 'CategoryController@store', 'as' => 'categories.store']);+ Route::post('/categories/update/{category}', ['uses' => 'CategoryController@update', 'as' => 'categories.update']);+ Route::post('/categories/destroy/{category}', ['uses' => 'CategoryController@destroy', 'as' => 'categories.destroy']);// profile controllerRoute::post('/profile/change-password', ['uses' => 'ProfileController@postChangePassword']);- // category controller- Route::post('/categories/store',['uses' => 'CategoryController@store', 'as' => 'categories.store']);- Route::post('/categories/update', ['uses' => 'CategoryController@update', 'as' => 'categories.update']);- Route::post('/categories/destroy', ['uses' => 'CategoryController@destroy', 'as' => 'categories.destroy']);+// migration controllerRoute::post('/migrate', ['uses' => 'MigrationController@postIndex']);diff --git a/app/tests/controllers/CategoryControllerTest.php b/app/tests/controllers/CategoryControllerTest.phpnew file mode 100644index 0000000000..9f4e8c6e73--- /dev/null+++ b/app/tests/controllers/CategoryControllerTest.php@@ -0,0 +1,181 @@+<?php++use Carbon\Carbon;+use Illuminate\Database\Eloquent\Collection;+use Mockery as m;+use Zizaco\FactoryMuff\Facade\FactoryMuff as f;++/**+ * Class CategoryControllerTest+ */+class CategoryControllerTest extends TestCase+{+ protected $_repository;+ protected $_user;+ protected $_category;++ public function setUp()+ {+ parent::setUp();+ Artisan::call('migrate');+ Artisan::call('db:seed');+ $this->_repository = $this->mock('Firefly\Storage\Category\CategoryRepositoryInterface');+ $this->_category = $this->mock('Firefly\Helper\Controllers\CategoryInterface');+ $this->_user = m::mock('User', 'Eloquent');++ }++ public function tearDown()+ {+ Mockery::close();+ }++ public function testCreate()+ {+ $this->action('GET', 'CategoryController@create');+ $this->assertResponseOk();++ }++ public function testDelete()+ {++ $category = f::create('Category');++ // for successful binding:+ Auth::shouldReceive('user')->andReturn($this->_user);+ Auth::shouldReceive('check')->andReturn(true);+ $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($category->user_id);+ $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email');++ $this->action('GET', 'CategoryController@delete', $category->id);+ $this->assertResponseOk();+ }++ public function testDestroy()+ {+ $category = f::create('Category');++ // for successful binding:+ Auth::shouldReceive('user')->andReturn($this->_user);+ Auth::shouldReceive('check')->andReturn(true);+ $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($category->user_id);+ $this->_repository->shouldReceive('destroy')->once()->andReturn(true);++ $this->action('POST', 'CategoryController@destroy', $category->id);+ $this->assertRedirectedToRoute('categories.index');+ $this->assertSessionHas('success');+ }++ public function testDestroyFails()+ {+ $category = f::create('Category');++ // for successful binding:+ Auth::shouldReceive('user')->andReturn($this->_user);+ Auth::shouldReceive('check')->andReturn(true);+ $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($category->user_id);+ $this->_repository->shouldReceive('destroy')->once()->andReturn(false);++ $this->action('POST', 'CategoryController@destroy', $category->id);+ $this->assertRedirectedToRoute('categories.index');+ $this->assertSessionHas('error');+ }++ public function testEdit()+ {+ $category = f::create('Category');++ // for successful binding.+ Auth::shouldReceive('user')->andReturn($this->_user);+ Auth::shouldReceive('check')->andReturn(true);+ $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($category->user_id);+ $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email');++ $this->action('GET', 'CategoryController@edit', $category->id);+ $this->assertResponseOk();+ }++ public function testIndex()+ {+ $category = f::create('Category');+ $collection = new Collection();++ $this->_repository->shouldReceive('get')->with()->once()->andReturn($collection);+ $this->action('GET', 'CategoryController@index');+ $this->assertResponseOk();+ }++ public function testShow()+ {+ $category = f::create('Category');++ // for successful binding.+ Auth::shouldReceive('user')->andReturn($this->_user);+ Auth::shouldReceive('check')->andReturn(true);+ $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($category->user_id);+ $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($category->email);+ $this->session(['start' => new Carbon, 'end' => new Carbon]);+++++ $this->_category->shouldReceive('journalsInRange')->once()->andReturn([]);+ $this->action('GET', 'CategoryController@show', $category->id);+ $this->assertResponseOk();+ }++ public function testStore()+ {+ $category = f::create('Category');+ $this->_repository->shouldReceive('store')->andReturn($category);+ $this->action('POST', 'CategoryController@store');+ $this->assertRedirectedToRoute('categories.index');+ }++ public function testStoreFails()+ {+ $category = f::create('Category');+ unset($category->id);+ $this->_repository->shouldReceive('store')->andReturn($category);+ $this->action('POST', 'CategoryController@store');+ $this->assertRedirectedToRoute('categories.create');+ }++ public function testStoreRecreate()+ {+ $category = f::create('Category');+ $this->_repository->shouldReceive('store')->andReturn($category);+ $this->action('POST', 'CategoryController@store', ['create' => '1']);+ $this->assertRedirectedToRoute('categories.create');+ }++ public function testUpdate()+ {+ $category = f::create('Category');+ // for successful binding.+ Auth::shouldReceive('user')->andReturn($this->_user);+ Auth::shouldReceive('check')->andReturn(true);+ $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($category->user_id);+ $this->_repository->shouldReceive('update')->andReturn($category);++ $this->action('POST', 'CategoryController@update', $category->id);+ $this->assertRedirectedToRoute('categories.index');++ }++ public function testUpdateFails()+ {+ $category = f::create('Category');+ unset($category->name);+ // for successful binding.+ Auth::shouldReceive('user')->andReturn($this->_user);+ Auth::shouldReceive('check')->andReturn(true);+ $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($category->user_id);+ $this->_repository->shouldReceive('update')->andReturn($category);++ $this->action('POST', 'CategoryController@update', [$category->id]);+ $this->assertResponseStatus(302);++ }+}\ No newline at end of filediff --git a/app/views/categories/delete.blade.php b/app/views/categories/delete.blade.phpindex 4931e0c267..16f7853ae2 100644--- a/app/views/categories/delete.blade.php+++ b/app/views/categories/delete.blade.php@@ -11,8 +11,7 @@</div></div>-{{Form::open(['class' => 'form-horizontal','url' => route('categories.destroy')])}}-{{Form::hidden('id',$category->id)}}+{{Form::open(['class' => 'form-horizontal','url' => route('categories.destroy',$category->id)])}}<div class="row"><div class="col-lg-12 col-md-12 col-sm-12">@if($category->transactionjournals()->count() > 0)diff --git a/app/views/categories/edit.blade.php b/app/views/categories/edit.blade.phpindex 400dc67d55..2026a1c9c9 100644--- a/app/views/categories/edit.blade.php+++ b/app/views/categories/edit.blade.php@@ -9,9 +9,8 @@</div></div>-{{Form::open(['class' => 'form-horizontal','url' => route('categories.update')])}}+{{Form::open(['class' => 'form-horizontal','url' => route('categories.update',$category->id)])}}-{{Form::hidden('id',$category->id)}}<div class="row"><div class="col-lg-6 col-md-12 col-sm-6">