Improve tests for import.

This commit is contained in:
James Cole 2018-01-05 07:41:26 +01:00
parent 246af608d3
commit d3cad66b69
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 635 additions and 3 deletions

View File

@ -91,7 +91,7 @@ class FileConfigurator implements ConfiguratorInterface
$class = $this->getConfigurationClass();
$job = $this->job;
/** @var ConfigurationInterface $object */
$object = new $class($this->job);
$object = app($class);
$object->setJob($job);
$result = $object->storeConfiguration($data);
$this->warning = $object->getWarningMessage();
@ -187,6 +187,7 @@ class FileConfigurator implements ConfiguratorInterface
*/
public function setJob(ImportJob $job)
{
Log::debug(sprintf('FileConfigurator::setJob(#%d: %s)', $job->id, $job->key));
$this->job = $job;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($job->user);
@ -236,10 +237,10 @@ class FileConfigurator implements ConfiguratorInterface
}
if (false === $class || 0 === strlen($class)) {
throw new FireflyException('Cannot handle current job state in getConfigurationClass().');
throw new FireflyException(sprintf('Cannot handle job stage "%s" in getConfigurationClass().', $stage));
}
if (!class_exists($class)) {
throw new FireflyException(sprintf('Class %s does not exist in getConfigurationClass().', $class));
throw new FireflyException(sprintf('Class %s does not exist in getConfigurationClass().', $class)); // @codeCoverageIgnore
}
Log::debug(sprintf('Configuration class is "%s"', $class));

View File

