Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
New command and test
James Colefb1af395f9cd
diff --git a/app/Console/Commands/Upgrade/RenameAccountMeta.php b/app/Console/Commands/Upgrade/RenameAccountMeta.phpnew file mode 100644index 0000000000..a68f0cfcdb--- /dev/null+++ b/app/Console/Commands/Upgrade/RenameAccountMeta.php@@ -0,0 +1,110 @@+<?php+/**+ * RenameAccountMeta.php+ * Copyright (c) 2019 [email protected]+ *+ * 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);+ }+}diff --git a/app/Console/Commands/Upgrade/UpgradeSkeleton.php.stub b/app/Console/Commands/Upgrade/UpgradeSkeleton.php.stubindex 7ff9d96597..f45211806e 100644--- a/app/Console/Commands/Upgrade/UpgradeSkeleton.php.stub+++ b/app/Console/Commands/Upgrade/UpgradeSkeleton.php.stub@@ -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;}diff --git a/tests/Unit/Console/Commands/Upgrade/RenameAccountMetaTest.php b/tests/Unit/Console/Commands/Upgrade/RenameAccountMetaTest.phpnew file mode 100644index 0000000000..b6b6aa77ef--- /dev/null+++ b/tests/Unit/Console/Commands/Upgrade/RenameAccountMetaTest.php@@ -0,0 +1,94 @@+<?php+/**+ * RenameAccountMetaTest.php+ * Copyright (c) 2019 [email protected]+ *+ * 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();+ }++}\ No newline at end of file