Improve test coverage.

This commit is contained in:
James Cole
2019-06-13 06:39:05 +02:00
parent 6fdfa722dd
commit aacd218056
13 changed files with 1204 additions and 591 deletions

View File

@@ -55,13 +55,17 @@ class MigrateAttachments extends Command
*/
public function handle(): int
{
// @codeCoverageIgnoreStart
$start = microtime(true);
if ($this->isExecuted() && true !== $this->option('force')) {
$this->warn('This command has already been executed.');
return 0;
}
// @codeCoverageIgnoreEnd
$attachments = Attachment::get();
$count = 0;
/** @var Attachment $att */
foreach ($attachments as $att) {
@@ -69,6 +73,7 @@ class MigrateAttachments extends Command
// move description:
$description = (string)$att->description;
if ('' !== $description) {
// find or create note:
$note = $att->notes()->first();
if (null === $note) {
@@ -82,9 +87,16 @@ class MigrateAttachments extends Command
$att->description = '';
$att->save();
Log::debug(sprintf('Migrated attachment #%s description to note #%d', $att->id, $note->id));
Log::debug(sprintf('Migrated attachment #%s description to note #%d.', $att->id, $note->id));
$count++;
}
}
if (0 === $count) {
$this->line('All attachments are OK.');
}
if (0 !== $count) {
$this->line(sprintf('Updated %d attachment(s).',$count));
}
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Migrated attachment notes in %s seconds.', $end));
$this->markAsExecuted();

View File

@@ -1,6 +1,6 @@
<?php
/**
* MigrateNotes.php
* MigrateJournalNotes.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
@@ -31,9 +31,9 @@ use Illuminate\Console\Command;
use Log;
/**
* Class MigrateNotes
* Class MigrateJournalNotes
*/
class MigrateNotes extends Command
class MigrateJournalNotes extends Command
{
public const CONFIG_NAME = '4780_migrate_notes';
/**
@@ -41,7 +41,7 @@ class MigrateNotes extends Command
*
* @var string
*/
protected $description = 'Migrate notes';
protected $description = 'Migrate notes for transaction journals.';
/**
* The name and signature of the console command.
*
@@ -57,12 +57,14 @@ class MigrateNotes extends Command
public function handle(): int
{
$start = microtime(true);
// @codeCoverageIgnoreStart
if ($this->isExecuted() && true !== $this->option('force')) {
$this->warn('This command has already been executed.');
return 0;
}
// @codeCoverageIgnoreEnd
$count = 0;
/** @noinspection PhpUndefinedMethodInspection */
$set = TransactionJournalMeta::whereName('notes')->get();
/** @var TransactionJournalMeta $meta */
@@ -79,10 +81,21 @@ class MigrateNotes extends Command
Log::debug(sprintf('Migrated meta note #%d to Note #%d', $meta->id, $note->id));
try {
$meta->delete();
// @codeCoverageIgnoreStart
} catch (Exception $e) {
Log::error(sprintf('Could not delete old meta entry #%d: %s', $meta->id, $e->getMessage()));
}
// @codeCoverageIgnoreEnd
$count++;
}
if (0 === $count) {
$this->line('No notes to migrate.');
}
if (0 !== $count) {
$this->line(sprintf('Migrated %d note(s).', $count));
}
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Migrated notes in %s seconds.', $end));
$this->markAsExecuted();

View File

@@ -60,6 +60,7 @@ class MigrateToGroups extends Command
private $journalRepository;
/** @var JournalDestroyService */
private $service;
private $count;
/**
* Create a new command instance.
@@ -69,6 +70,7 @@ class MigrateToGroups extends Command
public function __construct()
{
parent::__construct();
$this->count = 0;
$this->journalRepository = app(JournalRepositoryInterface::class);
$this->service = app(JournalDestroyService::class);
$this->groupFactory = app(TransactionGroupFactory::class);
@@ -83,14 +85,17 @@ class MigrateToGroups extends Command
public function handle(): int
{
$start = microtime(true);
// @codeCoverageIgnoreStart
if ($this->isMigrated() && true !== $this->option('force')) {
$this->info('Database already seems to be migrated.');
return 0;
}
if (true === $this->option('force')) {
$this->warn('Forcing the migration.');
}
// @codeCoverageIgnoreEnd
Log::debug('---- start group migration ----');
$this->makeGroupsFromSplitJournals();
@@ -102,14 +107,24 @@ class MigrateToGroups extends Command
Log::debug('---- end group migration ----');
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Migrate all journals to groups in %s seconds.', $end));
if (0 !== $this->count) {
$this->line(sprintf('Migrated %d transaction journal(s).', $this->count));
}
if (0 === $this->count) {
$this->line('No journals to migrate to groups.');
}
$this->markAsMigrated();
return 0;
}
/**
* @param TransactionJournal $journal
* @param Transaction $transaction
* @param Transaction $transaction
*
* @return Transaction|null
*/
@@ -117,7 +132,12 @@ class MigrateToGroups extends Command
{
$set = $journal->transactions->filter(
static function (Transaction $subject) use ($transaction) {
return $transaction->amount * -1 === (float)$subject->amount && $transaction->identifier === $subject->identifier;
$amount = (float)$transaction->amount * -1 === (float)$subject->amount;
$identifier = $transaction->identifier === $subject->identifier;
Log::debug(sprintf('Amount the same? %s', var_export($amount, true)));
Log::debug(sprintf('ID the same? %s', var_export($identifier, true)));
return $amount && $identifier;
}
);
@@ -152,6 +172,7 @@ class MigrateToGroups extends Command
]
);
DB::table('transaction_journals')->where('id', $array['id'])->update(['transaction_group_id' => $groupId]);
$this->count++;
}
/**
@@ -209,14 +230,17 @@ class MigrateToGroups extends Command
* @param TransactionJournal $journal
*
* @throws Exception
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function makeMultiGroup(TransactionJournal $journal): void
{
// double check transaction count.
if ($journal->transactions->count() <= 2) {
// @codeCoverageIgnoreStart
Log::debug(sprintf('Will not try to convert journal #%d because it has 2 or less transactions.', $journal->id));
return;
// @codeCoverageIgnoreEnd
}
Log::debug(sprintf('Will now try to convert journal #%d', $journal->id));
@@ -264,6 +288,7 @@ class MigrateToGroups extends Command
$opposingTr = $this->findOpposingTransaction($journal, $transaction);
if (null === $opposingTr) {
// @codeCoverageIgnoreStart
$this->error(
sprintf(
'Journal #%d has no opposing transaction for transaction #%d. Cannot upgrade this entry.',
@@ -271,6 +296,7 @@ class MigrateToGroups extends Command
)
);
continue;
// @codeCoverageIgnoreEnd
}
$tArray = [
@@ -321,12 +347,16 @@ class MigrateToGroups extends Command
// delete the old transaction journal.
$this->service->destroy($journal);
$this->count++;
// report on result:
Log::debug(
sprintf('Migrated journal #%d into group #%d with these journals: #%s', $journal->id, $group->id, implode(', #', $group->transactionJournals->pluck('id')->toArray()))
sprintf('Migrated journal #%d into group #%d with these journals: #%s',
$journal->id, $group->id, implode(', #', $group->transactionJournals->pluck('id')->toArray()))
);
$this->line(
sprintf('Migrated journal #%d into group #%d with these journals: #%s', $journal->id, $group->id, implode(', #', $group->transactionJournals->pluck('id')->toArray()))
sprintf('Migrated journal #%d into group #%d with these journals: #%s',
$journal->id, $group->id, implode(', #', $group->transactionJournals->pluck('id')->toArray()))
);
}

View File

@@ -24,19 +24,16 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\Upgrade;
use Crypt;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Preference;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Console\Command;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Collection;
use Log;
/**
* Class MigrateToRules
@@ -57,171 +54,60 @@ class MigrateToRules extends Command
*/
protected $signature = 'firefly-iii:bills-to-rules {--F|force : Force the execution of this command.}';
/** @var UserRepositoryInterface */
private $userRepository;
/** @var RuleGroupRepositoryInterface */
private $ruleGroupRepository;
/** @var BillRepositoryInterface */
private $billRepository;
/** @var RuleRepositoryInterface */
private $ruleRepository;
private $count;
/**
* MigrateToRules constructor.
*/
public function __construct()
{
parent::__construct();
$this->count = 0;
$this->userRepository = app(UserRepositoryInterface::class);
$this->ruleGroupRepository = app(RuleGroupRepositoryInterface::class);
$this->billRepository = app(BillRepositoryInterface::class);
$this->ruleRepository = app(RuleRepositoryInterface::class);
}
/**
* Execute the console command.
*
* @return int
* @throws FireflyException
*/
public function handle(): int
{
$start = microtime(true);
$start = microtime(true);
// @codeCoverageIgnoreStart
if ($this->isExecuted() && true !== $this->option('force')) {
$this->warn('This command has already been executed.');
return 0;
}
// @codeCoverageIgnoreEnd
foreach (User::get() as $user) {
/** @var Preference $lang */
$lang = app('preferences')->getForUser($user, 'language', 'en_US');
$groupName = (string)trans('firefly.rulegroup_for_bills_title', [], $lang->data);
$ruleGroup = $user->ruleGroups()->where('title', $groupName)->first();
$currencyPreference = app('preferences')->getForUser($user, 'currencyPreference', config('firefly.default_currency', 'EUR'));
if (null === $currencyPreference) {
$this->error('User has no currency preference. Impossible.');
return 1;
}
$currencyCode = $this->tryDecrypt($currencyPreference->data);
// try json decrypt just in case.
if (strlen($currencyCode) > 3) {
$currencyCode = json_decode($currencyCode) ?? 'EUR';
}
$currency = TransactionCurrency::where('code', $currencyCode)->first();
if (null === $currency) {
$this->line('Fall back to default currency in migrateBillsToRules().');
$currency = app('amount')->getDefaultCurrencyByUser($user);
}
if (null === $ruleGroup) {
$array = RuleGroup::get(['order'])->pluck('order')->toArray();
$order = count($array) > 0 ? max($array) + 1 : 1;
$ruleGroup = RuleGroup::create(
[
'user_id' => $user->id,
'title' => (string)trans('firefly.rulegroup_for_bills_title', [], $lang->data),
'description' => (string)trans('firefly.rulegroup_for_bills_description', [], $lang->data),
'order' => $order,
'active' => 1,
]
);
}
// loop bills.
$order = 1;
$count = 0;
/** @var Collection $collection */
$collection = $user->bills()->get();
/** @var Bill $bill */
foreach ($collection as $bill) {
if ('MIGRATED_TO_RULES' !== $bill->match) {
$count++;
$rule = Rule::create(
[
'user_id' => $user->id,
'rule_group_id' => $ruleGroup->id,
'title' => (string)trans('firefly.rule_for_bill_title', ['name' => $bill->name], $lang->data),
'description' => (string)trans('firefly.rule_for_bill_description', ['name' => $bill->name], $lang->data),
'order' => $order,
'active' => $bill->active,
'stop_processing' => 1,
]
);
// add default trigger
RuleTrigger::create(
[
'rule_id' => $rule->id,
'trigger_type' => 'user_action',
'trigger_value' => 'store-journal',
'active' => 1,
'stop_processing' => 0,
'order' => 1,
]
);
// add trigger for description
$match = implode(' ', explode(',', $bill->match));
RuleTrigger::create(
[
'rule_id' => $rule->id,
'trigger_type' => 'description_contains',
'trigger_value' => $match,
'active' => 1,
'stop_processing' => 0,
'order' => 2,
]
);
if ($bill->amount_max !== $bill->amount_min) {
// add triggers for amounts:
RuleTrigger::create(
[
'rule_id' => $rule->id,
'trigger_type' => 'amount_less',
'trigger_value' => round($bill->amount_max, $currency->decimal_places),
'active' => 1,
'stop_processing' => 0,
'order' => 3,
]
);
RuleTrigger::create(
[
'rule_id' => $rule->id,
'trigger_type' => 'amount_more',
'trigger_value' => round((float)$bill->amount_min, $currency->decimal_places),
'active' => 1,
'stop_processing' => 0,
'order' => 4,
]
);
}
if ($bill->amount_max === $bill->amount_min) {
RuleTrigger::create(
[
'rule_id' => $rule->id,
'trigger_type' => 'amount_exactly',
'trigger_value' => round((float)$bill->amount_min, $currency->decimal_places),
'active' => 1,
'stop_processing' => 0,
'order' => 3,
]
);
}
// create action
RuleAction::create(
[
'rule_id' => $rule->id,
'action_type' => 'link_to_bill',
'action_value' => $bill->name,
'order' => 1,
'active' => 1,
'stop_processing' => 0,
]
);
$order++;
$bill->match = 'MIGRATED_TO_RULES';
$bill->save();
$this->line(sprintf('Updated bill #%d ("%s") so it will use rules.', $bill->id, $bill->name));
}
// give bills a currency when they dont have one.
if (null === $bill->transaction_currency_id) {
$this->line(sprintf('Gave bill #%d ("%s") a currency (%s).', $bill->id, $bill->name, $currency->name));
$bill->transactionCurrency()->associate($currency);
$bill->save();
}
}
if ($count > 0) {
$this->info(sprintf('Migrated %d bills for user %s', $count, $user->email));
}
if (0 === $count) {
$this->info(sprintf('Bills are correct for user %s.', $user->email));
}
$users = $this->userRepository->all();
/** @var User $user */
foreach ($users as $user) {
$this->migrateUser($user);
}
if (0 === $this->count) {
$this->line('All bills are OK.');
}
if (0 !== $this->count) {
$this->line(sprintf('Verified and fixed %d bill(s).', $this->count));
}
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Verified and fixed bills in %s seconds.', $end));
$this->markAsExecuted();
@@ -251,18 +137,107 @@ class MigrateToRules extends Command
}
/**
* @param string $value
* Migrate bills to new rule structure for a specific user.
*
* @return string
* @param User $user
* @throws FireflyException
*/
private function tryDecrypt(string $value): string
private function migrateUser(User $user): void
{
try {
$value = Crypt::decrypt($value);
} catch (DecryptException $e) {
Log::debug(sprintf('Could not decrypt. %s', $e->getMessage()));
$this->ruleGroupRepository->setUser($user);
$this->billRepository->setUser($user);
$this->ruleRepository->setUser($user);
/** @var Preference $lang */
$lang = app('preferences')->getForUser($user, 'language', 'en_US');
$groupTitle = (string)trans('firefly.rulegroup_for_bills_title', [], $lang->data);
$ruleGroup = $this->ruleGroupRepository->findByTitle($groupTitle);
//$currency = $this->getCurrency($user);
if (null === $ruleGroup) {
$ruleGroup = $this->ruleGroupRepository->store(
[
'title' => (string)trans('firefly.rulegroup_for_bills_title', [], $lang->data),
'description' => (string)trans('firefly.rulegroup_for_bills_description', [], $lang->data),
'active' => true,
]
);
}
$bills = $this->billRepository->getBills();
/** @var Bill $bill */
foreach ($bills as $bill) {
$this->migrateBill($ruleGroup, $bill, $lang);
}
return $value;
}
/**
* @param RuleGroup $ruleGroup
* @param Bill $bill
* @throws FireflyException
*/
private function migrateBill(RuleGroup $ruleGroup, Bill $bill, Preference $language): void
{
if ('MIGRATED_TO_RULES' === $bill->match) {
return;
}
// get match thing:
$match = implode(' ', explode(',', $bill->match));
$newRule = [
'rule_group_id' => $ruleGroup->id,
'active' => true,
'strict' => false,
'stop_processing' => false, // field is no longer used.
'title' => (string)trans('firefly.rule_for_bill_title', ['name' => $bill->name], $language->data),
'description' => (string)trans('firefly.rule_for_bill_description', ['name' => $bill->name], $language->data),
'trigger' => 'store-journal',
'triggers' => [
[
'type' => 'description_contains',
'value' => $match,
],
],
'actions' => [
'type' => 'link_to_bill',
'value' => $bill->name,
],
];
// two triggers or one, depends on bill content:
if ($bill->amount_max === $bill->amount_min) {
$newRule['triggers'][] = [
'type' => 'amount_exactly',
'value' => $bill->amount_min,
];
}
if ($bill->amount_max !== $bill->amount_min) {
$newRule['triggers'][] = [
'type' => 'amount_less',
'value' => $bill->amount_max,
];
$newRule['triggers'][] = [
'type' => 'amount_more',
'value' => $bill->amount_min,
];
}
$this->ruleRepository->store($newRule);
// update bill:
$newBillData = [
'currency_id' => $bill->transaction_currency_id,
'name' => $bill->name,
'match' => 'MIGRATED_TO_RULES',
'amount_min' => $bill->amount_min,
'amount_max' => $bill->amount_max,
'date' => $bill->date,
'repeat_freq' => $bill->repeat_freq,
'skip' => $bill->skip,
'active' => $bill->active,
];
$this->billRepository->update($bill, $newBillData);
$this->count++;
}
}