Finish command tests

This commit is contained in:
James Cole
2019-06-13 15:48:35 +02:00
parent 6bcb2ec144
commit 6964424bdc
29 changed files with 783 additions and 531 deletions

View File

@@ -76,21 +76,6 @@ class TransferCurrenciesCorrections extends Command
/** @var TransactionCurrency The currency preference of the destination account of the current journal. */
private $destinationCurrency;
/**
* JournalCurrencies constructor.
*/
public function __construct()
{
parent::__construct();
$this->count = 0;
$this->accountRepos = app(AccountRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
$this->journalRepos = app(JournalRepositoryInterface::class);
$this->accountCurrencies = [];
$this->resetInformation();
}
/**
* Execute the console command.
*
@@ -98,6 +83,7 @@ class TransferCurrenciesCorrections extends Command
*/
public function handle(): int
{
$this->stupidLaravel();
$start = microtime(true);
// @codeCoverageIgnoreStart
if ($this->isExecuted() && true !== $this->option('force')) {
@@ -111,10 +97,14 @@ class TransferCurrenciesCorrections extends Command
$this->markAsExecuted();
if (0 === $this->count) {
$this->line('All transfers have correct currency information.');
$message = 'All transfers have correct currency information.';
$this->line($message);
Log::debug($message);
}
if (0 !== $this->count) {
$this->line(sprintf('Verified currency information of %d transfer(s).', $this->count));
$message = sprintf('Verified currency information of %d transfer(s).', $this->count);
$this->line($message);
Log::debug($message);
}
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Verified and fixed currency information for transfers in %s seconds.', $end));
@@ -122,6 +112,23 @@ class TransferCurrenciesCorrections extends Command
return 0;
}
/**
* Laravel will execute ALL __construct() methods for ALL commands whenever a SINGLE command is
* executed. This leads to noticeable slow-downs and class calls. To prevent this, this method should
* be called from the handle method instead of using the constructor to initialize the command.
*
* @codeCoverageIgnore
*/
private function stupidLaravel(): void
{
$this->count = 0;
$this->accountRepos = app(AccountRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
$this->journalRepos = app(JournalRepositoryInterface::class);
$this->accountCurrencies = [];
$this->resetInformation();
}
/**
* @param Account $account
*
@@ -131,17 +138,19 @@ class TransferCurrenciesCorrections extends Command
{
$accountId = $account->id;
if (isset($this->accountCurrencies[$accountId]) && 0 === $this->accountCurrencies[$accountId]) {
return null;
return null; // @codeCoverageIgnore
}
if (isset($this->accountCurrencies[$accountId]) && $this->accountCurrencies[$accountId] instanceof TransactionCurrency) {
return $this->accountCurrencies[$accountId];
return $this->accountCurrencies[$accountId]; // @codeCoverageIgnore
}
$currencyId = (int)$this->accountRepos->getMetaValue($account, 'currency_id');
$result = $this->currencyRepos->findNull($currencyId);
if (null === $result) {
// @codeCoverageIgnoreStart
$this->accountCurrencies[$accountId] = 0;
return null;
// @codeCoverageIgnoreEnd
}
$this->accountCurrencies[$accountId] = $result;
@@ -154,20 +163,22 @@ class TransferCurrenciesCorrections extends Command
* @param TransactionJournal $transfer
*
* @return Transaction|null
* @codeCoverageIgnore
*/
private function getDestinationTransaction(TransactionJournal $transfer): ?Transaction
{
return $transfer->transactions->firstWhere('amount', '>', 0);
return $transfer->transactions()->where('amount', '>', 0)->first();
}
/**
* @param TransactionJournal $transfer
*
* @return Transaction|null
* @codeCoverageIgnore
*/
private function getSourceTransaction(TransactionJournal $transfer): ?Transaction
{
return $transfer->transactions->firstWhere('amount', '<', 0);
return $transfer->transactions()->where('amount', '<', 0)->first();
}
/**
@@ -198,19 +209,19 @@ class TransferCurrenciesCorrections extends Command
*/
private function fixTransactionJournalCurrency(TransactionJournal $journal): void
{
if ($journal->transaction_currency_id !== $this->sourceCurrency->id) {
if ((int)$journal->transaction_currency_id !== (int)$this->sourceCurrency->id) {
$oldCurrencyCode = $journal->transactionCurrency->code ?? '(nothing)';
$journal->transaction_currency_id = $this->sourceCurrency->id;
$this->count++;
$this->line(
sprintf(
'Transfer #%d ("%s") has been updated to use %s instead of %s.',
$journal->id,
$journal->description,
$this->sourceCurrency->code,
$oldCurrencyCode
)
$message = sprintf(
'Transfer #%d ("%s") has been updated to use %s instead of %s.',
$journal->id,
$journal->description,
$this->sourceCurrency->code,
$oldCurrencyCode
);
$this->count++;
$this->line($message);
Log::debug($message);
$journal->save();
}
}
@@ -235,7 +246,8 @@ class TransferCurrenciesCorrections extends Command
}
/**
* Reset all the class fields for the current transfer
* Reset all the class fields for the current transfer.
* @codeCoverageIgnore
*/
private function resetInformation(): void
{
@@ -249,7 +261,9 @@ class TransferCurrenciesCorrections extends Command
/**
* Extract source transaction, source account + source account currency from the journal.
*
* @param TransactionJournal $journal
* @codeCoverageIgnore
*/
private function getSourceInformation(TransactionJournal $journal): void
{
@@ -260,7 +274,9 @@ class TransferCurrenciesCorrections extends Command
/**
* Extract destination transaction, destination account + destination account currency from the journal.
*
* @param TransactionJournal $journal
* @codeCoverageIgnore
*/
private function getDestinationInformation(TransactionJournal $journal): void
{
@@ -276,26 +292,37 @@ class TransferCurrenciesCorrections extends Command
*/
private function updateTransferCurrency(TransactionJournal $transfer): void
{
$this->resetInformation();
// @codeCoverageIgnoreStart
if ($this->isSplitJournal($transfer)) {
$this->line(sprintf(sprintf('Transaction journal #%d is a split journal. Cannot continue.', $transfer->id)));
return;
}
// @codeCoverageIgnoreEnd
$this->getSourceInformation($transfer);
$this->getDestinationInformation($transfer);
// unexpectedly, either one is null:
// @codeCoverageIgnoreStart
if ($this->isEmptyTransactions()) {
$this->error(sprintf('Source or destination information for transaction journal #%d is null. Cannot fix this one.', $transfer->id));
return;
}
// @codeCoverageIgnoreEnd
// both accounts must have currency preference:
// @codeCoverageIgnoreStart
if ($this->isNoCurrencyPresent()) {
return;
}
// @codeCoverageIgnoreEnd
// fix source transaction having no currency.
$this->fixSourceNoCurrency();
@@ -311,16 +338,17 @@ class TransferCurrenciesCorrections extends Command
// remove foreign currency information if not necessary.
$this->fixInvalidForeignCurrency();
// correct foreign currency info if necessary.
$this->fixMismatchedForeignCurrency();
// restore missing foreign currency amount.
$this->fixSourceNullForeignAmount();
$this->fixDestNullForeignAmount();
// fix journal itself:
$this->fixTransactionJournalCurrency($transfer);
}
/**
@@ -416,6 +444,7 @@ class TransferCurrenciesCorrections extends Command
*
* @param TransactionJournal $transfer
* @return bool
* @codeCoverageIgnore
*/
private function isSplitJournal(TransactionJournal $transfer): bool
{
@@ -425,6 +454,7 @@ class TransferCurrenciesCorrections extends Command
/**
* Is either the source or destination transaction NULL?
* @return bool
* @codeCoverageIgnore
*/
private function isEmptyTransactions(): bool
{
@@ -436,6 +466,7 @@ class TransferCurrenciesCorrections extends Command
* If the destination account currency is the same as the source currency,
* both foreign_amount and foreign_currency_id fields must be NULL
* for both transactions (because foreign currency info would not make sense)
*
*/
private function fixInvalidForeignCurrency(): void
{
@@ -459,21 +490,21 @@ class TransferCurrenciesCorrections extends Command
$this->sourceTransaction->id, $this->destinationTransaction->id, $this->sourceCurrency->code
)
);
$this->count++;
return;
}
}
/**
* If destination account currency is different from source account currency, then
* both transactions must have each others currency as foreign currency id.
* If destination account currency is different from source account currency,
* then both transactions must get the source account's currency as normal currency
* and the opposing account's currency as foreign currency.
*/
private function fixMismatchedForeignCurrency(): void
{
if ((int)$this->sourceCurrency->id !== (int)$this->destinationCurrency->id) {
$this->sourceTransaction->foreign_currency_id = $this->destinationCurrency->id;
$this->destinationTransaction->foreign_currency_id = $this->sourceCurrency->id;
$this->sourceTransaction->transaction_currency_id = $this->sourceCurrency->id;
$this->sourceTransaction->foreign_currency_id = $this->destinationCurrency->id;
$this->destinationTransaction->transaction_currency_id = $this->sourceCurrency->id;
$this->destinationTransaction->foreign_currency_id = $this->destinationCurrency->id;
$this->sourceTransaction->save();
$this->destinationTransaction->save();
@@ -514,6 +545,7 @@ class TransferCurrenciesCorrections extends Command
/**
* @return bool
* @codeCoverageIgnore
*/
private function isNoCurrencyPresent(): bool
{
@@ -523,7 +555,7 @@ class TransferCurrenciesCorrections extends Command
Log::error($message);
$this->error($message);
return false;
return true;
}
// destination account must have a currency preference.
@@ -533,10 +565,10 @@ class TransferCurrenciesCorrections extends Command
Log::error($message);
$this->error($message);
return false;
return true;
}
return true;
return false;
}
}