Improve test coverage.

This commit is contained in:
James Cole
2018-05-21 17:28:09 +02:00
parent ebf97f710f
commit 94e6816bf6
10 changed files with 1117 additions and 181 deletions

View File

@@ -23,8 +23,37 @@ declare(strict_types=1);
namespace FireflyIII\Support\Import\Information;
use FireflyIII\Models\ImportJob;
use FireflyIII\Services\Spectre\Object\Customer;
use FireflyIII\Services\Spectre\Object\Token;
use FireflyIII\Services\Spectre\Request\CreateTokenRequest;
use Log;
/**
* Trait GetSpectreTokenTrait
*
* @package FireflyIII\Support\Import\Information
*/
trait GetSpectreTokenTrait
{
/**
* @param ImportJob $importJob
* @param Customer $customer
*
* @return Token
* @throws \FireflyIII\Exceptions\FireflyException
*/
protected function getToken(ImportJob $importJob, Customer $customer): Token
{
Log::debug('Now in GetSpectreTokenTrait::ChooseLoginsHandler::getToken()');
/** @var CreateTokenRequest $request */
$request = app(CreateTokenRequest::class);
$request->setUser($importJob->user);
$request->setUri(route('import.job.status.index', [$importJob->key]));
$request->setCustomer($customer);
$request->call();
Log::debug('Call to get token is finished');
return $request->getToken();
}
}

View File

@@ -24,20 +24,16 @@ declare(strict_types=1);
namespace FireflyIII\Support\Import\JobConfiguration\Spectre;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Support\MessageBag;
use Log;
/**
* @codeCoverageIgnore
*
* Class AuthenticatedHandler
*/
class AuthenticatedHandler implements SpectreConfigurationInterface
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* Return true when this stage is complete.
*
@@ -59,7 +55,7 @@ class AuthenticatedHandler implements SpectreConfigurationInterface
*/
public function configureJob(array $data): MessageBag
{
Log::debug('AuthenticatedConfigHandler::configurationComplete() always returns empty message bag');
Log::debug('AuthenticatedConfigHandler::configureJob() always returns empty message bag');
return new MessageBag();
}
@@ -95,8 +91,5 @@ class AuthenticatedHandler implements SpectreConfigurationInterface
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}

View File

