From e46e366694d6d71078d6e4a170eb94319fc037f7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 11 May 2015 21:45:31 +0200 Subject: [PATCH 01/12] Added some tests to cover an event. --- .../Events/ConnectJournalToPiggyBank.php | 11 +- .../helpers/ConnectJournalToPiggyBankTest.php | 261 ++++++++++++++++++ 2 files changed, 267 insertions(+), 5 deletions(-) create mode 100644 tests/helpers/ConnectJournalToPiggyBankTest.php diff --git a/app/Handlers/Events/ConnectJournalToPiggyBank.php b/app/Handlers/Events/ConnectJournalToPiggyBank.php index 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.php new file mode 100644 index 0000000000..841e17d27e --- /dev/null +++ b/tests/helpers/ConnectJournalToPiggyBankTest.php @@ -0,0 +1,261 @@ +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 From 22f4d2979ad70eb991e4802cfacc97b58c1cd3bb Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 11 May 2015 22:12:53 +0200 Subject: [PATCH 02/12] Added some more ignores, edited some middleware. --- app/Helpers/Help/Help.php | 4 ++++ app/Http/Middleware/PiggyBanks.php | 2 +- app/Http/Middleware/Range.php | 2 +- app/Http/Middleware/Reminders.php | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Helpers/Help/Help.php b/app/Helpers/Help/Help.php index eddad2614f..edb1093e85 100644 --- a/app/Helpers/Help/Help.php +++ b/app/Helpers/Help/Help.php @@ -17,6 +17,7 @@ class Help implements HelpInterface { /** + * @codeCoverageIgnore * @param $key * * @return string @@ -54,6 +55,7 @@ class Help implements HelpInterface } /** + * @codeCoverageIgnore * @param $route * * @return bool @@ -64,6 +66,7 @@ class Help implements HelpInterface } /** + * @codeCoverageIgnore * @param $route * @param array $content * @@ -76,6 +79,7 @@ class Help implements HelpInterface } /** + * @codeCoverageIgnore * @param $route * * @return bool diff --git a/app/Http/Middleware/PiggyBanks.php b/app/Http/Middleware/PiggyBanks.php index 8c00cf2376..810f396fce 100644 --- a/app/Http/Middleware/PiggyBanks.php +++ b/app/Http/Middleware/PiggyBanks.php @@ -46,7 +46,7 @@ class PiggyBanks */ public function handle(Request $request, Closure $next) { - if ($this->auth->check() && !$request->isXmlHttpRequest() && App::environment() != 'testing') { + if ($this->auth->check() && !$request->isXmlHttpRequest()) { // get piggy banks without a repetition: /** @var Collection $set */ $set = $this->auth->user()->piggybanks() diff --git a/app/Http/Middleware/Range.php b/app/Http/Middleware/Range.php index 7407e4a7d2..aa7fd7a98b 100644 --- a/app/Http/Middleware/Range.php +++ b/app/Http/Middleware/Range.php @@ -48,7 +48,7 @@ class Range */ public function handle(Request $request, Closure $theNext) { - if ($this->auth->check() && App::environment() != 'testing') { + if ($this->auth->check()) { // ignore preference. set the range to be the current month: if (!Session::has('start') && !Session::has('end')) { diff --git a/app/Http/Middleware/Reminders.php b/app/Http/Middleware/Reminders.php index 457e4e2543..55c555aec0 100644 --- a/app/Http/Middleware/Reminders.php +++ b/app/Http/Middleware/Reminders.php @@ -46,7 +46,7 @@ class Reminders */ public function handle(Request $request, Closure $next) { - if ($this->auth->check() && !$request->isXmlHttpRequest() && App::environment() != 'testing') { + if ($this->auth->check() && !$request->isXmlHttpRequest()) { // do reminders stuff. $piggyBanks = $this->auth->user()->piggyBanks()->where('remind_me', 1)->get(); $today = new Carbon; From 571e7df807408dba1dd504dc351b89bfc2f21956 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 12 May 2015 20:51:03 +0200 Subject: [PATCH 03/12] Some tiny optimisations. --- app/Handlers/Events/RescanJournal.php | 1 + app/Handlers/Events/UpdateJournalConnection.php | 1 + app/Helpers/Reminders/ReminderHelper.php | 2 -- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Handlers/Events/RescanJournal.php b/app/Handlers/Events/RescanJournal.php index aaa21b515a..b44924b6d8 100644 --- a/app/Handlers/Events/RescanJournal.php +++ b/app/Handlers/Events/RescanJournal.php @@ -7,6 +7,7 @@ use Log; /** * Class RescanJournal * + * @codeCoverageIgnore * @package FireflyIII\Handlers\Events */ class RescanJournal diff --git a/app/Handlers/Events/UpdateJournalConnection.php b/app/Handlers/Events/UpdateJournalConnection.php index f18f0bf17e..13b60c8420 100644 --- a/app/Handlers/Events/UpdateJournalConnection.php +++ b/app/Handlers/Events/UpdateJournalConnection.php @@ -6,6 +6,7 @@ use FireflyIII\Models\PiggyBankEvent; /** * Class UpdateJournalConnection * + * @codeCoverageIgnore * @package FireflyIII\Handlers\Events */ class UpdateJournalConnection diff --git a/app/Helpers/Reminders/ReminderHelper.php b/app/Helpers/Reminders/ReminderHelper.php index 530d3b3c78..23329db748 100644 --- a/app/Helpers/Reminders/ReminderHelper.php +++ b/app/Helpers/Reminders/ReminderHelper.php @@ -46,8 +46,6 @@ class ReminderHelper implements ReminderHelperInterface 'leftToSave' => $left, ]; - - // create one: $reminder = new Reminder; $reminder->user()->associate(Auth::user()); $reminder->startdate = $start; From 77e133e67c56d84378d46505d1fa47b95f03314f Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 13 May 2015 06:31:32 +0200 Subject: [PATCH 04/12] Some cleaning up for mobile screens [skip ci] --- resources/twig/layout/default.twig | 2 +- resources/twig/partials/menu.twig | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/resources/twig/layout/default.twig b/resources/twig/layout/default.twig index 149ecced8c..e65e7d4199 100644 --- a/resources/twig/layout/default.twig +++ b/resources/twig/layout/default.twig @@ -73,7 +73,7 @@ {{ subTitle }} {% endif %} -