@ -0,0 +1,631 @@
<?php
/**
* FileConfiguratorTest.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\Configuration;
use FireflyIII\Import\Configuration\FileConfigurator;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\Configuration\File\Initial;
use FireflyIII\Support\Import\Configuration\File\Map;
use FireflyIII\Support\Import\Configuration\File\Roles;
use FireflyIII\Support\Import\Configuration\File\UploadConfig;
use Mockery;
use Tests\TestCase;
/**
* Class FileConfiguratorTest
*/
class FileConfiguratorTest extends TestCase
{
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::configureJob
* @covers \FireflyIII\Import\Configuration\FileConfigurator::setJob
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getConfig
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getConfigurationClass
*/
public function testConfigureJobInitial()
{
// data
$config = ['stage' => 'initial'];
$data = ['some' => 'array'];
$job = $this->getJob($config);
// mock repository
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// assert that new initial is created:
$processor = $this->mock(Initial::class);
$processor->shouldReceive('setJob')->withArgs([$job])->once();
$processor->shouldReceive('storeConfiguration')->withArgs([$data])->once()->andReturn(true);
$processor->shouldReceive('getWarningMessage')->andReturn('')->once();
// config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->configureJob($data);
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::configureJob
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getConfigurationClass
*/
public function testConfigureJobMap()
{
// data
$config = ['stage' => 'map'];
$data = ['some' => 'array'];
$job = $this->getJob($config);
// mock repository
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// assert that new Roles is created:
$processor = $this->mock(Map::class);
$processor->shouldReceive('setJob')->withArgs([$job])->once();
$processor->shouldReceive('storeConfiguration')->withArgs([$data])->once()->andReturn(true);
$processor->shouldReceive('getWarningMessage')->andReturn('')->once();
// config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->configureJob($data);
}
/**
* Should throw a FireflyException when $job is null.
*
* @covers \FireflyIII\Import\Configuration\FileConfigurator::__construct
* @covers \FireflyIII\Import\Configuration\FileConfigurator::configureJob
* @expectedException \FireflyIII\Exceptions\FireflyException
*/
public function testConfigureJobNoJob()
{
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
// config
$configurator = new FileConfigurator();
$configurator->configureJob([]);
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::configureJob
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getConfigurationClass
* @expectedException \FireflyIII\Exceptions\FireflyException
* @expectedExceptionMessage Cannot handle job stage "ready" in getConfigurationClass().
*/
public function testConfigureJobReady()
{
// data
$config = ['stage' => 'ready'];
$data = ['some' => 'array'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->configureJob($data);
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::configureJob
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getConfigurationClass
*/
public function testConfigureJobRoles()
{
$config = ['stage' => 'roles'];
$data = ['some' => 'array'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// assert that new Roles is created:
$processor = $this->mock(Roles::class);
$processor->shouldReceive('setJob')->withArgs([$job])->once();
$processor->shouldReceive('storeConfiguration')->withArgs([$data])->once()->andReturn(true);
$processor->shouldReceive('getWarningMessage')->andReturn('')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->configureJob($data);
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::configureJob
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getConfigurationClass
*/
public function testConfigureJobUploadConfig()
{
// data
$config = ['stage' => 'upload-config'];
$data = ['some' => 'array'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// assert that new UploadConfig is created:
$processor = $this->mock(UploadConfig::class);
$processor->shouldReceive('setJob')->withArgs([$job])->once();
$processor->shouldReceive('storeConfiguration')->withArgs([$data])->once()->andReturn(true);
$processor->shouldReceive('getWarningMessage')->andReturn('')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->configureJob($data);
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextData
*/
public function testGetNextDataInitial()
{
// data
$config = ['stage' => 'initial'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// assert that new Initial is created:
$processor = $this->mock(Initial::class);
$processor->shouldReceive('setJob')->withArgs([$job])->once();
$processor->shouldReceive('getData')->once();
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->getNextData();
}
/**
* Should throw a FireflyException when $job is null.
*
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextData
* @expectedException \FireflyIII\Exceptions\FireflyException
*/
public function testGetNextDataNoJob()
{
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator = new FileConfigurator();
$configurator->getNextData();
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextData
*/
public function testGetNextDataUploadConfig()
{
// data
$config = ['stage' => 'upload-config'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// assert that new Initial is created:
$processor = $this->mock(UploadConfig::class);
$processor->shouldReceive('setJob')->withArgs([$job])->once();
$processor->shouldReceive('getData')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->getNextData();
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextData
* @expectedException \FireflyIII\Exceptions\FireflyException
* @expectedExceptionMessage Cannot handle job stage "ksksjje" in getConfigurationClass().
*/
public function testGetNextDataUploadInvalid()
{
// data
$config = ['stage' => 'ksksjje'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// should throw error
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->getNextData();
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextData
*/
public function testGetNextDataUploadMap()
{
// data:
$config = ['stage' => 'map'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// assert that new Initial is created:
$processor = $this->mock(Map::class);
$processor->shouldReceive('setJob')->withArgs([$job])->once();
$processor->shouldReceive('getData')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->getNextData();
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextData
* @expectedException \FireflyIII\Exceptions\FireflyException
* @expectedExceptionMessage Cannot handle job stage "ready" in getConfigurationClass().
*/
public function testGetNextDataUploadReady()
{
// data
$config = ['stage' => 'ready'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->getNextData();
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextData
*/
public function testGetNextDataUploadRoles()
{
// data
$config = ['stage' => 'roles'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// assert that new Initial is created:
$processor = $this->mock(Roles::class);
$processor->shouldReceive('setJob')->withArgs([$job])->once();
$processor->shouldReceive('getData')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->getNextData();
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextView
*/
public function testGetNextViewInitial()
{
// data
$config = ['stage' => 'initial'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// run
$configurator = new FileConfigurator();
$configurator->setJob($job);
$view = $configurator->getNextView();
// test
$this->assertEquals('import.file.initial', $view);
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextView
* @expectedException \FireflyIII\Exceptions\FireflyException
* @expectedExceptionMessage No view for stage "slkds903ms90k"
*/
public function testGetNextViewInvalid()
{
// data
$config = ['stage' => 'slkds903ms90k'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->getNextView();
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextView
*/
public function testGetNextViewMap()
{
// data
$config = ['stage' => 'map'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$view = $configurator->getNextView();
// test
$this->assertEquals('import.file.map', $view);
}
/**
* Should throw a FireflyException when $job is null.
*
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextView
* @expectedException \FireflyIII\Exceptions\FireflyException
*/
public function testGetNextViewNoJob()
{
$repository = $this->mock(ImportJobRepositoryInterface::class);
$configurator = new FileConfigurator();
$configurator->getNextView();
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextView
* @expectedException \FireflyIII\Exceptions\FireflyException
* @expectedExceptionMessage No view for stage "ready"
*/
public function testGetNextViewReady()
{
// data
$config = ['stage' => 'ready'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// run configx§
$configurator = new FileConfigurator();
$configurator->setJob($job);
$configurator->getNextView();
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextView
*/
public function testGetNextViewRoles()
{
// data
$config = ['stage' => 'roles'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$view = $configurator->getNextView();
// test
$this->assertEquals('import.file.roles', $view);
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getNextView
*/
public function testGetNextViewUploadConfig()
{
// data
$config = ['stage' => 'upload-config'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$view = $configurator->getNextView();
// test
$this->assertEquals('import.file.upload-config', $view);
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getWarningMessage
*/
public function testGetWarningMessage()
{
// data
$config = ['stage' => 'upload-config'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->once();
$repository->shouldReceive('setConfiguration')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$warning = $configurator->getWarningMessage();
// test
$this->assertEquals('', $warning);
}
/**
* Should throw a FireflyException when $job is null.
*
* @covers \FireflyIII\Import\Configuration\FileConfigurator::getWarningMessage
* @expectedException \FireflyIII\Exceptions\FireflyException
*/
public function testGetWarningMessageNoJob()
{
$configurator = new FileConfigurator();
$configurator->getWarningMessage();
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::isJobConfigured
*/
public function testIsJobConfiguredFalse()
{
// data
$config = ['stage' => 'upload-config'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$result = $configurator->isJobConfigured();
// test
$this->assertFalse($result);
}
/**
* @covers \FireflyIII\Import\Configuration\FileConfigurator::isJobConfigured
*/
public function testIsJobConfiguredTrue()
{
// data
$config = ['stage' => 'ready'];
$job = $this->getJob($config);
// mock repos
$repository = $this->mock(ImportJobRepositoryInterface::class);
$repository->shouldReceive('setUser')->withArgs([Mockery::any()])->once();
$repository->shouldReceive('getConfiguration')->andReturn($config)->twice();
$repository->shouldReceive('setConfiguration')->once();
// run config
$configurator = new FileConfigurator();
$configurator->setJob($job);
$result = $configurator->isJobConfigured();
// test
$this->assertTrue($result);
}
/**
* Should throw a FireflyException when $job is null.
*
* @covers \FireflyIII\Import\Configuration\FileConfigurator::isJobConfigured
* @expectedException \FireflyIII\Exceptions\FireflyException
*/
public function testIsJobConfiguredNoJob()
{
$configurator = new FileConfigurator();
$configurator->isJobConfigured();
}
/**
* @param array $config
*
* @return ImportJob
*/
protected function getJob(array $config): ImportJob
{
$job = new ImportJob;
$job->file_type = 'file';
$job->status = 'new';
$job->key = 'x' . rand(1, 100000);
$job->user()->associate($this->user());
$job->configuration = $config;
return $job;
}
}