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

@@ -64,7 +64,7 @@ class JobConfigurationControllerTest extends TestCase
$configurator = $this->mock(FakeJobConfiguration::class);
// mock calls:
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('setImportJob')->once();
$configurator->shouldReceive('configurationComplete')->once()->andReturn(false);
$configurator->shouldReceive('getNextView')->once()->andReturn('import.fake.apply-rules');
$configurator->shouldReceive('getNextData')->once()
@@ -122,7 +122,7 @@ class JobConfigurationControllerTest extends TestCase
$configurator = $this->mock(FakeJobConfiguration::class);
// mock calls:
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('setImportJob')->once();
$configurator->shouldReceive('configurationComplete')->once()->andReturn(true);
$repository->shouldReceive('updateStatus')->withArgs([Mockery::any(), 'ready_to_run']);
@@ -154,7 +154,7 @@ class JobConfigurationControllerTest extends TestCase
$configurator = $this->mock(FakeJobConfiguration::class);
// mock calls:
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('setImportJob')->once();
$configurator->shouldReceive('configurationComplete')->once()->andReturn(false);
$configurator->shouldReceive('configureJob')->withArgs([[]])->once()->andReturn($messages);
@@ -214,7 +214,7 @@ class JobConfigurationControllerTest extends TestCase
$configurator = $this->mock(FakeJobConfiguration::class);
// mock calls:
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('setImportJob')->once();
$configurator->shouldReceive('configurationComplete')->once()->andReturn(true);
$repository->shouldReceive('updateStatus')->withArgs([Mockery::any(), 'ready_to_run']);
@@ -247,7 +247,7 @@ class JobConfigurationControllerTest extends TestCase
$configurator = $this->mock(FakeJobConfiguration::class);
// mock calls:
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('setImportJob')->once();
$configurator->shouldReceive('configurationComplete')->once()->andReturn(false);
$configurator->shouldReceive('configureJob')->once()->andReturn($messages);
$repository->shouldReceive('storeFileUpload')->once()->andReturn(new MessageBag);

View File

@@ -198,7 +198,7 @@ class JobStatusControllerTest extends TestCase
// mock calls:
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'running']);
$routine->shouldReceive('setJob')->once();
$routine->shouldReceive('setImportJob')->once();
$routine->shouldReceive('run')->once();
// call thing.
@@ -229,7 +229,7 @@ class JobStatusControllerTest extends TestCase
// mock calls:
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'running']);
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'error']);
$routine->shouldReceive('setJob')->once();
$routine->shouldReceive('setImportJob')->once();
$routine->shouldReceive('run')->andThrow(new Exception('Unknown exception'));
// call thing.
@@ -260,7 +260,7 @@ class JobStatusControllerTest extends TestCase
// mock calls:
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'running']);
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'error']);
$routine->shouldReceive('setJob')->once();
$routine->shouldReceive('setImportJob')->once();
$routine->shouldReceive('run')->andThrow(new FireflyException('Unknown exception'));
// call thing.
@@ -312,7 +312,7 @@ class JobStatusControllerTest extends TestCase
// mock calls:
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'storing_data']);
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'storage_finished']);
$storage->shouldReceive('setJob')->once();
$storage->shouldReceive('setImportJob')->once();
$storage->shouldReceive('store')->once();
@@ -343,7 +343,7 @@ class JobStatusControllerTest extends TestCase
// mock calls:
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'storing_data']);
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'error']);
$storage->shouldReceive('setJob')->once();
$storage->shouldReceive('setImportJob')->once();
$storage->shouldReceive('store')->once()->andThrow(new FireflyException('Some storage exception.'));

View File

