Refactor and rename test code.

This commit is contained in:
James Cole
2018-05-21 07:22:38 +02:00
parent 620c5f515e
commit 714b54ed06
37 changed files with 363 additions and 939 deletions

View File

@@ -0,0 +1,488 @@
<?php
/**
* ConfigureMappingHandlerTest.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\JobConfiguration\File;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Import\Mapper\Budgets;
use FireflyIII\Import\MapperPreProcess\TagsSpace;
use FireflyIII\Import\Specifics\IngDescription;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\JobConfiguration\File\ConfigureMappingHandler;
use Illuminate\Support\Collection;
use League\Csv\Exception;
use League\Csv\Reader;
use Mockery;
use Tests\TestCase;
/**
* Class ConfigureMappingHandlerTest
*
*/
class ConfigureMappingHandlerTest extends TestCase
{
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureMappingHandler
*/
public function testApplySpecifics(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'mapG' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
$expected = ['a' => 'ING'];
// mock ING description (see below)
$ingDescr = $this->mock(IngDescription::class);
$ingDescr->shouldReceive('run')->once()->andReturn($expected);
$config = [
'specifics' => [
'IngDescription' => 1,
'bad-specific' => 1,
],
];
$handler = new ConfigureMappingHandler;
$handler->setImportJob($job);
$result = $handler->applySpecifics($config, []);
$this->assertEquals($expected, $result);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureMappingHandler
*/
public function testConfigureJob(): void
{
// create fake input for class method:
$input = [
'mapping' => [
0 => [// column
'fake-iban' => 1,
'other-fake-value' => '2', // string
],
1 => [
3 => 2, // fake number
'final-fake-value' => 3,
'mapped-to-zero' => 0,
],
],
];
$expectedResult = [
'column-mapping-config' =>
[
0 => [
'fake-iban' => 1,
'other-fake-value' => 2,
],
1 => [
'3' => 2,
'final-fake-value' => 3,
],
],
];
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'mapA' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
// run configure mapping handler.
// expect specific results:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'ready_to_run']);
$repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $expectedResult]);
$handler = new ConfigureMappingHandler;
$handler->setImportJob($job);
$handler->configureJob($input);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureMappingHandler
*/
public function testDoColumnConfig(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'mapE' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
$fakeBudgets = [
0 => 'dont map',
1 => 'Fake budget A',
4 => 'Other fake budget',
];
// fake budget mapper (see below)
$budgetMapper = $this->mock(Budgets::class);
$budgetMapper->shouldReceive('getMap')->once()->andReturn($fakeBudgets);
// input array:
$input = [
'column-roles' => [
0 => 'description', // cannot be mapped
1 => 'sepa-ct-id', // cannot be mapped
2 => 'tags-space', // cannot be mapped, has a pre-processor.
3 => 'account-id', // can be mapped
4 => 'budget-id' // can be mapped.
],
'column-do-mapping' => [
0 => false, // don't try to map description
1 => true, // try to map sepa (cannot)
2 => true, // try to map tags (cannot)
3 => false, // dont map mappable
4 => true, // want to map, AND can map.
],
];
$expected = [
4 => [
'name' => 'budget-id',
'options' => $fakeBudgets,
'preProcessMap' => '',
'values' => [],
],
];
$handler = new ConfigureMappingHandler;
$handler->setImportJob($job);
try {
$result = $handler->doColumnConfig($input);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals($expected, $result);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureMappingHandler
*/
public function testDoMapOfColumn(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'mapC' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
$combinations = [
['role' => 'description', 'expected' => false, 'requested' => false], // description cannot be mapped. Will always return false.
['role' => 'description', 'expected' => false, 'requested' => true], // description cannot be mapped. Will always return false.
['role' => 'currency-id', 'expected' => false, 'requested' => false], // if not requested, return false.
['role' => 'currency-id', 'expected' => true, 'requested' => true], // if requested, return true.
];
$handler = new ConfigureMappingHandler;
$handler->setImportJob($job);
foreach ($combinations as $info) {
$this->assertEquals($info['expected'], $handler->doMapOfColumn($info['role'], $info['requested']));
}
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureMappingHandler
*/
public function testGetNextData(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'mapH' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [
'column-roles' => [
0 => 'description', // cannot be mapped
1 => 'sepa-ct-id', // cannot be mapped
2 => 'tags-space', // cannot be mapped, has a pre-processor.
3 => 'account-id', // can be mapped
4 => 'budget-id' // can be mapped.
],
'column-do-mapping' => [
0 => false, // don't try to map description
1 => true, // try to map sepa (cannot)
2 => true, // try to map tags (cannot)
3 => false, // dont map mappable
4 => true, // want to map, AND can map.
],
'delimiter' => ',',
];
$job->save();
// make one attachment.
$att = new Attachment;
$att->filename = 'import_file';
$att->user_id = $this->user()->id;
$att->attachable_id = $job->id;
$att->attachable_type = ImportJob::class;
$att->md5 = md5('hello');
$att->mime = 'fake';
$att->size = 3;
$att->save();
// fake some data.
$fileContent = "column1,column2,column3,column4,column5\nvalue1,value2,value3,value4,value5";
$fakeBudgets = [
0 => 'dont map',
1 => 'Fake budget A',
4 => 'Other fake budget',
];
// mock some helpers:
$attachments = $this->mock(AttachmentHelperInterface::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('getConfiguration')->once()->withArgs([Mockery::any()])->andReturn($job->configuration);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getAttachments')->once()->withArgs([Mockery::any()])->andReturn(new Collection([$att]));
$attachments->shouldReceive('getAttachmentContent')->withArgs([Mockery::any()])->andReturn($fileContent);
$budgetMapper = $this->mock(Budgets::class);
$budgetMapper->shouldReceive('getMap')->once()->andReturn($fakeBudgets);
$handler = new ConfigureMappingHandler;
$handler->setImportJob($job);
try {
$result = $handler->getNextData();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$expected = [
4 => [ // is the one with the budget id, remember?
'name' => 'budget-id',
'options' => $fakeBudgets,
'preProcessMap' => '',
'values' => ['column5', 'value5'], // see $fileContent
],
];
$this->assertEquals($expected, $result);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureMappingHandler
*/
public function testGetPreProcessorName(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'mapD' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
$combinations = [
['role' => 'tags-space', 'expected' => TagsSpace::class], // tags- space has a pre-processor. Return it.
['role' => 'description', 'expected' => ''], // description has not.
['role' => 'no-such-role', 'expected' => ''], // not existing role has not.
];
$handler = new ConfigureMappingHandler;
$handler->setImportJob($job);
foreach ($combinations as $info) {
$this->assertEquals($info['expected'], $handler->getPreProcessorName($info['role']));
}
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureMappingHandler
*/
public function testGetReader(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'mapF' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
// make one attachment.
$att = new Attachment;
$att->filename = 'import_file';
$att->user_id = $this->user()->id;
$att->attachable_id = $job->id;
$att->attachable_type = ImportJob::class;
$att->md5 = md5('hello');
$att->mime = 'fake';
$att->size = 3;
$att->save();
$config = [
'delimiter' => ',',
];
$fileContent = "column1,column2,column3\nvalue1,value2,value3";
// mock some helpers:
$attachments = $this->mock(AttachmentHelperInterface::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('getConfiguration')->once()->withArgs([Mockery::any()])->andReturn($config);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getAttachments')->once()->withArgs([Mockery::any()])->andReturn(new Collection([$att]));
$attachments->shouldReceive('getAttachmentContent')->withArgs([Mockery::any()])->andReturn($fileContent);
$handler = new ConfigureMappingHandler;
$handler->setImportJob($job);
try {
$reader = $handler->getReader();
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureMappingHandler
*/
public function testGetValuesForMapping(): void
{
// create a reader to use in method.
// 5 columns, of which #4 (index 3) is budget-id
// 5 columns, of which #5 (index 4) is tags-space
$file = "value1,value2,value3,1,some tags here\nvalue4,value5,value6,2,more tags there\nvalueX,valueY,valueZ\nA,B,C,,\nd,e,f,1,xxx";
$reader = Reader::createFromString($file);
// make config for use in method.
$config = [
'has-headers' => false,
];
// make column config
$columnConfig = [
3 => [
'name' => 'budget-id',
'options' => [
0 => 'dont map',
1 => 'Fake budget A',
4 => 'Other fake budget',
],
'preProcessMap' => '',
'values' => [],
],
];
// expected result
$expected = [
3 => [
'name' => 'budget-id',
'options' => [
0 => 'dont map',
1 => 'Fake budget A',
4 => 'Other fake budget',
],
'preProcessMap' => '',
'values' => ['1', '2'] // all values from column 3 of "CSV" file, minus double values
],
];
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'mapB' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
$handler = new ConfigureMappingHandler;
$handler->setImportJob($job);
$result = [];
try {
$result = $handler->getValuesForMapping($reader, $config, $columnConfig);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals($expected, $result);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureMappingHandler
*/
public function testSanitizeColumnName(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'mapB' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
$handler = new ConfigureMappingHandler;
$handler->setImportJob($job);
$keys = array_keys(config('csv.import_roles'));
foreach ($keys as $key) {
$this->assertEquals($key, $handler->sanitizeColumnName($key));
}
$this->assertEquals('_ignore', $handler->sanitizeColumnName('some-bad-name'));
}
}

View File

@@ -0,0 +1,525 @@
<?php
/**
* ConfigureRolesHandlerTest.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\JobConfiguration\File;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Import\Specifics\IngDescription;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler;
use Illuminate\Support\Collection;
use League\Csv\Reader;
use Mockery;
use Tests\TestCase;
/**
* Class ConfigureRolesHandlerTest
*/
class ConfigureRolesHandlerTest extends TestCase
{
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testConfigurationCompleteBasic(): void
{
$config = [
'column-count' => 5,
'column-roles' => [
0 => 'amount',
1 => 'description',
2 => 'note',
3 => 'foreign-currency-code',
4 => 'amount_foreign',
],
];
$handler = new ConfigureRolesHandler();
$result = $handler->configurationComplete($config);
$this->assertCount(0, $result);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testConfigurationCompleteForeign(): void
{
$config = [
'column-count' => 5,
'column-roles' => [
0 => 'amount',
1 => 'description',
2 => 'note',
3 => 'amount_foreign',
4 => 'sepa-cc',
],
];
$handler = new ConfigureRolesHandler();
$result = $handler->configurationComplete($config);
$this->assertCount(1, $result);
$this->assertEquals(
'If you mark a column as containing an amount in a foreign currency, you must also set the column that contains which currency it is.',
$result->get('error')[0]
);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testConfigurationCompleteNoAmount(): void
{
$config = [
'column-count' => 5,
'column-roles' => [
0 => 'sepa-cc',
1 => 'description',
2 => 'note',
3 => 'foreign-currency-code',
4 => 'amount_foreign',
],
];
$handler = new ConfigureRolesHandler();
$result = $handler->configurationComplete($config);
$this->assertCount(1, $result);
$this->assertEquals(
'At the very least, mark one column as the amount-column. It is advisable to also select a column for the description, date and the opposing account.',
$result->get('error')[0]
);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testConfigureJob(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'role-B' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [
'column-count' => 5,
];
$job->save();
$data = [
'role' => [
0 => 'description',
1 => 'budget-id',
2 => 'sepa-cc',
4 => 'amount', // no column 3.
],
'map' => [
0 => '1', // map column 0 (which cannot be mapped anyway)
1 => '1', // map column 1 (which CAN be mapped)
],
];
$expected = [
'column-count' => 5,
'column-roles' => [
0 => 'description',
1 => 'budget-id',
2 => 'sepa-cc',
3 => '_ignore', // added column 3
4 => 'amount',
],
'column-do-mapping' => [false, true, false, false, false],
];
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'ready_to_run']);
$repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'map']);
$repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $expected]);
$handler = new ConfigureRolesHandler();
$handler->setImportJob($job);
$handler->configureJob($data);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testGetExampleFromLine(): void
{
$lines = [
['one', 'two', '', 'three'],
['four', 'five', '', 'six'],
];
$handler = new ConfigureRolesHandler;
foreach ($lines as $line) {
$handler->getExampleFromLine($line);
}
$expected = [
0 => ['one', 'four'],
1 => ['two', 'five'],
3 => ['three', 'six'],
];
$this->assertEquals($expected, $handler->getExamples());
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testGetExamplesFromFile(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'role-x' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [
'specifics' => [],
'has-headers' => false,
];
$job->save();
$file = "one,two,,three\nfour,five,,six\none,three,X,three";
$reader = Reader::createFromString($file);
$handler = new ConfigureRolesHandler;
$handler->setImportJob($job);
try {
$handler->getExamplesFromFile($reader, $job->configuration);
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
$expected = [
0 => ['one', 'four'],
1 => ['two', 'five', 'three'],
2 => ['X'],
3 => ['three', 'six'],
];
$this->assertEquals($expected, $handler->getExamples());
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testGetHeadersHas(): void
{
// create a reader to use in method.
// 5 columns, of which #4 (index 3) is budget-id
// 5 columns, of which #5 (index 4) is tags-space
$file = "header1,header2,header3,header4,header5\nvalue4,value5,value6,2,more tags there\nvalueX,valueY,valueZ\nA,B,C,,\nd,e,f,1,xxx";
$reader = Reader::createFromString($file);
$config = ['has-headers' => true];
$handler = new ConfigureRolesHandler;
try {
$headers = $handler->getHeaders($reader, $config);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals(['header1', 'header2', 'header3', 'header4', 'header5'], $headers);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testGetHeadersNone(): void
{
// create a reader to use in method.
// 5 columns, of which #4 (index 3) is budget-id
// 5 columns, of which #5 (index 4) is tags-space
$file = "header1,header2,header3,header4,header5\nvalue4,value5,value6,2,more tags there\nvalueX,valueY,valueZ\nA,B,C,,\nd,e,f,1,xxx";
$reader = Reader::createFromString($file);
$config = ['has-headers' => false];
$handler = new ConfigureRolesHandler;
try {
$headers = $handler->getHeaders($reader, $config);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals([], $headers);
}
public function testGetNextData(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'role-x' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [
'delimiter' => ',',
'has-headers' => true,
];
$job->save();
// make one attachment.
$att = new Attachment;
$att->filename = 'import_file';
$att->user_id = $this->user()->id;
$att->attachable_id = $job->id;
$att->attachable_type = ImportJob::class;
$att->md5 = md5('hello');
$att->mime = 'fake';
$att->size = 3;
$att->save();
$fileContent = "column1,column2,column3\nvalue1,value2,value3";
// mock some helpers:
$attachments = $this->mock(AttachmentHelperInterface::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('getConfiguration')->once()->withArgs([Mockery::any()])->andReturn($job->configuration);
$repository->shouldReceive('setConfiguration')->once()->withArgs(
[Mockery::any(),
[
'delimiter' => ',',
'has-headers' => true,
'column-count' => 3,
],
]
);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getAttachments')->once()->withArgs([Mockery::any()])->andReturn(new Collection([$att]));
$attachments->shouldReceive('getAttachmentContent')->withArgs([Mockery::any()])->andReturn($fileContent);
$expected = [
'examples' => [
0 => ['value1'],
1 => ['value2'],
2 => ['value3'],
],
'total' => 3,
'headers' => ['column1', 'column2', 'column3'],
];
$handler = new ConfigureRolesHandler();
$handler->setImportJob($job);
try {
$result = $handler->getNextData();
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals($expected['examples'], $result['examples']);
$this->assertEquals($expected['total'], $result['total']);
$this->assertEquals($expected['headers'], $result['headers']);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testGetReader(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'role-x' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
// make one attachment.
$att = new Attachment;
$att->filename = 'import_file';
$att->user_id = $this->user()->id;
$att->attachable_id = $job->id;
$att->attachable_type = ImportJob::class;
$att->md5 = md5('hello');
$att->mime = 'fake';
$att->size = 3;
$att->save();
$config = [
'delimiter' => ',',
];
$fileContent = "column1,column2,column3\nvalue1,value2,value3";
// mock some helpers:
$attachments = $this->mock(AttachmentHelperInterface::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('getConfiguration')->once()->withArgs([Mockery::any()])->andReturn($config);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getAttachments')->once()->withArgs([Mockery::any()])->andReturn(new Collection([$att]));
$attachments->shouldReceive('getAttachmentContent')->withArgs([Mockery::any()])->andReturn($fileContent);
$handler = new ConfigureRolesHandler();
$handler->setImportJob($job);
try {
$reader = $handler->getReader();
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testIgnoreUnmappableColumns(): void
{
$config = [
'column-count' => 5,
'column-roles' => [
'description', // cannot be mapped.
'budget-id',
'sepa-cc', // cannot be mapped.
'category-id',
'tags-comma', // cannot be mapped.
],
'column-do-mapping' => [
0 => true,
1 => true,
2 => true,
3 => true,
4 => true,
],
];
$expected = [
'column-count' => 5,
'column-roles' => [
'description', // cannot be mapped.
'budget-id',
'sepa-cc', // cannot be mapped.
'category-id',
'tags-comma', // cannot be mapped.
],
'column-do-mapping' => [
0 => false,
1 => true,
2 => false,
3 => true,
4 => false,
],
];
$handler = new ConfigureRolesHandler;
$this->assertEquals($expected, $handler->ignoreUnmappableColumns($config));
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testIsMappingNecessaryNo(): void
{
$config = [
'column-do-mapping' => [false, false, false],
];
$handler = new ConfigureRolesHandler();
$result = $handler->isMappingNecessary($config);
$this->assertFalse($result);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testIsMappingNecessaryYes(): void
{
$config = [
'column-do-mapping' => [false, true, false, false],
];
$handler = new ConfigureRolesHandler();
$result = $handler->isMappingNecessary($config);
$this->assertTrue($result);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testMakeExamplesUnique(): void
{
$lines = [
['one', 'two', '', 'three'],
['four', 'five', '', 'six'],
['one', 'three', 'X', 'three'],
];
$handler = new ConfigureRolesHandler;
foreach ($lines as $line) {
$handler->getExampleFromLine($line);
}
$handler->makeExamplesUnique();
$expected = [
0 => ['one', 'four'],
1 => ['two', 'five', 'three'],
2 => ['X'],
3 => ['three', 'six'],
];
$this->assertEquals($expected, $handler->getExamples());
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testProcessSpecifics(): void
{
$line = [];
$config = [
'specifics' => [
'IngDescription' => true,
'some-bad-specific' => true,
],
];
$ingDescription = $this->mock(IngDescription::class);
$ingDescription->shouldReceive('run')->once()->withArgs([[]])->andReturn(['a' => 'b']);
$handler = new ConfigureRolesHandler;
$this->assertEquals(['a' => 'b'], $handler->processSpecifics($config, []));
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureRolesHandler
*/
public function testSaveColumCount(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'role-A' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser');
$repository->shouldReceive('setConfiguration')->once()
->withArgs([Mockery::any(), ['column-count' => 0]]);
$handler = new ConfigureRolesHandler();
$handler->setImportJob($job);
$handler->saveColumCount();
}
}

View File

@@ -0,0 +1,185 @@
<?php
/**
* ConfigureUploadHandlerTest.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\JobConfiguration\File;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\JobConfiguration\File\ConfigureUploadHandler;
use Mockery;
use Tests\TestCase;
/**
* Class ConfigureUploadHandlerTest
*/
class ConfigureUploadHandlerTest extends TestCase
{
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureUploadHandler
*/
public function testConfigureJobAccount(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'upload-B' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
$data = [
'csv_import_account' => '1',
'csv_delimiter' => ',',
'has_headers' => '1',
'date_format' => 'Y-m-d',
'apply_rules' => '1',
'specifics' => ['IngDescription'],
];
$expectedConfig = [
'has-headers' => true,
'date-format' => 'Y-m-d',
'delimiter' => ',',
'apply-rules' => true,
'specifics' => [
'IngDescription' => 1,
],
'import-account' => 1,
];
$repository = $this->mock(ImportJobRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('findNull')->once()->withArgs([1])->andReturn($this->user()->accounts()->first());
$repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $expectedConfig]);
$repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'roles']);
$handler = new ConfigureUploadHandler;
$handler->setImportJob($job);
$result = $handler->configureJob($data);
$this->assertCount(0, $result);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureUploadHandler
*/
public function testConfigureJobNoAccount(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'upload-B' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
$data = [
'csv_import_account' => '1',
'csv_delimiter' => ',',
'has_headers' => '1',
'date_format' => 'Y-m-d',
'apply_rules' => '1',
'specifics' => ['IngDescription'],
];
$expectedConfig = [
'has-headers' => true,
'date-format' => 'Y-m-d',
'delimiter' => ',',
'apply-rules' => true,
'specifics' => [
'IngDescription' => 1,
],
];
$repository = $this->mock(ImportJobRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('findNull')->once()->withArgs([1])->andReturn(null);
$repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $expectedConfig]);
$handler = new ConfigureUploadHandler;
$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]);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureUploadHandler
*/
public function testGetNextData(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'upload-A' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser');
$repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), ['date-format' => 'Ymd']]);
$handler = new ConfigureUploadHandler;
$handler->setImportJob($job);
$result = $handler->getNextData();
$expected = [
'accounts' => [],
'delimiters' => [],
];
// not much to compare, really.
$this->assertEquals($expected['accounts'], $result['accounts']);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\ConfigureUploadHandler
*/
public function testGetSpecifics(): void
{
$array = [
'specifics' => [
'IngDescription', 'BadFakeNewsThing',
],
];
$expected = [
'IngDescription' => 1,
];
$handler = new ConfigureUploadHandler;
$result = $handler->getSpecifics($array);
$this->assertEquals($expected, $result);
}
}

View File

@@ -0,0 +1,305 @@
<?php
/**
* NewFileJobHandlerTest.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\JobConfiguration\File;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\JobConfiguration\File\NewFileJobHandler;
use Illuminate\Support\Collection;
use Mockery;
use Tests\TestCase;
/**
* Class NewFileJobHandlerTest
*/
class NewFileJobHandlerTest extends TestCase
{
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\NewFileJobHandler
*/
public function testConfigureJob(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'newfile-A' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [
'delimiter' => ',',
'has-headers' => true,
];
$job->save();
// make one attachment.
$att = new Attachment;
$att->filename = 'configuration_file';
$att->user_id = $this->user()->id;
$att->attachable_id = $job->id;
$att->attachable_type = ImportJob::class;
$att->md5 = md5('hello');
$att->mime = 'fake';
$att->size = 3;
$att->save();
// mock stuff
$attachments = $this->mock(AttachmentHelperInterface::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getConfiguration')->andReturn([])->once();
$repository->shouldReceive('getAttachments')->twice()->andReturn(new Collection([$att]));
$attachments->shouldReceive('getAttachmentContent')->times(3)->andReturn('{"a": "b"}');
$repository->shouldReceive('setConfiguration')->withArgs([Mockery::any(), ['file-type' => 'csv']])->once();
$repository->shouldReceive('setConfiguration')->withArgs([Mockery::any(), ['a' => 'b']])->twice();
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'configure-upload'])->once();
// data for configure job:
$data = [
'import_file_type' => 'csv',
];
$handler = new NewFileJobHandler;
$handler->setImportJob($job);
try {
$messages = $handler->configureJob($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertCount(0, $messages);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\NewFileJobHandler
*/
public function testConfigureJobBadData(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'newfile-A' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [
'delimiter' => ',',
'has-headers' => true,
];
$job->save();
// make one attachment.
$att = new Attachment;
$att->filename = 'configuration_file';
$att->user_id = $this->user()->id;
$att->attachable_id = $job->id;
$att->attachable_type = ImportJob::class;
$att->md5 = md5('hello');
$att->mime = 'fake';
$att->size = 3;
$att->save();
// get file:
$content = file_get_contents(storage_path('build') . '/ebcdic.txt');
// mock stuff
$attachments = $this->mock(AttachmentHelperInterface::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getAttachments')->once()->andReturn(new Collection([$att]));
$attachments->shouldReceive('getAttachmentContent')->once()->andReturn($content);
// data for configure job:
$data = [
'import_file_type' => 'csv',
];
$handler = new NewFileJobHandler;
$handler->setImportJob($job);
try {
$messages = $handler->configureJob($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertCount(1, $messages);
$this->assertEquals(
'The file you have uploaded is not encoded as UTF-8 or ASCII. Firefly III cannot handle such files. Please use Notepad++ or Sublime to convert your file to UTF-8.',
$messages->first()
);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\NewFileJobHandler
*/
public function testStoreConfiguration(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'newfile-A' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [
'delimiter' => ',',
'has-headers' => true,
];
$job->save();
// make one attachment.
$att = new Attachment;
$att->filename = 'configuration_file';
$att->user_id = $this->user()->id;
$att->attachable_id = $job->id;
$att->attachable_type = ImportJob::class;
$att->md5 = md5('hello');
$att->mime = 'fake';
$att->size = 3;
$att->save();
// mock stuff
$attachments = $this->mock(AttachmentHelperInterface::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getAttachments')->once()->andReturn(new Collection([$att]));
$attachments->shouldReceive('getAttachmentContent')->once()->andReturn('{"a": "b"}');
$repository->shouldReceive('setConfiguration')->withArgs([Mockery::any(), ['a' => 'b']])->once();
$handler = new NewFileJobHandler;
$handler->setImportJob($job);
try {
$handler->storeConfiguration();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\NewFileJobHandler
*/
public function testValidateAttachments(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'newfile-x' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [
'delimiter' => ',',
'has-headers' => true,
];
$job->save();
// make one attachment.
$att = new Attachment;
$att->filename = 'import_file';
$att->user_id = $this->user()->id;
$att->attachable_id = $job->id;
$att->attachable_type = ImportJob::class;
$att->md5 = md5('hello');
$att->mime = 'fake';
$att->size = 3;
$att->save();
// mock stuff
$attachments = $this->mock(AttachmentHelperInterface::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getAttachments')->andReturn(new Collection([$att]));
$attachments->shouldReceive('getAttachmentContent')->once()->andReturn('Hello!');
$handler = new NewFileJobHandler;
$handler->setImportJob($job);
try {
$result = $handler->validateAttachments();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertCount(0, $result);
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\File\NewFileJobHandler
*/
public function testValidateNotUTF(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'newfile-x' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [
'delimiter' => ',',
'has-headers' => true,
];
$job->save();
// make one attachment.
$att = new Attachment;
$att->filename = 'import_file';
$att->user_id = $this->user()->id;
$att->attachable_id = $job->id;
$att->attachable_type = ImportJob::class;
$att->md5 = md5('hello');
$att->mime = 'fake';
$att->size = 3;
$att->save();
// get file:
$content = file_get_contents(storage_path('build') . '/ebcdic.txt');
// mock stuff
$attachments = $this->mock(AttachmentHelperInterface::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getAttachments')->andReturn(new Collection([$att]));
$attachments->shouldReceive('getAttachmentContent')->once()->andReturn($content);
$handler = new NewFileJobHandler;
$handler->setImportJob($job);
try {
$result = $handler->validateAttachments();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertCount(1, $result);
$this->assertEquals(
'The file you have uploaded is not encoded as UTF-8 or ASCII. Firefly III cannot handle such files. Please use Notepad++ or Sublime to convert your file to UTF-8.',
$result->first()
);
}
}