@@ -60,11 +60,12 @@ class ChooseAccountsHandler implements SpectreConfigurationInterface
*/
public function configurationComplete(): bool
{
Log::debug('Now in ChooseAccount::configurationComplete()');
Log::debug('Now in ChooseAccountsHandler::configurationComplete()');
$config = $this->importJob->configuration;
$importAccounts = $config['account_mapping'] ?? [];
$complete = \count($importAccounts) > 0 && $importAccounts !== [0 => 0];
if ($complete) {
// todo also actually validate content.
Log::debug('Looks like user has mapped import accounts to Firefly III accounts', $importAccounts);
$this->repository->setStage($this->importJob, 'go-for-import');
}
@@ -81,14 +82,14 @@ class ChooseAccountsHandler implements SpectreConfigurationInterface
*/
public function configureJob(array $data): MessageBag
{
Log::debug('Now in ChooseAccount::configureJob()', $data);
Log::debug('Now in ChooseAccountsHandler::configureJob()', $data);
$config = $this->importJob->configuration;
$mapping = $data['account_mapping'] ?? [];
$final = [];
foreach ($mapping as $spectreId => $fireflyIIIId) {
foreach ($mapping as $spectreId => $localId) {
// validate each
$spectreId = $this->validSpectreAccount((int)$spectreId);
$accountId = $this->validFireflyIIIAccount((int)$fireflyIIIId);
$accountId = $this->validLocalAccount((int)$localId);
$final[$spectreId] = $accountId;
}
@@ -112,11 +113,11 @@ class ChooseAccountsHandler implements SpectreConfigurationInterface
*/
public function getNextData(): array
{
Log::debug('Now in ChooseAccount::getnextData()');
Log::debug('Now in ChooseAccountsHandler::getnextData()');
$config = $this->importJob->configuration;
$accounts = $config['accounts'] ?? [];
if (\count($accounts) === 0) {
throw new FireflyException('It seems you have no accounts with this bank. The import cannot continue.');
throw new FireflyException('It seems you have no accounts with this bank. The import cannot continue.'); // @codeCoverageIgnore
}
$converted = [];
foreach ($accounts as $accountArray) {
@@ -128,7 +129,7 @@ class ChooseAccountsHandler implements SpectreConfigurationInterface
$logins = $config['all-logins'] ?? [];
$selected = $config['selected-login'] ?? 0;
if (\count($logins) === 0) {
throw new FireflyException('It seems you have no configured logins in this import job. The import cannot continue.');
throw new FireflyException('It seems you have no configured logins in this import job. The import cannot continue.'); // @codeCoverageIgnore
}
Log::debug(sprintf('Selected login to use is %d', $selected));
if ($selected === 0) {
@@ -145,7 +146,7 @@ class ChooseAccountsHandler implements SpectreConfigurationInterface
}
}
if (null === $login) {
throw new FireflyException('Was not able to determine which login to use. The import cannot continue.');
throw new FireflyException('Was not able to determine which login to use. The import cannot continue.'); // @codeCoverageIgnore
}
// list the users accounts:
@@ -173,6 +174,7 @@ class ChooseAccountsHandler implements SpectreConfigurationInterface
}
/**
* @codeCoverageIgnore
* Get the view for this stage.
*
* @return string
@@ -183,6 +185,7 @@ class ChooseAccountsHandler implements SpectreConfigurationInterface
}
/**
* @codeCoverageIgnore
* Set the import job.
*
* @param ImportJob $importJob
@@ -219,7 +222,7 @@ class ChooseAccountsHandler implements SpectreConfigurationInterface
*
* @return int
*/
private function validFireflyIIIAccount(int $accountId): int
private function validLocalAccount(int $accountId): int
{
$account = $this->accountRepository->findNull($accountId);
if (null === $account) {

View File

@@ -32,6 +32,8 @@ use FireflyIII\Services\Spectre\Object\Token;
use FireflyIII\Services\Spectre\Request\CreateTokenRequest;
use FireflyIII\Services\Spectre\Request\ListCustomersRequest;
use FireflyIII\Services\Spectre\Request\NewCustomerRequest;
use FireflyIII\Support\Import\Information\GetSpectreCustomerTrait;
use FireflyIII\Support\Import\Information\GetSpectreTokenTrait;
use Illuminate\Support\MessageBag;
use Log;
@@ -42,6 +44,7 @@ use Log;
*/
class ChooseLoginHandler implements SpectreConfigurationInterface
{
use GetSpectreCustomerTrait, GetSpectreTokenTrait;
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
@@ -77,7 +80,7 @@ class ChooseLoginHandler implements SpectreConfigurationInterface
public function configureJob(array $data): MessageBag
{
Log::debug('Now in ChooseLoginHandler::configureJob()');
$selectedLogin = (int)$data['spectre_login_id'];
$selectedLogin = (int)($data['spectre_login_id'] ?? 0.0);
$config = $this->importJob->configuration;
$config['selected-login'] = $selectedLogin;
$this->repository->setConfiguration($this->importJob, $config);
@@ -85,10 +88,10 @@ class ChooseLoginHandler implements SpectreConfigurationInterface
// if selected login is zero, create a new one.
if ($selectedLogin === 0) {
Log::debug('Login is zero, get a new customer + token and store it in config.');
$customer = $this->getCustomer();
Log::debug('Login is zero, get Spectre customer + token and store it in config.');
$customer = $this->getCustomer($this->importJob);
// get a token for the user and redirect to next stage
$token = $this->getToken($customer);
$token = $this->getToken($this->importJob, $customer);
$config['customer'] = $customer->toArray();
$config['token'] = $token->toArray();
$this->repository->setConfiguration($this->importJob, $config);
@@ -123,6 +126,7 @@ class ChooseLoginHandler implements SpectreConfigurationInterface
}
/**
* @codeCoverageIgnore
* Get the view for this stage.
*
* @return string
@@ -143,75 +147,4 @@ class ChooseLoginHandler implements SpectreConfigurationInterface
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
/**
* @return Customer
* @throws FireflyException
*/
private function getCustomer(): Customer
{
Log::debug('Now in stageNewHandler::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 ChooseLoginHandler::getExistingCustomer()');
$preference = app('preferences')->getForUser($this->importJob->user, 'spectre_customer');
if (null !== $preference) {
Log::debug('Customer is in user configuration');
$customer = new Customer($preference->data);
return $customer;
}
Log::debug('Customer is not in user config');
$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.');
app('preferences')->setForUser($this->importJob->user, 'spectre_customer', $customer->toArray());
break;
}
}
return $customer;
}
/**
* @param Customer $customer
*
* @throws FireflyException
* @return Token
*/
private function getToken(Customer $customer): Token
{
Log::debug('Now in ChooseLoginHandler::ChooseLoginsHandler::getToken()');
$request = new CreateTokenRequest($this->importJob->user);
$request->setUri(route('import.job.status.index', [$this->importJob->key]));
$request->setCustomer($customer);
$request->call();
Log::debug('Call to get token is finished');
return $request->getToken();
}
}

View File

@@ -26,11 +26,9 @@ namespace FireflyIII\Support\Import\JobConfiguration\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\Token;
use FireflyIII\Services\Spectre\Request\CreateTokenRequest;
use FireflyIII\Services\Spectre\Request\ListCustomersRequest;
use FireflyIII\Services\Spectre\Request\NewCustomerRequest;
use FireflyIII\Support\Import\Information\GetSpectreCustomerTrait;
use FireflyIII\Support\Import\Information\GetSpectreTokenTrait;
use Illuminate\Support\MessageBag;
use Log;
@@ -40,12 +38,14 @@ use Log;
*/
class DoAuthenticateHandler implements SpectreConfigurationInterface
{
use GetSpectreTokenTrait, GetSpectreCustomerTrait;
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* @codeCoverageIgnore
* Return true when this stage is complete.
*
* always returns false.
@@ -54,12 +54,13 @@ class DoAuthenticateHandler implements SpectreConfigurationInterface
*/
public function configurationComplete(): bool
{
Log::debug('AuthenticateConfig::configurationComplete() will always return false');
Log::debug('DoAuthenticateHandler::configurationComplete() will always return false');
return false;
}
/**
* @codeCoverageIgnore
* Store the job configuration.
*
* @param array $data
@@ -68,7 +69,7 @@ class DoAuthenticateHandler implements SpectreConfigurationInterface
*/
public function configureJob(array $data): MessageBag
{
Log::debug('AuthenticateConfig::configureJob() will do nothing.');
Log::debug('DoAuthenticateHandler::configureJob() will do nothing.');
return new MessageBag;
}
@@ -81,25 +82,29 @@ class DoAuthenticateHandler implements SpectreConfigurationInterface
*/
public function getNextData(): array
{
Log::debug('Now in AuthenticateConfig::getNextData()');
// next data only makes sure the job is ready for the next stage.
Log::debug('Now in DoAuthenticateHandler::getNextData()');
// getNextData() only makes sure the job is ready for the next stage.
$this->repository->setStatus($this->importJob, 'ready_to_run');
$this->repository->setStage($this->importJob, 'authenticated');
// get token from configuration:
$config = $this->importJob->configuration;
$token = isset($config['token']) ? new Token($config['token']) : null;
if (null !== $token) {
Log::debug(sprintf('Return "%s" from token in config.', $token->getConnectUrl()));
return ['token-url' => $token->getConnectUrl()];
if (null === $token) {
// get a new one from Spectre:
Log::debug('No existing token, get a new one.');
// get a new token from Spectre.
$customer = $this->getCustomer($this->importJob);
$token = $this->getToken($this->importJob, $customer);
}
Log::debug('No existing token, get a new one.');
// get a new token from Spectre.
$customer = $this->getCustomer();
$token = $this->getToken($customer);
return ['token-url' => $token->getConnectUrl()];
}
/**
* @codeCoverageIgnore
* Get the view for this stage.
*
* @return string
@@ -110,6 +115,7 @@ class DoAuthenticateHandler implements SpectreConfigurationInterface
}
/**
* @codeCoverageIgnore
* Set the import job.
*
* @param ImportJob $importJob
@@ -120,75 +126,4 @@ class DoAuthenticateHandler implements SpectreConfigurationInterface
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
/**
* @return Customer
* @throws FireflyException
*/
private function getCustomer(): Customer
{
Log::debug('Now in AuthenticateConfig::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 AuthenticateConfig::getExistingCustomer()');
$preference = app('preferences')->getForUser($this->importJob->user, 'spectre_customer');
if (null !== $preference) {
Log::debug('Customer is in user configuration');
$customer = new Customer($preference->data);
return $customer;
}
Log::debug('Customer is not in user config');
$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.');
app('preferences')->setForUser($this->importJob->user, 'spectre_customer', $customer->toArray());
break;
}
}
return $customer;
}
/**
* @param Customer $customer
*
* @throws FireflyException
* @return Token
*/
private function getToken(Customer $customer): Token
{
Log::debug('Now in AuthenticateConfig::getToken()');
$request = new CreateTokenRequest($this->importJob->user);
$request->setUri(route('import.job.status.index', [$this->importJob->key]));
$request->setCustomer($customer);
$request->call();
Log::debug('Call to get token is finished');
return $request->getToken();
}
}

View File

@@ -26,9 +26,12 @@ namespace FireflyIII\Support\Import\JobConfiguration\Spectre;
use FireflyIII\Models\ImportJob;
use Illuminate\Support\MessageBag;
use Log;
/**
* Class NewConfig
* @codeCoverageIgnore
*
* Class NewSpectreJobHandler
*
*/
class NewSpectreJobHandler implements SpectreConfigurationInterface
@@ -41,6 +44,8 @@ class NewSpectreJobHandler implements SpectreConfigurationInterface
*/
public function configurationComplete(): bool
{
Log::debug('NewSpectreJobHandler::configurationComplete() always returns true');
return true;
}
@@ -53,6 +58,8 @@ class NewSpectreJobHandler implements SpectreConfigurationInterface
*/
public function configureJob(array $data): MessageBag
{
Log::debug('NewSpectreJobHandler::configureJob() always returns an empty message bag');
return new MessageBag;
}
@@ -63,6 +70,8 @@ class NewSpectreJobHandler implements SpectreConfigurationInterface
*/
public function getNextData(): array
{
Log::debug('NewSpectreJobHandler::getNextData() always returns []');
return [];
}
@@ -73,6 +82,8 @@ class NewSpectreJobHandler implements SpectreConfigurationInterface
*/
public function getNextView(): string
{
Log::debug('NewSpectreJobHandler::getNextView() always returns ""');
return '';
}

View File

@@ -55,6 +55,7 @@ use FireflyIII\Models\Attachment;
/**
* Class User.
* @property int $id
*/
class User extends Authenticatable
{