From d4830052193cfff390dfc08a9810a45beffd96fc Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 5 Jun 2015 07:49:07 +0200 Subject: [PATCH] Expanded some tests. --- app/Http/Middleware/Reminders.php | 11 +-- app/Repositories/Tag/TagRepository.php | 8 +- .../Tag/TagRepositoryInterface.php | 2 +- tests/repositories/TagRepositoryTest.php | 87 ++++++++++++++++++- 4 files changed, 96 insertions(+), 12 deletions(-) diff --git a/app/Http/Middleware/Reminders.php b/app/Http/Middleware/Reminders.php index 15e54448b2..0be752e89e 100644 --- a/app/Http/Middleware/Reminders.php +++ b/app/Http/Middleware/Reminders.php @@ -8,6 +8,7 @@ use Closure; use FireflyIII\Models\PiggyBank; use FireflyIII\Models\Reminder; use FireflyIII\Support\CacheProperties; +use FireflyIII\User; use Illuminate\Contracts\Auth\Guard; use Illuminate\Http\Request; use View; @@ -49,7 +50,8 @@ class Reminders { - if ($this->auth->check() && !$request->isXmlHttpRequest()) { + $user = $this->auth->user(); + if ($this->auth->check() && !$request->isXmlHttpRequest() && $user instanceof User) { // do reminders stuff. // abuse CacheProperties to find out if we need to do this: @@ -63,7 +65,7 @@ class Reminders return $next($request); } - $piggyBanks = $this->auth->user()->piggyBanks()->where('remind_me', 1)->get(); + $piggyBanks = $user->piggyBanks()->where('remind_me', 1)->get(); /** @var \FireflyIII\Helpers\Reminders\ReminderHelperInterface $helper */ $helper = App::make('FireflyIII\Helpers\Reminders\ReminderHelperInterface'); @@ -75,12 +77,11 @@ class Reminders // delete invalid reminders // this is a construction SQLITE cannot handle :( if (env('DB_CONNECTION') != 'sqlite') { - Reminder::whereUserId($this->auth->user()->id)->leftJoin('piggy_banks', 'piggy_banks.id', '=', 'remindersable_id') - ->whereNull('piggy_banks.id')->delete(); + Reminder::whereUserId($user->id)->leftJoin('piggy_banks', 'piggy_banks.id', '=', 'remindersable_id')->whereNull('piggy_banks.id')->delete(); } // get and list active reminders: - $reminders = $this->auth->user()->reminders()->today()->get(); + $reminders = $user->reminders()->today()->get(); $reminders->each( function (Reminder $reminder) use ($helper) { $reminder->description = $helper->getReminderText($reminder); diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 252a08a2a9..6a3323f074 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -9,7 +9,6 @@ use FireflyIII\Models\Account; use FireflyIII\Models\Tag; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; -use Illuminate\Support\Collection; /** * Class TagRepository @@ -64,14 +63,15 @@ class TagRepository implements TagRepositoryInterface * @param Carbon $start * @param Carbon $end * - * @return integer + * @return string */ public function coveredByBalancingActs(Account $account, Carbon $start, Carbon $end) { // the quickest way to do this is by scanning all balancingAct tags // because there will be less of them any way. $tags = Auth::user()->tags()->where('tagMode', 'balancingAct')->get(); - $amount = 0; + $amount = '0'; + bcscale(2); /** @var Tag $tag */ foreach ($tags as $tag) { @@ -80,7 +80,7 @@ class TagRepository implements TagRepositoryInterface /** @var TransactionJournal $journal */ foreach ($journals as $journal) { if ($journal->destination_account->id == $account->id) { - $amount += $journal->amount; + $amount = bcadd($amount, $journal->amount); } } } diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php index 463e0979d2..7d76fa30e8 100644 --- a/app/Repositories/Tag/TagRepositoryInterface.php +++ b/app/Repositories/Tag/TagRepositoryInterface.php @@ -38,7 +38,7 @@ interface TagRepositoryInterface * @param Carbon $start * @param Carbon $end * - * @return float + * @return string */ public function coveredByBalancingActs(Account $account, Carbon $start, Carbon $end); diff --git a/tests/repositories/TagRepositoryTest.php b/tests/repositories/TagRepositoryTest.php index 3a8962dc29..a8b6697b58 100644 --- a/tests/repositories/TagRepositoryTest.php +++ b/tests/repositories/TagRepositoryTest.php @@ -1,6 +1,6 @@ be($user); + + // create transaction and account types: + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // transfer + FactoryMuffin::create('FireflyIII\Models\AccountType'); // expense + FactoryMuffin::create('FireflyIII\Models\AccountType'); // revenue + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); // asset + + // create two accounts: + $fromAccount = FactoryMuffin::create('FireflyIII\Models\Account'); // asset + $toAccount = FactoryMuffin::create('FireflyIII\Models\Account'); // asset + $fromAccount->account_type_id = $asset->id; + $toAccount->account_type_id = $asset->id; + $fromAccount->save(); + $toAccount->save(); + + + // create a tag + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'balancingAct'; + $tag->user_id = $user->id; + $tag->save(); + + // date + $today = new Carbon('2014-01-12'); + $start = new Carbon('2014-01-01'); + $end = new Carbon('2014-01-31'); + + // store five journals + for ($i = 0; $i < 5; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); // deposit! + // set date: + $journal->date = $today; + $journal->user_id = $user->id; + $journal->transaction_type_id = $transfer->id; + $journal->tags()->save($tag); + $journal->save(); + + // update accounts: + $journal->transactions[0]->account_id = $fromAccount->id; + $journal->transactions[0]->amount = '-100'; + $journal->transactions[0]->save(); + $journal->transactions[1]->account_id = $toAccount->id; + $journal->transactions[1]->amount = '100'; + $journal->transactions[1]->save(); + + } + + $amount = $this->object->coveredByBalancingActs($toAccount, $start, $end); + // five transactions, 100 each. + $this->assertEquals('500', $amount); + } + /** * @covers FireflyIII\Repositories\Tag\TagRepository::destroy */