mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-03 03:00:14 +00:00
Update test code.
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* ImportableCreatorTest.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\Support\Import\Placeholder\ColumnValue;
|
||||
use FireflyIII\Support\Import\Placeholder\ImportTransaction;
|
||||
use FireflyIII\Support\Import\Routine\File\ImportableCreator;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class ImportableCreatorTest
|
||||
*/
|
||||
class ImportableCreatorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\ImportableCreator
|
||||
*/
|
||||
public function testConvertSets(): void
|
||||
{
|
||||
$columnValue =new ColumnValue();
|
||||
$columnValue->setOriginalRole('account-name');
|
||||
$columnValue->setRole('account-id');
|
||||
$columnValue->setValue('Checking Account');
|
||||
$columnValue->setMappedValue(1);
|
||||
|
||||
$input = [
|
||||
[
|
||||
$columnValue
|
||||
]
|
||||
];
|
||||
|
||||
|
||||
$creator = new ImportableCreator;
|
||||
$result = $creator->convertSets($input);
|
||||
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertInstanceOf(ImportTransaction::class, $result[0]);
|
||||
$this->assertEquals(1, $result[0]->accountId);
|
||||
|
||||
}
|
||||
|
||||
}
|
102
tests/Unit/Support/Import/Routine/File/LineReaderTest.php
Normal file
102
tests/Unit/Support/Import/Routine/File/LineReaderTest.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* LineReaderTest.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\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\Routine\File\LineReader;
|
||||
use Illuminate\Support\Collection;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class LineReaderTest
|
||||
*/
|
||||
class LineReaderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\LineReader
|
||||
*/
|
||||
public function testGetLines(): void
|
||||
{
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'linerA' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'fake';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [
|
||||
'specifics' => [
|
||||
'IngDescription' => 1,
|
||||
'BadSpecific' => 1,
|
||||
],
|
||||
'has-headers' => true,
|
||||
'delimiter' => ',',
|
||||
];
|
||||
$job->save();
|
||||
|
||||
$att = new Attachment;
|
||||
$att->filename = 'import_file';
|
||||
$att->user_id = $this->user()->id;
|
||||
$att->attachable_id = $job->id;
|
||||
$att->attachable_type = Attachment::class;
|
||||
$att->md5 = md5('hello');
|
||||
$att->mime = 'fake';
|
||||
$att->size = 3;
|
||||
$att->save();
|
||||
|
||||
// mock repositories:
|
||||
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||
$attachments = $this->mock(AttachmentHelperInterface::class);
|
||||
$specific = $this->mock(IngDescription::class);
|
||||
|
||||
// fake file content:
|
||||
$content = "header1,header2,header3\ncolumn1,column2,column3\nA,B,C";
|
||||
$specifics = [['column1', 'column2', 'column3'], ['A', 'B', 'C']];
|
||||
|
||||
// mock calls and returns:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('getAttachments')->once()->andReturn(new Collection([$att]));
|
||||
$attachments->shouldReceive('getAttachmentContent')->andReturn($content)->once();
|
||||
$repository->shouldReceive('getConfiguration')->once()->andReturn($job->configuration);
|
||||
|
||||
// expect the rows to be run throught the specific.
|
||||
$specific->shouldReceive('run')->withArgs([$specifics[0]])->andReturn($specifics[0])->once();
|
||||
$specific->shouldReceive('run')->withArgs([$specifics[1]])->andReturn($specifics[1])->once();
|
||||
|
||||
$lineReader = new LineReader;
|
||||
$lineReader->setImportJob($job);
|
||||
try {
|
||||
$lines = $lineReader->getLines();
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertTrue(false, $e->getMessage());
|
||||
}
|
||||
$this->assertEquals($specifics, $lines);
|
||||
}
|
||||
|
||||
}
|
154
tests/Unit/Support/Import/Routine/File/MappingConvergerTest.php
Normal file
154
tests/Unit/Support/Import/Routine/File/MappingConvergerTest.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
/**
|
||||
* MappingConvergerTest.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\MappingConverger;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class MappingConvergerTest
|
||||
*/
|
||||
class MappingConvergerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\Routine\File\MappingConverger
|
||||
*/
|
||||
public function testConverge(): void
|
||||
{
|
||||
|
||||
// configuration
|
||||
$config = [
|
||||
'column-roles' => [
|
||||
0 => 'account-name',
|
||||
1 => 'bill-name',
|
||||
2 => 'budget-name',
|
||||
3 => 'currency-code',
|
||||
4 => 'category-name',
|
||||
5 => 'foreign-currency-code',
|
||||
6 => 'opposing-number',
|
||||
7 => 'description',
|
||||
8 => 'opposing-iban',
|
||||
],
|
||||
'column-mapping-config' => [
|
||||
0 => [
|
||||
'Checking Account' => 1,
|
||||
],
|
||||
1 => [
|
||||
'BillX' => 2,
|
||||
],
|
||||
2 => [
|
||||
'BudgetX' => 2,
|
||||
],
|
||||
3 => [
|
||||
'EUR' => 7,
|
||||
],
|
||||
4 => [
|
||||
'CategoryX' => 2,
|
||||
],
|
||||
5 => [
|
||||
'USD' => 4,
|
||||
],
|
||||
6 => [
|
||||
'SomeNumber' => 3,
|
||||
],
|
||||
8 => [
|
||||
'IBANX' => 2,
|
||||
],
|
||||
],
|
||||
'column-do-mapping' => [
|
||||
0 => true,
|
||||
1 => true,
|
||||
2 => true,
|
||||
3 => true,
|
||||
4 => true,
|
||||
5 => true,
|
||||
6 => true,
|
||||
7 => false,
|
||||
8 => false,
|
||||
],
|
||||
];
|
||||
|
||||
// just one line to process (should hit everything).
|
||||
$lines = [
|
||||
[
|
||||
0 => 'Checking Account',
|
||||
1 => 'BillX',
|
||||
2 => 'BudgetX',
|
||||
3 => 'EUR',
|
||||
4 => 'CategoryX',
|
||||
5 => 'USD',
|
||||
6 => 'SomeNumber',
|
||||
7 => 'I am a description',
|
||||
8 => 'IBANX'
|
||||
],
|
||||
[
|
||||
0 => 'CheckingX Account',
|
||||
1 => 'BillXA',
|
||||
2 => 'BudgetBX',
|
||||
3 => 'EUD',
|
||||
4 => 'CategoryX',
|
||||
5 => 'USA',
|
||||
6 => 'SomeANumber',
|
||||
7 => 'I am X description',
|
||||
8 => 'IBANXX'
|
||||
],
|
||||
];
|
||||
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'linerA' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'fake';
|
||||
$job->file_type = '';
|
||||
$job->configuration = $config;
|
||||
$job->save();
|
||||
|
||||
|
||||
$converger = new MappingConverger;
|
||||
$converger->setImportJob($job);
|
||||
try {
|
||||
$result = $converger->converge($lines);
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertTrue(false, $e->getMessage());
|
||||
}
|
||||
// do some comparing, we know what to expect.
|
||||
|
||||
// line 0, column 0 is the account name.
|
||||
$this->assertEquals($lines[0][0], $result[0][0]->getValue());
|
||||
$this->assertEquals($config['column-roles'][0], $result[0][0]->getOriginalRole());
|
||||
$this->assertEquals('account-id', $result[0][0]->getRole()); // role changed due to mapping.
|
||||
$this->assertEquals(1, $result[0][0]->getMappedValue()); // can see which value it was given.
|
||||
|
||||
// line 1, column 0 is the account name, but it could not be mapped.
|
||||
$this->assertEquals($lines[1][0], $result[1][0]->getValue());
|
||||
$this->assertEquals($config['column-roles'][0], $result[1][0]->getOriginalRole());
|
||||
$this->assertEquals($config['column-roles'][0], $result[1][0]->getRole()); // role did not change, no mapping.
|
||||
$this->assertEquals(0, $result[1][0]->getMappedValue()); // value of mapping is 0.
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user