From fbd056104a6ced6a76222d15143b62fa2313a06d Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 10 Aug 2014 11:30:14 +0200 Subject: [PATCH] Some code cleanup and fixes. --- app/controllers/AccountController.php | 13 +++++----- app/controllers/BudgetController.php | 4 +-- app/controllers/CategoryController.php | 4 +-- app/controllers/ChartController.php | 3 ++- app/controllers/HomeController.php | 8 +++--- app/controllers/LimitController.php | 23 +++++++++++------ app/controllers/PiggybankController.php | 25 +++++++++++++------ app/controllers/RecurringController.php | 2 +- app/controllers/TransactionController.php | 10 ++++---- .../Firefly/Helper/Controllers/Category.php | 4 +-- .../Account/EloquentAccountRepository.php | 4 ++- ...EloquentRecurringTransactionRepository.php | 12 ++++++--- .../EloquentTransactionJournalRepository.php | 2 +- .../Storage/User/EloquentUserRepository.php | 1 + app/models/TransactionJournal.php | 8 ++++++ .../controllers/AccountControllerTest.php | 2 +- .../controllers/BudgetControllerTest.php | 25 +++++++++---------- .../controllers/CategoryControllerTest.php | 4 +-- app/tests/controllers/ChartControllerTest.php | 2 +- app/tests/controllers/JsonControllerTest.php | 1 + app/tests/controllers/LimitControllerTest.php | 7 ++++-- .../controllers/PiggybankControllerTest.php | 25 ++++++++++++++++--- .../controllers/PreferencesControllerTest.php | 10 ++++---- .../controllers/ProfileControllerTest.php | 2 +- .../controllers/RecurringControllerTest.php | 19 +++++++------- .../controllers/TransactionControllerTest.php | 2 +- app/tests/models/ModelTest.php | 14 +++++------ 27 files changed, 145 insertions(+), 91 deletions(-) diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index 8c1e5bf966..d773185f71 100644 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -102,12 +102,7 @@ class AccountController extends \BaseController $account = $this->_repository->store(Input::all()); - if (!$account->id) { - // 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 { + if ($account->validate()) { // saved! return to wherever. Session::flash('success', 'Account "' . $account->name . '" created!'); if (Input::get('create') == '1') { @@ -115,6 +110,12 @@ class AccountController extends \BaseController } else { 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(); + } } diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php index c4e6a5ea1f..9ac215d29e 100644 --- a/app/controllers/BudgetController.php +++ b/app/controllers/BudgetController.php @@ -117,7 +117,7 @@ class BudgetController extends BaseController return View::make('budgets.show')->with('budget', $budget)->with('repetitions', $repetitions)->with( 'filters', $filters - )->with('highlight',Input::get('highlight')); + )->with('highlight', Input::get('highlight')); } /** @@ -127,7 +127,7 @@ class BudgetController extends BaseController { $budget = $this->_repository->store(Input::all()); - if ($budget->id) { + if ($budget->validate()) { Event::fire('budgets.change'); Session::flash('success', 'Budget created!'); diff --git a/app/controllers/CategoryController.php b/app/controllers/CategoryController.php index f3d3f1b946..7b5734d65d 100644 --- a/app/controllers/CategoryController.php +++ b/app/controllers/CategoryController.php @@ -68,8 +68,8 @@ class CategoryController extends BaseController public function store() { $category = $this->_repository->store(Input::all()); - if ($category->id) { - Session::flash('success', 'Category created!'); + if ($category->validate()) { + Session::flash('success', 'Category "' . $category->name . '" created!'); if (Input::get('create') == '1') { return Redirect::route('categories.create'); diff --git a/app/controllers/ChartController.php b/app/controllers/ChartController.php index eaddd83f3e..821a57211a 100644 --- a/app/controllers/ChartController.php +++ b/app/controllers/ChartController.php @@ -68,7 +68,8 @@ class ChartController extends BaseController // loop and get array data. $url = count($accounts) == 1 && is_array($accounts) - ? 'View more' : + ? 'View more' + : 'View more'; $data = [ 'chart_title' => count($accounts) == 1 ? $accounts[0]->name : 'All accounts', diff --git a/app/controllers/HomeController.php b/app/controllers/HomeController.php index 2919a7a1be..d1226a950d 100644 --- a/app/controllers/HomeController.php +++ b/app/controllers/HomeController.php @@ -15,10 +15,10 @@ class HomeController extends BaseController protected $_budgets; /** - * @param ARI $accounts - * @param PHI $preferences - * @param TJRI $journal - * @param BRI $budgets + * @param ARI $accounts + * @param PHI $preferences + * @param TJRI $journal + * @param BRI $budgets */ public function __construct(ARI $accounts, PHI $preferences, TJRI $journal, BRI $budgets) { diff --git a/app/controllers/LimitController.php b/app/controllers/LimitController.php index afe2cb66d9..2de1057159 100644 --- a/app/controllers/LimitController.php +++ b/app/controllers/LimitController.php @@ -86,16 +86,19 @@ class LimitController extends BaseController // find a limit with these properties, as we might already have one: $limit = $this->_limits->store(Input::all()); - if ($limit->id) { + if ($limit->validate()) { + Session::flash('success', 'Envelope created!'); if (Input::get('from') == 'date') { return Redirect::route('budgets.index'); } else { return Redirect::route('budgets.index.budget'); } } else { + Session::flash('success', 'Could not save new envelope.'); $budgetId = $budget ? $budget->id : null; - - return Redirect::route('budgets.limits.create', [$budgetId, 'from' => Input::get('from')])->withInput(); + $parameters = [$budgetId, 'from' => Input::get('from')]; + return Redirect::route('budgets.limits.create', $parameters)->withInput() + ->withErrors($limit->errors()); } } @@ -106,16 +109,13 @@ class LimitController extends BaseController */ public function update(\Limit $limit) { + // TODO move logic to repository. /** @var \Limit $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()); - - return Redirect::route('budgets.limits.edit', [$limit->id, 'from' => Input::get('from')])->withInput(); - } else { + if ($limit->save()) { Session::flash('success', 'Limit saved!'); foreach ($limit->limitrepetitions()->get() as $rep) { $rep->delete(); @@ -125,6 +125,13 @@ class LimitController extends BaseController } else { 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()); } } diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index 71c9f7e045..56bd42ad8c 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -82,19 +82,20 @@ class PiggybankController extends BaseController public function store() { $piggyBank = $this->_repository->store(Input::all()); - if (!$piggyBank->id) { - Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first()); - - return Redirect::route('piggybanks.create')->withInput(); - } else { + if ($piggyBank->validate()) { Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!'); - if (Input::get('create') == '1') { return Redirect::route('piggybanks.create')->withInput(); } 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()); - Session::flash('success', 'Piggy bank "' . $piggyBank->name . '" updated.'); + if ($piggyBank->validate()) { + Session::flash('success', 'Piggy bank "' . $piggyBank->name . '" updated.'); + + 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(); + } + - return Redirect::route('piggybanks.index'); } public function updateAmount(Piggybank $piggybank) diff --git a/app/controllers/RecurringController.php b/app/controllers/RecurringController.php index 612bdf7b89..80c87f97b9 100644 --- a/app/controllers/RecurringController.php +++ b/app/controllers/RecurringController.php @@ -60,7 +60,7 @@ class RecurringController extends BaseController public function store() { $recurringTransaction = $this->_repository->store(Input::all()); - if ($recurringTransaction->id) { + if ($recurringTransaction->validate()) { Session::flash('success', 'Recurring transaction "' . $recurringTransaction->name . '" saved!'); if (Input::get('create') == '1') { return Redirect::route('recurring.create')->withInput(); diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index 97d3d5ea37..1eeefa8d1a 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -145,19 +145,19 @@ class TransactionController extends BaseController */ public function store($what) { - $transactionJournal = $this->_repository->store($what, Input::all()); - if ($transactionJournal->id) { - Session::flash('success', 'Transaction "' . $transactionJournal->description . '" saved!'); + $journal = $this->_repository->store($what, Input::all()); + if ($journal->validate()) { + Session::flash('success', 'Transaction "' . $journal->description . '" saved!'); if (Input::get('create') == '1') { return Redirect::route('transactions.create', [$what])->withInput(); } else { return Redirect::route('transactions.index'); } } 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( - $transactionJournal->errors() + $journal->errors() ); } diff --git a/app/lib/Firefly/Helper/Controllers/Category.php b/app/lib/Firefly/Helper/Controllers/Category.php index bed42fcc56..d92fe60f7e 100644 --- a/app/lib/Firefly/Helper/Controllers/Category.php +++ b/app/lib/Firefly/Helper/Controllers/Category.php @@ -10,8 +10,8 @@ class Category implements CategoryInterface public function journalsInRange(\Category $category, Carbon $start, Carbon $end) { return $category->transactionjournals()->with( - ['transactions', 'transactions.account', 'transactiontype', 'components'] - )->orderBy('date', 'DESC')->orderBy('id', 'DESC')->before($end)->after($start)->get(); + ['transactions', 'transactions.account', 'transactiontype', 'components'] + )->orderBy('date', 'DESC')->orderBy('id', 'DESC')->before($end)->after($start)->get(); } } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php index a809e9879e..ee6988d9d4 100644 --- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php +++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php @@ -96,7 +96,9 @@ class EloquentAccountRepository implements AccountRepositoryInterface public function findByName($name, \AccountType $type = null) { $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(); } /** diff --git a/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php b/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php index fe13c980f0..c5591da6d9 100644 --- a/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php +++ b/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php @@ -7,8 +7,10 @@ use Carbon\Carbon; class EloquentRecurringTransactionRepository implements RecurringTransactionRepositoryInterface { - public function destroy(\RecurringTransaction $recurringTransaction) { + public function destroy(\RecurringTransaction $recurringTransaction) + { $recurringTransaction->delete(); + return true; } @@ -27,8 +29,9 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo $recurringTransaction->amount_min = floatval($data['amount_min']); // both amounts zero: - if($recurringTransaction->amount_max == 0 && $recurringTransaction->amount_min == 0) { - $recurringTransaction->errors()->add('amount_max','Amount max and min cannot both be zero.'); + if ($recurringTransaction->amount_max == 0 && $recurringTransaction->amount_min == 0) { + $recurringTransaction->errors()->add('amount_max', 'Amount max and min cannot both be zero.'); + return $recurringTransaction; } @@ -38,9 +41,10 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo $recurringTransaction->skip = isset($data['skip']) ? intval($data['skip']) : 0; $recurringTransaction->repeat_freq = $data['repeat_freq']; - if($recurringTransaction->validate()) { + if ($recurringTransaction->validate()) { $recurringTransaction->save(); } + return $recurringTransaction; } diff --git a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php index 4cae406b77..bf658b1d6f 100644 --- a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php +++ b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php @@ -392,7 +392,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito // do budget: $budget = $budRepository->find($data['budget_id']); - if(!is_null($budget)) { + if (!is_null($budget)) { $journal->budgets()->attach($budget); } diff --git a/app/lib/Firefly/Storage/User/EloquentUserRepository.php b/app/lib/Firefly/Storage/User/EloquentUserRepository.php index a677ae1f22..4d4d804b64 100644 --- a/app/lib/Firefly/Storage/User/EloquentUserRepository.php +++ b/app/lib/Firefly/Storage/User/EloquentUserRepository.php @@ -69,6 +69,7 @@ class EloquentUserRepository implements UserRepositoryInterface if (!$user->save()) { \Log::error('Invalid user'); \Session::flash('error', 'Input invalid, please try again: ' . $user->errors()->first()); + return false; } $user->save(); diff --git a/app/models/TransactionJournal.php b/app/models/TransactionJournal.php index 5643317d2a..472557fd50 100644 --- a/app/models/TransactionJournal.php +++ b/app/models/TransactionJournal.php @@ -63,6 +63,14 @@ 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 + * @property-read \Illuminate\Database\Eloquent\Collection|\ + * 'Budget[] $budgets + * @property-read \Illuminate\Database\Eloquent\Collection|\ + * 'Category[] $categories */ class TransactionJournal extends Ardent { diff --git a/app/tests/controllers/AccountControllerTest.php b/app/tests/controllers/AccountControllerTest.php index 1e879e1b45..3028f5b93a 100644 --- a/app/tests/controllers/AccountControllerTest.php +++ b/app/tests/controllers/AccountControllerTest.php @@ -163,7 +163,7 @@ class AccountControllerTest extends TestCase public function testStoreFails() { $account = f::create('Account'); - unset($account->id); + unset($account->name); $this->_repository->shouldReceive('store')->andReturn($account); $this->action('POST', 'AccountController@store'); $this->assertRedirectedToRoute('accounts.create'); diff --git a/app/tests/controllers/BudgetControllerTest.php b/app/tests/controllers/BudgetControllerTest.php index f781190924..b6fb29507b 100644 --- a/app/tests/controllers/BudgetControllerTest.php +++ b/app/tests/controllers/BudgetControllerTest.php @@ -5,7 +5,8 @@ use Illuminate\Database\Eloquent\Collection; use Mockery as m; use Zizaco\FactoryMuff\Facade\FactoryMuff as f; -class BudgetControllerTest extends TestCase { +class BudgetControllerTest extends TestCase +{ protected $_repository; protected $_user; protected $_budgets; @@ -77,7 +78,7 @@ class BudgetControllerTest extends TestCase { Event::shouldReceive('fire')->once()->with('budgets.change'); $this->_repository->shouldReceive('destroy')->once()->andReturn(true); - $this->action('POST', 'BudgetController@destroy', [$budget->id,'from' => 'date']); + $this->action('POST', 'BudgetController@destroy', [$budget->id, 'from' => 'date']); $this->assertRedirectedToRoute('budgets.index'); $this->assertSessionHas('success'); } @@ -140,7 +141,6 @@ class BudgetControllerTest extends TestCase { $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn($budget->email); - $this->session(['start' => new Carbon, 'end' => new Carbon]); $this->_budgets->shouldReceive('organizeRepetitions')->once()->andReturn([]); @@ -159,11 +159,10 @@ class BudgetControllerTest extends TestCase { $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->action('GET', 'BudgetController@show', [$budget->id, 'noenvelope' => 'true']); $this->assertResponseOk(); } @@ -178,11 +177,10 @@ class BudgetControllerTest extends TestCase { $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' => '']]; + $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(); @@ -195,20 +193,21 @@ class BudgetControllerTest extends TestCase { $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->action('POST', 'BudgetController@store', ['from' => 'date']); $this->assertRedirectedToRoute('budgets.index'); } public function testStoreFails() { $budget = f::create('Budget'); - unset($budget->id); + unset($budget->name); $this->_repository->shouldReceive('store')->andReturn($budget); - $this->action('POST', 'BudgetController@store',['from'=>'budget']); + $this->action('POST', 'BudgetController@store', ['from' => 'budget']); $this->assertRedirectedToRoute('budgets.create'); } @@ -216,8 +215,8 @@ class BudgetControllerTest extends TestCase { { $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']); + $this->action('POST', 'BudgetController@store', ['from' => 'budget', 'create' => '1']); + $this->assertRedirectedToRoute('budgets.create', ['from' => 'budget']); } public function testUpdate() @@ -243,7 +242,7 @@ class BudgetControllerTest extends TestCase { $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->action('POST', 'BudgetController@update', [$budget->id, 'from' => 'date']); $this->assertRedirectedToRoute('budgets.index'); } diff --git a/app/tests/controllers/CategoryControllerTest.php b/app/tests/controllers/CategoryControllerTest.php index 9f4e8c6e73..ba65f938bb 100644 --- a/app/tests/controllers/CategoryControllerTest.php +++ b/app/tests/controllers/CategoryControllerTest.php @@ -118,8 +118,6 @@ class CategoryControllerTest extends TestCase $this->session(['start' => new Carbon, 'end' => new Carbon]); - - $this->_category->shouldReceive('journalsInRange')->once()->andReturn([]); $this->action('GET', 'CategoryController@show', $category->id); $this->assertResponseOk(); @@ -136,7 +134,7 @@ class CategoryControllerTest extends TestCase public function testStoreFails() { $category = f::create('Category'); - unset($category->id); + unset($category->name); $this->_repository->shouldReceive('store')->andReturn($category); $this->action('POST', 'CategoryController@store'); $this->assertRedirectedToRoute('categories.create'); diff --git a/app/tests/controllers/ChartControllerTest.php b/app/tests/controllers/ChartControllerTest.php index c641cc8a7c..0e0d8d802c 100644 --- a/app/tests/controllers/ChartControllerTest.php +++ b/app/tests/controllers/ChartControllerTest.php @@ -86,7 +86,7 @@ class ChartControllerTest extends TestCase $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($account->user_id); $this->_accounts->shouldReceive('findByName')->andReturn($account); - $this->_charts->shouldReceive('accountDailySummary')->once()->andReturn(['rows' => [],'sum' => 0]); + $this->_charts->shouldReceive('accountDailySummary')->once()->andReturn(['rows' => [], 'sum' => 0]); $this->call('GET', 'chart/home/info/' . $account->name . '/01/08/2014'); $this->assertResponseOk(); diff --git a/app/tests/controllers/JsonControllerTest.php b/app/tests/controllers/JsonControllerTest.php index 1949d2516d..5dfca4b3eb 100644 --- a/app/tests/controllers/JsonControllerTest.php +++ b/app/tests/controllers/JsonControllerTest.php @@ -1,5 +1,6 @@ _limits->shouldReceive('store')->once()->andReturn($limit); $this->action('POST', 'LimitController@store'); + $this->assertRedirectedToRoute('budgets.index.budget'); $this->assertResponseStatus(302); } @@ -137,10 +138,12 @@ class LimitControllerTest extends TestCase $limit->save(); $limitrepetition = f::create('LimitRepetition'); $limit->limitrepetitions()->save($limitrepetition); - unset($limit->id); + unset($limit->startdate); + unset($limit->component_id); + $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); } diff --git a/app/tests/controllers/PiggybankControllerTest.php b/app/tests/controllers/PiggybankControllerTest.php index 15466bc956..40f0a1ad90 100644 --- a/app/tests/controllers/PiggybankControllerTest.php +++ b/app/tests/controllers/PiggybankControllerTest.php @@ -99,7 +99,7 @@ class PiggybankControllerTest extends TestCase $two->account()->associate($aOne); $three = f::create('Piggybank'); $three->account()->associate($aTwo); - $this->_piggybanks->shouldReceive('get')->andReturn([$one,$two,$three]); + $this->_piggybanks->shouldReceive('get')->andReturn([$one, $two, $three]); $this->_piggybanks->shouldReceive('count')->andReturn(1); $this->action('GET', 'PiggybankController@index'); $this->assertResponseOk(); @@ -131,7 +131,7 @@ class PiggybankControllerTest extends TestCase public function testStoreFails() { $piggyBank = f::create('Piggybank'); - unset($piggyBank->id); + unset($piggyBank->amount); $this->_piggybanks->shouldReceive('store')->andReturn($piggyBank); $this->action('POST', 'PiggybankController@store'); $this->assertResponseStatus(302); @@ -141,7 +141,7 @@ class PiggybankControllerTest extends TestCase { $piggyBank = f::create('Piggybank'); $this->_piggybanks->shouldReceive('store')->andReturn($piggyBank); - $this->action('POST', 'PiggybankController@store',['create' => '1']); + $this->action('POST', 'PiggybankController@store', ['create' => '1']); $this->assertResponseStatus(302); } @@ -163,6 +163,25 @@ class PiggybankControllerTest extends TestCase $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() { $piggyBank = f::create('Piggybank'); diff --git a/app/tests/controllers/PreferencesControllerTest.php b/app/tests/controllers/PreferencesControllerTest.php index 19b4a38958..ddff2d0702 100644 --- a/app/tests/controllers/PreferencesControllerTest.php +++ b/app/tests/controllers/PreferencesControllerTest.php @@ -35,8 +35,8 @@ class PreferencesControllerTest extends TestCase $viewRange->shouldReceive('getAttribute')->with('data')->andReturn('1M'); $this->_accounts->shouldReceive('getDefault')->andReturn([]); - $this->_helper->shouldReceive('get')->with('viewRange','1M')->andReturn($viewRange); - $this->_helper->shouldReceive('get')->with('frontpageAccounts',[])->andReturn([]); + $this->_helper->shouldReceive('get')->with('viewRange', '1M')->andReturn($viewRange); + $this->_helper->shouldReceive('get')->with('frontpageAccounts', [])->andReturn([]); $this->action('GET', 'PreferencesController@index'); @@ -45,9 +45,9 @@ class PreferencesControllerTest extends TestCase public function testPostIndex() { - $this->_helper->shouldReceive('set')->with('frontpageAccounts',[1]); - $this->_helper->shouldReceive('set')->with('viewRange','1M'); - $this->action('POST', 'PreferencesController@postIndex',['frontpageAccounts' => [1],'viewRange' => '1M']); + $this->_helper->shouldReceive('set')->with('frontpageAccounts', [1]); + $this->_helper->shouldReceive('set')->with('viewRange', '1M'); + $this->action('POST', 'PreferencesController@postIndex', ['frontpageAccounts' => [1], 'viewRange' => '1M']); $this->assertResponseStatus(302); } } \ No newline at end of file diff --git a/app/tests/controllers/ProfileControllerTest.php b/app/tests/controllers/ProfileControllerTest.php index 0688a914a1..37018484ef 100644 --- a/app/tests/controllers/ProfileControllerTest.php +++ b/app/tests/controllers/ProfileControllerTest.php @@ -92,7 +92,7 @@ class ProfileControllerTest extends TestCase $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('password')->andReturn('Blablabla'); - $this->action('POST', 'ProfileController@postChangePassword',['old' => '']); + $this->action('POST', 'ProfileController@postChangePassword', ['old' => '']); $this->assertResponseOk(); } diff --git a/app/tests/controllers/RecurringControllerTest.php b/app/tests/controllers/RecurringControllerTest.php index 93742695a0..3cdcf80c27 100644 --- a/app/tests/controllers/RecurringControllerTest.php +++ b/app/tests/controllers/RecurringControllerTest.php @@ -44,7 +44,7 @@ class RecurringControllerTest extends TestCase $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email'); - $this->action('GET', 'RecurringController@delete',$recurringTransaction->id); + $this->action('GET', 'RecurringController@delete', $recurringTransaction->id); $this->assertResponseOk(); } @@ -59,7 +59,7 @@ class RecurringControllerTest extends TestCase $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_repository->shouldReceive('destroy')->andReturn(true); - $this->action('POST', 'RecurringController@destroy',$recurringTransaction->id); + $this->action('POST', 'RecurringController@destroy', $recurringTransaction->id); $this->assertResponseStatus(302); } @@ -74,7 +74,7 @@ class RecurringControllerTest extends TestCase $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_repository->shouldReceive('destroy')->andReturn(false); - $this->action('POST', 'RecurringController@destroy',$recurringTransaction->id); + $this->action('POST', 'RecurringController@destroy', $recurringTransaction->id); $this->assertResponseStatus(302); } @@ -88,7 +88,7 @@ class RecurringControllerTest extends TestCase $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($recurringTransaction->user_id); $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email'); - $this->action('GET', 'RecurringController@edit',$recurringTransaction->id); + $this->action('GET', 'RecurringController@edit', $recurringTransaction->id); $this->assertResponseOk(); } @@ -112,7 +112,7 @@ class RecurringControllerTest extends TestCase $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); - $this->action('GET', 'RecurringController@show',$recurringTransaction->id); + $this->action('GET', 'RecurringController@show', $recurringTransaction->id); $this->assertResponseOk(); } @@ -130,17 +130,18 @@ class RecurringControllerTest extends TestCase $recurringTransaction = f::create('RecurringTransaction'); $this->_repository->shouldReceive('store')->andReturn($recurringTransaction); - $this->action('POST', 'RecurringController@store',['create' => '1']); + $this->action('POST', 'RecurringController@store', ['create' => '1']); $this->assertResponseStatus(302); } public function testStoreFails() { $recurringTransaction = f::create('RecurringTransaction'); - unset($recurringTransaction->id); + unset($recurringTransaction->active); + unset($recurringTransaction->automatch); $this->_repository->shouldReceive('store')->andReturn($recurringTransaction); - $this->action('POST', 'RecurringController@store',['create' => '1']); + $this->action('POST', 'RecurringController@store', ['create' => '1']); $this->assertResponseStatus(302); } @@ -155,7 +156,7 @@ class RecurringControllerTest extends TestCase $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); - $this->action('POST', 'RecurringController@update',$recurringTransaction->id); + $this->action('POST', 'RecurringController@update', $recurringTransaction->id); $this->assertResponseOk(); } } \ No newline at end of file diff --git a/app/tests/controllers/TransactionControllerTest.php b/app/tests/controllers/TransactionControllerTest.php index c997e3ce0c..49f0be001c 100644 --- a/app/tests/controllers/TransactionControllerTest.php +++ b/app/tests/controllers/TransactionControllerTest.php @@ -242,7 +242,7 @@ class TransactionControllerTest extends TestCase public function testStoreFails() { $journal = f::create('TransactionJournal'); - unset($journal->id); + unset($journal->description); $this->_repository->shouldReceive('store')->andReturn($journal); diff --git a/app/tests/models/ModelTest.php b/app/tests/models/ModelTest.php index 31170fbc2b..41f2fb9f19 100644 --- a/app/tests/models/ModelTest.php +++ b/app/tests/models/ModelTest.php @@ -311,25 +311,25 @@ class ModelTest extends TestCase $journal = f::create('TransactionJournal'); $user->accounts()->save($account); - $this->assertEquals($account->id,$user->accounts()->first()->id); + $this->assertEquals($account->id, $user->accounts()->first()->id); $user->components()->save($comp); - $this->assertEquals($comp->id,$user->components()->first()->id); + $this->assertEquals($comp->id, $user->components()->first()->id); $user->budgets()->save($bud); - $this->assertEquals($bud->id,$user->budgets()->first()->id); + $this->assertEquals($bud->id, $user->budgets()->first()->id); $user->categories()->save($cat); - $this->assertEquals($cat->id,$user->categories()->first()->id); + $this->assertEquals($cat->id, $user->categories()->first()->id); $user->preferences()->save($pref); - $this->assertEquals($pref->id,$user->preferences()->first()->id); + $this->assertEquals($pref->id, $user->preferences()->first()->id); $user->recurringtransactions()->save($rec); - $this->assertEquals($rec->id,$user->recurringtransactions()->first()->id); + $this->assertEquals($rec->id, $user->recurringtransactions()->first()->id); $user->transactionjournals()->save($journal); - $this->assertEquals($journal->id,$user->transactionjournals()->first()->id); + $this->assertEquals($journal->id, $user->transactionjournals()->first()->id); }