New command and test

This commit is contained in:
James Cole 2019-06-21 19:07:11 +02:00
parent f3123802a9
commit fb1af395f9
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 209 additions and 0 deletions

View File

@ -0,0 +1,110 @@
<?php
/**
* RenameAccountMeta.php
* Copyright (c) 2019 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/>.
*/
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Models\AccountMeta;
use Illuminate\Console\Command;
/**
* Class RenameAccountMeta
*/
class RenameAccountMeta extends Command
{
public const CONFIG_NAME = '4780_rename_account_meta';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Rename account meta-data to new format.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:rename-account-meta {--F|force : Force the execution of this command.}';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$start = microtime(true);
if ($this->isExecuted() && true !== $this->option('force')) {
$this->warn('This command has already been executed.');
return 0;
}
$array = [
'accountRole' => 'account_role',
'ccType' => 'cc_type',
'ccMonthlyPaymentDate' => 'cc_monthly_payment_date',
];
$count = 0;
/**
* @var string $old
* @var string $new
*/
foreach ($array as $old => $new) {
$count += AccountMeta::where('name', $old)->update(['name' => $new]);
}
$this->markAsExecuted();
if (0 === $count) {
$this->line('All account meta is OK.');
}
if (0 !== $count) {
$this->line(sprintf('Renamed %d account meta entries (entry).', $count));
}
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Fixed account meta data in %s seconds.', $end));
return 0;
}
/**
* @return bool
*/
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
return (bool)$configVar->data;
}
return false; // @codeCoverageIgnore
}
/**
*
*/
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
}

View File

@ -49,6 +49,7 @@ class UpgradeSkeleton extends Command
*/
public function handle(): int
{
$start = microtime(true);
if ($this->isExecuted() && true !== $this->option('force')) {
$this->warn('This command has already been executed.');
@ -57,6 +58,10 @@ class UpgradeSkeleton extends Command
$this->warn('Congrats, you found the skeleton command. Boo!');
//$this->markAsExecuted();
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('in %s seconds.', $end));
return 0;
}

View File

@ -0,0 +1,94 @@
<?php
/**
* RenameAccountMetaTest.php
* Copyright (c) 2019 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/>.
*/
namespace Tests\Unit\Console\Commands\Upgrade;
use FireflyConfig;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\Configuration;
use Log;
use Tests\TestCase;
/**
* Class RenameAccountMetaTest
*/
class RenameAccountMetaTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* Basic test. Assume nothing is wrong.
*
* @covers \FireflyIII\Console\Commands\Upgrade\RenameAccountMeta
*/
public function testHandle(): void
{
// assume all is well.
$this->artisan('firefly-iii:rename-account-meta')
->expectsOutput('All account meta is OK.')
->assertExitCode(0);
}
/**
* Create bad entry, then check if its renamed.
*
* @covers \FireflyIII\Console\Commands\Upgrade\RenameAccountMeta
*/
public function testHandleRename(): void
{
$false = new Configuration;
$false->data = false;
// check config
FireflyConfig::shouldReceive('get')->withArgs(['4780_rename_account_meta', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_rename_account_meta', true]);
$expense = $this->getRandomExpense();
$meta = AccountMeta::create(
[
'name' => 'accountRole',
'data' => 'defaultAsset',
'account_id' => $expense->id,
]
);
// assume all is well.
$this->artisan('firefly-iii:rename-account-meta')
->expectsOutput('Renamed 1 account meta entries (entry).')
->assertExitCode(0);
$this->assertCount(0, AccountMeta::where('id', $meta->id)->where('name', 'accountRole')->get());
$this->assertCount(1, AccountMeta::where('id', $meta->id)->where('name', 'account_role')->get());
$meta->forceDelete();
}
}