Expand test coverage

This commit is contained in:
James Cole
2019-06-13 07:17:31 +02:00
parent aacd218056
commit 6bcb2ec144
7 changed files with 520 additions and 81 deletions

View File

@@ -0,0 +1,263 @@
<?php
/**
* OtherCurrenciesCorrectionsTest.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\Configuration;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Mockery;
use Tests\TestCase;
/**
* Class OtherCurrenciesCorrectionsTest
*/
class OtherCurrenciesCorrectionsTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* Basic test. Assume nothing is wrong. Submit a withdrawal and a deposit.
*
* @covers \FireflyIII\Console\Commands\Upgrade\OtherCurrenciesCorrections
*/
public function testHandle(): void
{
// mock classes:
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$withdrawal = $this->getRandomWithdrawal();
$deposit = $this->getRandomDeposit();
$euro = $this->getEuro();
// collect all journals:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('getAllJournals')
->atLeast()->once()
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
->andReturn(new Collection([$withdrawal, $deposit]));
// account repos
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()
->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
// collect currency
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('findNull')->atLeast()->once()
->withArgs([1])->andReturn($euro);
// configuration
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_other_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_other_currencies', true]);
// assume all is well.
$this->artisan('firefly-iii:other-currencies')
->expectsOutput('Verified 2 transaction(s) and journal(s).')
->assertExitCode(0);
}
/**
* Basic test. Assume nothing is wrong. Submit an opening balance.
* Also fixes transaction currency ID.
*
* @covers \FireflyIII\Console\Commands\Upgrade\OtherCurrenciesCorrections
*/
public function testHandleOB(): void
{
$type = TransactionType::where('type', TransactionType::OPENING_BALANCE)->first();
$asset = $this->getRandomAsset();
$initial = $this->getRandomInitialBalance();
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => $type->id,
'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' => $initial->id,
'amount' => '10',
'identifier' => 1,
]
);
// mock classes:
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$euro = $this->getEuro();
// collect all journals:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('getAllJournals')
->atLeast()->once()
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
->andReturn(new Collection([$journal]));
// account repos
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()
->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
// collect currency
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('findNull')->atLeast()->once()
->withArgs([1])->andReturn($euro);
// configuration
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_other_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_other_currencies', true]);
$this->artisan('firefly-iii:other-currencies')
->expectsOutput('Verified 1 transaction(s) and journal(s).')
->assertExitCode(0);
// assume currency has been fixed for both transactions:
$this->assertCount(1, Transaction::where('id', $one->id)->where('transaction_currency_id', $euro->id)->get());
$this->assertCount(1, Transaction::where('id', $two->id)->where('transaction_currency_id', $euro->id)->get());
$one->forceDelete();
$two->forceDelete();
$journal->forceDelete();
}
/**
* Basic test. Assume nothing is wrong. Submit an opening balance.
* Also fixes bad transaction currency ID.
*
* @covers \FireflyIII\Console\Commands\Upgrade\OtherCurrenciesCorrections
*/
public function testHandleReconciliation(): void
{
$type = TransactionType::where('type', TransactionType::RECONCILIATION)->first();
$asset = $this->getRandomAsset();
$reconciliation = $this->getRandomReconciliation();
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => $type->id,
'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,
'transaction_currency_id' => 2,
]
);
$two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $reconciliation->id,
'amount' => '10',
'identifier' => 1,
'transaction_currency_id' => 2,
]
);
// mock classes:
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$euro = $this->getEuro();
// collect all journals:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('getAllJournals')
->atLeast()->once()
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
->andReturn(new Collection([$journal]));
// account repos
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()
->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
// collect currency
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('findNull')->atLeast()->once()
->withArgs([1])->andReturn($euro);
// configuration
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_other_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_other_currencies', true]);
$this->artisan('firefly-iii:other-currencies')
->expectsOutput('Verified 1 transaction(s) and journal(s).')
->assertExitCode(0);
// assume currency has been fixed for both transactions:
$this->assertCount(1, Transaction::where('id', $one->id)->where('transaction_currency_id', $euro->id)->get());
$this->assertCount(1, Transaction::where('id', $two->id)->where('transaction_currency_id', $euro->id)->get());
$this->assertCount(1, Transaction::where('id', $one->id)->where('foreign_currency_id', 2)->get());
$this->assertCount(1, Transaction::where('id', $two->id)->where('foreign_currency_id', 2)->get());
$one->forceDelete();
$two->forceDelete();
$journal->forceDelete();
}
}

View File

@@ -0,0 +1,152 @@
<?php
/**
* TransactionIdentifierTest.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\Configuration;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Tests\TestCase;
/**
* Class TransactionIdentifierTest
*/
class TransactionIdentifierTest 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\TransactionIdentifier
*/
public function testHandle(): void
{
// mock classes:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
// commands:
$journalRepos->shouldReceive('getSplitJournals')->andReturn(new Collection)
->atLeast()->once();
// configuration
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_transaction_identifier', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_transaction_identifier', true]);
// assume all is well.
$this->artisan('firefly-iii:transaction-identifiers')
->expectsOutput('All split journal transaction identifiers are correct.')
->assertExitCode(0);
}
/**
* Basic test. Assume nothing is wrong.
*
* @covers \FireflyIII\Console\Commands\Upgrade\TransactionIdentifier
*/
public function testHandleSplit(): void
{
// create a split journal:
$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' => 0,
]
);
$two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $expense->id,
'amount' => '10',
'identifier' => 0,
]
);
$three = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $asset->id,
'amount' => '-12',
'identifier' => 0,
]
);
$four = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $expense->id,
'amount' => '12',
'identifier' => 0,
]
);
// mock classes:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
// commands:
$journalRepos->shouldReceive('getSplitJournals')->andReturn(new Collection([$journal]))
->atLeast()->once();
// configuration
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_transaction_identifier', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_transaction_identifier', true]);
// assume all is well.
$this->artisan('firefly-iii:transaction-identifiers')
->expectsOutput('Fixed 2 split journal transaction identifier(s).')
->assertExitCode(0);
// see results:
$this->assertCount(1, Transaction::where('id', $one->id)->where('identifier', 0)->get());
$this->assertCount(1, Transaction::where('id', $three->id)->where('identifier', 1)->get());
}
}