Files
firefly-iii/tests/Unit/Console/Commands/Upgrade/MigrateToGroupsTest.php

248 lines
9.0 KiB
PHP
Raw Normal View History

2019-03-18 16:52:49 +01:00
<?php
/**
* MigrateToGroupsTest.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/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Console\Commands\Upgrade;
use FireflyConfig;
2019-06-13 06:39:05 +02:00
use FireflyIII\Factory\TransactionGroupFactory;
2019-03-18 16:52:49 +01:00
use FireflyIII\Models\Configuration;
use FireflyIII\Models\Transaction;
2019-06-13 06:39:05 +02:00
use FireflyIII\Models\TransactionJournal;
2019-03-18 16:52:49 +01:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2019-06-13 06:39:05 +02:00
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
2019-03-18 16:52:49 +01:00
use Illuminate\Support\Collection;
use Log;
use Mockery;
use Tests\TestCase;
/**
* Class MigrateToGroupsTest
*/
class MigrateToGroupsTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2019-03-18 16:52:49 +01:00
}
/**
2019-06-13 06:39:05 +02:00
* Basic test. Assume nothing is wrong.
*
2019-03-18 16:52:49 +01:00
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToGroups
*/
2019-06-13 06:39:05 +02:00
public function testHandle(): void
2019-03-18 16:52:49 +01:00
{
2019-06-13 06:39:05 +02:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
// mock calls:
$journalRepos->shouldReceive('getSplitJournals')
->atLeast()->once()
->andReturn(new Collection);
$journalRepos->shouldReceive('getJournalsWithoutGroup')
->atLeast()->once()
->andReturn([]);
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_migrated_to_groups', true]);
// assume all is well.
2019-04-09 20:05:20 +02:00
$this->artisan('firefly-iii:migrate-to-groups')
2019-06-13 06:39:05 +02:00
->expectsOutput('No journals to migrate to groups.')
2019-03-18 16:52:49 +01:00
->assertExitCode(0);
}
/**
2019-06-13 06:39:05 +02:00
* Return a journal without a group.
*
2019-03-18 16:52:49 +01:00
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToGroups
*/
2019-06-13 06:39:05 +02:00
public function testHandleNoGroup(): void
2019-03-18 16:52:49 +01:00
{
2019-06-13 06:39:05 +02:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$asset = $this->getRandomAsset();
$expense = $this->getRandomExpense();
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => 1,
'description' => 'Test',
'tag_count' => 0,
'date' => '2019-01-01',
]
);
$one = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $asset->id,
'amount' => '-10',
'identifier' => 1,
]
);
$two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $expense->id,
'amount' => '10',
'identifier' => 1,
]
);
$array = $journal->toArray();
// mock calls:
$journalRepos->shouldReceive('getSplitJournals')
->atLeast()->once()
->andReturn(new Collection);
$journalRepos->shouldReceive('getJournalsWithoutGroup')
->atLeast()->once()
->andReturn([$array]);
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_migrated_to_groups', true]);
// assume all is well.
2019-04-09 20:05:20 +02:00
$this->artisan('firefly-iii:migrate-to-groups')
2019-06-13 06:39:05 +02:00
->expectsOutput('Migrated 1 transaction journal(s).')
2019-03-18 16:52:49 +01:00
->assertExitCode(0);
2019-06-13 06:39:05 +02:00
// no longer without a group.
$this->assertCount(0, TransactionJournal::where('id', $journal->id)->whereNull('transaction_group_id')->get());
$journal->transactionGroup()->forceDelete();
$one->forceDelete();
$two->forceDelete();
$journal->forceDelete();
2019-03-18 16:52:49 +01:00
}
/**
2019-06-13 06:39:05 +02:00
* Create split withdrawal and see what the system will do.
*
2019-03-18 16:52:49 +01:00
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToGroups
*/
2019-06-13 06:39:05 +02:00
public function testHandleSplitJournal(): void
2019-03-18 16:52:49 +01:00
{
2019-06-13 06:39:05 +02:00
$asset = $this->getRandomAsset();
$expense = $this->getRandomExpense();
$group = $this->getRandomWithdrawalGroup();
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => 1,
'description' => 'Test',
'tag_count' => 0,
'date' => '2019-01-01',
]
);
$one = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $asset->id,
'amount' => '-10',
'identifier' => 1,
]
);
$two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $expense->id,
'amount' => '10',
'identifier' => 1,
]
);
$three = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $asset->id,
'amount' => '-12',
'identifier' => 2,
]
);
$four = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $expense->id,
'amount' => '12',
'identifier' => 2,
]
);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$service = $this->mock(JournalDestroyService::class);
$factory = $this->mock(TransactionGroupFactory::class);
// mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
2019-03-18 16:52:49 +01:00
2019-06-13 06:39:05 +02:00
// mock journal things:
$journalRepos->shouldReceive('getJournalBudgetId')->atLeast()->once()->andReturn(0);
$journalRepos->shouldReceive('getJournalCategoryId')->atLeast()->once()->andReturn(0);
$journalRepos->shouldReceive('getNoteText')->atLeast()->once()->andReturn('Some note.');
$journalRepos->shouldReceive('getTags')->atLeast()->once()->andReturn(['A', 'B']);
$journalRepos->shouldReceive('getMetaField')->atLeast()
->withArgs([Mockery::any(), Mockery::any()])
->once()->andReturn(null);
$journalRepos->shouldReceive('getMetaDate')->atLeast()
->withArgs([Mockery::any(), Mockery::any()])
->once()->andReturn(null);
// create a group
$factory->shouldReceive('create')->atLeast()->once()->andReturn($group);
$service->shouldReceive('destroy')->atLeast()->once();
$factory->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('getSplitJournals')
->atLeast()->once()
->andReturn(new Collection([$journal]));
$journalRepos->shouldReceive('getJournalsWithoutGroup')
->atLeast()->once()
->andReturn([]);
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_migrated_to_groups', true]);
2019-03-18 16:52:49 +01:00
2019-04-09 20:05:20 +02:00
$this->artisan('firefly-iii:migrate-to-groups')
2019-06-13 06:39:05 +02:00
->expectsOutput('Migrated 1 transaction journal(s).')
2019-03-18 16:52:49 +01:00
->assertExitCode(0);
2019-06-13 06:39:05 +02:00
// delete the created stuff:
$one->forceDelete();
$two->forceDelete();
$three->forceDelete();
$four->forceDelete();
$journal->forceDelete();
// the calls above let me know it's OK.
}
2019-03-18 16:52:49 +01:00
}