Improve test coverage.

This commit is contained in:
James Cole
2019-07-27 13:54:06 +02:00
parent d94d34ca63
commit 67c0ef6ec6
29 changed files with 788 additions and 346 deletions

View File

@@ -64,6 +64,7 @@ class AccountCurrencies extends Command
*/
public function handle(): int
{
Log::debug('Now in handle()');
$this->stupidLaravel();
$start = microtime(true);
if ($this->isExecuted() && true !== $this->option('force')) {
@@ -71,7 +72,6 @@ class AccountCurrencies extends Command
return 0;
}
Log::debug('Now in updateAccountCurrencies()');
$this->updateAccountCurrencies();
if (0 === $this->count) {
@@ -130,17 +130,23 @@ class AccountCurrencies extends Command
*/
private function updateAccount(Account $account, TransactionCurrency $currency): void
{
Log::debug(sprintf('Now in updateAccount(%d, %s)', $account->id, $currency->code));
$this->accountRepos->setUser($account->user);
$accountCurrency = (int)$this->accountRepos->getMetaValue($account, 'currency_id');
Log::debug(sprintf('Account currency is #%d', $accountCurrency));
$openingBalance = $this->accountRepos->getOpeningBalance($account);
$obCurrency = 0;
if (null !== $openingBalance) {
$obCurrency = (int)$openingBalance->transaction_currency_id;
Log::debug('Account has opening balance.');
}
Log::debug(sprintf('Account OB currency is #%d.', $obCurrency));
// both 0? set to default currency:
if (0 === $accountCurrency && 0 === $obCurrency) {
Log::debug(sprintf('Both currencies are 0, so reset to #%d (%s)', $currency->id, $currency->code));
AccountMeta::where('account_id', $account->id)->where('name', 'currency_id')->forceDelete();
AccountMeta::create(['account_id' => $account->id, 'name' => 'currency_id', 'data' => $currency->id]);
$this->line(sprintf('Account #%d ("%s") now has a currency setting (%s).', $account->id, $account->name, $currency->code));
@@ -151,6 +157,7 @@ class AccountCurrencies extends Command
// account is set to 0, opening balance is not?
if (0 === $accountCurrency && $obCurrency > 0) {
Log::debug(sprintf('Account is #0, OB is #%d, so set account to OB as well', $obCurrency));
AccountMeta::create(['account_id' => $account->id, 'name' => 'currency_id', 'data' => $obCurrency]);
$this->line(sprintf('Account #%d ("%s") now has a currency setting (#%d).', $account->id, $account->name, $obCurrency));
$this->count++;
@@ -161,6 +168,7 @@ class AccountCurrencies extends Command
// do not match and opening balance id is not null.
if ($accountCurrency !== $obCurrency && null !== $openingBalance) {
Log::debug(sprintf('Account (#%d) and OB currency (#%d) are different. Overrule OB, set to account currency.', $accountCurrency, $obCurrency));
// update opening balance:
$openingBalance->transaction_currency_id = $accountCurrency;
$openingBalance->save();
@@ -174,6 +182,7 @@ class AccountCurrencies extends Command
return;
}
Log::debug('No changes necessary for this account.');
}
/**
@@ -181,8 +190,10 @@ class AccountCurrencies extends Command
*/
private function updateAccountCurrencies(): void
{
Log::debug('Now in updateAccountCurrencies()');
$users = $this->userRepos->all();
$defaultCurrencyCode = (string)config('firefly.default_currency', 'EUR');
Log::debug(sprintf('Default currency is %s', $defaultCurrencyCode));
foreach ($users as $user) {
$this->updateCurrenciesForUser($user, $defaultCurrencyCode);
}
@@ -194,6 +205,7 @@ class AccountCurrencies extends Command
*/
private function updateCurrenciesForUser(User $user, string $systemCurrencyCode): void
{
Log::debug(sprintf('Now in updateCurrenciesForUser(%s, %s)', $user->email, $systemCurrencyCode));
$this->accountRepos->setUser($user);
$accounts = $this->accountRepos->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
@@ -202,10 +214,13 @@ class AccountCurrencies extends Command
if (!is_string($defaultCurrencyCode)) {
$defaultCurrencyCode = $systemCurrencyCode;
}
Log::debug(sprintf('Users currency pref is %s', $defaultCurrencyCode));
/** @var TransactionCurrency $defaultCurrency */
$defaultCurrency = TransactionCurrency::where('code', $defaultCurrencyCode)->first();
if (null === $defaultCurrency) {
Log::error(sprintf('Users currency pref "%s" does not exist!', $defaultCurrencyCode));
$this->error(sprintf('User has a preference for "%s", but this currency does not exist.', $defaultCurrencyCode));
return;

View File

@@ -119,7 +119,7 @@ class OtherCurrenciesCorrections extends Command
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);

View File

@@ -53,7 +53,6 @@ class TransactionGroupFactory
* @param array $data
*
* @return TransactionGroup
* @throws FireflyException
*/
public function create(array $data): TransactionGroup
{

View File

@@ -119,7 +119,6 @@ class TransactionJournalFactory
* @param array $data
*
* @return Collection
* @throws FireflyException
*/
public function create(array $data): Collection
{
@@ -217,7 +216,6 @@ class TransactionJournalFactory
* @param NullArrayObject $row
*
* @return TransactionJournal|null
* @throws FireflyException
*/
private function createJournal(NullArrayObject $row): ?TransactionJournal
{
@@ -239,12 +237,19 @@ class TransactionJournalFactory
/** Get source + destination account */
Log::debug(sprintf('Source info: ID #%d, name "%s"', $row['source_id'], $row['source_name']));
Log::debug(sprintf('Destination info: ID #%d, name "%s"', $row['destination_id'], $row['destination_name']));
// validate source and destination using a new Validator.
$this->validateAccounts($row);
/** create or get source and destination accounts */
$sourceAccount = $this->getAccount($type->type, 'source', (int)$row['source_id'], $row['source_name']);
$destinationAccount = $this->getAccount($type->type, 'destination', (int)$row['destination_id'], $row['destination_name']);
try {
// validate source and destination using a new Validator.
$this->validateAccounts($row);
/** create or get source and destination accounts */
$sourceAccount = $this->getAccount($type->type, 'source', (int)$row['source_id'], $row['source_name']);
$destinationAccount = $this->getAccount($type->type, 'destination', (int)$row['destination_id'], $row['destination_name']);
} catch (FireflyException $e) {
Log::error($e->getMessage());
return null;
}
// TODO After 4.8.0 better handling below:

View File

@@ -106,7 +106,7 @@ class JobStatusController extends Controller
// if count is zero:
if (null !== $importJob->tag_id) {
$count = $importJob->tag->transactionJournals->count();
$count = $this->repository->countByTag($importJob);
}
if (0 === $count) {
$json['report_txt'] = (string)trans('import.result_no_transactions');

View File

@@ -48,7 +48,6 @@ namespace FireflyIII\Jobs;
use Carbon\Carbon;
use FireflyIII\Events\RequestedReportOnJournals;
use FireflyIII\Events\StoredTransactionGroup;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\PiggyBankEventFactory;
use FireflyIII\Factory\PiggyBankFactory;
use FireflyIII\Models\Recurrence;
@@ -59,10 +58,7 @@ use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
use FireflyIII\TransactionRules\Engine\RuleEngine;
use FireflyIII\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@@ -73,8 +69,8 @@ use Log;
/**
* Class CreateRecurringTransactions.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*
*/
class CreateRecurringTransactions implements ShouldQueue
{
@@ -88,13 +84,19 @@ class CreateRecurringTransactions implements ShouldQueue
private $groupRepository;
/** @var RecurringRepositoryInterface Recurring transactions repository. */
private $repository;
/** @var array The users rules. */
private $rules = [];
/** @var bool Force the transaction to be created no matter what. */
private $force;
/** @var int Number of recurrences submitted */
public $submitted;
/** @var int Number of recurrences actually fired */
public $executed;
/** @var int Transaction groups created */
public $created;
/**
* Create a new job instance.
* @codeCoverageIgnore
*
* @param Carbon $date
*/
@@ -106,6 +108,9 @@ class CreateRecurringTransactions implements ShouldQueue
$this->journalRepository = app(JournalRepositoryInterface::class);
$this->groupRepository = app(TransactionGroupRepositoryInterface::class);
$this->force = false;
$this->submitted = 0;
$this->executed = 0;
$this->created = 0;
Log::debug(sprintf('Created new CreateRecurringTransactions("%s")', $this->date->format('Y-m-d')));
@@ -121,22 +126,18 @@ class CreateRecurringTransactions implements ShouldQueue
/**
* Execute the job.
*
* @throws FireflyException
*/
public function handle(): void
{
Log::debug(sprintf('Now at start of CreateRecurringTransactions() job for %s.', $this->date->format('D d M Y')));
$recurrences = $this->repository->getAll();
$result = [];
Log::debug(sprintf('Count of collection is %d', $recurrences->count()));
$recurrences = $this->repository->getAll();
$result = [];
$count = $recurrences->count();
$this->submitted = $count;
Log::debug(sprintf('Count of collection is %d', $count));
/** @var Collection $filtered */
$filtered = $recurrences->filter(
function (Recurrence $recurrence) {
return $this->validRecurrence($recurrence);
}
);
// filter recurrences:
$filtered = $this->filterRecurrences($recurrences);
Log::debug(sprintf('Left after filtering is %d', $filtered->count()));
/** @var Recurrence $recurrence */
foreach ($filtered as $recurrence) {
@@ -150,6 +151,7 @@ class CreateRecurringTransactions implements ShouldQueue
$created = $this->handleRepetitions($recurrence);
Log::debug(sprintf('Done with recurrence #%d', $recurrence->id));
$result[$recurrence->user_id] = $result[$recurrence->user_id]->merge($created);
$this->executed++;
}
Log::debug('Now running report thing.');
@@ -176,36 +178,6 @@ class CreateRecurringTransactions implements ShouldQueue
return $recurrence->active;
}
/**
* Apply the users rules to newly created journals.
*
* @param User $user
* @param Collection $groups
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function applyRules(User $user, Collection $groups): void
{
$userId = $user->id;
if (!isset($this->rules[$userId])) {
$this->rules[$userId] = $this->getRules($user);
}
/** @var RuleEngine $ruleEngine */
$ruleEngine = app(RuleEngine::class);
$ruleEngine->setUser($user);
$ruleEngine->setAllRules(true);
// run the rules:
/** @var TransactionGroup $group */
foreach ($groups as $group) {
/** @var TransactionJournal $journal */
foreach ($group->transactionJournals as $journal) {
//$ruleEngine->processTransactionJournal($journal);
}
}
}
/**
* Helper function for debug information.
*
@@ -223,43 +195,6 @@ class CreateRecurringTransactions implements ShouldQueue
return $return;
}
/**
* @param Recurrence $recurrence
*
* @return int
*/
private function getPiggyId(Recurrence $recurrence): int
{
$meta = $recurrence->recurrenceMeta;
/** @var RecurrenceMeta $metaEntry */
foreach ($meta as $metaEntry) {
if ('piggy_bank_id' === $metaEntry->name) {
return (int)$metaEntry->value;
}
}
return 0;
}
/**
* Get the users rule groups.
*
* @param User $user
*
* @return Collection
*/
private function getRules(User $user): Collection
{
/** @var RuleRepositoryInterface $repository */
$repository = app(RuleRepositoryInterface::class);
$repository->setUser($user);
$set = $repository->getForImport();
Log::debug(sprintf('Found %d user rules.', $set->count()));
return $set;
}
/**
* Get the start date of a recurrence.
*
@@ -284,6 +219,7 @@ class CreateRecurringTransactions implements ShouldQueue
* @param Carbon $date
*
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function getTransactionData(Recurrence $recurrence, Carbon $date): array
{
@@ -334,82 +270,80 @@ class CreateRecurringTransactions implements ShouldQueue
* @param array $occurrences
*
* @return Collection
* @throws \FireflyIII\Exceptions\FireflyException
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function handleOccurrences(Recurrence $recurrence, array $occurrences): Collection
{
$collection = new Collection;
/** @var Carbon $date */
foreach ($occurrences as $date) {
Log::debug(sprintf('Now at date %s.', $date->format('Y-m-d')));
if ($date->ne($this->date)) {
Log::debug(sprintf('%s is not not today (%s)', $date->format('Y-m-d'), $this->date->format('Y-m-d')));
continue;
$result = $this->handleOccurrence($recurrence, $date);
if (null !== $result) {
$collection->push($result);
}
Log::debug(sprintf('%s IS today (%s)', $date->format('Y-m-d'), $this->date->format('Y-m-d')));
// count created journals on THIS day.
$journalCount = $this->repository->getJournalCount($recurrence, $date, $date);
if ($journalCount > 0 && false === $this->force) {
Log::info(sprintf('Already created %d journal(s) for date %s', $journalCount, $date->format('Y-m-d')));
continue;
}
if ($journalCount > 0 && true === $this->force) {
Log::warning(sprintf('Already created %d groups for date %s but FORCED to continue.', $journalCount, $date->format('Y-m-d')));
}
// create transaction array and send to factory.
$groupTitle = null;
if ($recurrence->recurrenceTransactions->count() > 0) {
/** @var RecurrenceTransaction $first */
$first = $recurrence->recurrenceTransactions()->first();
$groupTitle = $first->description;
}
$array = [
'user' => $recurrence->user_id,
'group_title' => $groupTitle,
'transactions' => $this->getTransactionData($recurrence, $date),
];
/** @var TransactionGroup $group */
$group = $this->groupRepository->store($array);
Log::info(sprintf('Created new transaction group #%d', $group->id));
/** @var TransactionJournal $journal */
foreach ($group->transactionJournals as $journal) {
// get piggy bank ID from meta data:
$piggyBankId = $this->getPiggyId($recurrence);
Log::debug(sprintf('Piggy bank ID for recurrence #%d is #%d', $recurrence->id, $piggyBankId));
// link to piggy bank:
/** @var PiggyBankFactory $factory */
$factory = app(PiggyBankFactory::class);
$factory->setUser($recurrence->user);
$piggyBank = $factory->find($piggyBankId, null);
if (null !== $piggyBank) {
/** @var PiggyBankEventFactory $factory */
$factory = app(PiggyBankEventFactory::class);
$factory->create($journal, $piggyBank);
}
}
// trigger event:
event(new StoredTransactionGroup($group, $recurrence->apply_rules));
// update recurring thing:
$recurrence->latest_date = $date;
$recurrence->save();
$collection->push($group);
}
return $collection;
}
/**
* @param Recurrence $recurrence
* @param Carbon $date
* @return TransactionGroup|null
*/
private function handleOccurrence(Recurrence $recurrence, Carbon $date): ?TransactionGroup
{
Log::debug(sprintf('Now at date %s.', $date->format('Y-m-d')));
if ($date->ne($this->date)) {
Log::debug(sprintf('%s is not not today (%s)', $date->format('Y-m-d'), $this->date->format('Y-m-d')));
return null;
}
Log::debug(sprintf('%s IS today (%s)', $date->format('Y-m-d'), $this->date->format('Y-m-d')));
// count created journals on THIS day.
$journalCount = $this->repository->getJournalCount($recurrence, $date, $date);
if ($journalCount > 0 && false === $this->force) {
Log::info(sprintf('Already created %d journal(s) for date %s', $journalCount, $date->format('Y-m-d')));
return null;
}
if ($journalCount > 0 && true === $this->force) {
Log::warning(sprintf('Already created %d groups for date %s but FORCED to continue.', $journalCount, $date->format('Y-m-d')));
}
// create transaction array and send to factory.
$groupTitle = null;
if ($recurrence->recurrenceTransactions->count() > 1) {
/** @var RecurrenceTransaction $first */
// @codeCoverageIgnoreStart
$first = $recurrence->recurrenceTransactions()->first();
$groupTitle = $first->description;
// @codeCoverageIgnoreEnd
}
$array = [
'user' => $recurrence->user_id,
'group_title' => $groupTitle,
'transactions' => $this->getTransactionData($recurrence, $date),
];
/** @var TransactionGroup $group */
$group = $this->groupRepository->store($array);
$this->created++;
Log::info(sprintf('Created new transaction group #%d', $group->id));
// link to piggy:
$this->linkGroupToPiggies($recurrence, $group);
// trigger event:
event(new StoredTransactionGroup($group, $recurrence->apply_rules));
// update recurring thing:
$recurrence->latest_date = $date;
$recurrence->save();
return $group;
}
/**
* Separate method that will loop all repetitions and do something with it. Will return
* all created transaction journals.
@@ -417,8 +351,6 @@ class CreateRecurringTransactions implements ShouldQueue
* @param Recurrence $recurrence
*
* @return Collection
*
* @throws \FireflyIII\Exceptions\FireflyException
*/
private function handleRepetitions(Recurrence $recurrence): Collection
{
@@ -549,7 +481,7 @@ class CreateRecurringTransactions implements ShouldQueue
}
// already fired today (with success):
if ($this->hasFiredToday($recurrence) && false === $this->force) {
if (false === $this->force && $this->hasFiredToday($recurrence)) {
Log::info(sprintf('Recurrence #%d has already fired today. Skipped.', $recurrence->id));
return false;
@@ -558,4 +490,37 @@ class CreateRecurringTransactions implements ShouldQueue
return true;
}
/**
* @param Collection $recurrences
* @return Collection
*/
private function filterRecurrences(Collection $recurrences): Collection
{
return $recurrences->filter(
function (Recurrence $recurrence) {
return $this->validRecurrence($recurrence);
}
);
}
/***
* @param Recurrence $recurrence
* @param TransactionGroup $group
*/
private function linkGroupToPiggies(Recurrence $recurrence, TransactionGroup $group): void
{
/** @var TransactionJournal $journal */
foreach ($group->transactionJournals as $journal) {
// get piggy bank ID from meta data:
$piggyBank = $this->repository->getPiggyBank($recurrence);
if (null !== $piggyBank) {
/** @var PiggyBankEventFactory $factory */
$factory = app(PiggyBankEventFactory::class);
$factory->create($journal, $piggyBank);
}
}
}
}

View File

@@ -467,4 +467,14 @@ class ImportJobRepository implements ImportJobRepositoryInterface
return $size > $this->maxUploadSize;
}
/**
* @param ImportJob $job
*
* @return int
*/
public function countByTag(ImportJob $job): int
{
return $job->tag->transactionJournals->count();
}
}

View File

@@ -62,6 +62,13 @@ interface ImportJobRepositoryInterface
*/
public function countTransactions(ImportJob $job): int;
/**
* @param ImportJob $job
*
* @return int
*/
public function countByTag(ImportJob $job): int;
/**
* @param string $importProvider
*

View File

@@ -28,6 +28,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\RecurrenceFactory;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Note;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\Preference;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\RecurrenceMeta;
@@ -208,6 +209,24 @@ class RecurringRepository implements RecurringRepositoryInterface
return '';
}
/**
* @param Recurrence $recurrence
* @return PiggyBank|null
*/
public function getPiggyBank(Recurrence $recurrence): ?PiggyBank
{
$meta = $recurrence->recurrenceMeta;
/** @var RecurrenceMeta $metaEntry */
foreach ($meta as $metaEntry) {
if ('piggy_bank_id' === $metaEntry->name) {
$piggyId = (int)$metaEntry->value;
return $this->user->piggyBanks()->where('id', $piggyId)->first(['piggy_banks.*']);
}
}
return null;
}
/**
* Generate events in the date range.
*

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\Recurring;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\RecurrenceRepetition;
use FireflyIII\Models\RecurrenceTransaction;
@@ -114,12 +115,16 @@ interface RecurringRepositoryInterface
* @param Carbon $start
* @param Carbon $end
*
* @throws FireflyException
*
* @return array
*/
public function getOccurrencesInRange(RecurrenceRepetition $repetition, Carbon $start, Carbon $end): array;
/**
* @param Recurrence $recurrence
* @return PiggyBank|null
*/
public function getPiggyBank(Recurrence $recurrence): ?PiggyBank;
/**
* Get the tags from the recurring transaction.
*

View File

@@ -288,8 +288,6 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
* @param array $data
*
* @return TransactionGroup
*
* @throws FireflyException
*/
public function store(array $data): TransactionGroup
{

View File

@@ -112,7 +112,6 @@ interface TransactionGroupRepositoryInterface
* @param array $data
*
* @return TransactionGroup
* @throws FireflyException
*/
public function store(array $data): TransactionGroup;

View File

@@ -87,7 +87,6 @@ trait JournalServiceTrait
* @param string|null $accountName
*
* @return Account
* @throws FireflyException
*/
protected function getAccount(string $transactionType, string $direction, ?int $accountId, ?string $accountName): Account
{