Skip to content

Firefly III: a personal finances manager

Improve code test coverage.

James Colec47a5379ae43
diff --git a/app/Support/Import/Configuration/File/ConfigureUploadHandler.php b/app/Support/Import/Configuration/File/ConfigureUploadHandler.php
index 553423aa07..640f39833a 100644
--- a/app/Support/Import/Configuration/File/ConfigureUploadHandler.php
+++ b/app/Support/Import/Configuration/File/ConfigureUploadHandler.php
@@ -66,14 +66,13 @@ class ConfigureUploadHandler implements ConfigurationInterface
// collect specifics.
foreach (config('csv.import_specifics') as $name => $className) {
$specifics[$name] = [
- 'name' => $className::getName(),
- 'description' => $className::getDescription(),
+ 'name' => trans($className::getName()),
+ 'description' => trans($className::getDescription()),
];
}
$data = [
'accounts' => [],
- 'specifix' => [],
'delimiters' => $delimiters,
'specifics' => $specifics,
];
diff --git a/tests/Unit/Support/Import/Configuration/File/ConfigureUploadHandlerTest.php b/tests/Unit/Support/Import/Configuration/File/ConfigureUploadHandlerTest.php
new file mode 100644
index 0000000000..baab7a6b62
--- /dev/null
+++ b/tests/Unit/Support/Import/Configuration/File/ConfigureUploadHandlerTest.php
@@ -0,0 +1,185 @@
+<?php
+/**
+ * ConfigureUploadHandlerTest.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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\Models\ImportJob;
+use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
+use FireflyIII\Support\Import\Configuration\File\ConfigureUploadHandler;
+use Mockery;
+use Tests\TestCase;
+
+/**
+ * Class ConfigureUploadHandlerTest
+ */
+class ConfigureUploadHandlerTest extends TestCase
+{
+ /**
+ * @covers \FireflyIII\Support\Import\Configuration\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->setJob($job);
+ $result = $handler->configureJob($data);
+ $this->assertCount(0, $result);
+ }
+
+ /**
+ * @covers \FireflyIII\Support\Import\Configuration\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->setJob($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\Configuration\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->setJob($job);
+ $result = $handler->getNextData();
+ $expected = [
+ 'accounts' => [],
+ 'delimiters' => [],
+ ];
+ // not much to compare, really.
+ $this->assertEquals($expected['accounts'], $result['accounts']);
+ }
+
+ /**
+ * @covers \FireflyIII\Support\Import\Configuration\File\ConfigureUploadHandler
+ */
+ public function testGetSpecifics(): void
+ {
+ $array = [
+ 'specifics' => [
+ 'IngDescription', 'BadFakeNewsThing',
+ ],
+ ];
+ $expected = [
+ 'IngDescription' => 1,
+ ];
+
+ $handler = new ConfigureUploadHandler;
+ $result = $handler->getSpecifics($array);
+ $this->assertEquals($expected, $result);
+ }
+
+}
\ No newline at end of file