From ae95d60c4643114506c97960026125f0613edb66 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 17 Aug 2014 12:55:27 +0200 Subject: [PATCH] This should fix all tests, although coverage isn't quite there yet. --- app/controllers/PiggybankController.php | 17 +- .../Trigger/Limits/EloquentLimitTrigger.php | 2 +- .../Piggybanks/EloquentPiggybankTrigger.php | 205 +++++++++--------- .../controllers/BudgetControllerTest.php | 3 + .../controllers/PiggybankControllerTest.php | 56 +---- .../controllers/TransactionControllerTest.php | 6 + app/views/piggybanks/modifyAmount.blade.php | 3 +- 7 files changed, 129 insertions(+), 163 deletions(-) diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index ccabc5d802..89a6a297b2 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -133,16 +133,16 @@ class PiggybankController extends BaseController break; case 'add': $maxAdd = $this->_repository->leftOnAccount($piggyBank->account); - if (round($amount,2) <= round(min($maxAdd, $piggyBank->targetamount),2)) { - Session::flash('success','Amount updated!'); + if (round($amount, 2) <= round(min($maxAdd, $piggyBank->targetamount), 2)) { + Session::flash('success', 'Amount updated!'); $this->_repository->modifyAmount($piggyBank, $amount); } else { - Session::flash('warning','Could not!'); + Session::flash('warning', 'Could not!'); } break; case 'remove': $maxRemove = $piggyBank->currentRelevantRep()->currentamount; - if(round($amount,2) <= round($maxRemove,2)) { + if (round($amount, 2) <= round($maxRemove, 2)) { $this->_repository->modifyAmount($piggyBank, ($amount * -1)); } break; @@ -250,13 +250,4 @@ class PiggybankController extends BaseController } - - /** - * @param Piggybank $piggybank - */ - public function updateAmount(Piggybank $piggybank) - { - Event::fire('piggybanks.change'); - $this->_repository->updateAmount($piggybank, Input::get('amount')); - } } \ No newline at end of file diff --git a/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php b/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php index 84dabe1353..a3d443f586 100644 --- a/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php +++ b/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php @@ -27,7 +27,7 @@ class EloquentLimitTrigger */ public function updateLimitRepetitions() { - if (!\Auth::check()) { + if (!\Auth::check() || is_null(\Auth::user())) { return; } diff --git a/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php b/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php index 54f1888705..9df4a2ee22 100644 --- a/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php +++ b/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php @@ -30,118 +30,121 @@ class EloquentPiggybankTrigger public function updatePiggybankRepetitions() { // grab all piggy banks. - $piggybanks = \Auth::user()->piggybanks()->with(['piggybankrepetitions'])->where('repeats', 0)->get(); - $today = new Carbon; - /** @var \Piggybank $piggy */ - foreach ($piggybanks as $piggy) { - if (count($piggy->piggybankrepetitions) == 0) { + if (\Auth::check()) { + $piggybanks = \Auth::user()->piggybanks()->with(['piggybankrepetitions'])->where('repeats', 0)->get(); + $today = new Carbon; + /** @var \Piggybank $piggy */ + foreach ($piggybanks as $piggy) { + if (count($piggy->piggybankrepetitions) == 0) { + $rep = new \PiggybankRepetition; + $rep->piggybank()->associate($piggy); + $rep->targetdate = $piggy->targetdate; + $rep->startdate = $piggy->startdate; + $rep->currentamount = 0; + try { + $rep->save(); + } catch (QueryException $e) { + } + } + + // whatever we did here, we now have all repetitions for this + // piggy bank, and we can find transactions that fall within + // that repetition (to fix the "saved amount". + $reps = $piggy->piggybankrepetitions()->get(); + /** @var \PiggybankRepetition $rep */ + foreach ($reps as $rep) { + if ($rep->currentamount == 0) { + $query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin( + 'transaction_journals', 'transaction_journals.id', '=', + 'transactions.transaction_journal_id' + ); + if (!is_null($rep->startdate)) { + $query->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d')); + } + if (!is_null($rep->targetdate)) { + $query->where( + 'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d') + ); + } + $sum = $query->sum('transactions.amount'); + $rep->currentamount = floatval($sum); + } + $rep->save(); + + + } + + } + unset($piggy, $piggybanks, $rep); + + // grab all repeated transactions. + $repeatedExpenses = \Auth::user()->piggybanks()->with(['piggybankrepetitions'])->where('repeats', 1)->get(); + /** @var \Piggybank $repeated */ + foreach ($repeatedExpenses as $repeated) { + // loop from start to today or something $rep = new \PiggybankRepetition; - $rep->piggybank()->associate($piggy); - $rep->targetdate = $piggy->targetdate; - $rep->startdate = $piggy->startdate; + $rep->piggybank()->associate($repeated); + $rep->startdate = $repeated->startdate; + $rep->targetdate = $repeated->targetdate; $rep->currentamount = 0; try { $rep->save(); } catch (QueryException $e) { } - } + unset($rep); - // whatever we did here, we now have all repetitions for this - // piggy bank, and we can find transactions that fall within - // that repetition (to fix the "saved amount". - $reps = $piggy->piggybankrepetitions()->get(); - /** @var \PiggybankRepetition $rep */ - foreach ($reps as $rep) { - if ($rep->currentamount == 0) { - $query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin( + if ($repeated->targetdate <= $today) { + // add 1 month to startdate, or maybe X period, like 3 weeks. + $startTarget = clone $repeated->targetdate; + while ($startTarget <= $today) { + $startCurrent = clone $startTarget; + + // add some kind of period to start current making $endCurrent. + $endCurrent = clone $startCurrent; + switch ($repeated->rep_length) { + default: + die('No rep lengt!'); + break; + case 'day': + $endCurrent->addDays($repeated->rep_every); + break; + case 'week': + $endCurrent->addWeeks($repeated->rep_every); + break; + case 'month': + $endCurrent->addMonths($repeated->rep_every); + break; + case 'year': + $endCurrent->addYears($repeated->rep_every); + break; + } + + $rep = new \PiggybankRepetition; + $rep->piggybank()->associate($repeated); + $rep->startdate = $startCurrent; + $rep->targetdate = $endCurrent; + $rep->currentamount = 0; + $startTarget = $endCurrent; + try { + $rep->save(); + } catch (QueryException $e) { + + } + } + } + $reps = $repeated->piggybankrepetitions()->get(); + /** @var \PiggybankRepetition $rep */ + foreach ($reps as $rep) { + $sum = \Transaction::where('piggybank_id', $repeated->id)->leftJoin( 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id' - ); - if (!is_null($rep->startdate)) { - $query->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d')); - } - if (!is_null($rep->targetdate)) { - $query->where( + )->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where( 'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d') - ); - } - $sum = $query->sum('transactions.amount'); + )->sum('transactions.amount'); $rep->currentamount = floatval($sum); + $rep->save(); + + } - $rep->save(); - - - } - - } - unset($piggy, $piggybanks, $rep); - - // grab all repeated transactions. - $repeatedExpenses = \Auth::user()->piggybanks()->with(['piggybankrepetitions'])->where('repeats', 1)->get(); - /** @var \Piggybank $repeated */ - foreach ($repeatedExpenses as $repeated) { - // loop from start to today or something - $rep = new \PiggybankRepetition; - $rep->piggybank()->associate($repeated); - $rep->startdate = $repeated->startdate; - $rep->targetdate = $repeated->targetdate; - $rep->currentamount = 0; - try { - $rep->save(); - } catch (QueryException $e) { - } - unset($rep); - - if ($repeated->targetdate <= $today) { - // add 1 month to startdate, or maybe X period, like 3 weeks. - $startTarget = clone $repeated->targetdate; - while ($startTarget <= $today) { - $startCurrent = clone $startTarget; - - // add some kind of period to start current making $endCurrent. - $endCurrent = clone $startCurrent; - switch ($repeated->rep_length) { - default: - die('No rep lengt!'); - break; - case 'day': - $endCurrent->addDays($repeated->rep_every); - break; - case 'week': - $endCurrent->addWeeks($repeated->rep_every); - break; - case 'month': - $endCurrent->addMonths($repeated->rep_every); - break; - case 'year': - $endCurrent->addYears($repeated->rep_every); - break; - } - - $rep = new \PiggybankRepetition; - $rep->piggybank()->associate($repeated); - $rep->startdate = $startCurrent; - $rep->targetdate = $endCurrent; - $rep->currentamount = 0; - $startTarget = $endCurrent; - try { - $rep->save(); - } catch (QueryException $e) { - - } - } - } - $reps = $repeated->piggybankrepetitions()->get(); - /** @var \PiggybankRepetition $rep */ - foreach ($reps as $rep) { - $sum = \Transaction::where('piggybank_id', $repeated->id)->leftJoin( - 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id' - )->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where( - 'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d') - )->sum('transactions.amount'); - $rep->currentamount = floatval($sum); - $rep->save(); - - } } } diff --git a/app/tests/controllers/BudgetControllerTest.php b/app/tests/controllers/BudgetControllerTest.php index 5434e651e9..217e464ab7 100644 --- a/app/tests/controllers/BudgetControllerTest.php +++ b/app/tests/controllers/BudgetControllerTest.php @@ -233,6 +233,7 @@ class BudgetControllerTest extends TestCase Auth::shouldReceive('check')->andReturn(true); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $this->_repository->shouldReceive('update')->andReturn($budget); + Event::shouldReceive('fire')->with('budgets.change'); $this->action('POST', 'BudgetController@update', $budget->id); $this->assertRedirectedToRoute('budgets.index.budget'); @@ -247,6 +248,8 @@ class BudgetControllerTest extends TestCase Auth::shouldReceive('check')->andReturn(true); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $this->_repository->shouldReceive('update')->andReturn($budget); + Event::shouldReceive('fire')->with('budgets.change'); + //$this->_user->shouldReceive('budgets')->andReturn([]); // trigger $this->action('POST', 'BudgetController@update', [$budget->id, 'from' => 'date']); $this->assertRedirectedToRoute('budgets.index'); diff --git a/app/tests/controllers/PiggybankControllerTest.php b/app/tests/controllers/PiggybankControllerTest.php index 3c3bcebb3c..fc5b883a08 100644 --- a/app/tests/controllers/PiggybankControllerTest.php +++ b/app/tests/controllers/PiggybankControllerTest.php @@ -32,14 +32,6 @@ class PiggybankControllerTest extends TestCase m::close(); } - public function testCreate() - { - $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->once()->andReturn([]); - - $this->action('GET', 'PiggybankController@create'); - $this->assertResponseOk(); - } - public function testDelete() { $piggyBank = f::create('Piggybank'); @@ -67,6 +59,9 @@ class PiggybankControllerTest extends TestCase $piggyBank->account()->first()->user_id ); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + $this->_piggybanks->shouldReceive('destroy')->andReturn(true); + Event::shouldReceive('fire')->with('piggybanks.change'); + $this->action('POST', 'PiggybankController@destroy', $piggyBank->id); $this->assertResponseStatus(302); @@ -103,7 +98,11 @@ class PiggybankControllerTest extends TestCase $three = f::create('Piggybank'); $three->account()->associate($aTwo); $this->_piggybanks->shouldReceive('get')->andReturn([$one, $two, $three]); - $this->_piggybanks->shouldReceive('count')->andReturn(1); + $this->_piggybanks->shouldReceive('countRepeating')->andReturn(0); + $this->_piggybanks->shouldReceive('countNonrepeating')->andReturn(0); + Event::shouldReceive('fire')->with('piggybanks.change'); + + $this->action('GET', 'PiggybankController@index'); $this->assertResponseOk(); } @@ -123,30 +122,7 @@ class PiggybankControllerTest extends TestCase $this->assertResponseOk(); } - public function testStore() - { - $piggyBank = f::create('Piggybank'); - $this->_piggybanks->shouldReceive('store')->andReturn($piggyBank); - $this->action('POST', 'PiggybankController@store'); - $this->assertResponseStatus(302); - } - public function testStoreFails() - { - $piggyBank = f::create('Piggybank'); - unset($piggyBank->amount); - $this->_piggybanks->shouldReceive('store')->andReturn($piggyBank); - $this->action('POST', 'PiggybankController@store'); - $this->assertResponseStatus(302); - } - - public function testStoreRedirect() - { - $piggyBank = f::create('Piggybank'); - $this->_piggybanks->shouldReceive('store')->andReturn($piggyBank); - $this->action('POST', 'PiggybankController@store', ['create' => '1']); - $this->assertResponseStatus(302); - } public function testUpdate() { @@ -161,6 +137,7 @@ class PiggybankControllerTest extends TestCase $piggyBank->account()->first()->user_id ); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + Event::shouldReceive('fire')->with('piggybanks.change'); $this->action('POST', 'PiggybankController@update', $piggyBank->id); $this->assertResponseStatus(302); @@ -180,26 +157,13 @@ class PiggybankControllerTest extends TestCase $piggyBank->account()->first()->user_id ); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + Event::shouldReceive('fire')->with('piggybanks.change'); $this->action('POST', 'PiggybankController@update', $piggyBank->id); $this->assertResponseStatus(302); } - public function testUpdateAmount() - { - $piggyBank = f::create('Piggybank'); - $this->_piggybanks->shouldReceive('updateAmount')->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@updateAmount', $piggyBank->id); - $this->assertResponseOk(); - } } \ No newline at end of file diff --git a/app/tests/controllers/TransactionControllerTest.php b/app/tests/controllers/TransactionControllerTest.php index 6159565ac6..40d7fcb138 100644 --- a/app/tests/controllers/TransactionControllerTest.php +++ b/app/tests/controllers/TransactionControllerTest.php @@ -16,6 +16,7 @@ class TransactionControllerTest extends TestCase protected $_accounts; protected $_budgets; + protected $_piggies; public function setUp() { @@ -26,6 +27,7 @@ class TransactionControllerTest extends TestCase $this->_repository = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'); $this->_accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface'); $this->_budgets = $this->mock('Firefly\Storage\Budget\BudgetRepositoryInterface'); + $this->_piggies = $this->mock('Firefly\Storage\Piggybank\PiggybankRepositoryInterface'); } @@ -38,6 +40,8 @@ class TransactionControllerTest extends TestCase { $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]); $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); + $this->_piggies->shouldReceive('get')->andReturn([]); + $this->action('GET', 'TransactionController@create', ['what' => 'deposit']); $this->assertResponseOk(); } @@ -46,6 +50,7 @@ class TransactionControllerTest extends TestCase { $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]); $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); + $this->_piggies->shouldReceive('get')->andReturn([]); $this->action('GET', 'TransactionController@create', ['what' => 'transfer']); $this->assertResponseOk(); } @@ -54,6 +59,7 @@ class TransactionControllerTest extends TestCase { $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]); $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); + $this->_piggies->shouldReceive('get')->andReturn([]); $this->action('GET', 'TransactionController@create', ['what' => 'withdrawal']); $this->assertResponseOk(); } diff --git a/app/views/piggybanks/modifyAmount.blade.php b/app/views/piggybanks/modifyAmount.blade.php index 122abf461a..776022d1a9 100644 --- a/app/views/piggybanks/modifyAmount.blade.php +++ b/app/views/piggybanks/modifyAmount.blade.php @@ -21,8 +21,7 @@ piggy bank @endif by transferring it from one of your accounts to "{{{$piggybank->account->name}}}". However, - since there is still {{mf($maxAdd)}} on that account not locked in any piggy bank or repeated expense, - you can also add it manually. + since there is still {{mf($maxAdd)}} you can add manually. @else