Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Added some tests to cover an event.
James Colee46e366694d6
diff --git a/app/Handlers/Events/ConnectJournalToPiggyBank.php b/app/Handlers/Events/ConnectJournalToPiggyBank.phpindex 7d19f28521..1f3d2a2e70 100644--- a/app/Handlers/Events/ConnectJournalToPiggyBank.php+++ b/app/Handlers/Events/ConnectJournalToPiggyBank.php@@ -30,7 +30,7 @@ class ConnectJournalToPiggyBank** @param JournalCreated $event*- * @return void+ * @return boolean*/public function handle(JournalCreated $event){@@ -38,7 +38,7 @@ class ConnectJournalToPiggyBank$journal = $event->journal;$piggyBankId = $event->piggyBankId;if (intval($piggyBankId) < 1) {- return;+ return false;}Log::debug('JournalCreated event: ' . $journal->id . ', ' . $piggyBankId);@@ -47,20 +47,20 @@ class ConnectJournalToPiggyBank$piggyBank = Auth::user()->piggybanks()->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']);if (is_null($piggyBank) || $journal->transactionType->type != 'Transfer') {- return;+ return false;}Log::debug('Found a piggy bank');$amount = $journal->amount;Log::debug('Amount: ' . $amount);if ($amount == 0) {- return;+ return false;}// update piggy bank rep for date of transaction journal.$repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first();if (is_null($repetition)) {Log::debug('Found no repetition for piggy bank for date ' . $journal->date->format('Y M d'));- return;+ return false;}Log::debug('Found rep! ' . $repetition->id);@@ -91,6 +91,7 @@ class ConnectJournalToPiggyBank'amount' => $amount]);+ return true;}diff --git a/tests/helpers/ConnectJournalToPiggyBankTest.php b/tests/helpers/ConnectJournalToPiggyBankTest.phpnew file mode 100644index 0000000000..841e17d27e--- /dev/null+++ b/tests/helpers/ConnectJournalToPiggyBankTest.php@@ -0,0 +1,261 @@+<?php+use FireflyIII\Events\JournalCreated;+use FireflyIII\Handlers\Events\ConnectJournalToPiggyBank;+use FireflyIII\Models\PiggyBankRepetition;+use FireflyIII\Models\Transaction;+use League\FactoryMuffin\Facade as FactoryMuffin;++/**+ * Class ConnectJournalToPiggyBankTest+ */+class ConnectJournalToPiggyBankTest extends TestCase+{+ //event(new JournalCreated($journal, intval($request->get('piggy_bank_id'))));++ /**+ * Sets up the fixture, for example, opens a network connection.+ * This method is called before a test is executed.+ */+ public function setUp()+ {+ parent::setUp();+ }++ /**+ * Tears down the fixture, for example, closes a network connection.+ * This method is called after a test is executed.+ */+ public function tearDown()+ {+ parent::tearDown();+ }+++ public function testEmptyPiggyBankId()+ {+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');+ $event = new JournalCreated($journal, 0);+ $class = new ConnectJournalToPiggyBank();+ $result = $class->handle($event);++ $this->assertFalse($result);+ }++ public function testNoRepetition()+ {+ FactoryMuffin::create('FireflyIII\Models\TransactionType');+ FactoryMuffin::create('FireflyIII\Models\TransactionType');++ $user = FactoryMuffin::create('FireflyIII\User');+ $this->be($user);++ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');+ $account2 = FactoryMuffin::create('FireflyIII\Models\Account');+ $account1->user_id = $user->id;+ $account2->user_id = $user->id;+ $piggyBank->account_id = $account1->id;+ $account1->save();+ $account2->save();+ $piggyBank->save();++ Transaction::create(+ [+ 'account_id' => $account1->id,+ 'transaction_journal_id' => $journal->id,+ 'amount' => 100+ ]+ );+ Transaction::create(+ [+ 'account_id' => $account2->id,+ 'transaction_journal_id' => $journal->id,+ 'amount' => -100+ ]+ );++ // two transactions:+++ $event = new JournalCreated($journal, $piggyBank->id);+ $class = new ConnectJournalToPiggyBank();+ $result = $class->handle($event);+++ $this->assertFalse($result);+ }++ public function testNoSuchPiggy()+ {+ $user = FactoryMuffin::create('FireflyIII\User');+ $this->be($user);++ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');+ $event = new JournalCreated($journal, 1);+ $class = new ConnectJournalToPiggyBank();+ $result = $class->handle($event);+++ $this->assertFalse($result);+ }++ public function testWithRepetition()+ {+ FactoryMuffin::create('FireflyIII\Models\TransactionType');+ FactoryMuffin::create('FireflyIII\Models\TransactionType');++ $user = FactoryMuffin::create('FireflyIII\User');+ $this->be($user);++ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');+ $account2 = FactoryMuffin::create('FireflyIII\Models\Account');+ $account1->user_id = $user->id;+ $account2->user_id = $user->id;+ $piggyBank->account_id = $account1->id;+ $account1->save();+ $account2->save();+ $piggyBank->save();++ $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,+ ]+ );++ Transaction::create(+ [+ 'account_id' => $account1->id,+ 'transaction_journal_id' => $journal->id,+ 'amount' => 100+ ]+ );+ Transaction::create(+ [+ 'account_id' => $account2->id,+ 'transaction_journal_id' => $journal->id,+ 'amount' => -100+ ]+ );++ $event = new JournalCreated($journal, $piggyBank->id);+ $class = new ConnectJournalToPiggyBank();+ $result = $class->handle($event);+++ $this->assertTrue($result);+ $this->assertCount(1, $piggyBank->piggyBankEvents()->get());+ }++ public function testWithRepetitionReversed()+ {+ FactoryMuffin::create('FireflyIII\Models\TransactionType');+ FactoryMuffin::create('FireflyIII\Models\TransactionType');++ $user = FactoryMuffin::create('FireflyIII\User');+ $this->be($user);++ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');+ $account2 = FactoryMuffin::create('FireflyIII\Models\Account');+ $account1->user_id = $user->id;+ $account2->user_id = $user->id;+ $piggyBank->account_id = $account1->id;+ $account1->save();+ $account2->save();+ $piggyBank->save();++ $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,+ ]+ );++ Transaction::create(+ [+ 'account_id' => $account1->id,+ 'transaction_journal_id' => $journal->id,+ 'amount' => -100+ ]+ );+ Transaction::create(+ [+ 'account_id' => $account2->id,+ 'transaction_journal_id' => $journal->id,+ 'amount' => 100+ ]+ );++ $event = new JournalCreated($journal, $piggyBank->id);+ $class = new ConnectJournalToPiggyBank();+ $result = $class->handle($event);+++ $this->assertTrue($result);+ $this->assertCount(1, $piggyBank->piggyBankEvents()->get());+ }++ public function testZeroAmount()+ {+ FactoryMuffin::create('FireflyIII\Models\TransactionType');+ FactoryMuffin::create('FireflyIII\Models\TransactionType');++ $user = FactoryMuffin::create('FireflyIII\User');+ $this->be($user);++ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');+ $account2 = FactoryMuffin::create('FireflyIII\Models\Account');+ $account1->user_id = $user->id;+ $account2->user_id = $user->id;+ $piggyBank->account_id = $account1->id;+ $account1->save();+ $account2->save();+ $piggyBank->save();++ Transaction::create(+ [+ 'account_id' => $account1->id,+ 'transaction_journal_id' => $journal->id,+ 'amount' => 0+ ]+ );+ Transaction::create(+ [+ 'account_id' => $account2->id,+ 'transaction_journal_id' => $journal->id,+ 'amount' => 0+ ]+ );++ // two transactions:+++ $event = new JournalCreated($journal, $piggyBank->id);+ $class = new ConnectJournalToPiggyBank();+ $result = $class->handle($event);+++ $this->assertFalse($result);+ }+}\ No newline at end of file