Expand test coverage.

This commit is contained in:
James Cole
2018-01-05 17:29:42 +01:00
parent c329ffa545
commit 3e9f98b43e
24 changed files with 1150 additions and 93 deletions

View File

@@ -26,7 +26,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Object\ImportJournal;
use FireflyIII\Import\Specifics\SpecificInterface;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\TransactionJournalMeta;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Support\Collection;
use Iterator;
use League\Csv\Reader;
@@ -43,6 +43,8 @@ class CsvProcessor implements FileProcessorInterface
private $job;
/** @var Collection */
private $objects;
/** @var ImportJobRepositoryInterface */
private $repository;
/** @var array */
private $validConverters = [];
/** @var array */
@@ -60,9 +62,14 @@ class CsvProcessor implements FileProcessorInterface
/**
* @return Collection
* @throws FireflyException
*/
public function getObjects(): Collection
{
if (is_null($this->job)) {
throw new FireflyException('Cannot call getObjects() without a job.');
}
return $this->objects;
}
@@ -73,9 +80,13 @@ class CsvProcessor implements FileProcessorInterface
*
* @throws \League\Csv\Exception
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
* @throws FireflyException
*/
public function run(): bool
{
if (is_null($this->job)) {
throw new FireflyException('Cannot call run() without a job.');
}
Log::debug('Now in CsvProcessor run(). Job is now running...');
$entries = new Collection($this->getImportArray());
@@ -86,8 +97,8 @@ class CsvProcessor implements FileProcessorInterface
$row = array_values($row);
if ($this->rowAlreadyImported($row)) {
$message = sprintf('Row #%d has already been imported.', $index);
$this->job->addError($index, $message);
$this->job->addStepsDone(5); // all steps.
$this->repository->addStepsDone($this->job, 5);
$this->addError($index, $message);
Log::info($message);
return null;
@@ -99,23 +110,34 @@ class CsvProcessor implements FileProcessorInterface
Log::debug(sprintf('Number of entries left: %d', $notImported->count()));
// set (new) number of steps:
$status = $this->job->extended_status;
$status['steps'] = $notImported->count() * 5;
$this->job->extended_status = $status;
$this->job->save();
Log::debug(sprintf('Number of steps: %d', $notImported->count() * 5));
$extended = $this->getExtendedStatus();
$steps = $notImported->count() * 5;
$extended['steps'] = $steps;
$this->setExtendedStatus($extended);
Log::debug(sprintf('Number of steps: %d', $steps));
$notImported->each(
function (array $row, int $index) {
$journal = $this->importRow($index, $row);
$this->objects->push($journal);
$this->job->addStepsDone(1);
$this->repository->addStepsDone($this->job, 1);
}
);
return true;
}
/**
* @codeCoverageIgnore
* Shorthand method
*
* @param array $array
*/
public function setExtendedStatus(array $array)
{
$this->repository->setExtendedStatus($this->job, $array);
}
/**
* Set import job for this processor.
*
@@ -125,11 +147,30 @@ class CsvProcessor implements FileProcessorInterface
*/
public function setJob(ImportJob $job): FileProcessorInterface
{
$this->job = $job;
$this->job = $job;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($job->user);
return $this;
}
/**
* Shorthand method.
*
* @codeCoverageIgnore
*
* @param int $index
* @param string $message
*/
private function addError(int $index, string $message): void
{
$extended = $this->getExtendedStatus();
$extended['errors'][$index][] = $message;
$this->setExtendedStatus($extended);
return;
}
/**
* Add meta data to the individual value and verify that it can be handled in a later stage.
*
@@ -142,7 +183,7 @@ class CsvProcessor implements FileProcessorInterface
*/
private function annotateValue(int $index, string $value)
{
$config = $this->job->configuration;
$config = $this->getConfig();
$role = $config['column-roles'][$index] ?? '_ignore';
$mapped = $config['column-mapping-config'][$index][$value] ?? null;
@@ -160,6 +201,29 @@ class CsvProcessor implements FileProcessorInterface
return $entry;
}
/**
* @codeCoverageIgnore
* Shorthand method.
*
* @return array
*/
private function getConfig(): array
{
return $this->repository->getConfiguration($this->job);
}
/**
* @codeCoverageIgnore
* Shorthand method.
*
* @return array
*/
private function getExtendedStatus(): array
{
return $this->repository->getExtendedStatus($this->job);
}
/**
* @return Iterator
*
@@ -169,16 +233,17 @@ class CsvProcessor implements FileProcessorInterface
*/
private function getImportArray(): Iterator
{
$content = $this->job->uploadFileContents();
$config = $this->job->configuration;
$reader = Reader::createFromString($content);
$delimiter = $config['delimiter'];
$content = $this->repository->uploadFileContents($this->job);
$config = $this->getConfig();
$reader = Reader::createFromString($content);
$delimiter = $config['delimiter'] ?? ',';
$hasHeaders = isset($config['has-headers']) ? $config['has-headers'] : false;
if ('tab' === $delimiter) {
$delimiter = "\t";
$delimiter = "\t"; // @codeCoverageIgnore
}
$reader->setDelimiter($delimiter);
if ($config['has-headers']) {
$reader->setHeaderOffset(0);
if ($hasHeaders) {
$reader->setHeaderOffset(0); // @codeCoverageIgnore
}
$results = $reader->getRecords();
Log::debug('Created a CSV reader.');
@@ -191,6 +256,7 @@ class CsvProcessor implements FileProcessorInterface
*
* @param int $jsonError
*
* @codeCoverageIgnore
* @return string
*/
private function getJsonError(int $jsonError): string
@@ -230,7 +296,7 @@ class CsvProcessor implements FileProcessorInterface
$jsonError = json_last_error();
if (false === $json) {
throw new FireflyException(sprintf('Error while encoding JSON for CSV row: %s', $this->getJsonError($jsonError)));
throw new FireflyException(sprintf('Error while encoding JSON for CSV row: %s', $this->getJsonError($jsonError))); // @codeCoverageIgnore
}
$hash = hash('sha256', $json);
@@ -251,8 +317,9 @@ class CsvProcessor implements FileProcessorInterface
{
$row = array_values($row);
Log::debug(sprintf('Now at row %d', $index));
$row = $this->specifics($row);
$hash = $this->getRowHash($row);
$row = $this->specifics($row);
$hash = $this->getRowHash($row);
$config = $this->getConfig();
$journal = new ImportJournal;
$journal->setUser($this->job->user);
@@ -271,7 +338,8 @@ class CsvProcessor implements FileProcessorInterface
}
}
// set some extra info:
$journal->asset->setDefaultAccountId($this->job->configuration['import-account']);
$importAccount = intval($config['import-account'] ?? 0);
$journal->asset->setDefaultAccountId($importAccount);
return $journal;
}
@@ -288,12 +356,8 @@ class CsvProcessor implements FileProcessorInterface
private function rowAlreadyImported(array $array): bool
{
$hash = $this->getRowHash($array);
$json = json_encode($hash);
$entry = TransactionJournalMeta::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'journal_meta.transaction_journal_id')
->where('data', $json)
->where('name', 'importHash')
->first();
if (null !== $entry) {
$count = $this->repository->countByHash($hash);
if ($count > 0) {
return true;
}

View File

@@ -44,19 +44,17 @@ class AssetAccountIbans implements MapperInterface
/** @var Account $account */
foreach ($set as $account) {
$iban = $account->iban ?? '';
$iban = $account->iban ?? '';
$accountId = intval($account->id);
if (strlen($iban) > 0) {
$topList[$account->id] = $account->iban . ' (' . $account->name . ')';
$topList[$accountId] = $account->iban . ' (' . $account->name . ')';
}
if (0 === strlen($iban)) {
$list[$account->id] = $account->name;
$list[$accountId] = $account->name;
}
}
asort($topList);
asort($list);
$list = $topList + $list;
$list = [0 => trans('import.map_do_not_map')] + $list;
$list = array_merge($topList, $list);
$list = array_merge([0 => trans('import.map_do_not_map')], $list);
return $list;
}

View File

@@ -43,17 +43,15 @@ class AssetAccounts implements MapperInterface
/** @var Account $account */
foreach ($set as $account) {
$name = $account->name;
$iban = $account->iban ?? '';
$accountId = intval($account->id);
$name = $account->name;
$iban = $account->iban ?? '';
if (strlen($iban) > 0) {
$name .= ' (' . $account->iban . ')';
$name .= ' (' . $iban . ')';
}
$list[$account->id] = $name;
$list[$accountId] = $name;
}
asort($list);
$list = [0 => trans('import.map_do_not_map')] + $list;
$list = array_merge([0 => trans('import.map_do_not_map')], $list);
return $list;
}

