Some code cleanup and fixes.

This commit is contained in:
James Cole
2014-08-10 11:30:14 +02:00
parent c50a9b4b04
commit fbd056104a
27 changed files with 145 additions and 91 deletions

View File

@@ -102,12 +102,7 @@ class AccountController extends \BaseController
$account = $this->_repository->store(Input::all()); $account = $this->_repository->store(Input::all());
if (!$account->id) { if ($account->validate()) {
// did not save, return with error:
Session::flash('error', 'Could not save the new account. Please check the form.');
return Redirect::route('accounts.create')->withErrors($account->errors())->withInput();
} else {
// saved! return to wherever. // saved! return to wherever.
Session::flash('success', 'Account "' . $account->name . '" created!'); Session::flash('success', 'Account "' . $account->name . '" created!');
if (Input::get('create') == '1') { if (Input::get('create') == '1') {
@@ -115,6 +110,12 @@ class AccountController extends \BaseController
} else { } else {
return Redirect::route('accounts.index'); return Redirect::route('accounts.index');
} }
} else {
// did not save, return with error:
Session::flash('error', 'Could not save the new account. Please check the form.');
return Redirect::route('accounts.create')->withErrors($account->errors())->withInput();
} }
} }

View File

@@ -127,7 +127,7 @@ class BudgetController extends BaseController
{ {
$budget = $this->_repository->store(Input::all()); $budget = $this->_repository->store(Input::all());
if ($budget->id) { if ($budget->validate()) {
Event::fire('budgets.change'); Event::fire('budgets.change');
Session::flash('success', 'Budget created!'); Session::flash('success', 'Budget created!');

View File

@@ -68,8 +68,8 @@ class CategoryController extends BaseController
public function store() public function store()
{ {
$category = $this->_repository->store(Input::all()); $category = $this->_repository->store(Input::all());
if ($category->id) { if ($category->validate()) {
Session::flash('success', 'Category created!'); Session::flash('success', 'Category "' . $category->name . '" created!');
if (Input::get('create') == '1') { if (Input::get('create') == '1') {
return Redirect::route('categories.create'); return Redirect::route('categories.create');

View File

@@ -68,7 +68,8 @@ class ChartController extends BaseController
// loop and get array data. // loop and get array data.
$url = count($accounts) == 1 && is_array($accounts) $url = count($accounts) == 1 && is_array($accounts)
? '<a href="' . route('accounts.show', [$account->id]) . '">View more</a>' : ? '<a href="' . route('accounts.show', [$account->id]) . '">View more</a>'
:
'<a href="' . route('accounts.index') . '">View more</a>'; '<a href="' . route('accounts.index') . '">View more</a>';
$data = [ $data = [
'chart_title' => count($accounts) == 1 ? $accounts[0]->name : 'All accounts', 'chart_title' => count($accounts) == 1 ? $accounts[0]->name : 'All accounts',

View File

@@ -86,16 +86,19 @@ class LimitController extends BaseController
// find a limit with these properties, as we might already have one: // find a limit with these properties, as we might already have one:
$limit = $this->_limits->store(Input::all()); $limit = $this->_limits->store(Input::all());
if ($limit->id) { if ($limit->validate()) {
Session::flash('success', 'Envelope created!');
if (Input::get('from') == 'date') { if (Input::get('from') == 'date') {
return Redirect::route('budgets.index'); return Redirect::route('budgets.index');
} else { } else {
return Redirect::route('budgets.index.budget'); return Redirect::route('budgets.index.budget');
} }
} else { } else {
Session::flash('success', 'Could not save new envelope.');
$budgetId = $budget ? $budget->id : null; $budgetId = $budget ? $budget->id : null;
$parameters = [$budgetId, 'from' => Input::get('from')];
return Redirect::route('budgets.limits.create', [$budgetId, 'from' => Input::get('from')])->withInput(); return Redirect::route('budgets.limits.create', $parameters)->withInput()
->withErrors($limit->errors());
} }
} }
@@ -106,16 +109,13 @@ class LimitController extends BaseController
*/ */
public function update(\Limit $limit) public function update(\Limit $limit)
{ {
// TODO move logic to repository.
/** @var \Limit $limit */ /** @var \Limit $limit */
$limit->startdate = new \Carbon\Carbon(Input::get('date')); $limit->startdate = new \Carbon\Carbon(Input::get('date'));
$limit->repeat_freq = Input::get('period'); $limit->repeat_freq = Input::get('period');
$limit->repeats = !is_null(Input::get('repeats')) && Input::get('repeats') == '1' ? 1 : 0; $limit->repeats = !is_null(Input::get('repeats')) && Input::get('repeats') == '1' ? 1 : 0;
$limit->amount = floatval(Input::get('amount')); $limit->amount = floatval(Input::get('amount'));
if (!$limit->save()) { 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!'); Session::flash('success', 'Limit saved!');
foreach ($limit->limitrepetitions()->get() as $rep) { foreach ($limit->limitrepetitions()->get() as $rep) {
$rep->delete(); $rep->delete();
@@ -125,6 +125,13 @@ class LimitController extends BaseController
} else { } else {
return Redirect::route('budgets.index.budget'); return Redirect::route('budgets.index.budget');
} }
} else {
Session::flash('error', 'Could not save new limit: ' . $limit->errors()->first());
return Redirect::route('budgets.limits.edit', [$limit->id, 'from' => Input::get('from')])->withInput()
->withErrors($limit->errors());
} }
} }

View File

@@ -82,19 +82,20 @@ class PiggybankController extends BaseController
public function store() public function store()
{ {
$piggyBank = $this->_repository->store(Input::all()); $piggyBank = $this->_repository->store(Input::all());
if (!$piggyBank->id) { if ($piggyBank->validate()) {
Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first());
return Redirect::route('piggybanks.create')->withInput();
} else {
Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!'); Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
if (Input::get('create') == '1') { if (Input::get('create') == '1') {
return Redirect::route('piggybanks.create')->withInput(); return Redirect::route('piggybanks.create')->withInput();
} }
return Redirect::route('piggybanks.index'); return Redirect::route('piggybanks.index');
} else {
Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first());
return Redirect::route('piggybanks.create')->withInput();
} }
} }
@@ -103,9 +104,17 @@ class PiggybankController extends BaseController
{ {
$piggyBank = $this->_repository->update(Input::all()); $piggyBank = $this->_repository->update(Input::all());
if ($piggyBank->validate()) {
Session::flash('success', 'Piggy bank "' . $piggyBank->name . '" updated.'); Session::flash('success', 'Piggy bank "' . $piggyBank->name . '" updated.');
return Redirect::route('piggybanks.index'); return Redirect::route('piggybanks.index');
} else {
Session::flash('error', 'Could not update piggy bank: ' . $piggyBank->errors()->first());
return Redirect::route('piggybanks.edit', $piggyBank->id)->withErrors($piggyBank->errors())->withInput();
}
} }
public function updateAmount(Piggybank $piggybank) public function updateAmount(Piggybank $piggybank)

View File

@@ -60,7 +60,7 @@ class RecurringController extends BaseController
public function store() public function store()
{ {
$recurringTransaction = $this->_repository->store(Input::all()); $recurringTransaction = $this->_repository->store(Input::all());
if ($recurringTransaction->id) { if ($recurringTransaction->validate()) {
Session::flash('success', 'Recurring transaction "' . $recurringTransaction->name . '" saved!'); Session::flash('success', 'Recurring transaction "' . $recurringTransaction->name . '" saved!');
if (Input::get('create') == '1') { if (Input::get('create') == '1') {
return Redirect::route('recurring.create')->withInput(); return Redirect::route('recurring.create')->withInput();

View File

@@ -145,19 +145,19 @@ class TransactionController extends BaseController
*/ */
public function store($what) public function store($what)
{ {
$transactionJournal = $this->_repository->store($what, Input::all()); $journal = $this->_repository->store($what, Input::all());
if ($transactionJournal->id) { if ($journal->validate()) {
Session::flash('success', 'Transaction "' . $transactionJournal->description . '" saved!'); Session::flash('success', 'Transaction "' . $journal->description . '" saved!');
if (Input::get('create') == '1') { if (Input::get('create') == '1') {
return Redirect::route('transactions.create', [$what])->withInput(); return Redirect::route('transactions.create', [$what])->withInput();
} else { } else {
return Redirect::route('transactions.index'); return Redirect::route('transactions.index');
} }
} else { } else {
Session::flash('error', 'Could not save transaction: ' . $transactionJournal->errors()->first()); Session::flash('error', 'Could not save transaction: ' . $journal->errors()->first());
return Redirect::route('transactions.create', [$what])->withInput()->withErrors( return Redirect::route('transactions.create', [$what])->withInput()->withErrors(
$transactionJournal->errors() $journal->errors()
); );
} }

View File

@@ -96,7 +96,9 @@ class EloquentAccountRepository implements AccountRepositoryInterface
public function findByName($name, \AccountType $type = null) public function findByName($name, \AccountType $type = null)
{ {
$type = is_null($type) ? \AccountType::where('description', 'Default account')->first() : $type; $type = is_null($type) ? \AccountType::where('description', 'Default account')->first() : $type;
return \Auth::user()->accounts()->where('account_type_id',$type->id)->where('name', 'like', '%' . $name . '%')->first();
return \Auth::user()->accounts()->where('account_type_id', $type->id)->where('name', 'like', '%' . $name . '%')
->first();
} }
/** /**

View File

@@ -7,8 +7,10 @@ use Carbon\Carbon;
class EloquentRecurringTransactionRepository implements RecurringTransactionRepositoryInterface class EloquentRecurringTransactionRepository implements RecurringTransactionRepositoryInterface
{ {
public function destroy(\RecurringTransaction $recurringTransaction) { public function destroy(\RecurringTransaction $recurringTransaction)
{
$recurringTransaction->delete(); $recurringTransaction->delete();
return true; return true;
} }
@@ -29,6 +31,7 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
// both amounts zero: // both amounts zero:
if ($recurringTransaction->amount_max == 0 && $recurringTransaction->amount_min == 0) { if ($recurringTransaction->amount_max == 0 && $recurringTransaction->amount_min == 0) {
$recurringTransaction->errors()->add('amount_max', 'Amount max and min cannot both be zero.'); $recurringTransaction->errors()->add('amount_max', 'Amount max and min cannot both be zero.');
return $recurringTransaction; return $recurringTransaction;
} }
@@ -41,6 +44,7 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
if ($recurringTransaction->validate()) { if ($recurringTransaction->validate()) {
$recurringTransaction->save(); $recurringTransaction->save();
} }
return $recurringTransaction; return $recurringTransaction;
} }

View File

@@ -69,6 +69,7 @@ class EloquentUserRepository implements UserRepositoryInterface
if (!$user->save()) { if (!$user->save()) {
\Log::error('Invalid user'); \Log::error('Invalid user');
\Session::flash('error', 'Input invalid, please try again: ' . $user->errors()->first()); \Session::flash('error', 'Input invalid, please try again: ' . $user->errors()->first());
return false; return false;
} }
$user->save(); $user->save();

View File

@@ -63,6 +63,14 @@ use LaravelBook\Ardent\Ardent;
* 'Budget[] $budgets * 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\ * @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories * 'Category[] $categories
* @property-read \Illuminate\Database\Eloquent\Collection|\
* '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 class TransactionJournal extends Ardent
{ {

View File

@@ -163,7 +163,7 @@ class AccountControllerTest extends TestCase
public function testStoreFails() public function testStoreFails()
{ {
$account = f::create('Account'); $account = f::create('Account');
unset($account->id); unset($account->name);
$this->_repository->shouldReceive('store')->andReturn($account); $this->_repository->shouldReceive('store')->andReturn($account);
$this->action('POST', 'AccountController@store'); $this->action('POST', 'AccountController@store');
$this->assertRedirectedToRoute('accounts.create'); $this->assertRedirectedToRoute('accounts.create');

View File

@@ -5,7 +5,8 @@ use Illuminate\Database\Eloquent\Collection;
use Mockery as m; use Mockery as m;
use Zizaco\FactoryMuff\Facade\FactoryMuff as f; use Zizaco\FactoryMuff\Facade\FactoryMuff as f;
class BudgetControllerTest extends TestCase { class BudgetControllerTest extends TestCase
{
protected $_repository; protected $_repository;
protected $_user; protected $_user;
protected $_budgets; protected $_budgets;
@@ -140,7 +141,6 @@ class BudgetControllerTest extends TestCase {
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($budget->email); $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($budget->email);
$this->session(['start' => new Carbon, 'end' => new Carbon]); $this->session(['start' => new Carbon, 'end' => new Carbon]);
$this->_budgets->shouldReceive('organizeRepetitions')->once()->andReturn([]); $this->_budgets->shouldReceive('organizeRepetitions')->once()->andReturn([]);
@@ -159,7 +159,6 @@ class BudgetControllerTest extends TestCase {
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($budget->email); $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($budget->email);
$this->session(['start' => new Carbon, 'end' => new Carbon]); $this->session(['start' => new Carbon, 'end' => new Carbon]);
$this->_budgets->shouldReceive('outsideRepetitions')->once()->andReturn([]); $this->_budgets->shouldReceive('outsideRepetitions')->once()->andReturn([]);
@@ -178,7 +177,6 @@ class BudgetControllerTest extends TestCase {
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($budget->email); $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($budget->email);
$this->session(['start' => new Carbon, 'end' => new Carbon]); $this->session(['start' => new Carbon, 'end' => new Carbon]);
// $this->_budgets->shouldReceive('show')->once()->andReturn([]); // $this->_budgets->shouldReceive('show')->once()->andReturn([]);
@@ -195,6 +193,7 @@ class BudgetControllerTest extends TestCase {
$this->action('POST', 'BudgetController@store'); $this->action('POST', 'BudgetController@store');
$this->assertRedirectedToRoute('budgets.index.budget'); $this->assertRedirectedToRoute('budgets.index.budget');
} }
public function testStoreFromDate() public function testStoreFromDate()
{ {
$budget = f::create('Budget'); $budget = f::create('Budget');
@@ -206,7 +205,7 @@ class BudgetControllerTest extends TestCase {
public function testStoreFails() public function testStoreFails()
{ {
$budget = f::create('Budget'); $budget = f::create('Budget');
unset($budget->id); unset($budget->name);
$this->_repository->shouldReceive('store')->andReturn($budget); $this->_repository->shouldReceive('store')->andReturn($budget);
$this->action('POST', 'BudgetController@store', ['from' => 'budget']); $this->action('POST', 'BudgetController@store', ['from' => 'budget']);
$this->assertRedirectedToRoute('budgets.create'); $this->assertRedirectedToRoute('budgets.create');

View File

@@ -118,8 +118,6 @@ class CategoryControllerTest extends TestCase
$this->session(['start' => new Carbon, 'end' => new Carbon]); $this->session(['start' => new Carbon, 'end' => new Carbon]);
$this->_category->shouldReceive('journalsInRange')->once()->andReturn([]); $this->_category->shouldReceive('journalsInRange')->once()->andReturn([]);
$this->action('GET', 'CategoryController@show', $category->id); $this->action('GET', 'CategoryController@show', $category->id);
$this->assertResponseOk(); $this->assertResponseOk();
@@ -136,7 +134,7 @@ class CategoryControllerTest extends TestCase
public function testStoreFails() public function testStoreFails()
{ {
$category = f::create('Category'); $category = f::create('Category');
unset($category->id); unset($category->name);
$this->_repository->shouldReceive('store')->andReturn($category); $this->_repository->shouldReceive('store')->andReturn($category);
$this->action('POST', 'CategoryController@store'); $this->action('POST', 'CategoryController@store');
$this->assertRedirectedToRoute('categories.create'); $this->assertRedirectedToRoute('categories.create');

View File

@@ -1,5 +1,6 @@
<?php <?php
use Zizaco\FactoryMuff\Facade\FactoryMuff as f; use Zizaco\FactoryMuff\Facade\FactoryMuff as f;
/** /**
* Class JsonControllerTest * Class JsonControllerTest
*/ */

View File

@@ -126,6 +126,7 @@ class LimitControllerTest extends TestCase
$this->_limits->shouldReceive('store')->once()->andReturn($limit); $this->_limits->shouldReceive('store')->once()->andReturn($limit);
$this->action('POST', 'LimitController@store'); $this->action('POST', 'LimitController@store');
$this->assertRedirectedToRoute('budgets.index.budget');
$this->assertResponseStatus(302); $this->assertResponseStatus(302);
} }
@@ -137,10 +138,12 @@ class LimitControllerTest extends TestCase
$limit->save(); $limit->save();
$limitrepetition = f::create('LimitRepetition'); $limitrepetition = f::create('LimitRepetition');
$limit->limitrepetitions()->save($limitrepetition); $limit->limitrepetitions()->save($limitrepetition);
unset($limit->id); unset($limit->startdate);
unset($limit->component_id);
$this->_limits->shouldReceive('store')->once()->andReturn($limit); $this->_limits->shouldReceive('store')->once()->andReturn($limit);
$this->action('POST', 'LimitController@store', [$budget->id, 'from' => 'date']); $this->action('POST', 'LimitController@store', $budget->id);
$this->assertResponseStatus(302); $this->assertResponseStatus(302);
} }

View File

@@ -131,7 +131,7 @@ class PiggybankControllerTest extends TestCase
public function testStoreFails() public function testStoreFails()
{ {
$piggyBank = f::create('Piggybank'); $piggyBank = f::create('Piggybank');
unset($piggyBank->id); unset($piggyBank->amount);
$this->_piggybanks->shouldReceive('store')->andReturn($piggyBank); $this->_piggybanks->shouldReceive('store')->andReturn($piggyBank);
$this->action('POST', 'PiggybankController@store'); $this->action('POST', 'PiggybankController@store');
$this->assertResponseStatus(302); $this->assertResponseStatus(302);
@@ -163,6 +163,25 @@ class PiggybankControllerTest extends TestCase
$this->assertResponseStatus(302); $this->assertResponseStatus(302);
} }
public function testUpdateFails()
{
$piggyBank = f::create('Piggybank');
unset($piggyBank->amount);
$this->_piggybanks->shouldReceive('update')->andReturn($piggyBank);
// for binding
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->andReturn(
$piggyBank->account()->first()->user_id
);
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
$this->action('POST', 'PiggybankController@update', $piggyBank->id);
$this->assertResponseStatus(302);
}
public function testUpdateAmount() public function testUpdateAmount()
{ {
$piggyBank = f::create('Piggybank'); $piggyBank = f::create('Piggybank');

View File

@@ -137,7 +137,8 @@ class RecurringControllerTest extends TestCase
public function testStoreFails() public function testStoreFails()
{ {
$recurringTransaction = f::create('RecurringTransaction'); $recurringTransaction = f::create('RecurringTransaction');
unset($recurringTransaction->id); unset($recurringTransaction->active);
unset($recurringTransaction->automatch);
$this->_repository->shouldReceive('store')->andReturn($recurringTransaction); $this->_repository->shouldReceive('store')->andReturn($recurringTransaction);
$this->action('POST', 'RecurringController@store', ['create' => '1']); $this->action('POST', 'RecurringController@store', ['create' => '1']);

View File

@@ -242,7 +242,7 @@ class TransactionControllerTest extends TestCase
public function testStoreFails() public function testStoreFails()
{ {
$journal = f::create('TransactionJournal'); $journal = f::create('TransactionJournal');
unset($journal->id); unset($journal->description);
$this->_repository->shouldReceive('store')->andReturn($journal); $this->_repository->shouldReceive('store')->andReturn($journal);