This should fix all tests, although coverage isn't quite there yet.

This commit is contained in:
James Cole
2014-08-17 12:55:27 +02:00
parent c99a8a6072
commit ae95d60c46
7 changed files with 129 additions and 163 deletions

View File

@@ -133,16 +133,16 @@ class PiggybankController extends BaseController
break; break;
case 'add': case 'add':
$maxAdd = $this->_repository->leftOnAccount($piggyBank->account); $maxAdd = $this->_repository->leftOnAccount($piggyBank->account);
if (round($amount,2) <= round(min($maxAdd, $piggyBank->targetamount),2)) { if (round($amount, 2) <= round(min($maxAdd, $piggyBank->targetamount), 2)) {
Session::flash('success','Amount updated!'); Session::flash('success', 'Amount updated!');
$this->_repository->modifyAmount($piggyBank, $amount); $this->_repository->modifyAmount($piggyBank, $amount);
} else { } else {
Session::flash('warning','Could not!'); Session::flash('warning', 'Could not!');
} }
break; break;
case 'remove': case 'remove':
$maxRemove = $piggyBank->currentRelevantRep()->currentamount; $maxRemove = $piggyBank->currentRelevantRep()->currentamount;
if(round($amount,2) <= round($maxRemove,2)) { if (round($amount, 2) <= round($maxRemove, 2)) {
$this->_repository->modifyAmount($piggyBank, ($amount * -1)); $this->_repository->modifyAmount($piggyBank, ($amount * -1));
} }
break; 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'));
}
} }

View File

@@ -27,7 +27,7 @@ class EloquentLimitTrigger
*/ */
public function updateLimitRepetitions() public function updateLimitRepetitions()
{ {
if (!\Auth::check()) { if (!\Auth::check() || is_null(\Auth::user())) {
return; return;
} }

View File

@@ -30,118 +30,121 @@ class EloquentPiggybankTrigger
public function updatePiggybankRepetitions() public function updatePiggybankRepetitions()
{ {
// grab all piggy banks. // grab all piggy banks.
$piggybanks = \Auth::user()->piggybanks()->with(['piggybankrepetitions'])->where('repeats', 0)->get(); if (\Auth::check()) {
$today = new Carbon; $piggybanks = \Auth::user()->piggybanks()->with(['piggybankrepetitions'])->where('repeats', 0)->get();
/** @var \Piggybank $piggy */ $today = new Carbon;
foreach ($piggybanks as $piggy) { /** @var \Piggybank $piggy */
if (count($piggy->piggybankrepetitions) == 0) { 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 = new \PiggybankRepetition;
$rep->piggybank()->associate($piggy); $rep->piggybank()->associate($repeated);
$rep->targetdate = $piggy->targetdate; $rep->startdate = $repeated->startdate;
$rep->startdate = $piggy->startdate; $rep->targetdate = $repeated->targetdate;
$rep->currentamount = 0; $rep->currentamount = 0;
try { try {
$rep->save(); $rep->save();
} catch (QueryException $e) { } catch (QueryException $e) {
} }
} unset($rep);
// whatever we did here, we now have all repetitions for this if ($repeated->targetdate <= $today) {
// piggy bank, and we can find transactions that fall within // add 1 month to startdate, or maybe X period, like 3 weeks.
// that repetition (to fix the "saved amount". $startTarget = clone $repeated->targetdate;
$reps = $piggy->piggybankrepetitions()->get(); while ($startTarget <= $today) {
/** @var \PiggybankRepetition $rep */ $startCurrent = clone $startTarget;
foreach ($reps as $rep) {
if ($rep->currentamount == 0) { // add some kind of period to start current making $endCurrent.
$query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin( $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' 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
); )->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where(
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') 'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
); )->sum('transactions.amount');
}
$sum = $query->sum('transactions.amount');
$rep->currentamount = floatval($sum); $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();
} }
} }
} }

View File

@@ -233,6 +233,7 @@ class BudgetControllerTest extends TestCase
Auth::shouldReceive('check')->andReturn(true); Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_repository->shouldReceive('update')->andReturn($budget); $this->_repository->shouldReceive('update')->andReturn($budget);
Event::shouldReceive('fire')->with('budgets.change');
$this->action('POST', 'BudgetController@update', $budget->id); $this->action('POST', 'BudgetController@update', $budget->id);
$this->assertRedirectedToRoute('budgets.index.budget'); $this->assertRedirectedToRoute('budgets.index.budget');
@@ -247,6 +248,8 @@ class BudgetControllerTest extends TestCase
Auth::shouldReceive('check')->andReturn(true); Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_repository->shouldReceive('update')->andReturn($budget); $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->action('POST', 'BudgetController@update', [$budget->id, 'from' => 'date']);
$this->assertRedirectedToRoute('budgets.index'); $this->assertRedirectedToRoute('budgets.index');