View File

@@ -42,11 +42,10 @@ class Bills implements MapperInterface
/** @var Bill $bill */
foreach ($result as $bill) {
$list[$bill->id] = $bill->name . ' [' . $bill->match . ']';
$billId = intval($bill->id);
$list[$billId] = $bill->name . ' [' . $bill->match . ']';
}
asort($list);
$list = [0 => trans('import.map_do_not_map')] + $list;
$list = array_merge([0 => trans('import.map_do_not_map')], $list);
return $list;
}

View File

@@ -37,16 +37,15 @@ class Budgets implements MapperInterface
{
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$result = $repository->getBudgets();
$result = $repository->getActiveBudgets();
$list = [];
/** @var Budget $budget */
foreach ($result as $budget) {
$list[$budget->id] = $budget->name;
$budgetId = intval($budget->id);
$list[$budgetId] = $budget->name;
}
asort($list);
$list = [0 => trans('import.map_do_not_map')] + $list;
$list = array_merge([0 => trans('import.map_do_not_map')], $list);
return $list;
}

View File

@@ -42,11 +42,10 @@ class Categories implements MapperInterface
/** @var Category $category */
foreach ($result as $category) {
$list[$category->id] = $category->name;
$categoryId = intval($category->id);
$list[$categoryId] = $category->name;
}
asort($list);
$list = [0 => trans('import.map_do_not_map')] + $list;
$list = array_merge([0 => trans('import.map_do_not_map')], $list);
return $list;
}

