Improved code coverage for events and reports.

This commit is contained in:
James Cole
2018-08-24 16:07:33 +02:00
parent 2b54363dd7
commit 850a0ae17e
14 changed files with 566 additions and 101 deletions

View File

@@ -0,0 +1,230 @@
<?php
/**
* MonthReportGeneratorTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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']);
}
}

View File

@@ -0,0 +1,76 @@
<?php
/**
* APIEventHandlerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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;
}
);
}
}

View File

@@ -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
}
);
}
}
}

View File

@@ -0,0 +1,80 @@
<?php
/**
* AutomationHandlerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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;
}
);
}
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* StoredJournalEventHandlerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@@ -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

View File

@@ -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)));
}
/**
*
*/