View File

@@ -32,14 +32,6 @@ class PiggybankControllerTest extends TestCase
m::close(); m::close();
} }
public function testCreate()
{
$this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->once()->andReturn([]);
$this->action('GET', 'PiggybankController@create');
$this->assertResponseOk();
}
public function testDelete() public function testDelete()
{ {
$piggyBank = f::create('Piggybank'); $piggyBank = f::create('Piggybank');
@@ -67,6 +59,9 @@ class PiggybankControllerTest extends TestCase
$piggyBank->account()->first()->user_id $piggyBank->account()->first()->user_id
); );
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $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->action('POST', 'PiggybankController@destroy', $piggyBank->id);
$this->assertResponseStatus(302); $this->assertResponseStatus(302);
@@ -103,7 +98,11 @@ class PiggybankControllerTest extends TestCase
$three = f::create('Piggybank'); $three = f::create('Piggybank');
$three->account()->associate($aTwo); $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->_piggybanks->shouldReceive('countRepeating')->andReturn(0);
$this->_piggybanks->shouldReceive('countNonrepeating')->andReturn(0);
Event::shouldReceive('fire')->with('piggybanks.change');
$this->action('GET', 'PiggybankController@index'); $this->action('GET', 'PiggybankController@index');
$this->assertResponseOk(); $this->assertResponseOk();
} }
@@ -123,30 +122,7 @@ class PiggybankControllerTest extends TestCase
$this->assertResponseOk(); $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() public function testUpdate()
{ {
@@ -161,6 +137,7 @@ class PiggybankControllerTest extends TestCase
$piggyBank->account()->first()->user_id $piggyBank->account()->first()->user_id
); );
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
Event::shouldReceive('fire')->with('piggybanks.change');
$this->action('POST', 'PiggybankController@update', $piggyBank->id); $this->action('POST', 'PiggybankController@update', $piggyBank->id);
$this->assertResponseStatus(302); $this->assertResponseStatus(302);
@@ -180,26 +157,13 @@ class PiggybankControllerTest extends TestCase
$piggyBank->account()->first()->user_id $piggyBank->account()->first()->user_id
); );
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
Event::shouldReceive('fire')->with('piggybanks.change');
$this->action('POST', 'PiggybankController@update', $piggyBank->id); $this->action('POST', 'PiggybankController@update', $piggyBank->id);
$this->assertResponseStatus(302); $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();
}
} }

View File

@@ -16,6 +16,7 @@ class TransactionControllerTest extends TestCase
protected $_accounts; protected $_accounts;
protected $_budgets; protected $_budgets;
protected $_piggies;
public function setUp() public function setUp()
{ {
@@ -26,6 +27,7 @@ class TransactionControllerTest extends TestCase
$this->_repository = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'); $this->_repository = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$this->_accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface'); $this->_accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$this->_budgets = $this->mock('Firefly\Storage\Budget\BudgetRepositoryInterface'); $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->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]);
$this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]);
$this->_piggies->shouldReceive('get')->andReturn([]);
$this->action('GET', 'TransactionController@create', ['what' => 'deposit']); $this->action('GET', 'TransactionController@create', ['what' => 'deposit']);
$this->assertResponseOk(); $this->assertResponseOk();
} }
@@ -46,6 +50,7 @@ class TransactionControllerTest extends TestCase
{ {
$this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]); $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]);
$this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]);
$this->_piggies->shouldReceive('get')->andReturn([]);
$this->action('GET', 'TransactionController@create', ['what' => 'transfer']); $this->action('GET', 'TransactionController@create', ['what' => 'transfer']);
$this->assertResponseOk(); $this->assertResponseOk();
} }
@@ -54,6 +59,7 @@ class TransactionControllerTest extends TestCase
{ {
$this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]); $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]);
$this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]);
$this->_piggies->shouldReceive('get')->andReturn([]);
$this->action('GET', 'TransactionController@create', ['what' => 'withdrawal']); $this->action('GET', 'TransactionController@create', ['what' => 'withdrawal']);
$this->assertResponseOk(); $this->assertResponseOk();
} }

View File

@@ -21,8 +21,7 @@
piggy bank piggy bank
@endif @endif
by transferring it from one of your accounts to "{{{$piggybank->account->name}}}". However, 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, since there is still {{mf($maxAdd)}} you can add manually.
you can also add it manually.
@else @else