Fix test that could come up with journals with 0 transactions, and improve test coverage for file routine.

This commit is contained in:
James Cole
2018-05-10 20:05:02 +02:00
parent 6bd23d897f
commit 274162afcd
10 changed files with 655 additions and 400 deletions

View File

@@ -26,6 +26,7 @@ namespace Tests\Feature\Controllers\Import;
use FireflyIII\Import\JobConfiguration\FakeJobConfiguration;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\MessageBag;
use Log;
use Mockery;
@@ -165,6 +166,35 @@ class JobConfigurationControllerTest extends TestCase
$response->assertSessionHas('warning', $messages->first());
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
*/
public function testPostBadState(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Ffake_job_' . random_int(1, 1000);
$job->status = 'some_bad_state';
$job->provider = 'fake';
$job->file_type = '';
$job->save();
$messages = new MessageBag;
$messages->add('some', 'srrange message');
// mock repositories and configuration handling classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator = $this->mock(FakeJobConfiguration::class);
// call thing.
$this->be($this->user());
$response = $this->post(route('import.job.configuration.post', [$job->key]));
$response->assertStatus(302);
$response->assertRedirect(route('import.index'));
$response->assertSessionHas('error', 'To access this page, your import job cannot have status "some_bad_state".');
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
*/
@@ -198,13 +228,13 @@ class JobConfigurationControllerTest extends TestCase
/**
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
*/
public function testPostBadState(): void
public function testPostWithUpload(): void
{
$file = UploadedFile::fake()->image('avatar.jpg');
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Ffake_job_' . random_int(1, 1000);
$job->status = 'some_bad_state';
$job->key = 'Dfake_job_' . random_int(1, 1000);
$job->status = 'has_prereq';
$job->provider = 'fake';
$job->file_type = '';
$job->save();
@@ -216,12 +246,18 @@ class JobConfigurationControllerTest extends TestCase
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator = $this->mock(FakeJobConfiguration::class);
// mock calls:
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('configurationComplete')->once()->andReturn(false);
$configurator->shouldReceive('configureJob')->once()->andReturn($messages);
$repository->shouldReceive('storeFileUpload')->once()->andReturn(new MessageBag);
// call thing.
$this->be($this->user());
$response = $this->post(route('import.job.configuration.post', [$job->key]));
$response = $this->post(route('import.job.configuration.post', [$job->key]), ['import_file' => $file]);
$response->assertStatus(302);
$response->assertRedirect(route('import.index'));
$response->assertSessionHas('error', 'To access this page, your import job cannot have status "some_bad_state".');
$response->assertRedirect(route('import.job.configuration.index', [$job->key]));
$response->assertSessionHas('warning', $messages->first());
}

View File

@@ -34,17 +34,7 @@ class RabobankDebitCreditTest extends TestCase
/**
* @covers \FireflyIII\Import\Converter\RabobankDebitCredit::convert()
*/
public function testConvertAf()
{
$converter = new RabobankDebitCredit;
$result = $converter->convert('D');
$this->assertEquals(-1, $result);
}
/**
* @covers \FireflyIII\Import\Converter\RabobankDebitCredit::convert()
*/
public function testConvertAnything()
public function testConvertAnything(): void
{
$converter = new RabobankDebitCredit;
$result = $converter->convert('9083jkdkj');
@@ -54,10 +44,40 @@ class RabobankDebitCreditTest extends TestCase
/**
* @covers \FireflyIII\Import\Converter\RabobankDebitCredit::convert()
*/
public function testConvertBij()
public function testConvertCredit(): void
{
$converter = new RabobankDebitCredit;
$result = $converter->convert('C');
$this->assertEquals(1, $result);
}
/**
* @covers \FireflyIII\Import\Converter\RabobankDebitCredit::convert()
*/
public function testConvertCreditOld(): void
{
$converter = new RabobankDebitCredit;
$result = $converter->convert('B');
$this->assertEquals(1, $result);
}
/**
* @covers \FireflyIII\Import\Converter\RabobankDebitCredit::convert()
*/
public function testConvertDebit(): void
{
$converter = new RabobankDebitCredit;
$result = $converter->convert('D');
$this->assertEquals(-1, $result);
}
/**
* @covers \FireflyIII\Import\Converter\RabobankDebitCredit::convert()
*/
public function testConvertDebitOld(): void
{
$converter = new RabobankDebitCredit;
$result = $converter->convert('A');
$this->assertEquals(-1, $result);
}
}

View File

@@ -0,0 +1,367 @@
<?php
/**
* FileJobConfigurationTest.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\Import\JobConfiguration;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\JobConfiguration\FileJobConfiguration;
use FireflyIII\Models\ImportJob;
use FireflyIII\Support\Import\Configuration\File\ConfigureMappingHandler;
use FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler;
use FireflyIII\Support\Import\Configuration\File\ConfigureUploadHandler;
use FireflyIII\Support\Import\Configuration\File\NewFileJobHandler;
use Illuminate\Support\MessageBag;
use Mockery;
use Tests\TestCase;
/**
* Class FileJobConfigurationTest
*/
class FileJobConfigurationTest extends TestCase
{
/**
* No config, job is new.
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testCCFalse(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'File_A_unit_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
// should be false:
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$this->assertFalse($configurator->configurationComplete());
}
/**
* Job is ready to run.
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testCCTrue(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'File_B_unit_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'ready_to_run';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = [];
$job->save();
// should be false:
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$this->assertTrue($configurator->configurationComplete());
}
/**
* Configure the job when the stage is "map". Won't test other combo's because they're covered by other tests.
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testConfigureJob(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'I-file_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'map';
$job->provider = 'file';
$job->file_type = '';
$job->configuration = [];
$job->save();
$bag = new MessageBag;
$result = null;
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$handler = $this->mock(ConfigureMappingHandler::class);
$handler->shouldReceive('setJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('configureJob')->withArgs([['c' => 'd']])->andReturn($bag)->once();
try {
$result = $configurator->configureJob(['c' => 'd']);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals($bag, $result);
}
/**
* Get next data when stage is "configure-upload". Expect a certain class to be called.
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testGetNextDataCU(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'G-file_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'configure-upload';
$job->provider = 'file';
$job->file_type = '';
$job->configuration = [];
$job->save();
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$handler = $this->mock(ConfigureUploadHandler::class);
$handler->shouldReceive('setJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('getNextData')->andReturn(['a' => 'b'])->withNoArgs()->once();
try {
$result = $configurator->getNextData();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals(['a' => 'b'], $result);
}
/**
* Get next data when stage is "map". Expect a certain class to be called.
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testGetNextDataMap(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'H-file_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'map';
$job->provider = 'file';
$job->file_type = '';
$job->configuration = [];
$job->save();
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$handler = $this->mock(ConfigureMappingHandler::class);
$handler->shouldReceive('setJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('getNextData')->andReturn(['a' => 'b'])->withNoArgs()->once();
try {
$result = $configurator->getNextData();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals(['a' => 'b'], $result);
}
/**
* Get next data when stage is "new". Expect a certain class to be called.
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testGetNextDataNew(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'F-file_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'file';
$job->file_type = '';
$job->configuration = [];
$job->save();
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$handler = $this->mock(NewFileJobHandler::class);
$handler->shouldReceive('setJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('getNextData')->andReturn(['a' => 'b'])->withNoArgs()->once();
try {
$result = $configurator->getNextData();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals(['a' => 'b'], $result);
}
/**
* Get next data when stage is "roles". Expect a certain class to be called.
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testGetNextDataRoles(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'H-file_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'roles';
$job->provider = 'file';
$job->file_type = '';
$job->configuration = [];
$job->save();
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
$handler = $this->mock(ConfigureRolesHandler::class);
$handler->shouldReceive('setJob')->once()->withArgs([Mockery::any()]);
$handler->shouldReceive('getNextData')->andReturn(['a' => 'b'])->withNoArgs()->once();
try {
$result = $configurator->getNextData();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals(['a' => 'b'], $result);
}
/**
* Get view when stage is "configure-upload".
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testGetNextViewCU(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Dfile_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'configure-upload';
$job->provider = 'file';
$job->file_type = '';
$job->configuration = [];
$job->save();
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
try {
$result = $configurator->getNextView();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals('import.file.configure-upload', $result);
}
/**
* Get view when stage is "map".
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testGetNextViewMap(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Ffile_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'map';
$job->provider = 'file';
$job->file_type = '';
$job->configuration = [];
$job->save();
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
try {
$result = $configurator->getNextView();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals('import.file.map', $result);
}
/**
* Get view when stage is "new".
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testGetNextViewNew(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Cfile_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'file';
$job->file_type = '';
$job->configuration = [];
$job->save();
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
try {
$result = $configurator->getNextView();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals('import.file.new', $result);
}
/**
* Get view when stage is "roles".
*
* @covers \FireflyIII\Import\JobConfiguration\FileJobConfiguration
*/
public function testGetNextViewRoles(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Efile_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'roles';
$job->provider = 'file';
$job->file_type = '';
$job->configuration = [];
$job->save();
$result = 'x';
$configurator = new FileJobConfiguration;
$configurator->setJob($job);
try {
$result = $configurator->getNextView();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals('import.file.roles', $result);
}
}

View File

@@ -41,7 +41,7 @@ class FakeRoutineTest extends TestCase
/**
* @covers \FireflyIII\Import\Routine\FakeRoutine
*/
public function testRunAhoy()
public function testRunAhoy(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
@@ -76,7 +76,7 @@ class FakeRoutineTest extends TestCase
/**
* @covers \FireflyIII\Import\Routine\FakeRoutine
*/
public function testRunFinal()
public function testRunFinal(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
@@ -112,7 +112,7 @@ class FakeRoutineTest extends TestCase
/**
* @covers \FireflyIII\Import\Routine\FakeRoutine
*/
public function testRunNew()
public function testRunNew(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;

View File

@@ -0,0 +1,78 @@
<?php
/**
* FileRoutineTest.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\Import\Routine;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Routine\FileRoutine;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\Routine\File\CSVProcessor;
use Mockery;
use Tests\TestCase;
/**
* Class FileRoutineTest
*/
class FileRoutineTest extends TestCase
{
/**
* @covers \FireflyIII\Import\Routine\FileRoutine
*/
public function testRunDefault(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'a_fr_' . random_int(1, 1000);
$job->status = 'running';
$job->stage = 'ready_to_run';
$job->provider = 'file';
$job->file_type = '';
$job->configuration = [];
$job->save();
// mock
$processor = $this->mock(CSVProcessor::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
// calls
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'provider_finished'])->once();
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'final'])->once();
$repository->shouldReceive('setTransactions')->withArgs([Mockery::any(), ['a' => 'b']])->once();
$repository->shouldReceive('getConfiguration')->withArgs([Mockery::any()])->once()->andReturn([]);
$processor->shouldReceive('setJob')->once();
$processor->shouldReceive('run')->once()->andReturn(['a' => 'b']);
$routine = new FileRoutine;
$routine->setJob($job);
try {
$routine->run();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
}
}

View File

@@ -35,7 +35,7 @@ class HasAnyCategoryTest extends TestCase
/**
* @covers \FireflyIII\TransactionRules\Triggers\HasAnyCategory::triggered
*/
public function testTriggered()
public function testTriggered(): void
{
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$category = $journal->user->categories()->first();
@@ -51,14 +51,14 @@ class HasAnyCategoryTest extends TestCase
/**
* @covers \FireflyIII\TransactionRules\Triggers\HasAnyCategory::triggered
*/
public function testTriggeredNot()
public function testTriggeredNot(): void
{
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$journal->categories()->detach();
// also detach transactions:
/** @var Transaction $transaction */
foreach($journal->transactions as $transaction) {
foreach ($journal->transactions as $transaction) {
$transaction->categories()->detach();
}
@@ -71,10 +71,16 @@ class HasAnyCategoryTest extends TestCase
/**
* @covers \FireflyIII\TransactionRules\Triggers\HasAnyCategory::triggered
*/
public function testTriggeredTransactions()
public function testTriggeredTransactions(): void
{
/** @var TransactionJournal $journal */
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$count = 0;
$journal = null;
while ($count === 0) {
/** @var TransactionJournal $journal */
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$count = $journal->transactions()->count();
}
$category = $journal->user->categories()->first();
$journal->categories()->detach();
$this->assertEquals(0, $journal->categories()->count());
@@ -94,7 +100,7 @@ class HasAnyCategoryTest extends TestCase
/**
* @covers \FireflyIII\TransactionRules\Triggers\HasAnyCategory::willMatchEverything
*/
public function testWillMatchEverything()
public function testWillMatchEverything(): void
{
$value = '';
$result = HasAnyCategory::willMatchEverything($value);