@@ -53,7 +53,7 @@ class FakeJobConfigurationTest extends TestCase
// should be false:
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$this->assertFalse($configurator->configurationComplete());
}
@@ -76,7 +76,7 @@ class FakeJobConfigurationTest extends TestCase
// should be false:
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$this->assertFalse($configurator->configurationComplete());
}
@@ -101,7 +101,7 @@ class FakeJobConfigurationTest extends TestCase
// should be false:
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$this->assertFalse($configurator->configurationComplete());
}
@@ -129,7 +129,7 @@ class FakeJobConfigurationTest extends TestCase
// should be false:
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$this->assertFalse($configurator->configurationComplete());
}
@@ -156,7 +156,7 @@ class FakeJobConfigurationTest extends TestCase
// should be false:
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$this->assertFalse($configurator->configurationComplete());
}
@@ -184,7 +184,7 @@ class FakeJobConfigurationTest extends TestCase
// should be false:
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$this->assertTrue($configurator->configurationComplete());
}
@@ -211,7 +211,7 @@ class FakeJobConfigurationTest extends TestCase
// should be false:
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$this->assertTrue($configurator->configurationComplete());
}
@@ -245,7 +245,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$messages = $configurator->configureJob($data);
$this->assertTrue($messages->has('some_key'));
}
@@ -280,7 +280,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$messages = $configurator->configureJob($data);
$this->assertTrue($messages->has('some_key'));
}
@@ -315,7 +315,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$messages = $configurator->configureJob($data);
$this->assertTrue($messages->has('some_key'));
}
@@ -350,7 +350,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$messages = $configurator->configureJob($data);
$this->assertTrue($messages->has('some_key'));
}
@@ -385,7 +385,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$messages = $configurator->configureJob($data);
$this->assertTrue($messages->has('some_key'));
}
@@ -420,7 +420,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$messages = $configurator->configureJob($data);
$this->assertTrue($messages->has('some_key'));
}
@@ -455,7 +455,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$messages = $configurator->configureJob($data);
$this->assertTrue($messages->has('some_key'));
}
@@ -490,7 +490,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$messages = $configurator->configureJob($data);
$this->assertTrue($messages->has('some_key'));
}
@@ -514,7 +514,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$view = $configurator->getNextView();
$this->assertEquals('import.fake.enter-album', $view);
}
@@ -538,7 +538,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$view = $configurator->getNextView();
$this->assertEquals('import.fake.enter-artist', $view);
}
@@ -562,7 +562,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$view = $configurator->getNextView();
$this->assertEquals('import.fake.apply-rules', $view);
}
@@ -586,7 +586,7 @@ class FakeJobConfigurationTest extends TestCase
// call configuration
$configurator = new FakeJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$view = $configurator->getNextView();
$this->assertEquals('import.fake.enter-song', $view);
}

View File

