mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Improve test coverage.
This commit is contained in:
parent
50874c9cf7
commit
5a560b42ef
@ -67,7 +67,7 @@ class ConfigureMappingHandler implements ConfigurationInterface
|
|||||||
$specifics = $config['specifics'] ?? [];
|
$specifics = $config['specifics'] ?? [];
|
||||||
$names = array_keys($specifics);
|
$names = array_keys($specifics);
|
||||||
foreach ($names as $name) {
|
foreach ($names as $name) {
|
||||||
if (!\in_array($name, $validSpecifics)) {
|
if (!\in_array($name, $validSpecifics, true)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$class = config(sprintf('csv.import_specifics.%s', $name));
|
$class = config(sprintf('csv.import_specifics.%s', $name));
|
||||||
@ -109,6 +109,26 @@ class ConfigureMappingHandler implements ConfigurationInterface
|
|||||||
return new MessageBag;
|
return new MessageBag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the "mapper" class that will eventually return the correct data for the user
|
||||||
|
* to map against. For example: a list of asset accounts. A list of budgets. A list of tags.
|
||||||
|
*
|
||||||
|
* @param string $column
|
||||||
|
*
|
||||||
|
* @return MapperInterface
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
public function createMapper(string $column): MapperInterface
|
||||||
|
{
|
||||||
|
$mapperClass = config('csv.import_roles.' . $column . '.mapper');
|
||||||
|
$mapperName = sprintf('FireflyIII\\Import\Mapper\\%s', $mapperClass);
|
||||||
|
if (!class_exists($mapperName)) {
|
||||||
|
throw new FireflyException(sprintf('Class "%s" does not exist. Cannot map "%s"', $mapperName, $column)); // @codeCoverageIgnore
|
||||||
|
}
|
||||||
|
|
||||||
|
return app($mapperName);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For each column in the configuration of the job, will:
|
* For each column in the configuration of the job, will:
|
||||||
* - validate the role.
|
* - validate the role.
|
||||||
@ -327,24 +347,4 @@ class ConfigureMappingHandler implements ConfigurationInterface
|
|||||||
$this->attachments = app(AttachmentHelperInterface::class);
|
$this->attachments = app(AttachmentHelperInterface::class);
|
||||||
$this->columnConfig = [];
|
$this->columnConfig = [];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Create the "mapper" class that will eventually return the correct data for the user
|
|
||||||
* to map against. For example: a list of asset accounts. A list of budgets. A list of tags.
|
|
||||||
*
|
|
||||||
* @param string $column
|
|
||||||
*
|
|
||||||
* @return MapperInterface
|
|
||||||
* @throws FireflyException
|
|
||||||
*/
|
|
||||||
private function createMapper(string $column): MapperInterface
|
|
||||||
{
|
|
||||||
$mapperClass = config('csv.import_roles.' . $column . '.mapper');
|
|
||||||
$mapperName = sprintf('FireflyIII\\Import\Mapper\\%s', $mapperClass);
|
|
||||||
if (!class_exists($mapperName)) {
|
|
||||||
throw new FireflyException(sprintf('Class "%s" does not exist. Cannot map "%s"', $mapperName, $column)); // @codeCoverageIgnore
|
|
||||||
}
|
|
||||||
|
|
||||||
return app($mapperName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -52,80 +52,6 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
/** @var int */
|
/** @var int */
|
||||||
private $totalColumns;
|
private $totalColumns;
|
||||||
|
|
||||||
/**
|
|
||||||
* Store data associated with current stage.
|
|
||||||
*
|
|
||||||
* @param array $data
|
|
||||||
*
|
|
||||||
* @return MessageBag
|
|
||||||
*/
|
|
||||||
public function configureJob(array $data): MessageBag
|
|
||||||
{
|
|
||||||
$config = $this->importJob->configuration;
|
|
||||||
$count = $config['column-count'];
|
|
||||||
for ($i = 0; $i < $count; ++$i) {
|
|
||||||
$role = $data['role'][$i] ?? '_ignore';
|
|
||||||
$mapping = (isset($data['map'][$i]) && $data['map'][$i] === '1');
|
|
||||||
$config['column-roles'][$i] = $role;
|
|
||||||
$config['column-do-mapping'][$i] = $mapping;
|
|
||||||
Log::debug(sprintf('Column %d has been given role %s (mapping: %s)', $i, $role, var_export($mapping, true)));
|
|
||||||
}
|
|
||||||
$config = $this->ignoreUnmappableColumns($config);
|
|
||||||
$messages = $this->configurationComplete($config);
|
|
||||||
|
|
||||||
if ($messages->count() === 0) {
|
|
||||||
$this->repository->setStage($this->importJob, 'ready_to_run');
|
|
||||||
if ($this->isMappingNecessary($config)) {
|
|
||||||
$this->repository->setStage($this->importJob, 'map');
|
|
||||||
}
|
|
||||||
$this->repository->setConfiguration($this->importJob, $config);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $messages;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the data necessary to show the configuration screen.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
* @throws FireflyException
|
|
||||||
*/
|
|
||||||
public function getNextData(): array
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$reader = $this->getReader();
|
|
||||||
} catch (Exception $e) {
|
|
||||||
Log::error($e->getMessage());
|
|
||||||
throw new FireflyException($e->getMessage());
|
|
||||||
}
|
|
||||||
$headers = $this->getHeaders($reader);
|
|
||||||
|
|
||||||
// get example rows:
|
|
||||||
$this->getExamples($reader);
|
|
||||||
|
|
||||||
return [
|
|
||||||
'examples' => $this->examples,
|
|
||||||
'roles' => $this->getRoles(),
|
|
||||||
'total' => $this->totalColumns,
|
|
||||||
'headers' => $headers,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set job and some start values.
|
|
||||||
*
|
|
||||||
* @param ImportJob $job
|
|
||||||
*/
|
|
||||||
public function setJob(ImportJob $job): void
|
|
||||||
{
|
|
||||||
$this->importJob = $job;
|
|
||||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
|
||||||
$this->repository->setUser($job->user);
|
|
||||||
$this->attachments = app(AttachmentHelperInterface::class);
|
|
||||||
$this->totalColumns = 0;
|
|
||||||
$this->examples = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies that the configuration of the job is actually complete, and valid.
|
* Verifies that the configuration of the job is actually complete, and valid.
|
||||||
*
|
*
|
||||||
@ -133,8 +59,10 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
*
|
*
|
||||||
* @return MessageBag
|
* @return MessageBag
|
||||||
*/
|
*/
|
||||||
private function configurationComplete(array $config): MessageBag
|
public function configurationComplete(array $config): MessageBag
|
||||||
{
|
{
|
||||||
|
/** @var array $roles */
|
||||||
|
$roles = $config['column-roles'];
|
||||||
$count = $config['column-count'];
|
$count = $config['column-count'];
|
||||||
$assigned = 0;
|
$assigned = 0;
|
||||||
|
|
||||||
@ -142,8 +70,7 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
$hasAmount = false;
|
$hasAmount = false;
|
||||||
$hasForeignAmount = false;
|
$hasForeignAmount = false;
|
||||||
$hasForeignCode = false;
|
$hasForeignCode = false;
|
||||||
for ($i = 0; $i < $count; ++$i) {
|
foreach ($roles as $role) {
|
||||||
$role = $config['column-roles'][$i] ?? '_ignore';
|
|
||||||
if ('_ignore' !== $role) {
|
if ('_ignore' !== $role) {
|
||||||
++$assigned;
|
++$assigned;
|
||||||
}
|
}
|
||||||
@ -180,7 +107,39 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return new MessageBag;
|
return new MessageBag; // @codeCoverageIgnore
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store data associated with current stage.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return MessageBag
|
||||||
|
*/
|
||||||
|
public function configureJob(array $data): MessageBag
|
||||||
|
{
|
||||||
|
$config = $this->importJob->configuration;
|
||||||
|
$count = $config['column-count'];
|
||||||
|
for ($i = 0; $i < $count; ++$i) {
|
||||||
|
$role = $data['role'][$i] ?? '_ignore';
|
||||||
|
$mapping = (isset($data['map'][$i]) && $data['map'][$i] === '1');
|
||||||
|
$config['column-roles'][$i] = $role;
|
||||||
|
$config['column-do-mapping'][$i] = $mapping;
|
||||||
|
Log::debug(sprintf('Column %d has been given role %s (mapping: %s)', $i, $role, var_export($mapping, true)));
|
||||||
|
}
|
||||||
|
$config = $this->ignoreUnmappableColumns($config);
|
||||||
|
$messages = $this->configurationComplete($config);
|
||||||
|
|
||||||
|
if ($messages->count() === 0) {
|
||||||
|
$this->repository->setStage($this->importJob, 'ready_to_run');
|
||||||
|
if ($this->isMappingNecessary($config)) {
|
||||||
|
$this->repository->setStage($this->importJob, 'map');
|
||||||
|
}
|
||||||
|
$this->repository->setConfiguration($this->importJob, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -188,7 +147,7 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
*
|
*
|
||||||
* @param array $line
|
* @param array $line
|
||||||
*/
|
*/
|
||||||
private function getExampleFromLine(array $line): void
|
public function getExampleFromLine(array $line): void
|
||||||
{
|
{
|
||||||
foreach ($line as $column => $value) {
|
foreach ($line as $column => $value) {
|
||||||
$value = trim($value);
|
$value = trim($value);
|
||||||
@ -198,34 +157,43 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getExamples(): array
|
||||||
|
{
|
||||||
|
return $this->examples;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a bunch of examples from the CSV file the user has uploaded.
|
* Return a bunch of examples from the CSV file the user has uploaded.
|
||||||
*
|
*
|
||||||
* @param Reader $reader
|
* @param Reader $reader
|
||||||
|
* @param array $config
|
||||||
*
|
*
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
private function getExamples(Reader $reader): void
|
public function getExamplesFromFile(Reader $reader, array $config): void
|
||||||
{
|
{
|
||||||
// configure example data:
|
|
||||||
$config = $this->importJob->configuration;
|
|
||||||
$limit = (int)config('csv.example_rows', 5);
|
$limit = (int)config('csv.example_rows', 5);
|
||||||
$offset = isset($config['has-headers']) && $config['has-headers'] === true ? 1 : 0;
|
$offset = isset($config['has-headers']) && $config['has-headers'] === true ? 1 : 0;
|
||||||
|
|
||||||
// make statement.
|
// make statement.
|
||||||
try {
|
try {
|
||||||
$stmt = (new Statement)->limit($limit)->offset($offset);
|
$stmt = (new Statement)->limit($limit)->offset($offset);
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::error($e->getMessage());
|
Log::error($e->getMessage());
|
||||||
throw new FireflyException($e->getMessage());
|
throw new FireflyException($e->getMessage());
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
|
||||||
// grab the records:
|
// grab the records:
|
||||||
$records = $stmt->process($reader);
|
$records = $stmt->process($reader);
|
||||||
/** @var array $line */
|
/** @var array $line */
|
||||||
foreach ($records as $line) {
|
foreach ($records as $line) {
|
||||||
$line = array_values($line);
|
$line = array_values($line);
|
||||||
$line = $this->processSpecifics($line);
|
$line = $this->processSpecifics($config, $line);
|
||||||
$count = \count($line);
|
$count = \count($line);
|
||||||
$this->totalColumns = $count > $this->totalColumns ? $count : $this->totalColumns;
|
$this->totalColumns = $count > $this->totalColumns ? $count : $this->totalColumns;
|
||||||
$this->getExampleFromLine($line);
|
$this->getExampleFromLine($line);
|
||||||
@ -239,39 +207,71 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
* Get the header row, if one is present.
|
* Get the header row, if one is present.
|
||||||
*
|
*
|
||||||
* @param Reader $reader
|
* @param Reader $reader
|
||||||
|
* @param array $config
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
private function getHeaders(Reader $reader): array
|
public function getHeaders(Reader $reader, array $config): array
|
||||||
{
|
{
|
||||||
$headers = [];
|
$headers = [];
|
||||||
$config = $this->importJob->configuration;
|
if (isset($config['has-headers']) && $config['has-headers'] === true) {
|
||||||
if ($config['has-headers']) {
|
|
||||||
try {
|
try {
|
||||||
$stmt = (new Statement)->limit(1)->offset(0);
|
$stmt = (new Statement)->limit(1)->offset(0);
|
||||||
$records = $stmt->process($reader);
|
$records = $stmt->process($reader);
|
||||||
$headers = $records->fetchOne(0);
|
$headers = $records->fetchOne(0);
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::error($e->getMessage());
|
Log::error($e->getMessage());
|
||||||
throw new FireflyException($e->getMessage());
|
throw new FireflyException($e->getMessage());
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
Log::debug('Detected file headers:', $headers);
|
Log::debug('Detected file headers:', $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $headers;
|
return $headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the data necessary to show the configuration screen.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
public function getNextData(): array
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$reader = $this->getReader();
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
|
} catch (Exception $e) {
|
||||||
|
Log::error($e->getMessage());
|
||||||
|
throw new FireflyException($e->getMessage());
|
||||||
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
$configuration = $this->importJob->configuration;
|
||||||
|
$headers = $this->getHeaders($reader, $configuration);
|
||||||
|
|
||||||
|
// get example rows:
|
||||||
|
$this->getExamplesFromFile($reader, $configuration);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'examples' => $this->examples,
|
||||||
|
'roles' => $this->getRoles(),
|
||||||
|
'total' => $this->totalColumns,
|
||||||
|
'headers' => $headers,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return an instance of a CSV file reader so content of the file can be read.
|
* Return an instance of a CSV file reader so content of the file can be read.
|
||||||
*
|
*
|
||||||
* @throws \League\Csv\Exception
|
* @throws \League\Csv\Exception
|
||||||
*/
|
*/
|
||||||
private function getReader(): Reader
|
public function getReader(): Reader
|
||||||
{
|
{
|
||||||
$content = '';
|
$content = '';
|
||||||
/** @var Collection $collection */
|
/** @var Collection $collection */
|
||||||
$collection = $this->importJob->attachments;
|
$collection = $this->repository->getAttachments($this->importJob);
|
||||||
/** @var Attachment $attachment */
|
/** @var Attachment $attachment */
|
||||||
foreach ($collection as $attachment) {
|
foreach ($collection as $attachment) {
|
||||||
if ($attachment->filename === 'import_file') {
|
if ($attachment->filename === 'import_file') {
|
||||||
@ -289,9 +289,10 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
/**
|
/**
|
||||||
* Returns all possible roles and translate their name. Then sort them.
|
* Returns all possible roles and translate their name. Then sort them.
|
||||||
*
|
*
|
||||||
|
* @codeCoverageIgnore
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function getRoles(): array
|
public function getRoles(): array
|
||||||
{
|
{
|
||||||
$roles = [];
|
$roles = [];
|
||||||
foreach (array_keys(config('csv.import_roles')) as $role) {
|
foreach (array_keys(config('csv.import_roles')) as $role) {
|
||||||
@ -310,7 +311,7 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function ignoreUnmappableColumns(array $config): array
|
public function ignoreUnmappableColumns(array $config): array
|
||||||
{
|
{
|
||||||
$count = $config['column-count'];
|
$count = $config['column-count'];
|
||||||
for ($i = 0; $i < $count; ++$i) {
|
for ($i = 0; $i < $count; ++$i) {
|
||||||
@ -333,13 +334,13 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function isMappingNecessary(array $config): bool
|
public function isMappingNecessary(array $config): bool
|
||||||
{
|
{
|
||||||
$count = $config['column-count'];
|
/** @var array $doMapping */
|
||||||
|
$doMapping = $config['column-do-mapping'] ?? [];
|
||||||
$toBeMapped = 0;
|
$toBeMapped = 0;
|
||||||
for ($i = 0; $i < $count; ++$i) {
|
foreach ($doMapping as $doMap) {
|
||||||
$mapping = $config['column-do-mapping'][$i] ?? false;
|
if (true === $doMap) {
|
||||||
if (true === $mapping) {
|
|
||||||
++$toBeMapped;
|
++$toBeMapped;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -350,7 +351,7 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
/**
|
/**
|
||||||
* Make sure that the examples do not contain double data values.
|
* Make sure that the examples do not contain double data values.
|
||||||
*/
|
*/
|
||||||
private function makeExamplesUnique(): void
|
public function makeExamplesUnique(): void
|
||||||
{
|
{
|
||||||
foreach ($this->examples as $index => $values) {
|
foreach ($this->examples as $index => $values) {
|
||||||
$this->examples[$index] = array_unique($values);
|
$this->examples[$index] = array_unique($values);
|
||||||
@ -360,16 +361,20 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
/**
|
/**
|
||||||
* if the user has configured specific fixes to be applied, they must be applied to the example data as well.
|
* if the user has configured specific fixes to be applied, they must be applied to the example data as well.
|
||||||
*
|
*
|
||||||
|
* @param array $config
|
||||||
* @param array $line
|
* @param array $line
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function processSpecifics(array $line): array
|
public function processSpecifics(array $config, array $line): array
|
||||||
{
|
{
|
||||||
$config = $this->importJob->configuration;
|
$validSpecifics = array_keys(config('csv.import_specifics'));
|
||||||
$specifics = $config['specifics'] ?? [];
|
$specifics = $config['specifics'] ?? [];
|
||||||
$names = array_keys($specifics);
|
$names = array_keys($specifics);
|
||||||
foreach ($names as $name) {
|
foreach ($names as $name) {
|
||||||
|
if (!\in_array($name, $validSpecifics, true)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
/** @var SpecificInterface $specific */
|
/** @var SpecificInterface $specific */
|
||||||
$specific = app('FireflyIII\Import\Specifics\\' . $name);
|
$specific = app('FireflyIII\Import\Specifics\\' . $name);
|
||||||
$line = $specific->run($line);
|
$line = $specific->run($line);
|
||||||
@ -381,13 +386,29 @@ class ConfigureRolesHandler implements ConfigurationInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Save the column count in the job. It's used in a later stage.
|
* Save the column count in the job. It's used in a later stage.
|
||||||
|
* TODO move config out of this method (make it a parameter).
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function saveColumCount(): void
|
public function saveColumCount(): void
|
||||||
{
|
{
|
||||||
$config = $this->importJob->configuration;
|
$config = $this->importJob->configuration;
|
||||||
$config['column-count'] = $this->totalColumns;
|
$config['column-count'] = $this->totalColumns;
|
||||||
$this->repository->setConfiguration($this->importJob, $config);
|
$this->repository->setConfiguration($this->importJob, $config);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Set job and some start values.
|
||||||
|
*
|
||||||
|
* @param ImportJob $job
|
||||||
|
*/
|
||||||
|
public function setJob(ImportJob $job): void
|
||||||
|
{
|
||||||
|
$this->importJob = $job;
|
||||||
|
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||||
|
$this->repository->setUser($job->user);
|
||||||
|
$this->attachments = app(AttachmentHelperInterface::class);
|
||||||
|
$this->totalColumns = 0;
|
||||||
|
$this->examples = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -145,7 +145,7 @@ class ConfigureUploadHandler implements ConfigurationInterface
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function getSpecifics(array $data): array
|
public function getSpecifics(array $data): array
|
||||||
{
|
{
|
||||||
$return = [];
|
$return = [];
|
||||||
// check if specifics given are correct:
|
// check if specifics given are correct:
|
||||||
@ -153,7 +153,7 @@ class ConfigureUploadHandler implements ConfigurationInterface
|
|||||||
|
|
||||||
foreach ($data['specifics'] as $name) {
|
foreach ($data['specifics'] as $name) {
|
||||||
// verify their content.
|
// verify their content.
|
||||||
$className = sprintf('FireflyIII\Import\Specifics\%s', $name);
|
$className = sprintf('FireflyIII\\Import\\Specifics\\%s', $name);
|
||||||
if (class_exists($className)) {
|
if (class_exists($className)) {
|
||||||
$return[$name] = 1;
|
$return[$name] = 1;
|
||||||
}
|
}
|
||||||
@ -162,4 +162,4 @@ class ConfigureUploadHandler implements ConfigurationInterface
|
|||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -219,4 +219,4 @@ class NewFileJobHandler implements ConfigurationInterface
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,4 +110,4 @@ class ColumnValue
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -300,7 +300,7 @@ class ImportTransaction
|
|||||||
* @var string $modifier
|
* @var string $modifier
|
||||||
*/
|
*/
|
||||||
foreach ($this->modifiers as $role => $modifier) {
|
foreach ($this->modifiers as $role => $modifier) {
|
||||||
$class = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $role)));
|
$class = sprintf('FireflyIII\\Import\\Converter\\%s', config(sprintf('csv.import_roles.%s.converter', $role)));
|
||||||
/** @var ConverterInterface $converter */
|
/** @var ConverterInterface $converter */
|
||||||
$converter = app($class);
|
$converter = app($class);
|
||||||
Log::debug(sprintf('Now launching converter %s', $class));
|
Log::debug(sprintf('Now launching converter %s', $class));
|
||||||
@ -342,7 +342,7 @@ class ImportTransaction
|
|||||||
* @var string $modifier
|
* @var string $modifier
|
||||||
*/
|
*/
|
||||||
foreach ($this->modifiers as $role => $modifier) {
|
foreach ($this->modifiers as $role => $modifier) {
|
||||||
$class = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $role)));
|
$class = sprintf('FireflyIII\\Import\\Converter\\%s', config(sprintf('csv.import_roles.%s.converter', $role)));
|
||||||
/** @var ConverterInterface $converter */
|
/** @var ConverterInterface $converter */
|
||||||
$converter = app($class);
|
$converter = app($class);
|
||||||
Log::debug(sprintf('Now launching converter %s', $class));
|
Log::debug(sprintf('Now launching converter %s', $class));
|
||||||
@ -565,4 +565,4 @@ class ImportTransaction
|
|||||||
return $info;
|
return $info;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -736,4 +736,4 @@ class CSVProcessor implements FileProcessorInterface
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,4 +46,4 @@ interface FileProcessorInterface
|
|||||||
* @param ImportJob $job
|
* @param ImportJob $job
|
||||||
*/
|
*/
|
||||||
public function setJob(ImportJob $job): void;
|
public function setJob(ImportJob $job): void;
|
||||||
}
|
}
|
||||||
|
@ -112,4 +112,4 @@
|
|||||||
</script>
|
</script>
|
||||||
<script type="text/javascript" src="js/lib/bootstrap-multiselect.js?v={{ FF_VERSION }}"></script>
|
<script type="text/javascript" src="js/lib/bootstrap-multiselect.js?v={{ FF_VERSION }}"></script>
|
||||||
<script type="text/javascript" src="js/ff/import/file/configure-upload.js?v={{ FF_VERSION }}"></script>
|
<script type="text/javascript" src="js/ff/import/file/configure-upload.js?v={{ FF_VERSION }}"></script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -364,4 +364,4 @@ class FileJobConfigurationTest extends TestCase
|
|||||||
}
|
}
|
||||||
$this->assertEquals('import.file.roles', $result);
|
$this->assertEquals('import.file.roles', $result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,4 +75,4 @@ class FileRoutineTest extends TestCase
|
|||||||
$this->assertTrue(false, $e->getMessage());
|
$this->assertTrue(false, $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -485,4 +485,4 @@ class ConfigureMappingHandlerTest extends TestCase
|
|||||||
$this->assertEquals('_ignore', $handler->sanitizeColumnName('some-bad-name'));
|
$this->assertEquals('_ignore', $handler->sanitizeColumnName('some-bad-name'));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,525 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ConfigureRolesHandlerTest.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\Configuration\File;
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
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\Configuration\File\ConfigureRolesHandler;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use League\Csv\Reader;
|
||||||
|
use Mockery;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ConfigureRolesHandlerTest
|
||||||
|
*/
|
||||||
|
class ConfigureRolesHandlerTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testConfigurationCompleteBasic(): void
|
||||||
|
{
|
||||||
|
$config = [
|
||||||
|
'column-count' => 5,
|
||||||
|
'column-roles' => [
|
||||||
|
0 => 'amount',
|
||||||
|
1 => 'description',
|
||||||
|
2 => 'note',
|
||||||
|
3 => 'foreign-currency-code',
|
||||||
|
4 => 'amount_foreign',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$handler = new ConfigureRolesHandler();
|
||||||
|
$result = $handler->configurationComplete($config);
|
||||||
|
$this->assertCount(0, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testConfigurationCompleteForeign(): void
|
||||||
|
{
|
||||||
|
$config = [
|
||||||
|
'column-count' => 5,
|
||||||
|
'column-roles' => [
|
||||||
|
0 => 'amount',
|
||||||
|
1 => 'description',
|
||||||
|
2 => 'note',
|
||||||
|
3 => 'amount_foreign',
|
||||||
|
4 => 'sepa-cc',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$handler = new ConfigureRolesHandler();
|
||||||
|
$result = $handler->configurationComplete($config);
|
||||||
|
$this->assertCount(1, $result);
|
||||||
|
$this->assertEquals(
|
||||||
|
'If you mark a column as containing an amount in a foreign currency, you must also set the column that contains which currency it is.',
|
||||||
|
$result->get('error')[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testConfigurationCompleteNoAmount(): void
|
||||||
|
{
|
||||||
|
$config = [
|
||||||
|
'column-count' => 5,
|
||||||
|
'column-roles' => [
|
||||||
|
0 => 'sepa-cc',
|
||||||
|
1 => 'description',
|
||||||
|
2 => 'note',
|
||||||
|
3 => 'foreign-currency-code',
|
||||||
|
4 => 'amount_foreign',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$handler = new ConfigureRolesHandler();
|
||||||
|
$result = $handler->configurationComplete($config);
|
||||||
|
$this->assertCount(1, $result);
|
||||||
|
$this->assertEquals(
|
||||||
|
'At the very least, mark one column as the amount-column. It is advisable to also select a column for the description, date and the opposing account.',
|
||||||
|
$result->get('error')[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testConfigureJob(): void
|
||||||
|
{
|
||||||
|
$job = new ImportJob;
|
||||||
|
$job->user_id = $this->user()->id;
|
||||||
|
$job->key = 'role-B' . random_int(1, 1000);
|
||||||
|
$job->status = 'new';
|
||||||
|
$job->stage = 'new';
|
||||||
|
$job->provider = 'fake';
|
||||||
|
$job->file_type = '';
|
||||||
|
$job->configuration = [
|
||||||
|
'column-count' => 5,
|
||||||
|
];
|
||||||
|
$job->save();
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'role' => [
|
||||||
|
0 => 'description',
|
||||||
|
1 => 'budget-id',
|
||||||
|
2 => 'sepa-cc',
|
||||||
|
4 => 'amount', // no column 3.
|
||||||
|
],
|
||||||
|
'map' => [
|
||||||
|
0 => '1', // map column 0 (which cannot be mapped anyway)
|
||||||
|
1 => '1', // map column 1 (which CAN be mapped)
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$expected = [
|
||||||
|
'column-count' => 5,
|
||||||
|
'column-roles' => [
|
||||||
|
0 => 'description',
|
||||||
|
1 => 'budget-id',
|
||||||
|
2 => 'sepa-cc',
|
||||||
|
3 => '_ignore', // added column 3
|
||||||
|
4 => 'amount',
|
||||||
|
],
|
||||||
|
'column-do-mapping' => [false, true, false, false, false],
|
||||||
|
];
|
||||||
|
|
||||||
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
$repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'ready_to_run']);
|
||||||
|
$repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'map']);
|
||||||
|
$repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $expected]);
|
||||||
|
|
||||||
|
$handler = new ConfigureRolesHandler();
|
||||||
|
$handler->setJob($job);
|
||||||
|
$handler->configureJob($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testGetExampleFromLine(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
$lines = [
|
||||||
|
['one', 'two', '', 'three'],
|
||||||
|
['four', 'five', '', 'six'],
|
||||||
|
];
|
||||||
|
|
||||||
|
$handler = new ConfigureRolesHandler;
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
$handler->getExampleFromLine($line);
|
||||||
|
}
|
||||||
|
$expected = [
|
||||||
|
0 => ['one', 'four'],
|
||||||
|
1 => ['two', 'five'],
|
||||||
|
3 => ['three', 'six'],
|
||||||
|
];
|
||||||
|
$this->assertEquals($expected, $handler->getExamples());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testGetExamplesFromFile(): void
|
||||||
|
{
|
||||||
|
$job = new ImportJob;
|
||||||
|
$job->user_id = $this->user()->id;
|
||||||
|
$job->key = 'role-x' . random_int(1, 1000);
|
||||||
|
$job->status = 'new';
|
||||||
|
$job->stage = 'new';
|
||||||
|
$job->provider = 'fake';
|
||||||
|
$job->file_type = '';
|
||||||
|
$job->configuration = [
|
||||||
|
'specifics' => [],
|
||||||
|
'has-headers' => false,
|
||||||
|
];
|
||||||
|
$job->save();
|
||||||
|
|
||||||
|
$file = "one,two,,three\nfour,five,,six\none,three,X,three";
|
||||||
|
$reader = Reader::createFromString($file);
|
||||||
|
$handler = new ConfigureRolesHandler;
|
||||||
|
$handler->setJob($job);
|
||||||
|
try {
|
||||||
|
$handler->getExamplesFromFile($reader, $job->configuration);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->assertTrue(false, $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
$expected = [
|
||||||
|
0 => ['one', 'four'],
|
||||||
|
1 => ['two', 'five', 'three'],
|
||||||
|
2 => ['X'],
|
||||||
|
3 => ['three', 'six'],
|
||||||
|
];
|
||||||
|
$this->assertEquals($expected, $handler->getExamples());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testGetHeadersHas(): void
|
||||||
|
{
|
||||||
|
// create a reader to use in method.
|
||||||
|
// 5 columns, of which #4 (index 3) is budget-id
|
||||||
|
// 5 columns, of which #5 (index 4) is tags-space
|
||||||
|
$file = "header1,header2,header3,header4,header5\nvalue4,value5,value6,2,more tags there\nvalueX,valueY,valueZ\nA,B,C,,\nd,e,f,1,xxx";
|
||||||
|
$reader = Reader::createFromString($file);
|
||||||
|
$config = ['has-headers' => true];
|
||||||
|
|
||||||
|
$handler = new ConfigureRolesHandler;
|
||||||
|
try {
|
||||||
|
$headers = $handler->getHeaders($reader, $config);
|
||||||
|
} catch (FireflyException $e) {
|
||||||
|
$this->assertTrue(false, $e->getMessage());
|
||||||
|
}
|
||||||
|
$this->assertEquals(['header1', 'header2', 'header3', 'header4', 'header5'], $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testGetHeadersNone(): void
|
||||||
|
{
|
||||||
|
// create a reader to use in method.
|
||||||
|
// 5 columns, of which #4 (index 3) is budget-id
|
||||||
|
// 5 columns, of which #5 (index 4) is tags-space
|
||||||
|
$file = "header1,header2,header3,header4,header5\nvalue4,value5,value6,2,more tags there\nvalueX,valueY,valueZ\nA,B,C,,\nd,e,f,1,xxx";
|
||||||
|
$reader = Reader::createFromString($file);
|
||||||
|
$config = ['has-headers' => false];
|
||||||
|
|
||||||
|
$handler = new ConfigureRolesHandler;
|
||||||
|
try {
|
||||||
|
$headers = $handler->getHeaders($reader, $config);
|
||||||
|
} catch (FireflyException $e) {
|
||||||
|
$this->assertTrue(false, $e->getMessage());
|
||||||
|
}
|
||||||
|
$this->assertEquals([], $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetNextData(): void
|
||||||
|
{
|
||||||
|
$job = new ImportJob;
|
||||||
|
$job->user_id = $this->user()->id;
|
||||||
|
$job->key = 'role-x' . random_int(1, 1000);
|
||||||
|
$job->status = 'new';
|
||||||
|
$job->stage = 'new';
|
||||||
|
$job->provider = 'fake';
|
||||||
|
$job->file_type = '';
|
||||||
|
$job->configuration = [
|
||||||
|
'delimiter' => ',',
|
||||||
|
'has-headers' => true,
|
||||||
|
];
|
||||||
|
$job->save();
|
||||||
|
|
||||||
|
// make one attachment.
|
||||||
|
$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();
|
||||||
|
|
||||||
|
$fileContent = "column1,column2,column3\nvalue1,value2,value3";
|
||||||
|
// mock some helpers:
|
||||||
|
$attachments = $this->mock(AttachmentHelperInterface::class);
|
||||||
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('getConfiguration')->once()->withArgs([Mockery::any()])->andReturn($job->configuration);
|
||||||
|
$repository->shouldReceive('setConfiguration')->once()->withArgs(
|
||||||
|
[Mockery::any(),
|
||||||
|
[
|
||||||
|
'delimiter' => ',',
|
||||||
|
'has-headers' => true,
|
||||||
|
'column-count' => 3,
|
||||||
|
],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
$repository->shouldReceive('getAttachments')->once()->withArgs([Mockery::any()])->andReturn(new Collection([$att]));
|
||||||
|
$attachments->shouldReceive('getAttachmentContent')->withArgs([Mockery::any()])->andReturn($fileContent);
|
||||||
|
|
||||||
|
$expected = [
|
||||||
|
'examples' => [
|
||||||
|
0 => ['value1'],
|
||||||
|
1 => ['value2'],
|
||||||
|
2 => ['value3'],
|
||||||
|
],
|
||||||
|
'total' => 3,
|
||||||
|
'headers' => ['column1', 'column2', 'column3'],
|
||||||
|
];
|
||||||
|
|
||||||
|
$handler = new ConfigureRolesHandler();
|
||||||
|
$handler->setJob($job);
|
||||||
|
try {
|
||||||
|
$result = $handler->getNextData();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->assertTrue(false, $e->getMessage());
|
||||||
|
}
|
||||||
|
$this->assertEquals($expected['examples'], $result['examples']);
|
||||||
|
$this->assertEquals($expected['total'], $result['total']);
|
||||||
|
$this->assertEquals($expected['headers'], $result['headers']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testGetReader(): void
|
||||||
|
{
|
||||||
|
$job = new ImportJob;
|
||||||
|
$job->user_id = $this->user()->id;
|
||||||
|
$job->key = 'role-x' . random_int(1, 1000);
|
||||||
|
$job->status = 'new';
|
||||||
|
$job->stage = 'new';
|
||||||
|
$job->provider = 'fake';
|
||||||
|
$job->file_type = '';
|
||||||
|
$job->configuration = [];
|
||||||
|
$job->save();
|
||||||
|
|
||||||
|
// make one attachment.
|
||||||
|
$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();
|
||||||
|
$config = [
|
||||||
|
'delimiter' => ',',
|
||||||
|
];
|
||||||
|
|
||||||
|
$fileContent = "column1,column2,column3\nvalue1,value2,value3";
|
||||||
|
|
||||||
|
// mock some helpers:
|
||||||
|
$attachments = $this->mock(AttachmentHelperInterface::class);
|
||||||
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('getConfiguration')->once()->withArgs([Mockery::any()])->andReturn($config);
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
$repository->shouldReceive('getAttachments')->once()->withArgs([Mockery::any()])->andReturn(new Collection([$att]));
|
||||||
|
$attachments->shouldReceive('getAttachmentContent')->withArgs([Mockery::any()])->andReturn($fileContent);
|
||||||
|
|
||||||
|
$handler = new ConfigureRolesHandler();
|
||||||
|
$handler->setJob($job);
|
||||||
|
try {
|
||||||
|
$reader = $handler->getReader();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->assertTrue(false, $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testIgnoreUnmappableColumns(): void
|
||||||
|
{
|
||||||
|
$config = [
|
||||||
|
'column-count' => 5,
|
||||||
|
'column-roles' => [
|
||||||
|
'description', // cannot be mapped.
|
||||||
|
'budget-id',
|
||||||
|
'sepa-cc', // cannot be mapped.
|
||||||
|
'category-id',
|
||||||
|
'tags-comma', // cannot be mapped.
|
||||||
|
],
|
||||||
|
'column-do-mapping' => [
|
||||||
|
0 => true,
|
||||||
|
1 => true,
|
||||||
|
2 => true,
|
||||||
|
3 => true,
|
||||||
|
4 => true,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$expected = [
|
||||||
|
'column-count' => 5,
|
||||||
|
'column-roles' => [
|
||||||
|
'description', // cannot be mapped.
|
||||||
|
'budget-id',
|
||||||
|
'sepa-cc', // cannot be mapped.
|
||||||
|
'category-id',
|
||||||
|
'tags-comma', // cannot be mapped.
|
||||||
|
],
|
||||||
|
'column-do-mapping' => [
|
||||||
|
0 => false,
|
||||||
|
1 => true,
|
||||||
|
2 => false,
|
||||||
|
3 => true,
|
||||||
|
4 => false,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$handler = new ConfigureRolesHandler;
|
||||||
|
$this->assertEquals($expected, $handler->ignoreUnmappableColumns($config));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testIsMappingNecessaryNo(): void
|
||||||
|
{
|
||||||
|
$config = [
|
||||||
|
'column-do-mapping' => [false, false, false],
|
||||||
|
];
|
||||||
|
$handler = new ConfigureRolesHandler();
|
||||||
|
$result = $handler->isMappingNecessary($config);
|
||||||
|
$this->assertFalse($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testIsMappingNecessaryYes(): void
|
||||||
|
{
|
||||||
|
$config = [
|
||||||
|
'column-do-mapping' => [false, true, false, false],
|
||||||
|
];
|
||||||
|
$handler = new ConfigureRolesHandler();
|
||||||
|
$result = $handler->isMappingNecessary($config);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testMakeExamplesUnique(): void
|
||||||
|
{
|
||||||
|
$lines = [
|
||||||
|
['one', 'two', '', 'three'],
|
||||||
|
['four', 'five', '', 'six'],
|
||||||
|
['one', 'three', 'X', 'three'],
|
||||||
|
];
|
||||||
|
|
||||||
|
$handler = new ConfigureRolesHandler;
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
$handler->getExampleFromLine($line);
|
||||||
|
}
|
||||||
|
$handler->makeExamplesUnique();
|
||||||
|
|
||||||
|
$expected = [
|
||||||
|
0 => ['one', 'four'],
|
||||||
|
1 => ['two', 'five', 'three'],
|
||||||
|
2 => ['X'],
|
||||||
|
3 => ['three', 'six'],
|
||||||
|
];
|
||||||
|
$this->assertEquals($expected, $handler->getExamples());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testProcessSpecifics(): void
|
||||||
|
{
|
||||||
|
$line = [];
|
||||||
|
$config = [
|
||||||
|
'specifics' => [
|
||||||
|
'IngDescription' => true,
|
||||||
|
'some-bad-specific' => true,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$ingDescription = $this->mock(IngDescription::class);
|
||||||
|
$ingDescription->shouldReceive('run')->once()->withArgs([[]])->andReturn(['a' => 'b']);
|
||||||
|
|
||||||
|
$handler = new ConfigureRolesHandler;
|
||||||
|
$this->assertEquals(['a' => 'b'], $handler->processSpecifics($config, []));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Support\Import\Configuration\File\ConfigureRolesHandler
|
||||||
|
*/
|
||||||
|
public function testSaveColumCount(): void
|
||||||
|
{
|
||||||
|
$job = new ImportJob;
|
||||||
|
$job->user_id = $this->user()->id;
|
||||||
|
$job->key = 'role-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(), ['column-count' => 0]]);
|
||||||
|
|
||||||
|
$handler = new ConfigureRolesHandler();
|
||||||
|
$handler->setJob($job);
|
||||||
|
$handler->saveColumCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user