Tests for category controller

This commit is contained in:
James Cole
2014-08-02 15:54:39 +02:00
parent 30b589d040
commit d01b480c0d
8 changed files with 241 additions and 54 deletions

View File

@@ -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 BaseController
return 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');
}

View File

@@ -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);
}

View File

@@ -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 CategoryRepositoryInterface
return $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;
}
}

View File

@@ -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
{

View File

@@ -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 controller
Route::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 controller
Route::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 controller
Route::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 controller
Route::post('/migrate', ['uses' => 'MigrationController@postIndex']);

View File

@@ -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);
}
}

View File

@@ -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)

View File

@@ -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">