@@ -59,7 +59,7 @@ class FileJobConfigurationTest extends TestCase
// should be false:
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$this->assertFalse($configurator->configurationComplete());
}
@@ -82,7 +82,7 @@ class FileJobConfigurationTest extends TestCase
// should be false:
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$this->assertTrue($configurator->configurationComplete());
}
@@ -107,10 +107,10 @@ class FileJobConfigurationTest extends TestCase
$result = null;
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$handler = $this->mock(ConfigureMappingHandler::class);
$handler->shouldReceive('setJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('setImportJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('configureJob')->withArgs([['c' => 'd']])->andReturn($bag)->once();
try {
@@ -140,10 +140,10 @@ class FileJobConfigurationTest extends TestCase
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$handler = $this->mock(ConfigureUploadHandler::class);
$handler->shouldReceive('setJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('setImportJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('getNextData')->andReturn(['a' => 'b'])->withNoArgs()->once();
try {
@@ -173,10 +173,10 @@ class FileJobConfigurationTest extends TestCase
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$handler = $this->mock(ConfigureMappingHandler::class);
$handler->shouldReceive('setJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('setImportJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('getNextData')->andReturn(['a' => 'b'])->withNoArgs()->once();
try {
@@ -206,10 +206,10 @@ class FileJobConfigurationTest extends TestCase
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$handler = $this->mock(NewFileJobHandler::class);
$handler->shouldReceive('setJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('setImportJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('getNextData')->andReturn(['a' => 'b'])->withNoArgs()->once();
try {
@@ -239,10 +239,10 @@ class FileJobConfigurationTest extends TestCase
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
$handler = $this->mock(ConfigureRolesHandler::class);
$handler->shouldReceive('setJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('setImportJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('getNextData')->andReturn(['a' => 'b'])->withNoArgs()->once();
try {
@@ -272,7 +272,7 @@ class FileJobConfigurationTest extends TestCase
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
try {
$result = $configurator->getNextView();
} catch (FireflyException $e) {
@@ -300,7 +300,7 @@ class FileJobConfigurationTest extends TestCase
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
try {
$result = $configurator->getNextView();
} catch (FireflyException $e) {
@@ -328,7 +328,7 @@ class FileJobConfigurationTest extends TestCase
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
try {
$result = $configurator->getNextView();
} catch (FireflyException $e) {
@@ -356,7 +356,7 @@ class FileJobConfigurationTest extends TestCase
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$configurator->setImportJob($job);
try {
$result = $configurator->getNextView();
} catch (FireflyException $e) {

View File

@@ -65,7 +65,7 @@ class FakeRoutineTest extends TestCase
$routine = new FakeRoutine;
$routine->setJob($job);
$routine->setImportJob($job);
try {
$routine->run();
} catch (FireflyException $e) {
@@ -98,10 +98,10 @@ class FakeRoutineTest extends TestCase
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'final'])->once();
$repository->shouldReceive('setTransactions')->withArgs([Mockery::any(), []])->once();
$handler->shouldReceive('getTransactions')->once()->andReturn([]);
$handler->shouldReceive('setJob')->once();
$handler->shouldReceive('setImportJob')->once();
$routine = new FakeRoutine;
$routine->setJob($job);
$routine->setImportJob($job);
try {
$routine->run();
} catch (FireflyException $e) {
@@ -136,7 +136,7 @@ class FakeRoutineTest extends TestCase
$routine = new FakeRoutine;
$routine->setJob($job);
$routine->setImportJob($job);
try {
$routine->run();
} catch (FireflyException $e) {

View File

@@ -63,12 +63,12 @@ class FileRoutineTest extends TestCase
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'final'])->once();
$repository->shouldReceive('setTransactions')->withArgs([Mockery::any(), ['a' => 'b']])->once();
$repository->shouldReceive('getConfiguration')->withArgs([Mockery::any()])->once()->andReturn([]);
$processor->shouldReceive('setJob')->once();
$processor->shouldReceive('setImportJob')->once();
$processor->shouldReceive('run')->once()->andReturn(['a' => 'b']);
$routine = new FileRoutine;
$routine->setJob($job);
$routine->setImportJob($job);
try {
$routine->run();
} catch (FireflyException $e) {

View File

@@ -76,7 +76,7 @@ class ImportArrayStorageTest extends TestCase
$journalRepos->shouldReceive('setUser')->once();
$storage = new ImportArrayStorage;
$storage->setJob($job);
$storage->setImportJob($job);
}
/**
@@ -131,7 +131,7 @@ class ImportArrayStorageTest extends TestCase
->withArgs([Mockery::any(), 'Entry #1 ("' . $transactions[1]['description'] . '") could not be imported. It already exists.']);
$storage = new ImportArrayStorage;
$storage->setJob($job);
$storage->setImportJob($job);
$result = new Collection;
try {
$result = $storage->store();
@@ -170,7 +170,7 @@ class ImportArrayStorageTest extends TestCase
$journalRepos->shouldReceive('setUser')->once();
$storage = new ImportArrayStorage;
$storage->setJob($job);
$storage->setImportJob($job);
$result = new Collection;
try {
$result = $storage->store();
@@ -214,7 +214,7 @@ class ImportArrayStorageTest extends TestCase
$journalRepos->shouldReceive('setUser')->once();
$storage = new ImportArrayStorage;
$storage->setJob($job);
$storage->setImportJob($job);
$result = new Collection;
try {
$result = $storage->store();
@@ -265,7 +265,7 @@ class ImportArrayStorageTest extends TestCase
$journalRepos->shouldReceive('findByHash')->andReturn(null)->once();
$storage = new ImportArrayStorage;
$storage->setJob($job);
$storage->setImportJob($job);
$result = new Collection;
try {
$result = $storage->store();
@@ -320,7 +320,7 @@ class ImportArrayStorageTest extends TestCase
$journalRepos->shouldReceive('findByHash')->andReturn(null)->once();
$storage = new ImportArrayStorage;
$storage->setJob($job);
$storage->setImportJob($job);
$result = new Collection;
try {
$result = $storage->store();
@@ -393,7 +393,7 @@ class ImportArrayStorageTest extends TestCase
$collector->shouldReceive('getJournals')->andReturn($transferCollection);
$storage = new ImportArrayStorage;
$storage->setJob($job);
$storage->setImportJob($job);
$result = new Collection;
try {
$result = $storage->store();
@@ -472,7 +472,7 @@ class ImportArrayStorageTest extends TestCase
$collector->shouldReceive('getJournals')->andReturn($transferCollection);
$storage = new ImportArrayStorage;
$storage->setJob($job);
$storage->setImportJob($job);
$result = new Collection;
try {
$result = $storage->store();

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