Code consistency and new tests.

This commit is contained in:
James Cole
2018-05-12 15:50:01 +02:00
parent a47da92d81
commit 5bf520b6ed
35 changed files with 1262 additions and 1157 deletions

View File

@@ -74,7 +74,7 @@ class ConfigureMappingHandlerTest extends TestCase
];
$handler = new ConfigureMappingHandler;
$handler->setJob($job);
$handler->setImportJob($job);
$result = $handler->applySpecifics($config, []);
$this->assertEquals($expected, $result);
@@ -139,7 +139,7 @@ class ConfigureMappingHandlerTest extends TestCase
$handler = new ConfigureMappingHandler;
$handler->setJob($job);
$handler->setImportJob($job);
$handler->configureJob($input);
}
@@ -197,7 +197,7 @@ class ConfigureMappingHandlerTest extends TestCase
];
$handler = new ConfigureMappingHandler;
$handler->setJob($job);
$handler->setImportJob($job);
try {
$result = $handler->doColumnConfig($input);
} catch (FireflyException $e) {
@@ -231,7 +231,7 @@ class ConfigureMappingHandlerTest extends TestCase
];
$handler = new ConfigureMappingHandler;
$handler->setJob($job);
$handler->setImportJob($job);
foreach ($combinations as $info) {
$this->assertEquals($info['expected'], $handler->doMapOfColumn($info['role'], $info['requested']));
}
@@ -298,7 +298,7 @@ class ConfigureMappingHandlerTest extends TestCase
$handler = new ConfigureMappingHandler;
$handler->setJob($job);
$handler->setImportJob($job);
try {
$result = $handler->getNextData();
} catch (FireflyException $e) {
@@ -340,7 +340,7 @@ class ConfigureMappingHandlerTest extends TestCase
];
$handler = new ConfigureMappingHandler;
$handler->setJob($job);
$handler->setImportJob($job);
foreach ($combinations as $info) {
$this->assertEquals($info['expected'], $handler->getPreProcessorName($info['role']));
}
@@ -386,7 +386,7 @@ class ConfigureMappingHandlerTest extends TestCase
$attachments->shouldReceive('getAttachmentContent')->withArgs([Mockery::any()])->andReturn($fileContent);
$handler = new ConfigureMappingHandler;
$handler->setJob($job);
$handler->setImportJob($job);
try {
$reader = $handler->getReader();
} catch (Exception $e) {
@@ -449,7 +449,7 @@ class ConfigureMappingHandlerTest extends TestCase
$job->save();
$handler = new ConfigureMappingHandler;
$handler->setJob($job);
$handler->setImportJob($job);
$result = [];
try {
$result = $handler->getValuesForMapping($reader, $config, $columnConfig);
@@ -478,7 +478,7 @@ class ConfigureMappingHandlerTest extends TestCase
$job->save();
$handler = new ConfigureMappingHandler;
$handler->setJob($job);
$handler->setImportJob($job);
$keys = array_keys(config('csv.import_roles'));
foreach ($keys as $key) {
$this->assertEquals($key, $handler->sanitizeColumnName($key));

View File

@@ -159,7 +159,7 @@ class ConfigureRolesHandlerTest extends TestCase
$repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $expected]);
$handler = new ConfigureRolesHandler();
$handler->setJob($job);
$handler->setImportJob($job);
$handler->configureJob($data);
}
@@ -207,7 +207,7 @@ class ConfigureRolesHandlerTest extends TestCase
$file = "one,two,,three\nfour,five,,six\none,three,X,three";
$reader = Reader::createFromString($file);
$handler = new ConfigureRolesHandler;
$handler->setJob($job);
$handler->setImportJob($job);
try {
$handler->getExamplesFromFile($reader, $job->configuration);
} catch (Exception $e) {
@@ -321,7 +321,7 @@ class ConfigureRolesHandlerTest extends TestCase
];
$handler = new ConfigureRolesHandler();
$handler->setJob($job);
$handler->setImportJob($job);
try {
$result = $handler->getNextData();
} catch (Exception $e) {
@@ -373,7 +373,7 @@ class ConfigureRolesHandlerTest extends TestCase
$attachments->shouldReceive('getAttachmentContent')->withArgs([Mockery::any()])->andReturn($fileContent);
$handler = new ConfigureRolesHandler();
$handler->setJob($job);
$handler->setImportJob($job);
try {
$reader = $handler->getReader();
} catch (Exception $e) {
@@ -518,7 +518,7 @@ class ConfigureRolesHandlerTest extends TestCase
->withArgs([Mockery::any(), ['column-count' => 0]]);
$handler = new ConfigureRolesHandler();
$handler->setJob($job);
$handler->setImportJob($job);
$handler->saveColumCount();
}

View File

@@ -80,7 +80,7 @@ class ConfigureUploadHandlerTest extends TestCase
$repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'roles']);
$handler = new ConfigureUploadHandler;
$handler->setJob($job);
$handler->setImportJob($job);
$result = $handler->configureJob($data);
$this->assertCount(0, $result);
}
@@ -127,7 +127,7 @@ class ConfigureUploadHandlerTest extends TestCase
$repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $expectedConfig]);
$handler = new ConfigureUploadHandler;
$handler->setJob($job);
$handler->setImportJob($job);
$result = $handler->configureJob($data);
$this->assertCount(1, $result);
$this->assertEquals('You have selected an invalid account to import into.', $result->get('account')[0]);
@@ -153,7 +153,7 @@ class ConfigureUploadHandlerTest extends TestCase
$repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), ['date-format' => 'Ymd']]);
$handler = new ConfigureUploadHandler;
$handler->setJob($job);
$handler->setImportJob($job);
$result = $handler->getNextData();
$expected = [
'accounts' => [],

View File

@@ -85,7 +85,7 @@ class NewFileJobHandlerTest extends TestCase
];
$handler = new NewFileJobHandler;
$handler->setJob($job);
$handler->setImportJob($job);
try {
$messages = $handler->configureJob($data);
} catch (FireflyException $e) {
@@ -139,7 +139,7 @@ class NewFileJobHandlerTest extends TestCase
];
$handler = new NewFileJobHandler;
$handler->setJob($job);
$handler->setImportJob($job);
try {
$messages = $handler->configureJob($data);
} catch (FireflyException $e) {
@@ -190,7 +190,7 @@ class NewFileJobHandlerTest extends TestCase
$repository->shouldReceive('setConfiguration')->withArgs([Mockery::any(), ['a' => 'b']])->once();
$handler = new NewFileJobHandler;
$handler->setJob($job);
$handler->setImportJob($job);
try {
$handler->storeConfiguration();
@@ -237,7 +237,7 @@ class NewFileJobHandlerTest extends TestCase
$handler = new NewFileJobHandler;
$handler->setJob($job);
$handler->setImportJob($job);
try {
$result = $handler->validateAttachments();
@@ -288,7 +288,7 @@ class NewFileJobHandlerTest extends TestCase
$handler = new NewFileJobHandler;
$handler->setJob($job);
$handler->setImportJob($job);
try {
$result = $handler->validateAttachments();

View File

@@ -0,0 +1,535 @@
<?php
/**
* ImportableConverterTest.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\Support\Import\Routine\File;
use Amount;
use Carbon\Carbon;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\Placeholder\ImportTransaction;
use FireflyIII\Support\Import\Routine\File\AssetAccountMapper;
use FireflyIII\Support\Import\Routine\File\CurrencyMapper;
use FireflyIII\Support\Import\Routine\File\ImportableConverter;
use FireflyIII\Support\Import\Routine\File\OpposingAccountMapper;
use Mockery;
use Tests\TestCase;
/**
* todo test foreign currency
* todo test budget (known and unknown)
* todo test category (known and unknown)
* todo test foreign currency
*
* Class ImportableConverterTest
*/
class ImportableConverterTest extends TestCase
{
/**
* Basic test. Should match a withdrawal. Amount is negative.
*
* @covers \FireflyIII\Support\Import\Routine\File\ImportableConverter
*/
public function testBasic(): void
{
$nullAccount = ['name' => null, 'iban' => null, 'number' => null, 'bic' => null];
$importable = new ImportTransaction;
$importable->amount = '-45.67';
$importable->date = '20180917';
$importable->tags = ['a', 'b', 'c'];
$importables = [$importable];
$job = $this->user()->importJobs()->first();
$job->configuration = [
'date-format' => 'Ymd',
];
$job->save();
// mock used classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$assetMapper = $this->mock(AssetAccountMapper::class);
$opposingMapper = $this->mock(OpposingAccountMapper::class);
$currencyMapper = $this->mock(CurrencyMapper::class);
// get default currency
$euro = TransactionCurrency::whereCode('EUR')->first();
$usd = TransactionCurrency::whereCode('USD')->first();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
// set user and config:
$repository->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setUser')->once();
$opposingMapper->shouldReceive('setUser')->once();
$currencyMapper->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setDefaultAccount')->withArgs([0])->once();
// respond to mapping call:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$expense = $this->user()->accounts()->where('account_type_id', 4)->first();
$assetMapper->shouldReceive('map')->once()->withArgs([null, $nullAccount])->andReturn($asset);
$opposingMapper->shouldReceive('map')->once()->withArgs([null, '-45.67', $nullAccount])->andReturn($expense);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['name' => null, 'code' => null, 'symbol' => null]])->andReturn($usd);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['code' => null]])->andReturn(null);
$converter = new ImportableConverter;
$converter->setImportJob($job);
$result = $converter->convert($importables);
// verify content of $result
$this->assertEquals('withdrawal', $result[0]['type']);
$this->assertEquals('2018-09-17', $result[0]['date']);
$this->assertEquals($importable->tags, $result[0]['tags']);
$this->assertEquals($usd->id, $result[0]['transactions'][0]['currency_id']);
}
/**
* Two asset accounts mean its a transfer.
*
* @covers \FireflyIII\Support\Import\Routine\File\ImportableConverter
*/
public function testBasicDefaultCurrency(): void
{
$nullAccount = ['name' => null, 'iban' => null, 'number' => null, 'bic' => null];
$importable = new ImportTransaction;
$importable->amount = '45.67';
$importables = [$importable];
$job = $this->user()->importJobs()->first();
$job->configuration = [
'date-format' => 'Ymd',
];
$job->save();
// mock used classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$assetMapper = $this->mock(AssetAccountMapper::class);
$opposingMapper = $this->mock(OpposingAccountMapper::class);
$currencyMapper = $this->mock(CurrencyMapper::class);
// get default currency
$euro = TransactionCurrency::whereCode('EUR')->first();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
// set user and config:
$repository->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setUser')->once();
$opposingMapper->shouldReceive('setUser')->once();
$currencyMapper->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setDefaultAccount')->withArgs([0])->once();
// respond to mapping call:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$other = $this->user()->accounts()->where('account_type_id', 3)->where('id', '!=', $asset->id)->first();
$assetMapper->shouldReceive('map')->once()->withArgs([null, $nullAccount])->andReturn($asset);
$opposingMapper->shouldReceive('map')->once()->withArgs([null, '45.67', $nullAccount])->andReturn($other);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['name' => null, 'code' => null, 'symbol' => null]])->andReturn(null);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['code' => null]])->andReturn(null);
$converter = new ImportableConverter;
$converter->setImportJob($job);
$result = $converter->convert($importables);
// verify content of $result
$today = new Carbon();
$this->assertEquals('transfer', $result[0]['type']);
$this->assertEquals($today->format('Y-m-d'), $result[0]['date']);
$this->assertEquals([], $result[0]['tags']);
$this->assertEquals($euro->id, $result[0]['transactions'][0]['currency_id']);
}
/**
* Positive amount, so transaction is a deposit.
*
* @covers \FireflyIII\Support\Import\Routine\File\ImportableConverter
*/
public function testBasicDeposit(): void
{
$nullAccount = ['name' => null, 'iban' => null, 'number' => null, 'bic' => null];
$importable = new ImportTransaction;
$importable->amount = '45.67';
$importable->date = '20180917';
$importable->meta['date-book'] = '2018-01-02';
$importables = [$importable];
$job = $this->user()->importJobs()->first();
$job->configuration = [
'date-format' => 'Ymd',
];
$job->save();
// mock used classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$assetMapper = $this->mock(AssetAccountMapper::class);
$opposingMapper = $this->mock(OpposingAccountMapper::class);
$currencyMapper = $this->mock(CurrencyMapper::class);
// get default currency
$euro = TransactionCurrency::whereCode('EUR')->first();
$usd = TransactionCurrency::whereCode('USD')->first();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
// set user and config:
$repository->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setUser')->once();
$opposingMapper->shouldReceive('setUser')->once();
$currencyMapper->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setDefaultAccount')->withArgs([0])->once();
// respond to mapping call:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$revenue = $this->user()->accounts()->where('account_type_id', 5)->first();
$assetMapper->shouldReceive('map')->once()->withArgs([null, $nullAccount])->andReturn($asset);
$opposingMapper->shouldReceive('map')->once()->withArgs([null, '45.67', $nullAccount])->andReturn($revenue);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['name' => null, 'code' => null, 'symbol' => null]])->andReturn($usd);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['code' => null]])->andReturn(null);
$converter = new ImportableConverter;
$converter->setImportJob($job);
$result = $converter->convert($importables);
// verify content of $result
$this->assertEquals('deposit', $result[0]['type']);
$this->assertEquals('2018-09-17', $result[0]['date']);
$this->assertEquals([], $result[0]['tags']);
$this->assertEquals($usd->id, $result[0]['transactions'][0]['currency_id']);
$this->assertEquals($revenue->id, $result[0]['transactions'][0]['source_id']);
$this->assertEquals($asset->id, $result[0]['transactions'][0]['destination_id']);
$this->assertEquals($importable->meta['date-book'], $result[0]['book_date']);
}
/**
* Source and destination are the same. Should result in error message.
*
* @covers \FireflyIII\Support\Import\Routine\File\ImportableConverter
*/
public function testBasicSameAssets(): void
{
$nullAccount = ['name' => null, 'iban' => null, 'number' => null, 'bic' => null];
$importable = new ImportTransaction;
$importable->amount = '-45.67';
$importable->date = '20180917';
$importables = [$importable];
$job = $this->user()->importJobs()->first();
$job->configuration = [
'date-format' => 'Ymd',
];
$job->save();
// mock used classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$assetMapper = $this->mock(AssetAccountMapper::class);
$opposingMapper = $this->mock(OpposingAccountMapper::class);
$currencyMapper = $this->mock(CurrencyMapper::class);
// get default currency
$euro = TransactionCurrency::whereCode('EUR')->first();
$usd = TransactionCurrency::whereCode('USD')->first();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
// set user and config:
$repository->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setUser')->once();
$opposingMapper->shouldReceive('setUser')->once();
$currencyMapper->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setDefaultAccount')->withArgs([0])->once();
// respond to mapping call:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$assetMapper->shouldReceive('map')->once()->withArgs([null, $nullAccount])->andReturn($asset);
$opposingMapper->shouldReceive('map')->once()->withArgs([null, '-45.67', $nullAccount])->andReturn($asset);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['name' => null, 'code' => null, 'symbol' => null]])->andReturn($usd);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['code' => null]])->andReturn(null);
$repository->shouldReceive('addErrorMessage')->withArgs(
[Mockery::any(),
'Row #1: Source ("' . $asset->name . '", #' . $asset->id . ') and destination ("' . $asset->name . '", #' . $asset->id . ') are the same account.']
)->once();
$converter = new ImportableConverter;
$converter->setImportJob($job);
$result = $converter->convert($importables);
$this->assertEquals([], $result);
}
/**
* Two asset accounts mean its a transfer. This has a positive amount.
*
* @covers \FireflyIII\Support\Import\Routine\File\ImportableConverter
*/
public function testBasicTransfer(): void
{
$nullAccount = ['name' => null, 'iban' => null, 'number' => null, 'bic' => null];
$importable = new ImportTransaction;
$importable->amount = '45.67';
$importable->date = '20180917';
$importable->billId = 2; // will be ignored because it's not valid.
$importable->billName = 'Some Bill'; // will be included because bill ID is not valid.
$importables = [$importable];
$job = $this->user()->importJobs()->first();
$job->configuration = [
'date-format' => 'Ymd',
];
$job->save();
// mock used classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$assetMapper = $this->mock(AssetAccountMapper::class);
$opposingMapper = $this->mock(OpposingAccountMapper::class);
$currencyMapper = $this->mock(CurrencyMapper::class);
// get default currency
$euro = TransactionCurrency::whereCode('EUR')->first();
$usd = TransactionCurrency::whereCode('USD')->first();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
// set user and config:
$repository->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setUser')->once();
$opposingMapper->shouldReceive('setUser')->once();
$currencyMapper->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setDefaultAccount')->withArgs([0])->once();
// respond to mapping call:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$other = $this->user()->accounts()->where('account_type_id', 3)->where('id', '!=', $asset->id)->first();
$assetMapper->shouldReceive('map')->once()->withArgs([null, $nullAccount])->andReturn($asset);
$opposingMapper->shouldReceive('map')->once()->withArgs([null, '45.67', $nullAccount])->andReturn($other);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['name' => null, 'code' => null, 'symbol' => null]])->andReturn($usd);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['code' => null]])->andReturn(null);
$converter = new ImportableConverter;
$converter->setImportJob($job);
$result = $converter->convert($importables);
// verify content of $result
$this->assertEquals('transfer', $result[0]['type']);
$this->assertEquals('2018-09-17', $result[0]['date']);
$this->assertEquals([], $result[0]['tags']);
$this->assertNull($result[0]['bill_id']);
$this->assertEquals($importable->billName, $result[0]['bill_name']);
$this->assertEquals($usd->id, $result[0]['transactions'][0]['currency_id']);
// since amount is positive, $asset recieves the money
$this->assertEquals($other->id, $result[0]['transactions'][0]['source_id']);
$this->assertEquals($asset->id, $result[0]['transactions'][0]['destination_id']);
}
/**
* Transfer with negative amount flows the other direction. See source_id and destination_id
*
* @covers \FireflyIII\Support\Import\Routine\File\ImportableConverter
*/
public function testBasicTransferNegative(): void
{
$nullAccount = ['name' => null, 'iban' => null, 'number' => null, 'bic' => null];
$importable = new ImportTransaction;
$importable->amount = '-45.67';
$importable->date = '20180917';
$importable->billId = 3; // is added to array of valid values, see below.
$importable->billName = 'Some bill'; // will be ignored because ID is valid.
$importables = [$importable];
$validMappings = [
'bill-id' => [3],
];
$job = $this->user()->importJobs()->first();
$job->configuration = [
'date-format' => 'Ymd',
];
$job->save();
// mock used classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$assetMapper = $this->mock(AssetAccountMapper::class);
$opposingMapper = $this->mock(OpposingAccountMapper::class);
$currencyMapper = $this->mock(CurrencyMapper::class);
// get default currency
$euro = TransactionCurrency::whereCode('EUR')->first();
$usd = TransactionCurrency::whereCode('USD')->first();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
// set user and config:
$repository->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setUser')->once();
$opposingMapper->shouldReceive('setUser')->once();
$currencyMapper->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setDefaultAccount')->withArgs([0])->once();
// respond to mapping call:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$other = $this->user()->accounts()->where('account_type_id', 3)->where('id', '!=', $asset->id)->first();
$assetMapper->shouldReceive('map')->once()->withArgs([null, $nullAccount])->andReturn($asset);
$opposingMapper->shouldReceive('map')->once()->withArgs([null, '-45.67', $nullAccount])->andReturn($other);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['name' => null, 'code' => null, 'symbol' => null]])->andReturn($usd);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['code' => null]])->andReturn(null);
$converter = new ImportableConverter;
$converter->setImportJob($job);
$converter->setMappedValues($validMappings);
$result = $converter->convert($importables);
// verify content of $result
$this->assertEquals('transfer', $result[0]['type']);
$this->assertEquals('2018-09-17', $result[0]['date']);
$this->assertEquals([], $result[0]['tags']);
$this->assertEquals(3, $result[0]['bill_id']);
$this->assertNull($result[0]['bill_name']);
$this->assertEquals($usd->id, $result[0]['transactions'][0]['currency_id']);
// since amount is negative, $asset sends the money
$this->assertEquals($asset->id, $result[0]['transactions'][0]['source_id']);
$this->assertEquals($other->id, $result[0]['transactions'][0]['destination_id']);
}
/**
* When source and dest are weird account types, will give error.
*
* @covers \FireflyIII\Support\Import\Routine\File\ImportableConverter
*/
public function testBasicWeirdAccounts(): void
{
$nullAccount = ['name' => null, 'iban' => null, 'number' => null, 'bic' => null];
$importable = new ImportTransaction;
$importable->amount = '-45.67';
$importable->date = '20180917';
$importables = [$importable];
$job = $this->user()->importJobs()->first();
$job->configuration = [
'date-format' => 'Ymd',
];
$job->save();
// mock used classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$assetMapper = $this->mock(AssetAccountMapper::class);
$opposingMapper = $this->mock(OpposingAccountMapper::class);
$currencyMapper = $this->mock(CurrencyMapper::class);
// get default currency
$euro = TransactionCurrency::whereCode('EUR')->first();
$usd = TransactionCurrency::whereCode('USD')->first();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
// set user and config:
$repository->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setUser')->once();
$opposingMapper->shouldReceive('setUser')->once();
$currencyMapper->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setDefaultAccount')->withArgs([0])->once();
// respond to mapping call:
$asset = $this->user()->accounts()->where('account_type_id', 6)->first();
$other = $this->user()->accounts()->where('account_type_id', 2)->where('id', '!=', $asset)->first();
$assetMapper->shouldReceive('map')->once()->withArgs([null, $nullAccount])->andReturn($asset);
$opposingMapper->shouldReceive('map')->once()->withArgs([null, '-45.67', $nullAccount])->andReturn($other);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['name' => null, 'code' => null, 'symbol' => null]])->andReturn($usd);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['code' => null]])->andReturn(null);
$repository->shouldReceive('addErrorMessage')->withArgs(
[Mockery::any(), 'Row #1: Cannot determine transaction type. Source account is a Initial balance account, destination is a Cash account']
)->once();
$converter = new ImportableConverter;
$converter->setImportJob($job);
$result = $converter->convert($importables);
$this->assertEquals([], $result);
}
/**
* Submit no amount information.
*
* @covers \FireflyIII\Support\Import\Routine\File\ImportableConverter
*/
public function testEmpty(): void
{
$job = $this->user()->importJobs()->first();
$job->configuration = [];
$job->save();
// mock used classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$assetMapper = $this->mock(AssetAccountMapper::class);
$opposingMapper = $this->mock(OpposingAccountMapper::class);
$currencyMapper = $this->mock(CurrencyMapper::class);
$euro = TransactionCurrency::whereCode('EUR')->first();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
$repository->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setUser')->once();
$opposingMapper->shouldReceive('setUser')->once();
$currencyMapper->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setDefaultAccount')->withArgs([0])->once();
$converter = new ImportableConverter;
$converter->setImportJob($job);
}
/**
* Basic input until it stops crashing.
*
* @covers \FireflyIII\Support\Import\Routine\File\ImportableConverter
*/
public function testNoAmount(): void
{
$importable = new ImportTransaction;
$importables = [$importable];
$job = $this->user()->importJobs()->first();
$job->configuration = [];
$job->save();
// mock used classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$assetMapper = $this->mock(AssetAccountMapper::class);
$opposingMapper = $this->mock(OpposingAccountMapper::class);
$currencyMapper = $this->mock(CurrencyMapper::class);
$euro = TransactionCurrency::whereCode('EUR')->first();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
$repository->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setUser')->once();
$opposingMapper->shouldReceive('setUser')->once();
$currencyMapper->shouldReceive('setUser')->once();
$assetMapper->shouldReceive('setDefaultAccount')->withArgs([0])->once();
$repository->shouldReceive('addErrorMessage')->withArgs([Mockery::any(), 'Row #1: No transaction amount information.'])->once();
$converter = new ImportableConverter;
$converter->setImportJob($job);
$result = $converter->convert($importables);
$this->assertEquals([], $result);
}
}