diff --git a/app/controllers/LimitController.php b/app/controllers/LimitController.php index e722a0d57f..d2906b6d36 100644 --- a/app/controllers/LimitController.php +++ b/app/controllers/LimitController.php @@ -110,6 +110,7 @@ class LimitController extends BaseController Session::flash('success', 'Could not save new envelope.'); $budgetId = $budget ? $budget->id : null; $parameters = [$budgetId, 'from' => Input::get('from')]; + return Redirect::route('budgets.limits.create', $parameters)->withInput() ->withErrors($limit->errors()); } diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index 89a6a297b2..f6753e83ae 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -122,10 +122,12 @@ class PiggybankController extends BaseController /** * @param Piggybank $piggyBank + * + * @return \Illuminate\Http\RedirectResponse + * @throws Firefly\Exception\FireflyException */ public function modMoney(Piggybank $piggyBank) { - var_dump(Input::all()); $amount = floatval(Input::get('amount')); switch (Input::get('what')) { default: @@ -141,9 +143,13 @@ class PiggybankController extends BaseController } break; case 'remove': - $maxRemove = $piggyBank->currentRelevantRep()->currentamount; + $rep = $piggyBank->currentRelevantRep(); + $maxRemove = $rep->currentamount; if (round($amount, 2) <= round($maxRemove, 2)) { + Session::flash('success', 'Amount updated!'); $this->_repository->modifyAmount($piggyBank, ($amount * -1)); + } else { + Session::flash('warning', 'Could not!'); } break; } diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index 0fb9d2bda5..010625c107 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -45,7 +45,7 @@ class TransactionController extends BaseController return View::make('transactions.create')->with('accounts', $accounts)->with('budgets', $budgets)->with( 'what', $what - )->with('piggies',$piggies); + )->with('piggies', $piggies); } /** diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index c0e91cec41..451488b22b 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -48,10 +48,12 @@ class UserController extends BaseController $result = Auth::attempt($data, $rememberMe); if ($result) { Session::flash('success', 'Logged in!'); + return Redirect::route('index'); } Session::flash('error', 'No good!'); + return View::make('user.login'); } diff --git a/app/models/Account.php b/app/models/Account.php index 8d6bc2d00b..8b5f787e14 100644 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -21,6 +21,7 @@ use LaravelBook\Ardent\Ardent as Ardent; * @method static \Illuminate\Database\Query\Builder|\Account whereAccountTypeId($value) * @method static \Illuminate\Database\Query\Builder|\Account whereName($value) * @method static \Illuminate\Database\Query\Builder|\Account whereActive($value) + * @property-read \Illuminate\Database\Eloquent\Collection|\Piggybank[] $piggybanks */ class Account extends Ardent { diff --git a/app/models/Piggybank.php b/app/models/Piggybank.php index 25dff01b90..a8392c9acd 100644 --- a/app/models/Piggybank.php +++ b/app/models/Piggybank.php @@ -5,38 +5,38 @@ use LaravelBook\Ardent\Ardent as Ardent; /** * Piggybank * - * @property integer $id - * @property \Carbon\Carbon $created_at - * @property \Carbon\Carbon $updated_at - * @property integer $account_id - * @property string $name - * @property float $targetamount - * @property \Carbon\Carbon $targetdate - * @property \Carbon\Carbon $startdate - * @property boolean $repeats - * @property string $rep_length - * @property integer $rep_every - * @property integer $rep_times - * @property string $reminder - * @property integer $reminder_skip - * @property integer $order - * @property-read \Account $account + * @property integer $id + * @property \Carbon\Carbon $created_at + * @property \Carbon\Carbon $updated_at + * @property integer $account_id + * @property string $name + * @property float $targetamount + * @property \Carbon\Carbon $targetdate + * @property \Carbon\Carbon $startdate + * @property boolean $repeats + * @property string $rep_length + * @property integer $rep_every + * @property integer $rep_times + * @property string $reminder + * @property integer $reminder_skip + * @property integer $order + * @property-read \Account $account * @property-read \Illuminate\Database\Eloquent\Collection|\PiggybankRepetition[] $piggybankrepetitions - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereId($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereCreatedAt($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereUpdatedAt($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereAccountId($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereName($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetamount($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetdate($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereStartdate($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepeats($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepLength($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepEvery($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepTimes($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminder($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminderSkip($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereOrder($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereId($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereCreatedAt($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereUpdatedAt($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereAccountId($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereName($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetamount($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetdate($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereStartdate($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepeats($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepLength($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepEvery($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepTimes($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminder($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminderSkip($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereOrder($value) */ class Piggybank extends Ardent { @@ -77,15 +77,16 @@ class Piggybank extends Ardent public static function factory() { $start = new Carbon; - $start->endOfMonth(); - $today = new Carbon; + $start->startOfMonth(); + $end = new Carbon; + $end->endOfMonth(); return [ 'account_id' => 'factory|Account', 'name' => 'string', - 'targetamount' => 'required|min:0', - 'startdate' => $today, - 'targetdate' => $start, + 'targetamount' => 'integer', + 'startdate' => $start->format('Y-m-d'), + 'targetdate' => $end->format('Y-m-d'), 'repeats' => 0, 'rep_length' => null, 'rep_times' => 0, @@ -109,7 +110,7 @@ class Piggybank extends Ardent */ public function getDates() { - return ['created_at', 'updated_at', 'targetdate','startdate']; + return ['created_at', 'updated_at', 'targetdate', 'startdate']; } /** @@ -119,59 +120,58 @@ class Piggybank extends Ardent */ public function nextReminderDate() { - if(is_null($this->reminder)) { + if (is_null($this->reminder)) { return null; } /** @var \PiggybankRepetition $rep */ $rep = $this->currentRelevantRep(); - if($rep) { - $today = new Carbon; - if(is_null($rep->startdate)) { - switch($this->reminder) { + $today = new Carbon; + if ($rep && is_null($rep->startdate)) { + switch ($this->reminder) { + case 'day': + return $today; + break; + case 'week': + return $today->endOfWeek(); + break; + case 'month': + return $today->endOfMonth(); + break; + case 'year': + return $today->endOfYear(); + break; + + } + return null; + } + if ($rep && !is_null($rep->startdate)) { + // start with the start date + // when its bigger than today, return it: + $start = clone $rep->startdate; + while ($start <= $today) { + switch ($this->reminder) { + default: + return null; + break; case 'day': - return $today; + $start->addDay(); break; case 'week': - return $today->endOfWeek(); + $start->addWeek(); break; case 'month': - return $today->endOfMonth(); + $start->addMonth(); break; case 'year': - return $today->endOfYear(); + $start->addYear(); break; } - } else { - // start with the start date - // when its bigger than today, return it: - $start = clone $rep->startdate; - while($start <= $today) { - switch($this->reminder) { - case 'day': - $start->addDay(); - break; - case 'week': - $start->addWeek(); - break; - case 'month': - $start->addMonth(); - break; - case 'year': - $start->addYear(); - break; - - } - } - return $start; } - // if start date is null, simple switch on - // the reminder period (if any) and go to the end of the period. - // otherwise, keep jumping until we are past today. + return $start; } - return new Carbon; } @@ -180,19 +180,26 @@ class Piggybank extends Ardent * * @returns \PiggybankRepetition */ - public function currentRelevantRep() { - return $this->piggybankrepetitions() - ->where(function ($q) { + public function currentRelevantRep() + { + $query = $this->piggybankrepetitions() + ->where( + function ($q) { $today = new Carbon; $q->whereNull('startdate'); - $q->orWhere('startdate','<=',$today->format('Y-m-d')); - }) - ->where(function ($q) { + $q->orWhere('startdate', '<=', $today->format('Y-m-d')); + } + ) + ->where( + function ($q) { $today = new Carbon; $q->whereNull('targetdate'); - $q->orWhere('targetdate','>=',$today->format('Y-m-d')); - }) - ->first(); + $q->orWhere('targetdate', '>=', $today->format('Y-m-d')); + } + ); + $result = $query->first(); + + return $result; } diff --git a/app/models/PiggybankRepetition.php b/app/models/PiggybankRepetition.php index d733b3dc84..9470881eaa 100644 --- a/app/models/PiggybankRepetition.php +++ b/app/models/PiggybankRepetition.php @@ -6,13 +6,13 @@ use LaravelBook\Ardent\Ardent as Ardent; /** * Class PiggybankRepetition * - * @property integer $id - * @property \Carbon\Carbon $created_at - * @property \Carbon\Carbon $updated_at - * @property integer $piggybank_id - * @property \Carbon\Carbon $targetdate - * @property \Carbon\Carbon $startdate - * @property float $currentamount + * @property integer $id + * @property \Carbon\Carbon $created_at + * @property \Carbon\Carbon $updated_at + * @property integer $piggybank_id + * @property \Carbon\Carbon $targetdate + * @property \Carbon\Carbon $startdate + * @property float $currentamount * @property-read \Piggybank $piggybank * @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereId($value) * @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereCreatedAt($value) @@ -37,12 +37,15 @@ class PiggybankRepetition extends Ardent */ public static function factory() { - $date = new Carbon; + $start = new Carbon; + $start->startOfMonth(); + $end = new Carbon; + $end->endOfMonth(); return [ 'piggybank_id' => 'factory|Piggybank', - 'targetdate' => $date, - 'startdate' => $date, + 'startdate' => $start->format('Y-m-d'), + 'targetdate' => $end->format('Y-m-d'), 'currentamount' => 200 ]; } diff --git a/app/models/Transaction.php b/app/models/Transaction.php index 29bc4e00a4..526124decd 100644 --- a/app/models/Transaction.php +++ b/app/models/Transaction.php @@ -25,6 +25,9 @@ use LaravelBook\Ardent\Ardent; * @method static \Illuminate\Database\Query\Builder|\Transaction whereTransactionJournalId($value) * @method static \Illuminate\Database\Query\Builder|\Transaction whereDescription($value) * @method static \Illuminate\Database\Query\Builder|\Transaction whereAmount($value) + * @property integer $piggybank_id + * @property-read \Piggybank $piggybank + * @method static \Illuminate\Database\Query\Builder|\Transaction wherePiggybankId($value) */ class Transaction extends Ardent { diff --git a/app/models/TransactionJournal.php b/app/models/TransactionJournal.php index 26a7a18235..3b5cafdadf 100644 --- a/app/models/TransactionJournal.php +++ b/app/models/TransactionJournal.php @@ -3,6 +3,39 @@ use LaravelBook\Ardent\Ardent; +/** + * TransactionJournal + * + * @property integer $id + * @property \Carbon\Carbon $created_at + * @property \Carbon\Carbon $updated_at + * @property integer $user_id + * @property integer $transaction_type_id + * @property integer $transaction_currency_id + * @property string $description + * @property boolean $completed + * @property \Carbon\Carbon $date + * @property-read \Illuminate\Database\Eloquent\Collection|\ + * 'Budget[] $budgets + * @property-read \Illuminate\Database\Eloquent\Collection|\ + * 'Category[] $categories + * @property-read \Illuminate\Database\Eloquent\Collection|\Component[] $components + * @property-read \TransactionCurrency $transactionCurrency + * @property-read \TransactionType $transactionType + * @property-read \Illuminate\Database\Eloquent\Collection|\Transaction[] $transactions + * @property-read \User $user + * @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereId($value) + * @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereCreatedAt($value) + * @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereUpdatedAt($value) + * @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereUserId($value) + * @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereTransactionTypeId($value) + * @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereTransactionCurrencyId($value) + * @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereDescription($value) + * @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereCompleted($value) + * @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereDate($value) + * @method static \TransactionJournal after($date) + * @method static \TransactionJournal before($date) + */ class TransactionJournal extends Ardent { diff --git a/app/tests/controllers/PiggybankControllerTest.php b/app/tests/controllers/PiggybankControllerTest.php index fc5b883a08..8f31084b10 100644 --- a/app/tests/controllers/PiggybankControllerTest.php +++ b/app/tests/controllers/PiggybankControllerTest.php @@ -32,6 +32,38 @@ class PiggybankControllerTest extends TestCase m::close(); } + public function testAddMoneyGET() + { + $piggyBank = f::create('Piggybank'); + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn( + $piggyBank->account()->first()->user_id + ); + $this->_piggybanks->shouldReceive('leftOnAccount')->andReturn(1); + + $this->action('GET', 'PiggybankController@addMoney', $piggyBank->id); + $this->assertResponseOk(); + + } + + public function testCreatePiggybank() + { + $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->once()->andReturn([]); + $this->action('GET', 'PiggybankController@createPiggybank'); + $this->assertResponseOk(); + + } + + public function testCreateRepeated() + { + $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->once()->andReturn([]); + $this->action('GET', 'PiggybankController@createRepeated'); + $this->assertResponseOk(); + + } + public function testDelete() { $piggyBank = f::create('Piggybank'); @@ -86,6 +118,28 @@ class PiggybankControllerTest extends TestCase $this->assertResponseOk(); } + public function testEditRepeated() + { + $piggyBank = f::create('Piggybank'); + $piggyBank->repeats = 1; + $piggyBank->save(); + + + $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->once()->andReturn([]); + + + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn( + $piggyBank->account()->first()->user_id + ); + $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email'); + + $this->action('GET', 'PiggybankController@edit', $piggyBank->id); + $this->assertResponseOk(); + } + public function testIndex() { $aOne = f::create('Account'); @@ -107,6 +161,208 @@ class PiggybankControllerTest extends TestCase $this->assertResponseOk(); } + public function testModifyMoneyAddPOST() + { + $piggyBank = f::create('Piggybank'); + $piggyBank->targetamount = 200; + $piggyBank->save(); + $input = [ + $piggyBank->id, + 'amount' => 10.0, + 'what' => 'add' + ]; + + // 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'); + Event::shouldReceive('fire')->with('piggybanks.change'); + $this->_piggybanks->shouldReceive('modifyAmount')->once(); + + $this->_piggybanks->shouldReceive('leftOnAccount')->once()->andReturn(200); + + + $this->action('POST', 'PiggybankController@modMoney', $input); + $this->assertSessionHas('success'); + $this->assertResponseStatus(302); + + } + + public function testModifyMoneyAddPOSTFails() + { + $piggyBank = f::create('Piggybank'); + $piggyBank->targetamount = 200; + $piggyBank->save(); + $input = [ + $piggyBank->id, + 'amount' => 10.0, + 'what' => 'add' + ]; + + // 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'); + Event::shouldReceive('fire')->with('piggybanks.change'); + $this->_piggybanks->shouldReceive('leftOnAccount')->once()->andReturn(5); + + + $this->action('POST', 'PiggybankController@modMoney', $input); + $this->assertSessionHas('warning'); + $this->assertResponseStatus(302); + + } + + /** + * @expectedException \Firefly\Exception\FireflyException + */ + public function testModifyMoneyPOSTException() + { + $piggyBank = f::create('Piggybank'); + $piggyBank->targetamount = 200; + $piggyBank->save(); + $input = [ + $piggyBank->id, + 'amount' => 10.0, + 'what' => 'yomoma' + ]; + + // 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@modMoney', $input); + $this->assertSessionHas('warning'); + $this->assertResponseStatus(302); + + + } + + public function testModifyMoneyRemovePOST() + { + $pig = $this->mock('Piggybank'); + $piggybank = f::create('Piggybank'); + $rep = f::create('PiggybankRepetition'); + $rep->piggybank_id = $piggybank->id; + $rep->save(); + + + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn( + $rep->piggybank()->first()->account()->first()->user_id + ); + $pig->shouldReceive('currentRelevantRep')->andReturn($rep); + $this->_piggybanks->shouldReceive('leftOnAccount')->andReturn(11); + $this->_piggybanks->shouldReceive('modifyAmount')->once(); + + $input = [ + $rep->piggybank()->first()->id, + 'amount' => 10.0, + 'what' => 'remove' + ]; + + $this->action('POST', 'PiggybankController@modMoney', $input); + $this->assertSessionHas('success'); + $this->assertResponseStatus(302); + + } + + public function testModifyMoneyRemovePOSTFails() + { + $pig = $this->mock('Piggybank'); + $piggybank = f::create('Piggybank'); + $rep = f::create('PiggybankRepetition'); + $rep->piggybank_id = $piggybank->id; + $rep->currentAmount = 5; + $rep->save(); + + + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn( + $rep->piggybank()->first()->account()->first()->user_id + ); + $pig->shouldReceive('currentRelevantRep')->andReturn($rep); + + $input = [ + $rep->piggybank()->first()->id, + 'amount' => 10.0, + 'what' => 'remove' + ]; + + $this->action('POST', 'PiggybankController@modMoney', $input); + $this->assertSessionHas('warning'); + $this->assertResponseStatus(302); + + } + + public function teststorePiggybank() + { + $piggy = f::create('Piggybank'); + $this->_piggybanks->shouldReceive('store')->once()->andReturn($piggy); + $this->action('POST', 'PiggybankController@storePiggybank'); + $this->assertResponseStatus(302); + } + + public function testStoreRepeated() + { + $piggy = f::create('Piggybank'); + $this->_piggybanks->shouldReceive('store')->once()->andReturn($piggy); + $this->action('POST', 'PiggybankController@storeRepeated'); + $this->assertResponseStatus(302); + } + + public function teststorePiggybankFails() + { + $piggy = f::create('Piggybank'); + unset($piggy->id); + $this->_piggybanks->shouldReceive('store')->once()->andReturn($piggy); + $this->action('POST', 'PiggybankController@storePiggybank'); + $this->assertResponseStatus(302); + } + + public function testStoreRepeatedFails() + { + $piggy = f::create('Piggybank'); + unset($piggy->id); + $this->_piggybanks->shouldReceive('store')->once()->andReturn($piggy); + $this->action('POST', 'PiggybankController@storeRepeated'); + $this->assertResponseStatus(302); + } + + public function testRemoveMoneyGET() + { + $pig = $this->mock('Piggybank'); + $piggybank = f::create('Piggybank'); + $rep = f::create('PiggybankRepetition'); + $rep->piggybank_id = $piggybank->id; + $rep->save(); + + + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn( + $rep->piggybank()->first()->account()->first()->user_id + ); + $pig->shouldReceive('currentRelevantRep')->andReturn($rep); + + $this->_piggybanks->shouldReceive('leftOnAccount')->andReturn(1)->once(); + + $this->action('GET', 'PiggybankController@removeMoney', $piggybank->id); + $this->assertResponseOk(); + } + public function testShow() { $piggyBank = f::create('Piggybank'); @@ -122,8 +378,6 @@ class PiggybankControllerTest extends TestCase $this->assertResponseOk(); } - - public function testUpdate() { $piggyBank = f::create('Piggybank'); @@ -146,7 +400,7 @@ class PiggybankControllerTest extends TestCase public function testUpdateFails() { $piggyBank = f::create('Piggybank'); - unset($piggyBank->amount); + unset($piggyBank->name); $this->_piggybanks->shouldReceive('update')->andReturn($piggyBank); @@ -164,6 +418,4 @@ class PiggybankControllerTest extends TestCase } - - -} \ No newline at end of file +} \ No newline at end of file diff --git a/app/tests/models/ModelTest.php b/app/tests/models/ModelTest.php index d5824be583..b6855ae3a9 100644 --- a/app/tests/models/ModelTest.php +++ b/app/tests/models/ModelTest.php @@ -29,12 +29,16 @@ class ModelTest extends TestCase $account = f::create('Account'); $user = f::create('User'); $type = f::create('AccountType'); + $piggybank = f::create('Piggybank'); $account->user()->associate($user); $account->accounttype()->associate($type); + $account->piggybanks()->save($piggybank); + $this->assertEquals($account->predict(new Carbon), null); $this->assertEquals($account->balance(new Carbon), null); $this->assertEquals($account->user_id, $user->id); + $this->assertEquals($piggybank->account_id, $account->id); $this->assertEquals($account->account_type_id, $type->id); } @@ -199,8 +203,51 @@ class ModelTest extends TestCase $piggy = f::create('Piggybank'); $account = f::create('Account'); $piggy->account()->associate($account); - $this->assertEquals($account->id, $piggy->account_id); + + $repetition = f::create('PiggybankRepetition'); + $repetition->piggybank()->associate($piggy); + $repetition->save(); + $list = ['day', 'week', 'month', 'year']; + + // with a start date, so next reminder is built from a loop: + foreach ($list as $reminder) { + $piggy->reminder = $reminder; + $repetition->save(); + $piggy->nextReminderDate(); + } + // set the reminder period to be invalid, should return NULL + $piggy->reminder = 'invalid'; + $piggy->save(); + $this->assertNull($piggy->nextReminderDate()); + + // set the start date to zero, give a valid $reminder, retry: + $repetition->startdate = null; + $piggy->reminder = 'month'; + $repetition->save(); + foreach ($list as $reminder) { + $piggy->reminder = $reminder; + $repetition->save(); + $piggy->nextReminderDate(); + } + // set the reminder to be invalid again: + $piggy->reminder = 'invalid'; + $piggy->save(); + $piggy->nextReminderDate(); + + // set it to be NULL + $piggy->reminder = null; + $piggy->save(); + $piggy->nextReminderDate(); + + + // remove the repetition, retry: + $piggy->reminder = 'month'; + $piggy->save(); + $repetition->delete(); + $piggy->nextReminderDate(); + + } public function testPreference() @@ -237,6 +284,7 @@ class ModelTest extends TestCase $budget = f::create('Budget'); $category = f::create('Category'); $account = f::create('Account'); + $piggy = f::create('Piggybank'); $transaction->transactionJournal()->associate($journal); $this->assertEquals($transaction->transaction_journal_id, $journal->id); @@ -248,6 +296,8 @@ class ModelTest extends TestCase $this->assertEquals($transaction->categories()->first()->id, $category->id); $transaction->account()->associate($account); $this->assertEquals($transaction->account_id, $account->id); + $transaction->piggybank()->associate($piggy); + $this->assertEquals($transaction->piggybank_id, $piggy->id); } public function testTransactionCurrency() @@ -314,6 +364,7 @@ class ModelTest extends TestCase $pref = f::create('Preference'); $rec = f::create('RecurringTransaction'); $journal = f::create('TransactionJournal'); + $piggy = f::create('Piggybank'); $user->accounts()->save($account); $this->assertEquals($account->id, $user->accounts()->first()->id); @@ -335,6 +386,10 @@ class ModelTest extends TestCase $user->transactionjournals()->save($journal); $this->assertEquals($journal->id, $user->transactionjournals()->first()->id); + + $piggy->account()->associate($account); + $piggy->save(); + $this->assertCount(1, $user->piggybanks()->get()); }