First step in improving test coverage.

This commit is contained in:
James Cole
2018-08-30 20:58:07 +02:00
parent e158b9b64e
commit 33fe6dbfa3
19 changed files with 517 additions and 29 deletions

View File

@@ -0,0 +1,126 @@
<?php
/**
* NetWorthTest.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\Helpers\Report;
use Amount;
use Carbon\Carbon;
use FireflyIII\Helpers\Report\NetWorth;
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 NetWorthTest
*/
class NetWorthTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Helpers\Report\NetWorth
*/
public function testGetNetWorthByCurrency(): void
{
// variables
$accounts = $this->user()->accounts()->take(2)->where('account_type_id', 3)->get();
$date = new Carbon('2018-01-01');
$balanceInfo = [];
foreach ($accounts as $account) {
$balanceInfo[$account->id] = '100';
}
// mock repositories
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
// mock calls to facades
Amount::shouldReceive('getDefaultCurrencyByUser')->once()->andReturn(TransactionCurrency::find(1));
Steam::shouldReceive('balancesByAccounts')->once()->withAnyArgs()->andReturn($balanceInfo);
// mock other calls:
$accountRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->times(2)->andReturn('1');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->times(2)->andReturn('defaultAsset');
$currencyRepos->shouldReceive('setUser')->once();
$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->times(1);
$helper = new NetWorth();
$helper->setUser($this->user());
$result = $helper->getNetWorthByCurrency($accounts, $date);
$this->assertEquals(200, (int)$result[0]['balance']);
}
/**
* @covers \FireflyIII\Helpers\Report\NetWorth
*/
public function testGetNetWorthByCurrencyWithCC(): void
{
// variables
$first = $this->user()->accounts()->take(1)->where('account_type_id', 3)->first();
$second = $this->user()->accounts()->take(1)->where('account_type_id', 3)->where('id', '!=', $first->id)->first();
$second->virtualBalance = '500';
$date = new Carbon('2018-01-01');
$balanceInfo = [];
$balanceInfo[$first->id] = '100';
$balanceInfo[$second->id] = '100';
// mock repositories
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
// mock calls to facades
Amount::shouldReceive('getDefaultCurrencyByUser')->once()->andReturn(TransactionCurrency::find(1));
Steam::shouldReceive('balancesByAccounts')->once()->withAnyArgs()->andReturn($balanceInfo);
// mock other calls:
$accountRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->times(2)->andReturn('1');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->times(2)->andReturn('defaultAsset', 'ccAsset');
$currencyRepos->shouldReceive('setUser')->once();
$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->times(1);
$helper = new NetWorth();
$helper->setUser($this->user());
$result = $helper->getNetWorthByCurrency(new Collection([$first, $second]), $date);
$this->assertEquals(-300, (int)$result[0]['balance']);
}
}

View File

@@ -63,9 +63,9 @@ class BunqRoutineTest extends TestCase
$handler->shouldReceive('setImportJob')->once();
$handler->shouldReceive('run')->once();
$handler->shouldReceive('getTransactions')->once()->andReturn(['a' => 'c']);
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'provider_finished'])->once();
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'final'])->once();
$repository->shouldReceive('setTransactions')->withArgs([Mockery::any(), ['a' => 'c']])->once();
//$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'provider_finished'])->once();
//$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'final'])->once();
$repository->shouldReceive('appendTransactions')->withArgs([Mockery::any(), ['a' => 'c']])->once();
$routine = new BunqRoutine;
$routine->setImportJob($job);

View File

@@ -172,9 +172,16 @@ class StageImportDataHandlerTest extends TestCase
->andReturn($deposit)->once();
// set new last transaction ID:
$lastPref = new Preference;
$lastPref->data =0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-last-transaction-1234', 0])->andReturn($lastPref)->once();
$lastPref = new Preference;
$lastPref->data = 0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-oldest-transaction-1234', 0])->andReturn($lastPref)->times(2);
$lastPref = new Preference;
$lastPref->data = 0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-newest-transaction-1234', 0])->andReturn($lastPref)->once();
// todo: improve test thing:
Preferences::shouldReceive('setForUser');
$handler = new StageImportDataHandler;
@@ -239,9 +246,16 @@ class StageImportDataHandlerTest extends TestCase
$payment->shouldReceive('listing')->once()->andReturn($list);
// set new last transaction ID:
$lastPref = new Preference;
$lastPref->data =0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-last-transaction-1234', 0])->andReturn($lastPref)->once();
$lastPref = new Preference;
$lastPref->data = 0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-oldest-transaction-1234', 0])->andReturn($lastPref)->times(2);
$lastPref = new Preference;
$lastPref->data = 0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-newest-transaction-1234', 0])->andReturn($lastPref)->once();
// todo: improve test thing:
Preferences::shouldReceive('setForUser');
$handler = new StageImportDataHandler;
$handler->setImportJob($job);
@@ -290,9 +304,16 @@ class StageImportDataHandlerTest extends TestCase
$pointer = new Pointer('iban', 'ES2364265841767173822054', 'Test Site');
// set new last transaction ID:
$lastPref = new Preference;
$lastPref->data =0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-last-transaction-1234', 0])->andReturn($lastPref)->once();
$lastPref = new Preference;
$lastPref->data = 0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-oldest-transaction-1234', 0])->andReturn($lastPref)->times(2);
$lastPref = new Preference;
$lastPref->data = 0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-newest-transaction-1234', 0])->andReturn($lastPref)->once();
// todo: improve test thing:
Preferences::shouldReceive('setForUser');
// ignore the deprecated fields:
@@ -447,9 +468,9 @@ class StageImportDataHandlerTest extends TestCase
$list = new BunqResponsePaymentList($value, [], null);
// set new last transaction ID:
$lastPref = new Preference;
$lastPref->data =0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-last-transaction-1234', 0])->andReturn($lastPref)->once();
$lastPref = new Preference;
$lastPref->data = 0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-oldest-transaction-1234', 0])->andReturn($lastPref)->once();
$expectedTransactions = [
[
@@ -510,6 +531,18 @@ class StageImportDataHandlerTest extends TestCase
$accountRepository->shouldReceive('findByIbanNull')->withArgs(['RS88844660406878687897', [AccountType::ASSET]])->once()->andReturn($asset);
// set new last transaction ID:
$lastPref = new Preference;
$lastPref->data = 0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-oldest-transaction-1234', 0])->andReturn($lastPref)->times(1);
$lastPref = new Preference;
$lastPref->data = 0;
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq-newest-transaction-1234', 0])->andReturn($lastPref)->once();
// todo: improve test thing:
Preferences::shouldReceive('setForUser');
$handler = new StageImportDataHandler;
$handler->setImportJob($job);
try {