Refactor and rename test code.

This commit is contained in:
James Cole
2018-05-21 07:22:38 +02:00
parent 620c5f515e
commit 714b54ed06
37 changed files with 363 additions and 939 deletions

View File

@@ -1,174 +0,0 @@
<?php
/**
* HaveAccounts.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 FireflyIII\Support\Import\Configuration\Bunq;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
/**
* @codeCoverageIgnore
* @deprecated
* Class HaveAccounts
*/
class HaveAccounts
{
/** @var ImportJob */
private $job;
/**
* Get the data necessary to show the configuration screen.
*
* @return array
*/
public function getData(): array
{
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
/** @var CurrencyRepositoryInterface $currencyRepository */
$currencyRepository = app(CurrencyRepositoryInterface::class);
$config = $this->job->configuration;
$collection = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$defaultCurrency = app('amount')->getDefaultCurrency();
$dbAccounts = [];
/** @var Account $dbAccount */
foreach ($collection as $dbAccount) {
$id = $dbAccount->id;
$currencyId = (int)$accountRepository->getMetaValue($dbAccount, 'currency_id');
$currency = $currencyRepository->findNull($currencyId);
$dbAccounts[$id] = [
'account' => $dbAccount,
'currency' => $currency ?? $defaultCurrency,
];
}
// loop Bunq accounts:
/**
* @var int $index
* @var array $bunqAccount
*/
foreach ($config['accounts'] as $index => $bunqAccount) {
// find accounts with currency code
$code = $bunqAccount['currency'];
$selection = $this->filterAccounts($dbAccounts, $code);
$config['accounts'][$index]['iban'] = $this->getIban($bunqAccount);
$config['accounts'][$index]['options'] = $selection;
}
return [
'config' => $config,
];
}
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return '';
}
/**
* @param ImportJob $job
*
* @return ConfigurationInterface
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
return $this;
}
/**
* Store the result.
*
* @param array $data
*
* @return bool
*/
public function storeConfiguration(array $data): bool
{
$accounts = $data['bunq_account_id'] ?? [];
$mapping = [];
foreach ($accounts as $bunqId) {
$bunqId = (int)$bunqId;
$doImport = (int)($data['do_import'][$bunqId] ?? 0.0) === 1;
$account = (int)($data['import'][$bunqId] ?? 0.0);
if ($doImport) {
$mapping[$bunqId] = $account;
}
}
$config = $this->job->configuration;
$config['accounts-mapped'] = $mapping;
$this->job->configuration = $config;
$this->job->save();
return true;
}
/**
* @param array $dbAccounts
* @param string $code
*
* @return array
*/
private function filterAccounts(array $dbAccounts, string $code): array
{
$accounts = [];
foreach ($dbAccounts as $accountId => $data) {
if ($data['currency']->code === $code) {
$accounts[$accountId] = [
'name' => $data['account']['name'],
'iban' => $data['account']['iban'],
];
}
}
return $accounts;
}
/**
* @param array $bunqAccount
*
* @return string
*/
private function getIban(array $bunqAccount): string
{
$iban = '';
if (\is_array($bunqAccount['alias'])) {
foreach ($bunqAccount['alias'] as $alias) {
if ($alias['type'] === 'IBAN') {
$iban = $alias['value'];
}
}
}
return $iban;
}
}

View File

@@ -1,155 +0,0 @@
<?php
/**
* HaveAccounts.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 FireflyIII\Support\Import\Configuration\Spectre;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
use Illuminate\Support\Collection;
/**
* @deprecated
* @codeCoverageIgnore
* Class HaveAccounts
*/
class HaveAccounts
{
/** @var ImportJob */
private $job;
/**
* Get the data necessary to show the configuration screen.
*
* @return array
*/
public function getData(): array
{
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
/** @var CurrencyRepositoryInterface $currencyRepository */
$currencyRepository = app(CurrencyRepositoryInterface::class);
$config = $this->job->configuration;
$collection = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$defaultCurrency = app('amount')->getDefaultCurrency();
$dbAccounts = [];
/** @var Account $dbAccount */
foreach ($collection as $dbAccount) {
$id = $dbAccount->id;
$currencyId = (int)$dbAccount->getMeta('currency_id');
$currency = $currencyRepository->find($currencyId);
$dbAccounts[$id] = [
'account' => $dbAccount,
'currency' => null === $currency->id ? $defaultCurrency : $currency,
];
}
// loop Spectre accounts:
/**
* @var int $index
* @var array $spectreAccount
*/
foreach ($config['accounts'] as $index => $spectreAccount) {
// find accounts with currency code
$code = $spectreAccount['currency_code'];
$selection = $this->filterAccounts($dbAccounts, $code);
$config['accounts'][$index]['options'] = app('expandedform')->makeSelectList($selection);
}
return [
'config' => $config,
];
}
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return '';
}
/**
* @param ImportJob $job
*
* @return ConfigurationInterface
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
return $this;
}
/**
* Store the result.
*
* @param array $data
*
* @return bool
*/
public function storeConfiguration(array $data): bool
{
$accounts = $data['spectre_account_id'] ?? [];
$mapping = [];
foreach ($accounts as $spectreId) {
$spectreId = (int)$spectreId;
$doImport = (int)($data['do_import'][$spectreId] ?? 0.0) === 1;
$account = (int)($data['import'][$spectreId] ?? 0.0);
if ($doImport) {
$mapping[$spectreId] = $account;
}
}
$config = $this->job->configuration;
$config['accounts-mapped'] = $mapping;
$this->job->configuration = $config;
$this->job->save();
return true;
}
/**
* @param array $dbAccounts
* @param string $code
*
* @return Collection
*/
private function filterAccounts(array $dbAccounts, string $code): Collection
{
$collection = new Collection;
foreach ($dbAccounts as $accountId => $data) {
if ($data['currency']->code === $code) {
$collection->push($data['account']);
}
}
return $collection;
}
}

