Tests for budget controller.

This commit is contained in:
James Cole 2014-08-02 15:23:29 +02:00
parent 913b5fd267
commit 30b589d040
8 changed files with 352 additions and 190 deletions

View File

@ -13,7 +13,6 @@ class BudgetController extends BaseController
protected $_budgets;
protected $_repository;
/**
* @param BI $budgets
* @param BRI $repository
@ -40,9 +39,10 @@ class BudgetController extends BaseController
return View::make('budgets.delete')->with('budget', $budget);
}
public function destroy()
public function destroy(Budget $budget)
{
$result = $this->_repository->destroy(Input::get('id'));
$result = $this->_repository->destroy($budget);
Event::fire('budgets.change');
if ($result === true) {
Session::flash('success', 'The budget was deleted.');
if (Input::get('from') == 'date') {
@ -82,7 +82,6 @@ class BudgetController extends BaseController
*/
public function indexByDate()
{
Event::fire('budgets.change');
// get a list of dates by getting all repetitions:
$set = $this->_repository->get();
$budgets = $this->_budgets->organizeByDate($set);
@ -129,6 +128,7 @@ class BudgetController extends BaseController
$budget = $this->_repository->store(Input::all());
if ($budget->id) {
Event::fire('budgets.change');
Session::flash('success', 'Budget created!');
if (Input::get('create') == '1') {
@ -143,20 +143,26 @@ class BudgetController extends BaseController
} else {
Session::flash('error', 'Could not save the new budget');
return Redirect::route('budgets.create')->withInput();
return Redirect::route('budgets.create')->withInput()->withErrors($budget->errors());
}
}
public function update()
public function update(Budget $budget)
{
$budget = $this->_repository->update(Input::all());
Session::flash('success', 'Budget "' . $budget->name . '" updated.');
$budget = $this->_repository->update($budget, Input::all());
if ($budget->validate()) {
Session::flash('success', 'Budget "' . $budget->name . '" updated.');
if (Input::get('from') == 'date') {
return Redirect::route('budgets.index');
if (Input::get('from') == 'date') {
return Redirect::route('budgets.index');
} else {
return Redirect::route('budgets.index.budget');
}
} else {
return Redirect::route('budgets.index.budget');
Session::flash('error', 'Could not update budget: ' . $budget->errors()->first());
return Redirect::route('budgets.edit', $budget->id)->withInput()->withErrors($budget->errors());
}
}

View File

@ -12,9 +12,18 @@ use Carbon\Carbon;
interface BudgetRepositoryInterface
{
/**
* @param $data
*
* @return mixed
*/
public function getAsSelectList();
public function destroy(\Budget $budget);
/**
* @param $budgetId
*
* @return mixed
*/
public function find($budgetId);
/**
* @return mixed
@ -22,11 +31,17 @@ interface BudgetRepositoryInterface
public function get();
/**
* @param $data
* @return mixed
*/
public function getAsSelectList();
/**
* @param Carbon $date
* @param $range
*
* @return mixed
*/
public function update($data);
public function getWithRepetitionsInPeriod(Carbon $date, $range);
/**
* @param $data
@ -40,21 +55,6 @@ interface BudgetRepositoryInterface
*
* @return mixed
*/
public function destroy($budgetId);
/**
* @param $budgetId
*
* @return mixed
*/
public function find($budgetId);
/**
* @param Carbon $date
* @param $range
*
* @return mixed
*/
public function getWithRepetitionsInPeriod(Carbon $date, $range);
public function update(\Budget $budget, $data);
}

View File

@ -12,6 +12,13 @@ use Carbon\Carbon;
class EloquentBudgetRepository implements BudgetRepositoryInterface
{
public function destroy(\Budget $budget)
{
$budget->delete();
return true;
}
/**
* @param $budgetId
*
@ -46,37 +53,6 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
return $set;
}
/**
* @param $data
*
* @return mixed
*/
public function update($data)
{
$budget = $this->find($data['id']);
if ($budget) {
// update account accordingly:
$budget->name = $data['name'];
if ($budget->validate()) {
$budget->save();
}
}
return $budget;
}
public function destroy($budgetId)
{
$budget = $this->find($budgetId);
if ($budget) {
$budget->delete();
return true;
}
return false;
}
/**
* @return array|mixed
*/
@ -161,4 +137,20 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
return $budget;
}
/**
* @param $data
*
* @return mixed
*/
public function update(\Budget $budget, $data)
{
// update account accordingly:
$budget->name = $data['name'];
if ($budget->validate()) {
$budget->save();
}
return $budget;
}
}

View File

@ -74,6 +74,13 @@ Route::group(['before' => 'auth'], function () {
Route::get('/accounts/{account}/edit', ['uses' => 'AccountController@edit', 'as' => 'accounts.edit']);
Route::get('/accounts/{account}/delete', ['uses' => 'AccountController@delete', 'as' => 'accounts.delete']);
// budget controller:
Route::get('/budgets',['uses' => 'BudgetController@indexByDate','as' => 'budgets.index']);
Route::get('/budgets/create',['uses' => 'BudgetController@create', 'as' => 'budgets.create']);
Route::get('/budgets/budget',['uses' => 'BudgetController@indexByBudget','as' => 'budgets.index.budget']);
Route::get('/budgets/show/{budget}',['uses' => 'BudgetController@show', 'as' => 'budgets.show']);
Route::get('/budgets/edit/{budget}',['uses' => 'BudgetController@edit', 'as' => 'budgets.edit']);
Route::get('/budgets/delete/{budget}',['uses' => 'BudgetController@delete', 'as' => 'budgets.delete']);
// home controller
Route::get('/', ['uses' => 'HomeController@index', 'as' => 'index']);
@ -113,13 +120,6 @@ Route::group(['before' => 'auth'], function () {
// budget controller:
Route::get('/budgets',['uses' => 'BudgetController@indexByDate','as' => 'budgets.index']);
Route::get('/budgets/create',['uses' => 'BudgetController@create', 'as' => 'budgets.create']);
Route::get('/budgets/budget',['uses' => 'BudgetController@indexByBudget','as' => 'budgets.index.budget']);
Route::get('/budgets/show/{budget}',['uses' => 'BudgetController@show', 'as' => 'budgets.show']);
Route::get('/budgets/edit/{budget}',['uses' => 'BudgetController@edit', 'as' => 'budgets.edit']);
Route::get('/budgets/delete/{budget}',['uses' => 'BudgetController@delete', 'as' => 'budgets.delete']);
// limit controller:
@ -152,14 +152,16 @@ Route::group(['before' => 'csrf|auth'], function () {
Route::post('/accounts/update/{account}', ['uses' => 'AccountController@update', 'as' => 'accounts.update']);
Route::post('/accounts/destroy/{account}', ['uses' => 'AccountController@destroy', 'as' => 'accounts.destroy']);
// budget controller:
Route::post('/budgets/store',['uses' => 'BudgetController@store', 'as' => 'budgets.store']);
Route::post('/budgets/update/{budget}', ['uses' => 'BudgetController@update', 'as' => 'budgets.update']);
Route::post('/budgets/destroy/{budget}', ['uses' => 'BudgetController@destroy', 'as' => 'budgets.destroy']);
// profile controller
Route::post('/profile/change-password', ['uses' => 'ProfileController@postChangePassword']);
// budget controller:
Route::post('/budgets/store/{budget}',['uses' => 'BudgetController@store', 'as' => 'budgets.store']);
Route::post('/budgets/update', ['uses' => 'BudgetController@update', 'as' => 'budgets.update']);
Route::post('/budgets/destroy', ['uses' => 'BudgetController@destroy', 'as' => 'budgets.destroy']);
// category controller
Route::post('/categories/store',['uses' => 'CategoryController@store', 'as' => 'categories.store']);

View File

@ -22,7 +22,6 @@ class AccountControllerTest extends TestCase
$this->_repository = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$this->_accounts = $this->mock('Firefly\Helper\Controllers\AccountInterface');
$this->_user = m::mock('User', 'Eloquent');
// $this->app->instance('User', $this->_user);
}
@ -92,12 +91,6 @@ class AccountControllerTest extends TestCase
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($account->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email');
// Auth::shouldReceive('user')->andReturn($this->_user);
// Auth::shouldReceive('check')->andReturn(true);
// $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($account->user_id);
// $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($account->email);
$this->_accounts->shouldReceive('openingBalanceTransaction')->once()->andReturn(null);
$this->action('GET', 'AccountController@edit', $account->id);
@ -155,15 +148,6 @@ class AccountControllerTest extends TestCase
];
$this->_accounts->shouldReceive('show')->once()->andReturn($data);
//$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email');
// Auth::shouldReceive('user')->andReturn($this->_user);
// Auth::shouldReceive('check')->andReturn(true);
// $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($account->user_id);
// $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($account->email);
// $this->_accounts->shouldReceive('paginate')->with($account,40)->once()->andReturn();
$this->action('GET', 'AccountController@show', $account->id);
$this->assertResponseOk();
}
@ -175,13 +159,6 @@ class AccountControllerTest extends TestCase
$this->action('POST', 'AccountController@store');
$this->assertRedirectedToRoute('accounts.index');
}
public function testStoreRecreate()
{
$account = f::create('Account');
$this->_repository->shouldReceive('store')->andReturn($account);
$this->action('POST', 'AccountController@store',['create' => '1']);
$this->assertRedirectedToRoute('accounts.create');
}
public function testStoreFails()
{
@ -192,6 +169,14 @@ class AccountControllerTest extends TestCase
$this->assertRedirectedToRoute('accounts.create');
}
public function testStoreRecreate()
{
$account = f::create('Account');
$this->_repository->shouldReceive('store')->andReturn($account);
$this->action('POST', 'AccountController@store', ['create' => '1']);
$this->assertRedirectedToRoute('accounts.create');
}
public function testUpdate()
{
$account = f::create('Account');
@ -201,10 +186,11 @@ class AccountControllerTest extends TestCase
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($account->user_id);
$this->_repository->shouldReceive('update')->andReturn($account);
$this->action('POST', 'AccountController@update',$account->id);
$this->action('POST', 'AccountController@update', $account->id);
$this->assertRedirectedToRoute('accounts.index');
}
public function testUpdateFails()
{
$account = f::create('Account');
@ -215,96 +201,8 @@ class AccountControllerTest extends TestCase
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($account->user_id);
$this->_repository->shouldReceive('update')->andReturn($account);
$this->action('POST', 'AccountController@update',$account->id);
$this->assertRedirectedToRoute('accounts.edit',$account->id);
$this->action('POST', 'AccountController@update', $account->id);
$this->assertRedirectedToRoute('accounts.edit', $account->id);
}
//
// public function testIndex()
// {
//// // mock account type(s):
//// $personal = $this->mock('AccountType');
//// $personal->shouldReceive('getAttribute', 'description')->andReturn('Default account');
////
//// $bene = $this->mock('AccountType');
//// $bene->shouldReceive('getAttribute', 'description')->andReturn('Beneficiary account');
////
//// $initial = $this->mock('AccountType');
//// $initial->shouldReceive('getAttribute', 'description')->andReturn('Initial balance account');
////
//// $cash = $this->mock('AccountType');
//// $cash->shouldReceive('getAttribute', 'description')->andReturn('Cash account');
////
////
//// // mock account(s)
//// $one = $this->mock('Account');
//// $one->shouldReceive('getAttribute')->andReturn($personal);
////
//// $two = $this->mock('Account');
//// $two->shouldReceive('getAttribute')->andReturn($bene);
////
//// $three = $this->mock('Account');
//// $three->shouldReceive('getAttribute')->andReturn($initial);
////
//// $four = $this->mock('Account');
//// $four->shouldReceive('getAttribute')->andReturn($cash);
//// $c = new \Illuminate\Database\Eloquent\Collection([$one, $two, $three, $four]);
////
//// // mock account repository:
//// $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
//// $accounts->shouldReceive('get')->andReturn($c);
////
////
//// $list = [
//// 'personal' => [$one],
//// 'beneficiaries' => [$two],
//// 'initial' => [$three],
//// 'cash' => [$four]
//// ];
////
//// // mock:
//// View::shouldReceive('share');
//// View::shouldReceive('make')->with('accounts.index')->once()->andReturn(\Mockery::self())
//// ->shouldReceive('with')->once()->with('accounts', $list)->andReturn(\Mockery::self())
//// ->shouldReceive('with')->once()->with('total', 4)->andReturn(\Mockery::self());
////
//
// // call
// $this->call('GET', '/accounts');
//
// // test
// $this->assertResponseOk();
//
// }
////
//// public function testCreate()
//// {
//// // mock:
//// View::shouldReceive('share');
//// View::shouldReceive('make')->with('accounts.create');
////
//// // call
//// $this->call('GET', '/accounts/create');
////
//// // test
//// $this->assertResponseOk();
//// }
////
//// public function testShow()
//// {
//// // mock account repository:
//// $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
//// $accounts->shouldReceive('get')->with(1)->andReturn([]);
////
//// // call
//// $this->call('GET', '/accounts/1');
////
//// // test
//// $this->assertResponseOk();
//// }
////
}

View File

@ -0,0 +1,266 @@
<?php
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Mockery as m;
use Zizaco\FactoryMuff\Facade\FactoryMuff as f;
class BudgetControllerTest extends TestCase {
protected $_repository;
protected $_user;
protected $_budgets;
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
Artisan::call('db:seed');
$this->_repository = $this->mock('Firefly\Storage\Budget\BudgetRepositoryInterface');
$this->_budgets = $this->mock('Firefly\Helper\Controllers\BudgetInterface');
$this->_user = m::mock('User', 'Eloquent');
}
public function tearDown()
{
Mockery::close();
}
public function testCreate()
{
$this->action('GET', 'BudgetController@create');
$this->assertResponseOk();
}
public function testDelete()
{
$budget = f::create('Budget');
// for successful binding:
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email');
$this->action('GET', 'BudgetController@delete', $budget->id);
$this->assertResponseOk();
}
public function testDestroy()
{
$budget = f::create('Budget');
// for successful binding:
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
Event::shouldReceive('fire')->once()->with('budgets.change');
$this->_repository->shouldReceive('destroy')->once()->andReturn(true);
$this->action('POST', 'BudgetController@destroy', $budget->id);
$this->assertRedirectedToRoute('budgets.index.budget');
$this->assertSessionHas('success');
}
public function testDestroyByDate()
{
$budget = f::create('Budget');
// for successful binding:
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
Event::shouldReceive('fire')->once()->with('budgets.change');
$this->_repository->shouldReceive('destroy')->once()->andReturn(true);
$this->action('POST', 'BudgetController@destroy', [$budget->id,'from' => 'date']);
$this->assertRedirectedToRoute('budgets.index');
$this->assertSessionHas('success');
}
public function testDestroyFails()
{
$budget = f::create('Budget');
// for successful binding:
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
Event::shouldReceive('fire')->once()->with('budgets.change');
$this->_repository->shouldReceive('destroy')->once()->andReturn(false);
$this->action('POST', 'BudgetController@destroy', $budget->id);
$this->assertRedirectedToRoute('budgets.index');
$this->assertSessionHas('error');
}
public function testEdit()
{
$budget = f::create('Budget');
// for successful binding.
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email');
$this->action('GET', 'BudgetController@edit', $budget->id);
$this->assertResponseOk();
}
public function testIndexByBudget()
{
$this->_repository->shouldReceive('get')->once()->andReturn([]);
$this->action('GET', 'BudgetController@indexByBudget');
$this->assertResponseOk();
}
public function testIndexByDate()
{
$collection = new Collection();
$this->_repository->shouldReceive('get')->once()->andReturn($collection);
$this->_budgets->shouldReceive('organizeByDate')->with($collection)->andReturn([]);
$this->action('GET', 'BudgetController@indexByDate');
$this->assertResponseOk();
}
public function testShow()
{
$budget = f::create('Budget');
// for successful binding.
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($budget->email);
$this->session(['start' => new Carbon, 'end' => new Carbon]);
$this->_budgets->shouldReceive('organizeRepetitions')->once()->andReturn([]);
$this->action('GET', 'BudgetController@show', $budget->id);
$this->assertResponseOk();
}
public function testShowNoEnvelope()
{
$budget = f::create('Budget');
// for successful binding.
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($budget->email);
$this->session(['start' => new Carbon, 'end' => new Carbon]);
$this->_budgets->shouldReceive('outsideRepetitions')->once()->andReturn([]);
$this->action('GET', 'BudgetController@show', [$budget->id,'noenvelope' => 'true']);
$this->assertResponseOk();
}
public function testShowWithRep()
{
$budget = f::create('Budget');
// for successful binding.
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($budget->email);
$this->session(['start' => new Carbon, 'end' => new Carbon]);
// $this->_budgets->shouldReceive('show')->once()->andReturn([]);
$arr = [0 => ['limitrepetition' => null, 'limit' => null,'date' => '']];
$this->_budgets->shouldReceive('organizeRepetition')->once()->andReturn($arr);
$this->action('GET', 'BudgetController@show', [$budget->id, 'rep' => '1']);
$this->assertResponseOk();
}
public function testStore()
{
$budget = f::create('Budget');
$this->_repository->shouldReceive('store')->andReturn($budget);
$this->action('POST', 'BudgetController@store');
$this->assertRedirectedToRoute('budgets.index.budget');
}
public function testStoreFromDate()
{
$budget = f::create('Budget');
$this->_repository->shouldReceive('store')->andReturn($budget);
$this->action('POST', 'BudgetController@store',['from' => 'date']);
$this->assertRedirectedToRoute('budgets.index');
}
public function testStoreFails()
{
$budget = f::create('Budget');
unset($budget->id);
$this->_repository->shouldReceive('store')->andReturn($budget);
$this->action('POST', 'BudgetController@store',['from'=>'budget']);
$this->assertRedirectedToRoute('budgets.create');
}
public function testStoreRecreate()
{
$budget = f::create('Budget');
$this->_repository->shouldReceive('store')->andReturn($budget);
$this->action('POST', 'BudgetController@store', ['from' => 'budget','create' => '1']);
$this->assertRedirectedToRoute('budgets.create',['from' => 'budget']);
}
public function testUpdate()
{
$budget = f::create('Budget');
// for successful binding.
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_repository->shouldReceive('update')->andReturn($budget);
$this->action('POST', 'BudgetController@update', $budget->id);
$this->assertRedirectedToRoute('budgets.index.budget');
}
public function testUpdateFromDate()
{
$budget = f::create('Budget');
// for successful binding.
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_repository->shouldReceive('update')->andReturn($budget);
$this->action('POST', 'BudgetController@update', [$budget->id,'from' => 'date']);
$this->assertRedirectedToRoute('budgets.index');
}
public function testUpdateFails()
{
$budget = f::create('Budget');
unset($budget->name);
// for successful binding.
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_repository->shouldReceive('update')->andReturn($budget);
$this->action('POST', 'BudgetController@update', $budget->id);
$this->assertRedirectedToRoute('budgets.edit', $budget->id);
}
}

View File

@ -11,8 +11,7 @@
</div>
</div>
{{Form::open(['class' => 'form-horizontal','url' => route('budgets.destroy')])}}
{{Form::hidden('id',$budget->id)}}
{{Form::open(['class' => 'form-horizontal','url' => route('budgets.destroy',$budget->id)])}}
{{Form::hidden('from',e(Input::get('from')))}}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">

View File

@ -9,9 +9,8 @@
</div>
</div>
{{Form::open(['class' => 'form-horizontal','url' => route('budgets.update')])}}
{{Form::open(['class' => 'form-horizontal','url' => route('budgets.update',$budget->id)])}}
{{Form::hidden('id',$budget->id)}}
{{Form::hidden('from',e(Input::get('from')))}}
<div class="row">