Improve test coverage, remove dead code.

This commit is contained in:
James Cole
2018-05-20 16:26:27 +02:00
parent c06fd12b07
commit 620c5f515e
18 changed files with 919 additions and 835 deletions

View File

@@ -0,0 +1,105 @@
<?php
/**
* GetSpectreCustomerTrait.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\Information;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Services\Spectre\Object\Customer;
use FireflyIII\Services\Spectre\Request\ListCustomersRequest;
use FireflyIII\Services\Spectre\Request\NewCustomerRequest;
use Log;
/**
* Trait GetSpectreCustomerTrait
*
* @package FireflyIII\Support\Import\Information
*/
trait GetSpectreCustomerTrait
{
/**
* @param ImportJob $importJob
*
* @return Customer
* @throws FireflyException
*/
protected function getCustomer(ImportJob $importJob): Customer
{
Log::debug('Now in GetSpectreCustomerTrait::getCustomer()');
$customer = $this->getExistingCustomer($importJob);
if (null === $customer) {
Log::debug('The customer is NULL, will fire a newCustomerRequest.');
/** @var NewCustomerRequest $request */
$request = app(NewCustomerRequest::class);
$request->setUser($importJob->user);
$customer = $request->getCustomer();
}
Log::debug('The customer is not null.');
return $customer;
}
/**
* @param ImportJob $importJob
*
* @return Customer|null
* @throws FireflyException
*/
protected function getExistingCustomer(ImportJob $importJob): ?Customer
{
Log::debug('Now in GetSpectreCustomerTrait::getExistingCustomer()');
$customer = null;
// check users preferences.
$preference = app('preferences')->getForUser($importJob->user, 'spectre_customer');
if (null !== $preference) {
Log::debug('Customer is in user configuration');
$customer = new Customer($preference->data);
return $customer;
}
/** @var ListCustomersRequest $request */
$request = app(ListCustomersRequest::class);
$request->setUser($importJob->user);
$request->call();
$customers = $request->getCustomers();
Log::debug(sprintf('Found %d customer(s)', \count($customers)));
/** @var Customer $current */
foreach ($customers as $current) {
if ('default_ff3_customer' === $current->getIdentifier()) {
$customer = $current;
Log::debug('Found the correct customer.');
break;
}
Log::debug(sprintf('Skip customer with name "%s"', $current->getIdentifier()));
}
// store in preferences.
app('preferences')->setForUser($importJob->user, 'spectre_customer', $customer->toArray());
return $customer;
}
}

View File

@@ -1,6 +1,6 @@
<?php
/**
* ManageLoginsHandler.php
* GetSpectreTokenTrait.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
@@ -21,23 +21,10 @@
declare(strict_types=1);
namespace FireflyIII\Support\Import\Routine\Spectre;
namespace FireflyIII\Support\Import\Information;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Services\Spectre\Object\Customer;
use FireflyIII\Services\Spectre\Object\Login;
use FireflyIII\Services\Spectre\Request\ListCustomersRequest;
use FireflyIII\Services\Spectre\Request\ListLoginsRequest;
use FireflyIII\Services\Spectre\Request\NewCustomerRequest;
use Log;
/**
* Class ManageLoginsHandler
*/
class ManageLoginsHandler
trait GetSpectreTokenTrait
{
}

View File

@@ -1,6 +1,6 @@
<?php
/**
* ImportDataHandler.php
* StageImportDataHandler.phpr.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
@@ -37,11 +37,11 @@ use FireflyIII\Support\Import\Routine\File\OpposingAccountMapper;
use Log;
/**
* Class ImportDataHandler
* Class StageImportDataHandler
*
* @package FireflyIII\Support\Import\Routine\Spectre
*/
class ImportDataHandler
class StageImportDataHandler
{
/** @var AccountRepositoryInterface */
private $accountRepository;
@@ -57,7 +57,7 @@ class ImportDataHandler
*/
public function run(): void
{
Log::debug('Now in ImportDataHandler::run()');
Log::debug('Now in StageImportDataHandler::run()');
$config = $this->importJob->configuration;
$accounts = $config['accounts'] ?? [];
Log::debug(sprintf('Count of accounts in array is %d', \count($accounts)));
@@ -103,7 +103,7 @@ class ImportDataHandler
{
$array = [];
$total = \count($transactions);
Log::debug(sprintf('Now in ImportDataHandler::convertToArray() with count %d', \count($transactions)));
Log::debug(sprintf('Now in StageImportDataHandler::convertToArray() with count %d', \count($transactions)));
/** @var SpectreTransaction $transaction */
foreach ($transactions as $index => $transaction) {
Log::debug(sprintf('Now creating array for transaction %d of %d', $index + 1, $total));

View File

@@ -26,11 +26,9 @@ namespace FireflyIII\Support\Import\Routine\Spectre;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Services\Spectre\Object\Customer;
use FireflyIII\Services\Spectre\Object\Login;
use FireflyIII\Services\Spectre\Request\ListCustomersRequest;
use FireflyIII\Services\Spectre\Request\ListLoginsRequest;
use FireflyIII\Services\Spectre\Request\NewCustomerRequest;
use FireflyIII\Support\Import\Information\GetSpectreCustomerTrait;
use Log;
/**
@@ -40,13 +38,24 @@ use Log;
*/
class StageNewHandler
{
use GetSpectreCustomerTrait;
/** @var int */
public $countLogins = 0;
private $countLogins = 0;
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* @codeCoverageIgnore
* @return int
*/
public function getCountLogins(): int
{
return $this->countLogins;
}
/**
* Tasks for this stage:
*
@@ -59,11 +68,13 @@ class StageNewHandler
public function run(): void
{
Log::debug('Now in ManageLoginsHandler::run()');
$customer = $this->getCustomer();
$config = $this->repository->getConfiguration($this->importJob);
$customer = $this->getCustomer($this->importJob);
$config = $this->repository->getConfiguration($this->importJob);
Log::debug('Going to get a list of logins.');
$request = new ListLoginsRequest($this->importJob->user);
/** @var ListLoginsRequest $request */
$request = app(ListLoginsRequest::class);
$request->setUser($this->importJob->user);
$request->setCustomer($customer);
$request->call();
@@ -94,49 +105,4 @@ class StageNewHandler
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
/**
* @return Customer
* @throws FireflyException
*/
private function getCustomer(): Customer
{
Log::debug('Now in manageLoginsHandler::getCustomer()');
$customer = $this->getExistingCustomer();
if (null === $customer) {
Log::debug('The customer is NULL, will fire a newCustomerRequest.');
$newCustomerRequest = new NewCustomerRequest($this->importJob->user);
$customer = $newCustomerRequest->getCustomer();
}
Log::debug('The customer is not null.');
return $customer;
}
/**
* @return Customer|null
* @throws FireflyException
*/
private function getExistingCustomer(): ?Customer
{
Log::debug('Now in manageLoginsHandler::getExistingCustomer()');
$customer = null;
$getCustomerRequest = new ListCustomersRequest($this->importJob->user);
$getCustomerRequest->call();
$customers = $getCustomerRequest->getCustomers();
Log::debug(sprintf('Found %d customer(s)', \count($customers)));
/** @var Customer $current */
foreach ($customers as $current) {
if ('default_ff3_customer' === $current->getIdentifier()) {
$customer = $current;
Log::debug('Found the correct customer.');
break;
}
Log::debug(sprintf('Skip customer with name "%s"', $current->getIdentifier()));
}
return $customer;
}
}