Expand tests

This commit is contained in:
James Cole
2020-11-07 14:05:53 +01:00
parent 5b35612be0
commit 413df5a005
5 changed files with 63 additions and 60 deletions

View File

@@ -23,12 +23,12 @@ namespace Tests\Feature\Console\Commands\Correction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Log; use Log;
use Tests\TestCase; use Tests\TestCase;
/** /**
* Class CorrectOpeningBalanceCurrenciesTest * Class CorrectOpeningBalanceCurrenciesTest
*
* @package Tests\Feature\Console\Commands\Correction * @package Tests\Feature\Console\Commands\Correction
*/ */
class CorrectOpeningBalanceCurrenciesTest extends TestCase class CorrectOpeningBalanceCurrenciesTest extends TestCase
@@ -59,7 +59,7 @@ class CorrectOpeningBalanceCurrenciesTest extends TestCase
public function testHandleBroken(): void public function testHandleBroken(): void
{ {
// create opening balance journal for test. Is enough to trigger this test. // create opening balance journal for test. Is enough to trigger this test.
factory(TransactionJournal::class)->state(TransactionType::OPENING_BALANCE)->create(); TransactionJournal::factory()->openingBalance()->create();
// run command // run command
$this->artisan('firefly-iii:fix-ob-currencies') $this->artisan('firefly-iii:fix-ob-currencies')
@@ -74,7 +74,7 @@ class CorrectOpeningBalanceCurrenciesTest extends TestCase
{ {
Log::debug('Now in testHandleNoAccount'); Log::debug('Now in testHandleNoAccount');
// create opening balance journal for test. Is enough to trigger this test. // create opening balance journal for test. Is enough to trigger this test.
$journal = factory(TransactionJournal::class)->state('ob_broken')->create(); $journal = TransactionJournal::factory()->brokenOpeningBalance()->create();
// run command // run command
$this->artisan('firefly-iii:fix-ob-currencies') $this->artisan('firefly-iii:fix-ob-currencies')

View File

@@ -26,11 +26,6 @@ namespace Tests\Feature\Console\Commands\Correction;
use FireflyIII\Models\Preference; use FireflyIII\Models\Preference;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Mockery;
use Preferences;
use Tests\TestCase; use Tests\TestCase;
/** /**

View File

@@ -24,7 +24,7 @@ declare(strict_types=1);
namespace Tests\Feature\Console\Commands\Correction; namespace Tests\Feature\Console\Commands\Correction;
use FireflyIII\Factory\AccountFactory; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
@@ -87,7 +87,6 @@ class FixAccountTypesTest extends TestCase
*/ */
public function testHandleWithdrawalLoanLoan(): void public function testHandleWithdrawalLoanLoan(): void
{ {
$this->mock(AccountFactory::class);
$source = $this->getRandomLoan(); $source = $this->getRandomLoan();
$destination = $this->getRandomLoan($source->id); $destination = $this->getRandomLoan($source->id);
$type = TransactionType::where('type', TransactionType::WITHDRAWAL)->first(); $type = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
@@ -134,7 +133,6 @@ class FixAccountTypesTest extends TestCase
*/ */
public function testHandleTransferAssetLoan(): void public function testHandleTransferAssetLoan(): void
{ {
$this->mock(AccountFactory::class);
$source = $this->getRandomAsset(); $source = $this->getRandomAsset();
$destination = $this->getRandomLoan(); $destination = $this->getRandomLoan();
$type = TransactionType::where('type', TransactionType::TRANSFER)->first(); $type = TransactionType::where('type', TransactionType::TRANSFER)->first();
@@ -183,7 +181,6 @@ class FixAccountTypesTest extends TestCase
*/ */
public function testHandleTransferLoanAsset(): void public function testHandleTransferLoanAsset(): void
{ {
$this->mock(AccountFactory::class);
$source = $this->getRandomLoan(); $source = $this->getRandomLoan();
$destination = $this->getRandomAsset(); $destination = $this->getRandomAsset();
$type = TransactionType::where('type', TransactionType::TRANSFER)->first(); $type = TransactionType::where('type', TransactionType::TRANSFER)->first();
@@ -233,8 +230,7 @@ class FixAccountTypesTest extends TestCase
public function testHandleWithdrawalAssetRevenue(): void public function testHandleWithdrawalAssetRevenue(): void
{ {
$source = $this->getRandomAsset(); $source = $this->getRandomAsset();
$destination = $this->getRandomRevenue(); $destination = $this->getRandomRevenue(); // is revenue account.
$newDestination = $this->getRandomExpense();
$withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first(); $withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
$journal = TransactionJournal::create( $journal = TransactionJournal::create(
[ [
@@ -256,29 +252,35 @@ class FixAccountTypesTest extends TestCase
$two = Transaction::create( $two = Transaction::create(
[ [
'transaction_journal_id' => $journal->id, 'transaction_journal_id' => $journal->id,
'account_id' => $destination->id, 'account_id' => $destination->id, // revenue cannot be destination.
'amount' => '10', 'amount' => '10',
] ]
); );
// create expense account with the same name:
$expense = AccountType::where('type', AccountType::EXPENSE)->first();
$newDestination = Account::create(
[
'name' => $destination->name,
'account_type_id' => $expense->id,
'user_id' => 1,
]
);
// asset we find bad destination.
$this->assertCount(0, Transaction::where('id', $two->id)->where('account_id', $newDestination->id)->get()); $this->assertCount(0, Transaction::where('id', $two->id)->where('account_id', $newDestination->id)->get());
$this->assertCount(1, Transaction::where('id', $two->id)->where('account_id', $destination->id)->get()); $this->assertCount(1, Transaction::where('id', $two->id)->where('account_id', $destination->id)->get());
// mock stuff
$factory = $this->mock(AccountFactory::class);
$factory->shouldReceive('setUser')->atLeast()->once();
$factory->shouldReceive('findOrCreate')
->withArgs([$destination->name, AccountType::EXPENSE])
->atLeast()->once()->andReturn($newDestination);
// Transaction journal #137, destination account changed from #1 ("Checking Account") to #29 ("Land lord"). // Transaction journal #137, destination account changed from #1 ("Checking Account") to #29 ("Land lord").
$this->artisan('firefly-iii:fix-account-types') $this->artisan('firefly-iii:fix-account-types')
->expectsOutput( ->expectsOutput(
sprintf('Transaction journal #%d, destination account changed from #%d ("%s") to #%d ("%s").', sprintf(
'Transaction journal #%d, destination account changed from #%d ("%s") to #%d ("%s").',
$journal->id, $journal->id,
$destination->id, $destination->name, $destination->id, $destination->name,
$newDestination->id, $newDestination->name $newDestination->id, $newDestination->name
)) )
)
->expectsOutput('Acted on 1 transaction(s)!') ->expectsOutput('Acted on 1 transaction(s)!')
->assertExitCode(0); ->assertExitCode(0);
@@ -296,8 +298,8 @@ class FixAccountTypesTest extends TestCase
*/ */
public function testHandleDepositAssetExpense(): void public function testHandleDepositAssetExpense(): void
{ {
$source = $this->getRandomExpense(); $source = $this->getRandomExpense(); // expense account
$newSource = $this->getRandomRevenue(); //$newSource = $this->getRandomRevenue();
$destination = $this->getRandomAsset(); $destination = $this->getRandomAsset();
$deposit = TransactionType::where('type', TransactionType::DEPOSIT)->first(); $deposit = TransactionType::where('type', TransactionType::DEPOSIT)->first();
@@ -314,7 +316,7 @@ class FixAccountTypesTest extends TestCase
$one = Transaction::create( $one = Transaction::create(
[ [
'transaction_journal_id' => $journal->id, 'transaction_journal_id' => $journal->id,
'account_id' => $source->id, 'account_id' => $source->id, // expense account cannot be source.
'amount' => '-10', 'amount' => '-10',
] ]
); );
@@ -325,25 +327,30 @@ class FixAccountTypesTest extends TestCase
'amount' => '10', 'amount' => '10',
] ]
); );
// create revenue account with the same name:
$revenue = AccountType::where('type', AccountType::REVENUE)->first();
$newSource = Account::create(
[
'name' => $source->name,
'account_type_id' => $revenue->id,
'user_id' => 1,
]
);
$this->assertCount(0, Transaction::where('id', $one->id)->where('account_id', $newSource->id)->get()); $this->assertCount(0, Transaction::where('id', $one->id)->where('account_id', $newSource->id)->get());
$this->assertCount(1, Transaction::where('id', $one->id)->where('account_id', $source->id)->get()); $this->assertCount(1, Transaction::where('id', $one->id)->where('account_id', $source->id)->get());
// mock stuff
$factory = $this->mock(AccountFactory::class);
$factory->shouldReceive('setUser')->atLeast()->once();
$factory->shouldReceive('findOrCreate')
->withArgs([$source->name, AccountType::REVENUE])
->atLeast()->once()->andReturn($newSource);
// Transaction journal #137, destination account changed from #1 ("Checking Account") to #29 ("Land lord"). // Transaction journal #137, destination account changed from #1 ("Checking Account") to #29 ("Land lord").
$this->artisan('firefly-iii:fix-account-types') $this->artisan('firefly-iii:fix-account-types')
->expectsOutput( ->expectsOutput(
sprintf('Transaction journal #%d, source account changed from #%d ("%s") to #%d ("%s").', sprintf(
'Transaction journal #%d, source account changed from #%d ("%s") to #%d ("%s").',
$journal->id, $journal->id,
$destination->id, $destination->name, $destination->id, $destination->name,
$newSource->id, $newSource->name $newSource->id, $newSource->name
)) )
)
->expectsOutput('Acted on 1 transaction(s)!') ->expectsOutput('Acted on 1 transaction(s)!')
->assertExitCode(0); ->assertExitCode(0);

View File

@@ -34,7 +34,9 @@ class FixTransactionTypesTest extends TestCase
*/ */
public function testHandle(): void public function testHandle(): void
{ {
$this->markTestIncomplete(); $this->artisan('firefly-iii:fix-transaction-types')
//->expectsOutput()
->assertExitCode(0);
} }

View File

@@ -23,18 +23,17 @@ declare(strict_types=1);
namespace Tests; namespace Tests;
use FireflyIII\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Tests\Traits\CollectsValues; use Tests\Traits\CollectsValues;
use Tests\Traits\MocksDefaultValues;
use Tests\Traits\TestHelpers;
/** /**
* Class TestCase * Class TestCase
*/ */
abstract class TestCase extends BaseTestCase abstract class TestCase extends BaseTestCase
{ {
use CreatesApplication, CollectsValues; // MocksDefaultValues TestHelpers use CreatesApplication, CollectsValues;
// MocksDefaultValues TestHelpers
/** /**
* @return array * @return array