View File

@@ -33,7 +33,6 @@ use Log;
/**
* Trait GetSpectreCustomerTrait
*
* @package FireflyIII\Support\Import\Information
*/
trait GetSpectreCustomerTrait
{
@@ -52,9 +51,11 @@ trait GetSpectreCustomerTrait
/** @var NewCustomerRequest $request */
$request = app(NewCustomerRequest::class);
$request->setUser($importJob->user);
// todo what if customer is still null?
$customer = $request->getCustomer();
}
Log::debug('The customer is not null.');
return $customer;
@@ -72,7 +73,7 @@ trait GetSpectreCustomerTrait
$customer = null;
// check users preferences.
$preference = app('preferences')->getForUser($importJob->user, 'spectre_customer');
$preference = app('preferences')->getForUser($importJob->user, 'spectre_customer', null);
if (null !== $preference) {
Log::debug('Customer is in user configuration');
$customer = new Customer($preference->data);
@@ -96,9 +97,10 @@ trait GetSpectreCustomerTrait
}
Log::debug(sprintf('Skip customer with name "%s"', $current->getIdentifier()));
}
// store in preferences.
app('preferences')->setForUser($importJob->user, 'spectre_customer', $customer->toArray());
if (null !== $customer) {
// store in preferences.
app('preferences')->setForUser($importJob->user, 'spectre_customer', $customer->toArray());
}
return $customer;
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* ConfigurationInterface.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
@@ -20,7 +20,7 @@
*/
declare(strict_types=1);
namespace FireflyIII\Support\Import\Configuration\File;
namespace FireflyIII\Support\Import\JobConfiguration\File;
use FireflyIII\Models\ImportJob;
use Illuminate\Support\MessageBag;

View File

@@ -21,7 +21,7 @@
declare(strict_types=1);
namespace FireflyIII\Support\Import\Configuration\File;
namespace FireflyIII\Support\Import\JobConfiguration\File;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;

View File

@@ -21,7 +21,7 @@
declare(strict_types=1);
namespace FireflyIII\Support\Import\Configuration\File;
namespace FireflyIII\Support\Import\JobConfiguration\File;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;

View File

@@ -1,6 +1,6 @@
<?php
/**
* ConfigureUploadHandlerphp
* ConfigureUploadHandler.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
@@ -21,7 +21,7 @@
declare(strict_types=1);
namespace FireflyIII\Support\Import\Configuration\File;
namespace FireflyIII\Support\Import\JobConfiguration\File;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
@@ -32,8 +32,6 @@ use Log;
/**
* Class ConfigureUploadHandler
*
* @package FireflyIII\Support\Import\Configuration\File
*/
class ConfigureUploadHandler implements ConfigurationInterface
{

View File

@@ -22,7 +22,7 @@
declare(strict_types=1);
namespace FireflyIII\Support\Import\Configuration\File;
namespace FireflyIII\Support\Import\JobConfiguration\File;
use Exception;
use FireflyIII\Exceptions\FireflyException;
@@ -36,8 +36,6 @@ use Log;
/**
* Class NewFileJobHandler
*
* @package FireflyIII\Support\Import\Configuration\File
*/
class NewFileJobHandler implements ConfigurationInterface
{

View File

@@ -37,7 +37,6 @@ use Log;
/**
* Class AuthenticateConfig
*
* @package FireflyIII\Support\Import\JobConfiguration\Spectre
*/
class AuthenticateConfig implements SpectreJobConfig
{

View File

@@ -40,7 +40,6 @@ use Log;
/**
* Class ChooseAccount
*
* @package FireflyIII\Support\Import\JobConfiguration\Spectre
*/
class ChooseAccount implements SpectreJobConfig
{

View File

@@ -39,7 +39,6 @@ use Log;
/**
* Class ChooseLoginHandler
*
* @package FireflyIII\Support\Import\JobConfiguration\Spectre
*/
class ChooseLoginHandler implements SpectreJobConfig
{

View File

@@ -30,7 +30,6 @@ use Illuminate\Support\MessageBag;
/**
* Class NewConfig
*
* @package FireflyIII\Support\Import\JobConfiguration\Spectre
*/
class NewConfig implements SpectreJobConfig
{

View File

@@ -30,7 +30,6 @@ use Illuminate\Support\MessageBag;
/**
* Interface SpectreJobConfig
*
* @package FireflyIII\Support\Import\JobConfiguration\Spectre
*/
interface SpectreJobConfig
{

View File

@@ -31,7 +31,6 @@ use FireflyIII\Models\ImportJob;
* @codeCoverageIgnore
* Class StageFinalHandler
*
* @package FireflyIII\Support\Import\Routine\Fake
*/
class StageFinalHandler
{

View File

@@ -31,7 +31,6 @@ use Log;
/**
* Class CSVProcessor
*
* @package FireflyIII\Support\Import\Routine\File
*/
class CSVProcessor implements FileProcessorInterface
{

View File

@@ -28,7 +28,6 @@ use FireflyIII\Models\ImportJob;
/**
* Interface FileProcessorInterface
*
* @package FireflyIII\Support\Import\Routine\File
*/
interface FileProcessorInterface
{

View File

@@ -39,7 +39,6 @@ use Log;
/**
* Class StageImportDataHandler
*
* @package FireflyIII\Support\Import\Routine\Spectre
*/
class StageImportDataHandler
{

View File

@@ -34,7 +34,6 @@ use Log;
/**
* Class StageNewHandler
*
* @package FireflyIII\Support\Import\Routine\Spectre
*/
class StageNewHandler
{