/** * */ protected function registerCreateEvents() { // move this routine to a filter // in case of repeated piggy banks and/or other problems. PiggyBank::created(function (PiggyBank $piggyBank) { $repetition = new PiggyBankRepetition(); $repetition->piggyBank()->associate($piggyBank); $repetition->startdate = is_null($piggyBank->startdate) ? null : $piggyBank->startdate; $repetition->targetdate = is_null($piggyBank->targetdate) ? null : $piggyBank->targetdate; $repetition->currentamount = 0; $repetition->save(); }); }
/** * @covers FireflyIII\Handlers\Events\ConnectJournalToPiggyBank::handle */ public function testWithRepetition() { $user = FactoryMuffin::create('FireflyIII\\User'); $this->be($user); FactoryMuffin::create('FireflyIII\\Models\\TransactionType'); // withdrawal FactoryMuffin::create('FireflyIII\\Models\\TransactionType'); // deposit $journal = FactoryMuffin::create('FireflyIII\\Models\\TransactionJournal'); $piggyBank = FactoryMuffin::create('FireflyIII\\Models\\PiggyBank'); $journal->user_id = $user->id; $journal->save(); // create piggy bank event to continue handler: $start = clone $journal->date; $end = clone $journal->date; $start->subDay(); $end->addDay(); PiggyBankRepetition::create(['piggy_bank_id' => $piggyBank->id, 'startdate' => $start->format('Y-m-d'), 'targetdate' => $end->format('Y-m-d'), 'currentamount' => 0]); /** @var Transaction $transaction */ foreach ($journal->transactions()->get() as $transaction) { if ($transaction->amount < 0) { $piggyBank->account_id = $transaction->account_id; $account = $transaction->account; $account->user_id = $user->id; $account->save(); $piggyBank->account_id = $account->id; $piggyBank->save(); } } $event = new JournalCreated($journal, $piggyBank->id); $class = new ConnectJournalToPiggyBank(); $result = $class->handle($event); $this->assertTrue($result); }