View File

@@ -50,19 +50,17 @@ class OpposingAccountIbans implements MapperInterface
/** @var Account $account */
foreach ($set as $account) {
$iban = $account->iban ?? '';
$iban = $account->iban ?? '';
$accountId = intval($account->id);
if (strlen($iban) > 0) {
$topList[$account->id] = $account->iban . ' (' . $account->name . ')';
$topList[$accountId] = $account->iban . ' (' . $account->name . ')';
}
if (0 === strlen($iban)) {
$list[$account->id] = $account->name;
$list[$accountId] = $account->name;
}
}
asort($topList);
asort($list);
$list = $topList + $list;
$list = [0 => trans('import.map_do_not_map')] + $list;
$list = array_merge($topList, $list);
$list = array_merge([0 => trans('import.map_do_not_map')], $list);
return $list;
}

View File

@@ -49,17 +49,15 @@ class OpposingAccounts implements MapperInterface
/** @var Account $account */
foreach ($set as $account) {
$name = $account->name;
$iban = $account->iban ?? '';
$accountId = intval($account->id);
$name = $account->name;
$iban = $account->iban ?? '';
if (strlen($iban) > 0) {
$name .= ' (' . $account->iban . ')';
$name .= ' (' . $iban . ')';
}
$list[$account->id] = $name;
$list[$accountId] = $name;
}
asort($list);
$list = [0 => trans('import.map_do_not_map')] + $list;
$list = array_merge([0 => trans('import.map_do_not_map')], $list);
return $list;
}

View File

@@ -42,11 +42,10 @@ class Tags implements MapperInterface
/** @var Tag $tag */
foreach ($result as $tag) {
$list[$tag->id] = $tag->tag;
$tagId = intval($tag->id);
$list[$tagId] = $tag->tag;
}
asort($list);
$list = [0 => trans('import.map_do_not_map')] + $list;
$list = array_merge([0 => trans('import.map_do_not_map')], $list);
return $list;
}

View File

@@ -22,7 +22,7 @@ declare(strict_types=1);
namespace FireflyIII\Import\Mapper;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
/**
* Class TransactionCurrencies.
@@ -34,15 +34,16 @@ class TransactionCurrencies implements MapperInterface
*/
public function getMap(): array
{
$currencies = TransactionCurrency::get();
/** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class);
$currencies = $repository->get();
$list = [];
foreach ($currencies as $currency) {
$list[$currency->id] = $currency->name . ' (' . $currency->code . ')';
$currencyId = intval($currency->id);
$list[$currencyId] = $currency->name . ' (' . $currency->code . ')';
}
asort($list);
$list = [0 => trans('import.map_do_not_map')] + $list;
$list = array_merge([0 => trans('import.map_do_not_map')], $list);
return $list;
}