Improved code for new import + some tests.

This commit is contained in:
James Cole
2018-05-04 20:21:27 +02:00
parent b541f7b944
commit 1c0da454db
22 changed files with 2770 additions and 2281 deletions

View File

@@ -116,7 +116,7 @@ class ExportControllerTest extends TestCase
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('create')->andReturn($job);
$repository->shouldReceive('cleanup');
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::ASSET, AccountType::DEFAULT]])->andReturn(new Collection);
$this->be($this->user());
$response = $this->get(route('export.index'));

View File

@@ -1,131 +0,0 @@
<?php
/**
* ConfigurationControllerTest.php
* Copyright (c) 2017 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\Feature\Controllers\Import;
use FireflyIII\Import\Configuration\FileConfigurator;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Log;
use Tests\TestCase;
/**
* Class AccountControllerTest
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ConfigurationControllerTest extends TestCase
{
/**
*
*/
public function setUp()
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\ConfigurationController::__construct
* @covers \FireflyIII\Http\Controllers\Import\ConfigurationController::index
* @covers \FireflyIII\Http\Controllers\Import\ConfigurationController::makeConfigurator
*/
public function testIndex()
{
/** @var ImportJob $job */
$job = $this->user()->importJobs()->where('key', 'configuring')->first();
$configurator = $this->mock(FileConfigurator::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('isJobConfigured')->once()->andReturn(false);
$configurator->shouldReceive('getNextView')->once()->andReturn('error'); // does not matter which view is returned.
$configurator->shouldReceive('getNextData')->once()->andReturn([]);
$repository->shouldReceive('updateStatus')->once();
$this->be($this->user());
$response = $this->get(route('import.configure', [$job->key]));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\ConfigurationController::__construct
* @covers \FireflyIII\Http\Controllers\Import\ConfigurationController::index
* @covers \FireflyIII\Http\Controllers\Import\ConfigurationController::makeConfigurator
*/
public function testIndexConfigured()
{
/** @var ImportJob $job */
$job = $this->user()->importJobs()->where('key', 'configured')->first();
$configurator = $this->mock(FileConfigurator::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('isJobConfigured')->once()->andReturn(true);
$repository->shouldReceive('updateStatus')->once();
$this->be($this->user());
$response = $this->get(route('import.configure', [$job->key]));
$response->assertStatus(302);
$response->assertRedirect(route('import.status', [$job->key]));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\ConfigurationController::post
*/
public function testPost()
{
/** @var ImportJob $job */
$job = $this->user()->importJobs()->where('key', 'configuring')->first();
$data = ['some' => 'config'];
$configurator = $this->mock(FileConfigurator::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('isJobConfigured')->once()->andReturn(false);
$configurator->shouldReceive('configureJob')->once()->withArgs([$data]);
$configurator->shouldReceive('getWarningMessage')->once()->andReturn('Some warning');
$this->be($this->user());
$response = $this->post(route('import.configure.post', [$job->key]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('import.configure', [$job->key]));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\ConfigurationController::post
*/
public function testPostConfigured()
{
/** @var ImportJob $job */
$job = $this->user()->importJobs()->where('key', 'configuring')->first();
$data = ['some' => 'config'];
$configurator = $this->mock(FileConfigurator::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('isJobConfigured')->once()->andReturn(true);
$this->be($this->user());
$response = $this->post(route('import.configure.post', [$job->key]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('import.status', [$job->key]));
}
}

View File

@@ -22,13 +22,18 @@ declare(strict_types=1);
namespace Tests\Feature\Controllers\Import;
use FireflyIII\Import\Routine\FileRoutine;
use FireflyIII\Import\Prerequisites\BunqPrerequisites;
use FireflyIII\Import\Prerequisites\FakePrerequisites;
use FireflyIII\Import\Prerequisites\FilePrerequisites;
use FireflyIII\Import\Prerequisites\SpectrePrerequisites;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Log;
use Mockery;
use Tests\TestCase;
/**
* Class AccountControllerTest
* Class IndexControllerTest
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -46,73 +51,85 @@ class IndexControllerTest extends TestCase
}
/**
* @covers \FireflyIII\Http\Controllers\Import\IndexController::create
* @covers \FireflyIII\Http\Controllers\Import\IndexController
*/
public function testCreate()
public function testCreateFake()
{
$job = $this->user()->importJobs()->where('key', 'new')->first();
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('create')->withArgs(['file'])->andReturn($job);
// mock stuff:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$fakePrerequisites = $this->mock(FakePrerequisites::class);
// fake job:
$importJob = new ImportJob;
$importJob->provider = 'fake';
$importJob->key = 'fake_job_1';
// mock call:
$repository->shouldReceive('create')->withArgs(['fake'])->andReturn($importJob);
$fakePrerequisites->shouldReceive('isComplete')->once()->andReturn(false);
$fakePrerequisites->shouldReceive('setUser')->once();
$this->be($this->user());
$response = $this->get(route('import.create-job', ['file']));
$response = $this->get(route('import.create', ['fake']));
$response->assertStatus(302);
$response->assertRedirect(route('import.configure', ['new']));
// expect a redirect to prerequisites
$response->assertRedirect(route('import.prerequisites.index', ['fake', 'fake_job_1']));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\IndexController::download
* @covers \FireflyIII\Http\Controllers\Import\IndexController
*/
public function testDownload()
public function testCreateFakeNoPrereq()
{
$repository = $this->mock(ImportJobRepositoryInterface::class);
//$job = $this->user()->importJobs()->where('key', 'testImport')->first();
// mock stuff:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$fakePrerequisites = $this->mock(FakePrerequisites::class);
// fake job:
$importJob = new ImportJob;
$importJob->provider = 'fake';
$importJob->key = 'fake_job_2';
// mock call:
$repository->shouldReceive('create')->withArgs(['fake'])->andReturn($importJob);
$fakePrerequisites->shouldReceive('isComplete')->once()->andReturn(true);
$fakePrerequisites->shouldReceive('setUser')->once();
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'has_prereq'])->andReturn($importJob)->once();
$this->be($this->user());
$response = $this->get(route('import.download', ['testImport']));
$response->assertStatus(200);
$response = $this->get(route('import.create', ['fake']));
$response->assertStatus(302);
// expect a redirect to prerequisites
$response->assertRedirect(route('import.job.configuration.index', ['fake_job_2']));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\IndexController::__construct
* @covers \FireflyIII\Http\Controllers\Import\IndexController::index
*/
public function testIndex()
{
$repository = $this->mock(ImportJobRepositoryInterface::class);
$this->be($this->user());
// fake prerequisites providers:
$fake = $this->mock(FakePrerequisites::class);
$file = $this->mock(FilePrerequisites::class);
$bunq = $this->mock(BunqPrerequisites::class);
$spectre = $this->mock(SpectrePrerequisites::class);
// call methods:
$fake->shouldReceive('setUser')->once();
$file->shouldReceive('setUser')->once();
$bunq->shouldReceive('setUser')->once();
$spectre->shouldReceive('setUser')->once();
$fake->shouldReceive('isComplete')->once()->andReturn(true);
$file->shouldReceive('isComplete')->once()->andReturn(true);
$bunq->shouldReceive('isComplete')->once()->andReturn(true);
$spectre->shouldReceive('isComplete')->once()->andReturn(true);
$response = $this->get(route('import.index'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\IndexController::start
*/
public function testStart()
{
$repository = $this->mock(ImportJobRepositoryInterface::class);
$routine = $this->mock(FileRoutine::class);
$routine->shouldReceive('setJob')->once();
$routine->shouldReceive('run')->once()->andReturn(true);
$this->be($this->user());
$response = $this->post(route('import.start', ['configured']));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\IndexController::start
* @expectedExceptionMessage Job did not complete successfully.
*/
public function testStartFailed()
{
$repository = $this->mock(ImportJobRepositoryInterface::class);
$routine = $this->mock(FileRoutine::class);
$routine->shouldReceive('setJob')->once();
$routine->shouldReceive('run')->once()->andReturn(false);
$this->be($this->user());
$response = $this->post(route('import.start', ['configured']));
$response->assertStatus(500);
$response->assertSee('<ol class="breadcrumb">');
}
}

View File

@@ -0,0 +1,228 @@
<?php
/**
* JobConfigurationControllerTest.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\Feature\Controllers\Import;
use FireflyIII\Import\JobConfiguration\FakeJobConfiguration;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Support\MessageBag;
use Log;
use Mockery;
use Tests\TestCase;
/**
* Class JobConfigurationControllerTest
*/
class JobConfigurationControllerTest extends TestCase
{
/**
*
*/
public function setUp()
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
*/
public function testIndex(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Afake_job_' . random_int(1, 1000);
$job->status = 'has_prereq';
$job->provider = 'fake';
$job->file_type = '';
$job->save();
// mock repositories and configuration handling classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator = $this->mock(FakeJobConfiguration::class);
// mock calls:
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('configurationComplete')->once()->andReturn(false);
$configurator->shouldReceive('getNextView')->once()->andReturn('import.fake.apply-rules');
$configurator->shouldReceive('getNextData')->once()
->andReturn(['rulesOptions' => [1 => 'Y', 0 => 'N',],]);
$this->be($this->user());
$response = $this->get(route('import.job.configuration.index', [$job->key]));
$response->assertStatus(200);
// expect a redirect to prerequisites
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
*/
public function testIndexBadState(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Bfake_job_' . random_int(1, 1000);
$job->status = 'some_bad_state';
$job->provider = 'fake';
$job->file_type = '';
$job->save();
// mock repositories and configuration handling classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator = $this->mock(FakeJobConfiguration::class);
// mock calls:
$this->be($this->user());
$response = $this->get(route('import.job.configuration.index', [$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
*/
public function testIndexComplete(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Cfake_job_' . random_int(1, 1000);
$job->status = 'has_prereq';
$job->provider = 'fake';
$job->file_type = '';
$job->save();
// mock repositories and configuration handling classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator = $this->mock(FakeJobConfiguration::class);
// mock calls:
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('configurationComplete')->once()->andReturn(true);
$repository->shouldReceive('updateStatus')->withArgs([Mockery::any(), 'ready_to_run']);
$this->be($this->user());
$response = $this->get(route('import.job.configuration.index', [$job->key]));
$response->assertStatus(302);
$response->assertRedirect(route('import.job.status.index', [$job->key]));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
*/
public function testPost(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Dfake_job_' . random_int(1, 1000);
$job->status = 'has_prereq';
$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);
// mock calls:
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('configurationComplete')->once()->andReturn(false);
$configurator->shouldReceive('configureJob')->withArgs([[]])->once()->andReturn($messages);
// call thing.
$this->be($this->user());
$response = $this->post(route('import.job.configuration.post', [$job->key]));
$response->assertStatus(302);
$response->assertRedirect(route('import.job.configuration.index', [$job->key]));
$response->assertSessionHas('warning', $messages->first());
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
*/
public function testPostComplete(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Efake_job_' . random_int(1, 1000);
$job->status = 'has_prereq';
$job->provider = 'fake';
$job->file_type = '';
$job->save();
// mock repositories and configuration handling classes:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator = $this->mock(FakeJobConfiguration::class);
// mock calls:
$configurator->shouldReceive('setJob')->once();
$configurator->shouldReceive('configurationComplete')->once()->andReturn(true);
$repository->shouldReceive('updateStatus')->withArgs([Mockery::any(), 'ready_to_run']);
// call thing.
$this->be($this->user());
$response = $this->post(route('import.job.configuration.post', [$job->key]));
$response->assertStatus(302);
$response->assertRedirect(route('import.job.status.index', [$job->key]));
}
/**
* @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".');
}
}

View File

@@ -0,0 +1,375 @@
<?php
/**
* JobStatusControllerTest.php
* Copyright (c) 2017 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\Feature\Controllers\Import;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Routine\FakeRoutine;
use FireflyIII\Import\Storage\ImportArrayStorage;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\Tag;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Log;
use Mockery;
use Tests\TestCase;
/**
* Class JobStatusControllerTest
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class JobStatusControllerTest extends TestCase
{
/**
*
*/
public function setUp()
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testIndex(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Afake_job_' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->provider = 'fake';
$job->file_type = '';
$job->save();
// call thing.
$this->be($this->user());
$response = $this->get(route('import.job.status.index', [$job->key]));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testJson(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Bfake_job_' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
// call thing.
$this->be($this->user());
$response = $this->get(route('import.job.status.json', [$job->key]));
$response->assertStatus(200);
$response->assertSee(
'No transactions have been imported. Perhaps they were all duplicates is simply no transactions where present to be imported. Perhaps the error message below can tell you what happened.'
);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testJsonWithTag(): void
{
$tag = $this->user()->tags()->first();
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Cfake_job_' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->tag()->associate($tag);
$job->save();
// call thing.
$this->be($this->user());
$response = $this->get(route('import.job.status.json', [$job->key]));
$response->assertStatus(200);
$response->assertSee(
'No transactions have been imported. Perhaps they were all duplicates is simply no transactions where present to be imported. Perhaps the error message below can tell you what happened.'
);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testJsonWithTagManyJournals(): void
{
/** @var Tag $tag */
$tag = $this->user()->tags()->first();
$journal = $this->user()->transactionJournals()->first();
$second = $this->user()->transactionJournals()->where('id', '!=', $journal->id)->first();
$tag->transactionJournals()->sync([$journal->id, $second->id]);
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Dfake_job_' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->tag()->associate($tag);
$job->save();
// call thing.
$this->be($this->user());
$response = $this->get(route('import.job.status.json', [$job->key]));
$response->assertStatus(200);
$response->assertSee(
'Firefly III has imported 2 transactions. They are stored under tag <a href=\"http:\/\/localhost\/tags\/show\/' . $tag->id
);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testJsonWithTagOneJournal(): void
{
/** @var Tag $tag */
$tag = $this->user()->tags()->first();
$journal = $this->user()->transactionJournals()->first();
$tag->transactionJournals()->sync([$journal->id]);
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Efake_job_' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->tag()->associate($tag);
$job->save();
// call thing.
$this->be($this->user());
$response = $this->get(route('import.job.status.json', [$job->key]));
$response->assertStatus(200);
$response->assertSee(
'Exactly one transaction has been imported. It is stored under tag <a href=\"http:\/\/localhost\/tags\/show\/' . $tag->id
);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testStart(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Ffake_job_' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
// mock stuff
$repository = $this->mock(ImportJobRepositoryInterface::class);
$routine = $this->mock(FakeRoutine::class);
// mock calls:
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'running']);
$routine->shouldReceive('setJob')->once();
$routine->shouldReceive('run')->once();
// call thing.
$this->be($this->user());
$response = $this->post(route('import.job.start', [$job->key]));
$response->assertStatus(200);
$response->assertExactJson(['status' => 'OK', 'message' => 'stage_finished']);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testStartException(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Gfake_job_' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
// mock stuff
$repository = $this->mock(ImportJobRepositoryInterface::class);
$routine = $this->mock(FakeRoutine::class);
// mock calls:
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'running']);
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'error']);
$routine->shouldReceive('setJob')->once();
$routine->shouldReceive('run')->andThrow(new Exception('Unknown exception'));
// call thing.
$this->be($this->user());
$response = $this->post(route('import.job.start', [$job->key]));
$response->assertStatus(200);
$response->assertExactJson(['status' => 'NOK', 'message' => 'The import routine crashed: Unknown exception']);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testStartFireflyException(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Hfake_job_' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
// mock stuff
$repository = $this->mock(ImportJobRepositoryInterface::class);
$routine = $this->mock(FakeRoutine::class);
// mock calls:
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'running']);
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'error']);
$routine->shouldReceive('setJob')->once();
$routine->shouldReceive('run')->andThrow(new FireflyException('Unknown exception'));
// call thing.
$this->be($this->user());
$response = $this->post(route('import.job.start', [$job->key]));
$response->assertStatus(200);
$response->assertExactJson(['status' => 'NOK', 'message' => 'The import routine crashed: Unknown exception']);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testStartInvalidState(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Ifake_job_' . random_int(1, 1000);
$job->status = 'bad_state';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
// call thing.
$this->be($this->user());
$response = $this->post(route('import.job.start', [$job->key]));
$response->assertStatus(200);
$response->assertExactJson(['status' => 'NOK', 'message' => 'JobStatusController::start expects state "ready_to_run".']);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testStore(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Jfake_job_' . random_int(1, 1000);
$job->status = 'provider_finished';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
// mock stuff
$repository = $this->mock(ImportJobRepositoryInterface::class);
$storage = $this->mock(ImportArrayStorage::class);
// mock calls:
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'storing_data']);
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'storage_finished']);
$storage->shouldReceive('setJob')->once();
$storage->shouldReceive('store')->once();
$this->be($this->user());
$response = $this->post(route('import.job.store', [$job->key]));
$response->assertStatus(200);
$response->assertExactJson(['status' => 'OK', 'message' => 'storage_finished']);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testStoreInvalidState(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Kfake_job_' . random_int(1, 1000);
$job->status = 'some_bad_state';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
$this->be($this->user());
$response = $this->post(route('import.job.store', [$job->key]));
$response->assertStatus(200);
$response->assertExactJson(['status' => 'NOK', 'message' => 'JobStatusController::start expects state "provider_finished".']);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\JobStatusController
*/
public function testStoreException(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'Lfake_job_' . random_int(1, 1000);
$job->status = 'provider_finished';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
// mock stuff
$repository = $this->mock(ImportJobRepositoryInterface::class);
$storage = $this->mock(ImportArrayStorage::class);
// mock calls:
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'storing_data']);
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'error']);
$storage->shouldReceive('setJob')->once();
$storage->shouldReceive('store')->once()->andThrow(new FireflyException('Some storage exception.'));
$this->be($this->user());
$response = $this->post(route('import.job.store', [$job->key]));
$response->assertStatus(200);
$response->assertExactJson(['status' => 'NOK', 'message' => 'The import storage routine crashed: Some storage exception.']);
}
}

View File

@@ -22,9 +22,12 @@ declare(strict_types=1);
namespace Tests\Feature\Controllers\Import;
use FireflyIII\Import\Prerequisites\FilePrerequisites;
use FireflyIII\Import\Prerequisites\FakePrerequisites;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Support\MessageBag;
use Log;
use Mockery;
use Tests\TestCase;
/**
@@ -46,74 +49,191 @@ class PrerequisitesControllerTest extends TestCase
}
/**
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController::__construct
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController::index
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
*/
public function testIndex()
public function testIndex(): void
{
$object = $this->mock(FilePrerequisites::class);
$object->shouldReceive('setUser');
$object->shouldReceive('hasPrerequisites')->andReturn(true);
$object->shouldReceive('getView')->andReturn('error'); // does not matter which view is returned
$object->shouldReceive('getViewParameters')->andReturn([]);
$this->be($this->user());
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'A_pre_job_' . random_int(1, 1000);
$job->status = 'new';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
$response = $this->get(route('import.prerequisites', ['file']));
// mock stuff
$prereq = $this->mock(FakePrerequisites::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$prereq->shouldReceive('setUser')->once();
$prereq->shouldReceive('isComplete')->once()->andReturn(false);
$prereq->shouldReceive('getView')->once()->andReturn('import.fake.prerequisites');
$prereq->shouldReceive('getViewParameters')->once()->andReturn(['api_key' => '']);
$this->be($this->user());
$response = $this->get(route('import.prerequisites.index', ['fake', $job->key]));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController::index
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
*/
public function testIndexRedirect()
public function testIndexBadState(): void
{
$object = $this->mock(FilePrerequisites::class);
$object->shouldReceive('setUser');
$object->shouldReceive('hasPrerequisites')->andReturn(false);
$this->be($this->user());
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'B_pre_job_' . random_int(1, 1000);
$job->status = 'some_Bad_state';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
$response = $this->get(route('import.prerequisites', ['file']));
$this->be($this->user());
$response = $this->get(route('import.prerequisites.index', ['fake', $job->key]));
$response->assertStatus(302);
$response->assertRedirect(route('import.create-job', ['file']));
$response->assertRedirect(route('import.index'));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
*/
public function testIndexComplete(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'C_pre_job_' . random_int(1, 1000);
$job->status = 'new';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
// mock stuff
$prereq = $this->mock(FakePrerequisites::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'has_prereq']);
$prereq->shouldReceive('setUser')->once();
$prereq->shouldReceive('isComplete')->once()->andReturn(true);
$this->be($this->user());
$response = $this->get(route('import.prerequisites.index', ['fake', $job->key]));
$response->assertStatus(302);
$response->assertRedirect(route('import.job.configuration.index', [$job->key]));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController::post
* Redirects to configuration.
*
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
*/
public function testPost()
public function testPost(): void
{
$messageBag = new MessageBag;
$messageBag->add('nomessage', 'nothing');
$object = $this->mock(FilePrerequisites::class);
$object->shouldReceive('setUser');
$object->shouldReceive('hasPrerequisites')->andReturn(true);
$object->shouldReceive('storePrerequisites')->andReturn($messageBag);
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'D_pre_job_' . random_int(1, 1000);
$job->status = 'new';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
// mock stuff
$prereq = $this->mock(FakePrerequisites::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$prereq->shouldReceive('setUser')->once();
$prereq->shouldReceive('storePrerequisites')->once()->andReturn(new MessageBag);
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'has_prereq']);
$this->be($this->user());
$response = $this->post(route('import.prerequisites.post', ['file']), []);
$response = $this->post(route('import.prerequisites.post', ['fake', $job->key]));
$response->assertStatus(302);
$response->assertSessionHas('error');
$response->assertRedirect(route('import.prerequisites', ['file']));
$response->assertRedirect(route('import.job.configuration.index', [$job->key]));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController::post
* Bad state gives errors.
*
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
*/
public function testPostDone()
public function testPostBadState(): void
{
$messageBag = new MessageBag;
$messageBag->add('nomessage', 'nothing');
$object = $this->mock(FilePrerequisites::class);
$object->shouldReceive('setUser');
$object->shouldReceive('hasPrerequisites')->andReturn(false);
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'D_pre_job_' . random_int(1, 1000);
$job->status = 'badstate';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
// mock stuff
$prereq = $this->mock(FakePrerequisites::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$this->be($this->user());
$response = $this->post(route('import.prerequisites.post', ['file']), []);
$response = $this->post(route('import.prerequisites.post', ['fake', $job->key]));
$response->assertStatus(302);
$response->assertRedirect(route('import.create-job', ['file']));
$response->assertRedirect(route('import.index'));
$response->assertSessionHas('error', 'To access this page, your import job cannot have status "badstate".');
}
/**
* Redirects to index.
*
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
*/
public function testPostNoJob(): void
{
// mock stuff
$prereq = $this->mock(FakePrerequisites::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$prereq->shouldReceive('setUser')->once();
$prereq->shouldReceive('storePrerequisites')->once()->andReturn(new MessageBag);
$this->be($this->user());
$response = $this->post(route('import.prerequisites.post', ['fake']));
$response->assertStatus(302);
$response->assertRedirect(route('import.index'));
}
/**
* Error messages? Redirect back
*
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
*/
public function testPostWithMessages(): void
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'D_pre_job_' . random_int(1, 1000);
$job->status = 'new';
$job->provider = 'fake';
$job->transactions = [];
$job->file_type = '';
$job->save();
$messages = new MessageBag;
$messages->add('some', 'message');
// mock stuff
$prereq = $this->mock(FakePrerequisites::class);
$repository = $this->mock(ImportJobRepositoryInterface::class);
$prereq->shouldReceive('setUser')->once();
$prereq->shouldReceive('storePrerequisites')->once()->andReturn($messages);
$this->be($this->user());
$response = $this->post(route('import.prerequisites.post', ['fake', $job->key]));
$response->assertStatus(302);
$response->assertRedirect(route('import.prerequisites.index', ['fake', $job->key]));
$response->assertSessionHas('error', 'message');
}
}

View File

@@ -1,96 +0,0 @@
<?php
/**
* StatusControllerTest.php
* Copyright (c) 2017 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\Feature\Controllers\Import;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Log;
use Tests\TestCase;
/**
* Class AccountControllerTest
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class StatusControllerTest extends TestCase
{
/**
*
*/
public function setUp()
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\StatusController::__construct
* @covers \FireflyIII\Http\Controllers\Import\StatusController::index
*/
public function testIndex()
{
$this->be($this->user());
$response = $this->get(route('import.status', ['configured']));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\StatusController::__construct
* @covers \FireflyIII\Http\Controllers\Import\StatusController::index
*/
public function testIndexRedirect()
{
$this->be($this->user());
$response = $this->get(route('import.status', ['new']));
$response->assertStatus(302);
$response->assertRedirect(route('import.configure', ['new']));
}
/**
* @covers \FireflyIII\Http\Controllers\Import\StatusController::__construct
* @covers \FireflyIII\Http\Controllers\Import\StatusController::json
*/
public function testStatusFinished()
{
$tag = $this->user()->tags()->first();
$repository = $this->mock(TagRepositoryInterface::class);
$repository->shouldReceive('find')->andReturn($tag);
$this->be($this->user());
$response = $this->get(route('import.status.json', ['finished']));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Import\StatusController::__construct
* @covers \FireflyIII\Http\Controllers\Import\StatusController::json
*/
public function testStatusRunning()
{
$this->be($this->user());
$response = $this->get(route('import.status.json', ['running']));
$response->assertStatus(200);
}
}