mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Lots of refactoring and new tests.
This commit is contained in:
@@ -273,7 +273,7 @@ class ConfigureMappingHandlerTest extends TestCase
|
||||
$att->filename = 'import_file';
|
||||
$att->user_id = $this->user()->id;
|
||||
$att->attachable_id = $job->id;
|
||||
$att->attachable_type = Attachment::class;
|
||||
$att->attachable_type = ImportJob::class;
|
||||
$att->md5 = md5('hello');
|
||||
$att->mime = 'fake';
|
||||
$att->size = 3;
|
||||
@@ -366,7 +366,7 @@ class ConfigureMappingHandlerTest extends TestCase
|
||||
$att->filename = 'import_file';
|
||||
$att->user_id = $this->user()->id;
|
||||
$att->attachable_id = $job->id;
|
||||
$att->attachable_type = Attachment::class;
|
||||
$att->attachable_type = ImportJob::class;
|
||||
$att->md5 = md5('hello');
|
||||
$att->mime = 'fake';
|
||||
$att->size = 3;
|
||||
|
@@ -286,7 +286,7 @@ class ConfigureRolesHandlerTest extends TestCase
|
||||
$att->filename = 'import_file';
|
||||
$att->user_id = $this->user()->id;
|
||||
$att->attachable_id = $job->id;
|
||||
$att->attachable_type = Attachment::class;
|
||||
$att->attachable_type = ImportJob::class;
|
||||
$att->md5 = md5('hello');
|
||||
$att->mime = 'fake';
|
||||
$att->size = 3;
|
||||
@@ -353,7 +353,7 @@ class ConfigureRolesHandlerTest extends TestCase
|
||||
$att->filename = 'import_file';
|
||||
$att->user_id = $this->user()->id;
|
||||
$att->attachable_id = $job->id;
|
||||
$att->attachable_type = Attachment::class;
|
||||
$att->attachable_type = ImportJob::class;
|
||||
$att->md5 = md5('hello');
|
||||
$att->mime = 'fake';
|
||||
$att->size = 3;
|
||||
|
@@ -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\Configuration\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\Configuration\File\NewFileJobHandler;
|
||||
use Illuminate\Support\Collection;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class NewFileJobHandlerTest
|
||||
*/
|
||||
class NewFileJobHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Configuration\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->setJob($job);
|
||||
try {
|
||||
$messages = $handler->configureJob($data);
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertTrue(false, $e->getMessage());
|
||||
}
|
||||
$this->assertCount(0, $messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Configuration\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->setJob($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\Configuration\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->setJob($job);
|
||||
|
||||
try {
|
||||
$handler->storeConfiguration();
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertTrue(false, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Configuration\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->setJob($job);
|
||||
|
||||
try {
|
||||
$result = $handler->validateAttachments();
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertTrue(false, $e->getMessage());
|
||||
}
|
||||
$this->assertCount(0, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Configuration\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->setJob($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()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
/**
|
||||
* AssetAccountMapperTest.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 FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Support\Import\Routine\File\AssetAccountMapper;
|
||||
use Illuminate\Support\Collection;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class AssetAccountMapperTest
|
||||
*/
|
||||
class AssetAccountMapperTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Should return with the given $default account and not the $bad one.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\AssetAccountMapper
|
||||
*/
|
||||
public function testBadAsset(): void
|
||||
{
|
||||
$bad = $this->user()->accounts()->where('account_type_id', 4)->inRandomOrder()->first();
|
||||
$default = $this->user()->accounts()->where('account_type_id', 3)->inRandomOrder()->first();
|
||||
// mock repository:
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findNull')->once()->withArgs([$bad->id])->andReturn($bad);
|
||||
$repository->shouldReceive('findNull')->once()->withArgs([$default->id])->andReturn($default);
|
||||
|
||||
$mapper = new AssetAccountMapper;
|
||||
$mapper->setUser($this->user());
|
||||
$mapper->setDefaultAccount($default->id);
|
||||
$result = $mapper->map($bad->id, []);
|
||||
$this->assertEquals($default->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should return with the given $expected account.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\AssetAccountMapper
|
||||
*/
|
||||
public function testCorrectAsset(): void
|
||||
{
|
||||
$expected = $this->user()->accounts()->where('account_type_id', 3)->inRandomOrder()->first();
|
||||
|
||||
// mock repository:
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findNull')->once()->withArgs([$expected->id])->andReturn($expected);
|
||||
|
||||
$mapper = new AssetAccountMapper;
|
||||
$mapper->setUser($this->user());
|
||||
$result = $mapper->map($expected->id, []);
|
||||
$this->assertEquals($expected->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should return with the $default account.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\AssetAccountMapper
|
||||
*/
|
||||
public function testEmpty(): void
|
||||
{
|
||||
$default = $this->user()->accounts()->where('account_type_id', 3)->inRandomOrder()->first();
|
||||
|
||||
// mock repository:
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findNull')->once()->withArgs([$default->id])->andReturn($default);
|
||||
|
||||
$mapper = new AssetAccountMapper;
|
||||
$mapper->setUser($this->user());
|
||||
$mapper->setDefaultAccount($default->id);
|
||||
$result = $mapper->map(null, []);
|
||||
$this->assertEquals($default->id, $result->id);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Should return with the $default account.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\AssetAccountMapper
|
||||
*/
|
||||
public function testEmptyNoDefault(): void
|
||||
{
|
||||
$fallback = $this->user()->accounts()->where('account_type_id', 3)->first();
|
||||
|
||||
// mock repository:
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::ASSET]])->andReturn(
|
||||
new Collection([$fallback])
|
||||
);
|
||||
|
||||
$mapper = new AssetAccountMapper;
|
||||
$mapper->setUser($this->user());
|
||||
$result = $mapper->map(null, []);
|
||||
$this->assertEquals($fallback->id, $result->id);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Should search for the given IBAN and return $expected.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\AssetAccountMapper
|
||||
*/
|
||||
public function testFindByIban(): void
|
||||
{
|
||||
$searchValue = 'IamIban';
|
||||
$expected = $this->user()->accounts()->where('account_type_id', 3)->inRandomOrder()->first();
|
||||
// mock repository:
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findByIbanNull')->once()->withArgs([$searchValue, [AccountType::ASSET]])->andReturn($expected);
|
||||
|
||||
$mapper = new AssetAccountMapper;
|
||||
$mapper->setUser($this->user());
|
||||
$result = $mapper->map(0, ['iban' => $searchValue]);
|
||||
$this->assertEquals($expected->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should search for the given name and return $expected.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\AssetAccountMapper
|
||||
*/
|
||||
public function testFindByName(): void
|
||||
{
|
||||
$searchValue = 'AccountName';
|
||||
$expected = $this->user()->accounts()->where('account_type_id', 3)->inRandomOrder()->first();
|
||||
// mock repository:
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findByName')->once()->withArgs([$searchValue, [AccountType::ASSET]])->andReturn($expected);
|
||||
|
||||
$mapper = new AssetAccountMapper;
|
||||
$mapper->setUser($this->user());
|
||||
$result = $mapper->map(0, ['name' => $searchValue]);
|
||||
$this->assertEquals($expected->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should search for the given number and return $expected.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\AssetAccountMapper
|
||||
*/
|
||||
public function testFindByNumber(): void
|
||||
{
|
||||
$searchValue = '123456';
|
||||
$expected = $this->user()->accounts()->where('account_type_id', 3)->inRandomOrder()->first();
|
||||
// mock repository:
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findByAccountNumber')->once()->withArgs([$searchValue, [AccountType::ASSET]])->andReturn($expected);
|
||||
|
||||
$mapper = new AssetAccountMapper;
|
||||
$mapper->setUser($this->user());
|
||||
$result = $mapper->map(0, ['number' => $searchValue]);
|
||||
$this->assertEquals($expected->id, $result->id);
|
||||
}
|
||||
|
||||
}
|
84
tests/Unit/Support/Import/Routine/File/CSVProcessorTest.php
Normal file
84
tests/Unit/Support/Import/Routine/File/CSVProcessorTest.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* CSVProcessorTest.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 FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Support\Import\Routine\File\CSVProcessor;
|
||||
use FireflyIII\Support\Import\Routine\File\ImportableConverter;
|
||||
use FireflyIII\Support\Import\Routine\File\ImportableCreator;
|
||||
use FireflyIII\Support\Import\Routine\File\LineReader;
|
||||
use FireflyIII\Support\Import\Routine\File\MappedValuesValidator;
|
||||
use FireflyIII\Support\Import\Routine\File\MappingConverger;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Do some end to end testing here, perhaps?
|
||||
*
|
||||
* Class CSVProcessorTest
|
||||
*/
|
||||
class CSVProcessorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\CSVProcessor
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
|
||||
// mock all classes:
|
||||
$lineReader = $this->mock(LineReader::class);
|
||||
$lineReader->shouldReceive('setImportJob')->once();
|
||||
$lineReader->shouldReceive('getLines')->once()->andReturn([]);
|
||||
|
||||
$mappingConverger = $this->mock(MappingConverger::class);
|
||||
$mappingConverger->shouldReceive('setImportJob')->once();
|
||||
$mappingConverger->shouldReceive('converge')->withArgs([[]])->andReturn([])->once();
|
||||
$mappingConverger->shouldReceive('getMappedValues')->andReturn([])->once();
|
||||
|
||||
|
||||
$validator = $this->mock(MappedValuesValidator::class);
|
||||
$validator->shouldReceive('validate')->andReturn([]);
|
||||
|
||||
$creator = $this->mock(ImportableCreator::class);
|
||||
$creator->shouldReceive('convertSets')->withArgs([[]])->andReturn([])->once();
|
||||
|
||||
$converter = $this->mock(ImportableConverter::class);
|
||||
$converter->shouldReceive('setImportJob')->once();
|
||||
$converter->shouldReceive('setMappedValues')->once()->withArgs([[]])->andReturn([]);
|
||||
$converter->shouldReceive('convert')->withArgs([[]])->once()->andReturn([]);
|
||||
|
||||
|
||||
/** @var ImportJob $job */
|
||||
$job = $this->user()->importJobs()->first();
|
||||
$processor = new CSVProcessor;
|
||||
$processor->setImportJob($job);
|
||||
try {
|
||||
$result = $processor->run();
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertTrue(false, $e->getMessage());
|
||||
}
|
||||
$this->assertEquals([], $result);
|
||||
}
|
||||
}
|
175
tests/Unit/Support/Import/Routine/File/CurrencyMapperTest.php
Normal file
175
tests/Unit/Support/Import/Routine/File/CurrencyMapperTest.php
Normal file
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
/**
|
||||
* CurrencyMapperTest.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 FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Support\Import\Routine\File\CurrencyMapper;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class CurrencyMapperTest
|
||||
*/
|
||||
class CurrencyMapperTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
$currency = TransactionCurrency::inRandomOrder()->first();
|
||||
// mock data
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findNull')->once()->withArgs([$currency->id])->andReturn($currency);
|
||||
$mapper = new CurrencyMapper();
|
||||
$mapper->setUser($this->user());
|
||||
|
||||
$result = $mapper->map($currency->id, []);
|
||||
$this->assertEquals($currency->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
|
||||
*/
|
||||
public function testBasicNotFound(): void
|
||||
{
|
||||
$currency = TransactionCurrency::inRandomOrder()->first();
|
||||
// mock data
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findNull')->once()->withArgs([$currency->id])->andReturn(null);
|
||||
$repository->shouldReceive('store')->once()
|
||||
->withArgs([['code' => null, 'name' => null, 'symbol' => null, 'decimal_places' => 2]])->andReturn(null);
|
||||
$mapper = new CurrencyMapper();
|
||||
$mapper->setUser($this->user());
|
||||
|
||||
$result = $mapper->map($currency->id, []);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
|
||||
*/
|
||||
public function testFindByCode(): void
|
||||
{
|
||||
$currency = TransactionCurrency::inRandomOrder()->first();
|
||||
// mock data
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findByCodeNull')->withArgs([$currency->code])
|
||||
->andReturn($currency)->once();
|
||||
|
||||
$mapper = new CurrencyMapper();
|
||||
$mapper->setUser($this->user());
|
||||
|
||||
$result = $mapper->map(null, ['code' => $currency->code]);
|
||||
$this->assertEquals($currency->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
|
||||
*/
|
||||
public function testFindByName(): void
|
||||
{
|
||||
$currency = TransactionCurrency::inRandomOrder()->first();
|
||||
// mock data
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findByNameNull')->withArgs([$currency->name])
|
||||
->andReturn($currency)->once();
|
||||
|
||||
$mapper = new CurrencyMapper();
|
||||
$mapper->setUser($this->user());
|
||||
|
||||
$result = $mapper->map(null, ['name' => $currency->name]);
|
||||
$this->assertEquals($currency->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
|
||||
*/
|
||||
public function testFindBySymbol(): void
|
||||
{
|
||||
$currency = TransactionCurrency::inRandomOrder()->first();
|
||||
// mock data
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findBySymbolNull')->withArgs([$currency->symbol])
|
||||
->andReturn($currency)->once();
|
||||
|
||||
$mapper = new CurrencyMapper();
|
||||
$mapper->setUser($this->user());
|
||||
|
||||
$result = $mapper->map(null, ['symbol' => $currency->symbol]);
|
||||
$this->assertEquals($currency->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
|
||||
*/
|
||||
public function testFindAndCreate(): void
|
||||
{
|
||||
$currency = TransactionCurrency::inRandomOrder()->first();
|
||||
// mock data
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findBySymbolNull')->withArgs([$currency->symbol])->andReturn(null)->once();
|
||||
$repository->shouldReceive('findByCodeNull')->withArgs([$currency->code])->andReturn(null)->once();
|
||||
$repository->shouldReceive('findByNameNull')->withArgs([$currency->name])->andReturn(null)->once();
|
||||
|
||||
// nothing found, mapper will try to create it.
|
||||
$repository->shouldReceive('store')
|
||||
->withArgs([['code' => $currency->code, 'name' => $currency->name, 'symbol' => $currency->symbol,'decimal_places'=>2]])
|
||||
->once()->andReturn($currency);
|
||||
|
||||
$mapper = new CurrencyMapper();
|
||||
$mapper->setUser($this->user());
|
||||
|
||||
$result = $mapper->map(null, ['name' => $currency->name, 'code' => $currency->code, 'symbol' => $currency->symbol]);
|
||||
$this->assertEquals($currency->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
|
||||
*/
|
||||
public function testEmpty(): void
|
||||
{
|
||||
|
||||
// mock data
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('store')->once()
|
||||
->withArgs([['code' => null, 'name' => null, 'symbol' => null, 'decimal_places' => 2]])->andReturn(null);
|
||||
|
||||
|
||||
$mapper = new CurrencyMapper();
|
||||
$mapper->setUser($this->user());
|
||||
|
||||
$result = $mapper->map(null, []);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
}
|
@@ -64,7 +64,7 @@ class LineReaderTest extends TestCase
|
||||
$att->filename = 'import_file';
|
||||
$att->user_id = $this->user()->id;
|
||||
$att->attachable_id = $job->id;
|
||||
$att->attachable_type = Attachment::class;
|
||||
$att->attachable_type = ImportJob::class;
|
||||
$att->md5 = md5('hello');
|
||||
$att->mime = 'fake';
|
||||
$att->size = 3;
|
||||
|
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* MappedValuesValidatorTest.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 FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Support\Import\Routine\File\MappedValuesValidator;
|
||||
use Illuminate\Support\Collection;
|
||||
use stdClass;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class MappedValuesValidatorTest
|
||||
*/
|
||||
class MappedValuesValidatorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\MappedValuesValidator
|
||||
*/
|
||||
public function testValidateBasic(): void
|
||||
{
|
||||
|
||||
$toValidate = [
|
||||
'opposing-id' => [1, 2, 3],
|
||||
'account-id' => [4, 5, 6],
|
||||
'currency-id' => [7, 8, 9],
|
||||
'foreign-currency-id' => [10, 11, 12],
|
||||
'bill-id' => [13, 14, 15],
|
||||
'budget-id' => [16, 17, 18],
|
||||
'category-id' => [19, 20, 21],
|
||||
];
|
||||
$return = [
|
||||
'opposing-id' => new Collection([$this->objectWithId(1), $this->objectWithId(2)]),
|
||||
'account-id' => new Collection([$this->objectWithId(4), $this->objectWithId(5)]),
|
||||
'currency-id' => new Collection([$this->objectWithId(7), $this->objectWithId(9)]),
|
||||
'foreign-currency-id' => new Collection([$this->objectWithId(10), $this->objectWithId(11)]),
|
||||
'bill-id' => new Collection([$this->objectWithId(13), $this->objectWithId(15)]),
|
||||
'budget-id' => new Collection([$this->objectWithId(16), $this->objectWithId(17)]),
|
||||
'category-id' => new Collection([$this->objectWithId(19), $this->objectWithId(21)]),
|
||||
];
|
||||
// mock stuff:
|
||||
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$billRepos = $this->mock(BillRepositoryInterface::class);
|
||||
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
|
||||
$catRepos = $this->mock(CategoryRepositoryInterface::class);
|
||||
|
||||
// should receive a lot of stuff:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$accountRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$billRepos->shouldReceive('setUser')->once();
|
||||
$budgetRepos->shouldReceive('setUser')->once();
|
||||
$catRepos->shouldReceive('setUser')->once();
|
||||
|
||||
$accountRepos->shouldReceive('getAccountsById')->once()->withArgs([$toValidate['account-id']])->andReturn($return['account-id']);
|
||||
$accountRepos->shouldReceive('getAccountsById')->once()->withArgs([$toValidate['opposing-id']])->andReturn($return['opposing-id']);
|
||||
$currencyRepos->shouldReceive('getByIds')->once()->withArgs([$toValidate['currency-id']])->andReturn($return['currency-id']);
|
||||
$currencyRepos->shouldReceive('getByIds')->once()->withArgs([$toValidate['foreign-currency-id']])->andReturn($return['foreign-currency-id']);
|
||||
$billRepos->shouldReceive('getByIds')->once()->withArgs([$toValidate['bill-id']])->andReturn($return['bill-id']);
|
||||
$budgetRepos->shouldReceive('getByIds')->once()->withArgs([$toValidate['budget-id']])->andReturn($return['budget-id']);
|
||||
$catRepos->shouldReceive('getByIds')->once()->withArgs([$toValidate['category-id']])->andReturn($return['category-id']);
|
||||
|
||||
|
||||
$expected = [
|
||||
'opposing-id' => [1, 2],
|
||||
'account-id' => [4, 5],
|
||||
'currency-id' => [7, 9],
|
||||
'foreign-currency-id' => [10, 11],
|
||||
'bill-id' => [13, 15],
|
||||
'budget-id' => [16, 17],
|
||||
'category-id' => [19, 21],
|
||||
];
|
||||
$validator = new MappedValuesValidator;
|
||||
$validator->setImportJob($this->user()->importJobs()->first());
|
||||
|
||||
try {
|
||||
$result = $validator->validate($toValidate);
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertTrue(false, $e->getMessage());
|
||||
}
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return stdClass
|
||||
*/
|
||||
private function objectWithId(int $id): stdClass
|
||||
{
|
||||
$obj = new stdClass();
|
||||
$obj->id = $id;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* OpposingAccountMapperTest.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 Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class OpposingAccountMapperTest
|
||||
*/
|
||||
class OpposingAccountMapperTest extends TestCase
|
||||
{
|
||||
|
||||
}
|
Reference in New Issue
Block a user