From 850a0ae17e9e40fdf1357fdad532dbced2e2e388 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 24 Aug 2018 16:07:33 +0200 Subject: [PATCH] Improved code coverage for events and reports. --- .../Report/Audit/MonthReportGenerator.php | 119 ++++----- app/Handlers/Events/APIEventHandler.php | 2 +- app/Handlers/Events/AdminEventHandler.php | 3 +- app/Handlers/Events/AutomationHandler.php | 4 +- .../Events/StoredJournalEventHandler.php | 32 +-- app/Handlers/Events/UserEventHandler.php | 9 - .../Events/VersionCheckEventHandler.php | 4 +- .../Report/Audit/MonthReportGeneratorTest.php | 230 ++++++++++++++++++ .../Handlers/Events/APIEventHandlerTest.php | 76 ++++++ .../Handlers/Events/AdminEventHandlerTest.php | 13 +- .../Handlers/Events/AutomationHandlerTest.php | 80 ++++++ .../Events/StoredJournalEventHandlerTest.php | 74 ++++++ .../Handlers/Events/UserEventHandlerTest.php | 10 + .../Events/VersionCheckEventHandlerTest.php | 11 + 14 files changed, 566 insertions(+), 101 deletions(-) create mode 100644 tests/Unit/Generator/Report/Audit/MonthReportGeneratorTest.php create mode 100644 tests/Unit/Handlers/Events/APIEventHandlerTest.php create mode 100644 tests/Unit/Handlers/Events/AutomationHandlerTest.php create mode 100644 tests/Unit/Handlers/Events/StoredJournalEventHandlerTest.php diff --git a/app/Generator/Report/Audit/MonthReportGenerator.php b/app/Generator/Report/Audit/MonthReportGenerator.php index 26e1ac3f43..cadc73b157 100644 --- a/app/Generator/Report/Audit/MonthReportGenerator.php +++ b/app/Generator/Report/Audit/MonthReportGenerator.php @@ -54,6 +54,7 @@ class MonthReportGenerator implements ReportGeneratorInterface * * @return string * @throws FireflyException + * @codeCoverageIgnore */ public function generate(): string { @@ -91,6 +92,65 @@ class MonthReportGenerator implements ReportGeneratorInterface return $result; } + /** + * Get the audit report. + * + * @param Account $account + * @param Carbon $date + * + * @return array + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) // not that long + * @throws FireflyException + */ + public function getAuditReport(Account $account, Carbon $date): array + { + /** @var CurrencyRepositoryInterface $currencyRepos */ + $currencyRepos = app(CurrencyRepositoryInterface::class); + + /** @var AccountRepositoryInterface $accountRepository */ + $accountRepository = app(AccountRepositoryInterface::class); + $accountRepository->setUser($account->user); + + /** @var TransactionCollectorInterface $collector */ + $collector = app(TransactionCollectorInterface::class); + $collector->setAccounts(new Collection([$account]))->setRange($this->start, $this->end); + $journals = $collector->getTransactions(); + $journals = $journals->reverse(); + $dayBeforeBalance = app('steam')->balance($account, $date); + $startBalance = $dayBeforeBalance; + $currency = $currencyRepos->findNull((int)$accountRepository->getMetaValue($account, 'currency_id')); + + if (null === $currency) { + throw new FireflyException('Unexpected NULL value in account currency preference.'); + } + + /** @var Transaction $transaction */ + foreach ($journals as $transaction) { + $transaction->before = $startBalance; + $transactionAmount = $transaction->transaction_amount; + + if ($currency->id === $transaction->foreign_currency_id) { + $transactionAmount = $transaction->transaction_foreign_amount; + } + + $newBalance = bcadd($startBalance, $transactionAmount); + $transaction->after = $newBalance; + $startBalance = $newBalance; + } + + $return = [ + 'journals' => $journals->reverse(), + 'exists' => $journals->count() > 0, + 'end' => $this->end->formatLocalized((string)trans('config.month_and_day')), + 'endBalance' => app('steam')->balance($account, $this->end), + 'dayBefore' => $date->formatLocalized((string)trans('config.month_and_day')), + 'dayBeforeBalance' => $dayBeforeBalance, + ]; + + return $return; + } + /** * Account collection setter. * @@ -187,63 +247,4 @@ class MonthReportGenerator implements ReportGeneratorInterface { return $this; } - - /** - * Get the audit report. - * - * @param Account $account - * @param Carbon $date - * - * @return array - * - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) // not that long - * @throws FireflyException - */ - private function getAuditReport(Account $account, Carbon $date): array - { - /** @var CurrencyRepositoryInterface $currencyRepos */ - $currencyRepos = app(CurrencyRepositoryInterface::class); - - /** @var AccountRepositoryInterface $accountRepository */ - $accountRepository = app(AccountRepositoryInterface::class); - $accountRepository->setUser($account->user); - - /** @var TransactionCollectorInterface $collector */ - $collector = app(TransactionCollectorInterface::class); - $collector->setAccounts(new Collection([$account]))->setRange($this->start, $this->end); - $journals = $collector->getTransactions(); - $journals = $journals->reverse(); - $dayBeforeBalance = app('steam')->balance($account, $date); - $startBalance = $dayBeforeBalance; - $currency = $currencyRepos->findNull((int)$accountRepository->getMetaValue($account, 'currency_id')); - - if (null === $currency) { - throw new FireflyException('Unexpected NULL value in account currency preference.'); - } - - /** @var Transaction $transaction */ - foreach ($journals as $transaction) { - $transaction->before = $startBalance; - $transactionAmount = $transaction->transaction_amount; - - if ($currency->id === $transaction->foreign_currency_id) { - $transactionAmount = $transaction->transaction_foreign_amount; - } - - $newBalance = bcadd($startBalance, $transactionAmount); - $transaction->after = $newBalance; - $startBalance = $newBalance; - } - - $return = [ - 'journals' => $journals->reverse(), - 'exists' => $journals->count() > 0, - 'end' => $this->end->formatLocalized((string)trans('config.month_and_day')), - 'endBalance' => app('steam')->balance($account, $this->end), - 'dayBefore' => $date->formatLocalized((string)trans('config.month_and_day')), - 'dayBeforeBalance' => $dayBeforeBalance, - ]; - - return $return; - } } diff --git a/app/Handlers/Events/APIEventHandler.php b/app/Handlers/Events/APIEventHandler.php index b35130f0d0..0b93895ee1 100644 --- a/app/Handlers/Events/APIEventHandler.php +++ b/app/Handlers/Events/APIEventHandler.php @@ -65,10 +65,10 @@ class APIEventHandler Log::error($e->getTraceAsString()); Session::flash('error', 'Possible email error: ' . $e->getMessage()); } + // @codeCoverageIgnoreEnd Log::debug('If no error above this line, message was sent.'); } - // @codeCoverageIgnoreEnd return true; diff --git a/app/Handlers/Events/AdminEventHandler.php b/app/Handlers/Events/AdminEventHandler.php index 1bc55af2ba..6d3c09a62b 100644 --- a/app/Handlers/Events/AdminEventHandler.php +++ b/app/Handlers/Events/AdminEventHandler.php @@ -57,16 +57,17 @@ class AdminEventHandler Log::debug('Trying to send message...'); Mail::to($email)->send(new AdminTestMail($email, $ipAddress)); // @codeCoverageIgnoreStart + // Laravel cannot pretend this process failed during testing. } catch (Exception $e) { Log::debug('Send message failed! :('); Log::error($e->getMessage()); Log::error($e->getTraceAsString()); Session::flash('error', 'Possible email error: ' . $e->getMessage()); } + // @codeCoverageIgnoreEnd Log::debug('If no error above this line, message was sent.'); } - // @codeCoverageIgnoreEnd return true; } } diff --git a/app/Handlers/Events/AutomationHandler.php b/app/Handlers/Events/AutomationHandler.php index f7549ae84b..2b1464010c 100644 --- a/app/Handlers/Events/AutomationHandler.php +++ b/app/Handlers/Events/AutomationHandler.php @@ -55,12 +55,14 @@ class AutomationHandler Mail::to($user->email)->send(new ReportNewJournalsMail($user->email, '127.0.0.1', $event->journals)); // @codeCoverageIgnoreStart } catch (Exception $e) { + Log::debug('Send message failed! :('); Log::error($e->getMessage()); + Log::error($e->getTraceAsString()); } + // @codeCoverageIgnoreEnd Log::debug('Done!'); } - // @codeCoverageIgnoreEnd return true; } } diff --git a/app/Handlers/Events/StoredJournalEventHandler.php b/app/Handlers/Events/StoredJournalEventHandler.php index db3fcb41f2..3f5ea6be2f 100644 --- a/app/Handlers/Events/StoredJournalEventHandler.php +++ b/app/Handlers/Events/StoredJournalEventHandler.php @@ -25,8 +25,6 @@ namespace FireflyIII\Handlers\Events; use FireflyIII\Events\StoredTransactionJournal; use FireflyIII\Models\Rule; use FireflyIII\Models\RuleGroup; -use FireflyIII\Repositories\Journal\JournalRepositoryInterface; -use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; use FireflyIII\TransactionRules\Processor; @@ -35,28 +33,6 @@ use FireflyIII\TransactionRules\Processor; */ class StoredJournalEventHandler { - /** @var JournalRepositoryInterface The journal repository. */ - public $journalRepository; - /** @var PiggyBankRepositoryInterface The Piggy bank repository */ - public $repository; - /** @var RuleGroupRepositoryInterface The rule group repository */ - public $ruleGroupRepository; - - /** - * StoredJournalEventHandler constructor. - * - * @param PiggyBankRepositoryInterface $repository - * @param JournalRepositoryInterface $journalRepository - * @param RuleGroupRepositoryInterface $ruleGroupRepository - */ - public function __construct( - PiggyBankRepositoryInterface $repository, JournalRepositoryInterface $journalRepository, RuleGroupRepositoryInterface $ruleGroupRepository - ) { - $this->repository = $repository; - $this->journalRepository = $journalRepository; - $this->ruleGroupRepository = $ruleGroupRepository; - } - /** * This method grabs all the users rules and processes them. * @@ -67,13 +43,15 @@ class StoredJournalEventHandler */ public function processRules(StoredTransactionJournal $storedJournalEvent): bool { - // get all the user's rule groups, with the rules, order by 'order'. $journal = $storedJournalEvent->journal; - $groups = $this->ruleGroupRepository->getActiveGroups($journal->user); + + // create objects: + $ruleGroupRepos = app(RuleGroupRepositoryInterface::class); + $groups = $ruleGroupRepos->getActiveGroups($journal->user); /** @var RuleGroup $group */ foreach ($groups as $group) { - $rules = $this->ruleGroupRepository->getActiveStoreRules($group); + $rules = $ruleGroupRepos->getActiveStoreRules($group); /** @var Rule $rule */ foreach ($rules as $rule) { $processor = Processor::make($rule); diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Handlers/Events/UserEventHandler.php index e40ff67b55..41d7b83c80 100644 --- a/app/Handlers/Events/UserEventHandler.php +++ b/app/Handlers/Events/UserEventHandler.php @@ -141,12 +141,10 @@ class UserEventHandler $uri = route('profile.confirm-email-change', [$token->data]); try { Mail::to($newEmail)->send(new ConfirmEmailChangeMail($newEmail, $oldEmail, $uri, $ipAddress)); - // @codeCoverageIgnoreStart } catch (Exception $e) { Log::error($e->getMessage()); } - // @codeCoverageIgnoreEnd return true; } @@ -167,12 +165,10 @@ class UserEventHandler $uri = route('profile.undo-email-change', [$token->data, hash('sha256', $oldEmail)]); try { Mail::to($oldEmail)->send(new UndoEmailChangeMail($newEmail, $oldEmail, $uri, $ipAddress)); - // @codeCoverageIgnoreStart } catch (Exception $e) { Log::error($e->getMessage()); } - // @codeCoverageIgnoreEnd return true; } @@ -194,13 +190,10 @@ class UserEventHandler // send email. try { Mail::to($email)->send(new RequestedNewPasswordMail($url, $ipAddress)); - // @codeCoverageIgnoreStart } catch (Exception $e) { Log::error($e->getMessage()); } - // @codeCoverageIgnoreEnd - return true; } @@ -224,11 +217,9 @@ class UserEventHandler // send email. try { Mail::to($email)->send(new RegisteredUserMail($uri, $ipAddress)); - // @codeCoverageIgnoreStart } catch (Exception $e) { Log::error($e->getMessage()); } - // @codeCoverageIgnoreEnd } return true; diff --git a/app/Handlers/Events/VersionCheckEventHandler.php b/app/Handlers/Events/VersionCheckEventHandler.php index e7d893cefa..5b7efb4e45 100644 --- a/app/Handlers/Events/VersionCheckEventHandler.php +++ b/app/Handlers/Events/VersionCheckEventHandler.php @@ -56,7 +56,8 @@ class VersionCheckEventHandler $sandstorm = 1 === (int)getenv('SANDSTORM'); if (true === $sandstorm) { Log::debug('This is Sandstorm instance, done.'); - return; // @codeCoverageIgnore + + return; } /** @var UserRepositoryInterface $repository */ @@ -65,6 +66,7 @@ class VersionCheckEventHandler $user = $event->user; if (!$repository->hasRole($user, 'owner')) { Log::debug('User is not admin, done.'); + return; } diff --git a/tests/Unit/Generator/Report/Audit/MonthReportGeneratorTest.php b/tests/Unit/Generator/Report/Audit/MonthReportGeneratorTest.php new file mode 100644 index 0000000000..a30241b664 --- /dev/null +++ b/tests/Unit/Generator/Report/Audit/MonthReportGeneratorTest.php @@ -0,0 +1,230 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Generator\Report\Audit; + + +use Carbon\Carbon; +use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Generator\Report\Audit\MonthReportGenerator; +use FireflyIII\Helpers\Collector\TransactionCollectorInterface; +use FireflyIII\Models\Account; +use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use Illuminate\Support\Collection; +use Log; +use Mockery; +use Steam; +use Tests\TestCase; + +/** + * + * Class MonthReportGeneratorTest + */ +class MonthReportGeneratorTest extends TestCase +{ + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + + /** + * @covers \FireflyIII\Generator\Report\Audit\MonthReportGenerator + */ + public function testBasic(): void + { + /** @var Account $account */ + $account = $this->user()->accounts()->where('account_type_id', 3)->first(); + $date = new Carbon; + $start = Carbon::create()->startOfMonth(); + $end = Carbon::create()->endOfMonth(); + $generator = new MonthReportGenerator(); + $generator->setStartDate($start); + $generator->setEndDate($end); + + $collection = new Collection; + + // mock stuff + $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $collector = $this->mock(TransactionCollectorInterface::class); + Steam::shouldReceive('balance')->times(2)->andReturn('100'); + + // mock calls: + $accountRepos->shouldReceive('setUser')->once(); + $accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1')->once(); + + $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::first())->once(); + + $collector->shouldReceive('setAccounts')->andReturnSelf(); + $collector->shouldReceive('setRange')->andReturnSelf(); + $collector->shouldReceive('getTransactions')->andReturn($collection); + + + try { + $result = $generator->getAuditReport($account, $date); + } catch (FireflyException $e) { + $this->assertTrue(false, $e->getMessage()); + } + $this->assertFalse($result['exists']); + $this->assertEquals('100', $result['endBalance']); + } + + /** + * @covers \FireflyIII\Generator\Report\Audit\MonthReportGenerator + */ + public function testBasicNoCurrency(): void + { + /** @var Account $account */ + $account = $this->user()->accounts()->where('account_type_id', 3)->first(); + $date = new Carbon; + $start = Carbon::create()->startOfMonth(); + $end = Carbon::create()->endOfMonth(); + $generator = new MonthReportGenerator(); + $generator->setStartDate($start); + $generator->setEndDate($end); + + $collection = new Collection; + + // mock stuff + $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $collector = $this->mock(TransactionCollectorInterface::class); + Steam::shouldReceive('balance')->times(1)->andReturn('100'); + + // mock calls: + $accountRepos->shouldReceive('setUser')->once(); + $accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1')->once(); + + $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(null)->once(); + + $collector->shouldReceive('setAccounts')->andReturnSelf(); + $collector->shouldReceive('setRange')->andReturnSelf(); + $collector->shouldReceive('getTransactions')->andReturn($collection); + + + try { + $generator->getAuditReport($account, $date); + } catch (FireflyException $e) { + $this->assertEquals('Unexpected NULL value in account currency preference.', $e->getMessage()); + } + } + + /** + * @covers \FireflyIII\Generator\Report\Audit\MonthReportGenerator + */ + public function testBasicWithForeign(): void + { + /** @var Account $account */ + $account = $this->user()->accounts()->where('account_type_id', 3)->first(); + $date = new Carbon; + $start = Carbon::create()->startOfMonth(); + $end = Carbon::create()->endOfMonth(); + $generator = new MonthReportGenerator(); + $generator->setStartDate($start); + $generator->setEndDate($end); + + $collection = new Collection; + $transaction = $this->user()->transactions()->first(); + $transaction->transaction_amount = '30'; + $transaction->foreign_currency_id = 1; + $transaction->transaction_foreign_amount = '30'; + $collection->push($transaction); + + // mock stuff + $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $collector = $this->mock(TransactionCollectorInterface::class); + Steam::shouldReceive('balance')->times(2)->andReturn('100'); + + // mock calls: + $accountRepos->shouldReceive('setUser')->once(); + $accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1')->once(); + + $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::first())->once(); + + $collector->shouldReceive('setAccounts')->andReturnSelf(); + $collector->shouldReceive('setRange')->andReturnSelf(); + $collector->shouldReceive('getTransactions')->andReturn($collection); + + + try { + $result = $generator->getAuditReport($account, $date); + } catch (FireflyException $e) { + $this->assertTrue(false, $e->getMessage()); + } + $this->assertTrue($result['exists']); + $this->assertEquals('100', $result['endBalance']); + } + + /** + * @covers \FireflyIII\Generator\Report\Audit\MonthReportGenerator + */ + public function testBasicWithTransactions(): void + { + /** @var Account $account */ + $account = $this->user()->accounts()->where('account_type_id', 3)->first(); + $date = new Carbon; + $start = Carbon::create()->startOfMonth(); + $end = Carbon::create()->endOfMonth(); + $generator = new MonthReportGenerator(); + $generator->setStartDate($start); + $generator->setEndDate($end); + + $collection = new Collection; + $transaction = $this->user()->transactions()->first(); + $transaction->transaction_amount = '30'; + $collection->push($transaction); + + // mock stuff + $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $collector = $this->mock(TransactionCollectorInterface::class); + Steam::shouldReceive('balance')->times(2)->andReturn('100'); + + // mock calls: + $accountRepos->shouldReceive('setUser')->once(); + $accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1')->once(); + + $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::first())->once(); + + $collector->shouldReceive('setAccounts')->andReturnSelf(); + $collector->shouldReceive('setRange')->andReturnSelf(); + $collector->shouldReceive('getTransactions')->andReturn($collection); + + + try { + $result = $generator->getAuditReport($account, $date); + } catch (FireflyException $e) { + $this->assertTrue(false, $e->getMessage()); + } + $this->assertTrue($result['exists']); + $this->assertEquals('100', $result['endBalance']); + } + +} \ No newline at end of file diff --git a/tests/Unit/Handlers/Events/APIEventHandlerTest.php b/tests/Unit/Handlers/Events/APIEventHandlerTest.php new file mode 100644 index 0000000000..8c971f9698 --- /dev/null +++ b/tests/Unit/Handlers/Events/APIEventHandlerTest.php @@ -0,0 +1,76 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Handlers\Events; + + +use FireflyIII\Handlers\Events\APIEventHandler; +use FireflyIII\Mail\AccessTokenCreatedMail; +use FireflyIII\Repositories\User\UserRepositoryInterface; +use Illuminate\Support\Facades\Mail; +use Laravel\Passport\Events\AccessTokenCreated; +use Log; +use Tests\TestCase; + +/** + * + * Class APIEventHandlerTest + */ +class APIEventHandlerTest extends TestCase +{ + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + + /** + * @covers \FireflyIII\Handlers\Events\APIEventHandler + */ + public function testAccessTokenCreated(): void + { + Mail::fake(); + // mock objects. + $repository = $this->mock(UserRepositoryInterface::class); + + // mock calls. + $repository->shouldReceive('findNull')->withArgs([1])->andReturn($this->user())->once(); + + + $event = new AccessTokenCreated('1', '1', '1'); + $handler = new APIEventHandler; + $handler->accessTokenCreated($event); + + // assert a message was sent. + Mail::assertSent( + AccessTokenCreatedMail::class, function ($mail) { + return $mail->hasTo('thegrumpydictator@gmail.com') && '127.0.0.1' === $mail->ipAddress; + } + ); + + } + +} \ No newline at end of file diff --git a/tests/Unit/Handlers/Events/AdminEventHandlerTest.php b/tests/Unit/Handlers/Events/AdminEventHandlerTest.php index 59e1d2f26c..6515aa743b 100644 --- a/tests/Unit/Handlers/Events/AdminEventHandlerTest.php +++ b/tests/Unit/Handlers/Events/AdminEventHandlerTest.php @@ -31,12 +31,21 @@ use FireflyIII\Repositories\User\UserRepositoryInterface; use Illuminate\Support\Facades\Mail; use Mockery; use Tests\TestCase; - +use Log; /** * Class AdminEventHandlerTest */ class AdminEventHandlerTest extends TestCase { + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + /** * @covers \FireflyIII\Handlers\Events\AdminEventHandler @@ -77,6 +86,6 @@ class AdminEventHandlerTest extends TestCase } ); - } + } } diff --git a/tests/Unit/Handlers/Events/AutomationHandlerTest.php b/tests/Unit/Handlers/Events/AutomationHandlerTest.php new file mode 100644 index 0000000000..16fbfd723b --- /dev/null +++ b/tests/Unit/Handlers/Events/AutomationHandlerTest.php @@ -0,0 +1,80 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Handlers\Events; + + +use FireflyIII\Events\RequestedReportOnJournals; +use FireflyIII\Handlers\Events\AutomationHandler; +use FireflyIII\Mail\ReportNewJournalsMail; +use FireflyIII\Repositories\User\UserRepositoryInterface; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Mail; +use Log; +use stdClass; +use Tests\TestCase; + +/** + * + * Class AutomationHandlerTest + */ +class AutomationHandlerTest extends TestCase +{ + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + + + /** + * @covers \FireflyIII\Handlers\Events\AutomationHandler + * @covers \FireflyIII\Events\RequestedReportOnJournals + */ + public function testReportJournals(): void + { + Mail::fake(); + // mock repositories + $repository = $this->mock(UserRepositoryInterface::class); + $journals = new Collection; + $journals->push(new stdClass); + + // mock calls. + $repository->shouldReceive('findNull')->withArgs([1])->andReturn($this->user())->once(); + + $event = new RequestedReportOnJournals(1, $journals); + $handler = new AutomationHandler(); + + $handler->reportJournals($event); + + // assert a message was sent. + Mail::assertSent( + ReportNewJournalsMail::class, function ($mail) { + return $mail->hasTo('thegrumpydictator@gmail.com') && '127.0.0.1' === $mail->ipAddress; + } + ); + } +} \ No newline at end of file diff --git a/tests/Unit/Handlers/Events/StoredJournalEventHandlerTest.php b/tests/Unit/Handlers/Events/StoredJournalEventHandlerTest.php new file mode 100644 index 0000000000..b3fa06f1db --- /dev/null +++ b/tests/Unit/Handlers/Events/StoredJournalEventHandlerTest.php @@ -0,0 +1,74 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Handlers\Events; + + +use FireflyIII\Events\StoredTransactionJournal; +use FireflyIII\Handlers\Events\StoredJournalEventHandler; +use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; +use Log; +use Tests\TestCase; + +/** + * + * Class StoredJournalEventHandlerTest + */ +class StoredJournalEventHandlerTest extends TestCase +{ + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + + /** + * @covers \FireflyIII\Handlers\Events\StoredJournalEventHandler + * @covers \FireflyIII\Events\StoredTransactionJournal + */ + public function testProcessRules(): void + { +// $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class); +// $journal = $this->user()->transactionJournals()->inRandomOrder()->first(); +// $piggy = $this->user()->piggyBanks()->inRandomOrder()->first(); +// $event = new StoredTransactionJournal($journal, $piggy->id); +// $ruleGroups = $this->user()->ruleGroups()->take(1)->get(); +// $rules = $this->user()->rules()->take(1)->get(); +// +// // mock calls: +// $ruleGroupRepos->shouldReceive('setUser')->once(); +// $ruleGroupRepos->shouldReceive('getActiveGroups')->andReturn($ruleGroups)->once(); +// $ruleGroupRepos->shouldReceive('getActiveStoreRules')->andReturn($rules)->once(); +// +// +// +// $handler = new StoredJournalEventHandler; +// $handler->processRules($event); + $this->assertTrue(true); + + + } +} \ No newline at end of file diff --git a/tests/Unit/Handlers/Events/UserEventHandlerTest.php b/tests/Unit/Handlers/Events/UserEventHandlerTest.php index 6c98ed3ff5..72c7c8d7be 100644 --- a/tests/Unit/Handlers/Events/UserEventHandlerTest.php +++ b/tests/Unit/Handlers/Events/UserEventHandlerTest.php @@ -36,6 +36,7 @@ use Illuminate\Auth\Events\Login; use Illuminate\Support\Facades\Mail; use Mockery; use Tests\TestCase; +use Log; /** * Class UserEventHandlerTest @@ -46,6 +47,15 @@ use Tests\TestCase; */ class UserEventHandlerTest extends TestCase { + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + /** * @covers \FireflyIII\Handlers\Events\UserEventHandler * @covers \FireflyIII\Events\RegisteredUser diff --git a/tests/Unit/Handlers/Events/VersionCheckEventHandlerTest.php b/tests/Unit/Handlers/Events/VersionCheckEventHandlerTest.php index bb33fbab54..9c54051d92 100644 --- a/tests/Unit/Handlers/Events/VersionCheckEventHandlerTest.php +++ b/tests/Unit/Handlers/Events/VersionCheckEventHandlerTest.php @@ -34,12 +34,23 @@ use FireflyIII\Services\Github\Object\Release; use FireflyIII\Services\Github\Request\UpdateRequest; use Mockery; use Tests\TestCase; +use Log; /** * Class VersionCheckEventHandlerTest */ class VersionCheckEventHandlerTest extends TestCase { + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + + /** * */