Skip to content

Firefly III: a personal finances manager

Expand tests

James Cole413df5a005ef
diff --git a/tests/Feature/Console/Commands/Correction/CorrectOpeningBalanceCurrenciesTest.php b/tests/Feature/Console/Commands/Correction/CorrectOpeningBalanceCurrenciesTest.php
index 64ad12dc22..b69e691ec7 100644
--- a/tests/Feature/Console/Commands/Correction/CorrectOpeningBalanceCurrenciesTest.php
+++ b/tests/Feature/Console/Commands/Correction/CorrectOpeningBalanceCurrenciesTest.php
@@ -23,12 +23,12 @@ namespace Tests\Feature\Console\Commands\Correction;
use FireflyIII\Models\TransactionJournal;
-use FireflyIII\Models\TransactionType;
use Log;
use Tests\TestCase;
/**
* Class CorrectOpeningBalanceCurrenciesTest
+ *
* @package Tests\Feature\Console\Commands\Correction
*/
class CorrectOpeningBalanceCurrenciesTest extends TestCase
@@ -59,7 +59,7 @@ class CorrectOpeningBalanceCurrenciesTest extends TestCase
public function testHandleBroken(): void
{
// 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
$this->artisan('firefly-iii:fix-ob-currencies')
@@ -74,11 +74,11 @@ class CorrectOpeningBalanceCurrenciesTest extends TestCase
{
Log::debug('Now in testHandleNoAccount');
// 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
$this->artisan('firefly-iii:fix-ob-currencies')
- ->expectsOutput(sprintf('Transaction journal #%d has no valid account. Cant fix this line.', $journal->id))
+ ->expectsOutput(sprintf('Transaction journal #%d has no valid account. Cant fix this line.', $journal->id))
//->expectsOutput('Cant fix this line.')
->assertExitCode(0);
diff --git a/tests/Feature/Console/Commands/Correction/CreateAccessTokensTest.php b/tests/Feature/Console/Commands/Correction/CreateAccessTokensTest.php
index f8cbb8ecd0..63b336279b 100644
--- a/tests/Feature/Console/Commands/Correction/CreateAccessTokensTest.php
+++ b/tests/Feature/Console/Commands/Correction/CreateAccessTokensTest.php
@@ -26,11 +26,6 @@ namespace Tests\Feature\Console\Commands\Correction;
use FireflyIII\Models\Preference;
-use FireflyIII\Repositories\User\UserRepositoryInterface;
-use Illuminate\Support\Collection;
-use Log;
-use Mockery;
-use Preferences;
use Tests\TestCase;
/**
@@ -44,7 +39,7 @@ class CreateAccessTokensTest extends TestCase
public function testHandle(): void
{
// remove preferences so token will be generated
- Preference::where('name','access_token')->delete();
+ Preference::where('name', 'access_token')->delete();
$this->artisan('firefly-iii:create-access-tokens')
->expectsOutput(sprintf('Generated access token for user %s', $this->user()->email))
@@ -56,9 +51,9 @@ class CreateAccessTokensTest extends TestCase
*/
public function testHandlePrefExists(): void
{
- $preference = new Preference;
- $preference->data = '123';
- $preference->name = 'access_token';
+ $preference = new Preference;
+ $preference->data = '123';
+ $preference->name = 'access_token';
$preference->user_id = $this->user()->id;
$preference->save();
diff --git a/tests/Feature/Console/Commands/Correction/FixAccountTypesTest.php b/tests/Feature/Console/Commands/Correction/FixAccountTypesTest.php
index b31196a4f6..d825bddb49 100644
--- a/tests/Feature/Console/Commands/Correction/FixAccountTypesTest.php
+++ b/tests/Feature/Console/Commands/Correction/FixAccountTypesTest.php
@@ -24,7 +24,7 @@ declare(strict_types=1);
namespace Tests\Feature\Console\Commands\Correction;
-use FireflyIII\Factory\AccountFactory;
+use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
@@ -87,7 +87,6 @@ class FixAccountTypesTest extends TestCase
*/
public function testHandleWithdrawalLoanLoan(): void
{
- $this->mock(AccountFactory::class);
$source = $this->getRandomLoan();
$destination = $this->getRandomLoan($source->id);
$type = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
@@ -134,7 +133,6 @@ class FixAccountTypesTest extends TestCase
*/
public function testHandleTransferAssetLoan(): void
{
- $this->mock(AccountFactory::class);
$source = $this->getRandomAsset();
$destination = $this->getRandomLoan();
$type = TransactionType::where('type', TransactionType::TRANSFER)->first();
@@ -183,7 +181,6 @@ class FixAccountTypesTest extends TestCase
*/
public function testHandleTransferLoanAsset(): void
{
- $this->mock(AccountFactory::class);
$source = $this->getRandomLoan();
$destination = $this->getRandomAsset();
$type = TransactionType::where('type', TransactionType::TRANSFER)->first();
@@ -232,11 +229,10 @@ class FixAccountTypesTest extends TestCase
*/
public function testHandleWithdrawalAssetRevenue(): void
{
- $source = $this->getRandomAsset();
- $destination = $this->getRandomRevenue();
- $newDestination = $this->getRandomExpense();
- $withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
- $journal = TransactionJournal::create(
+ $source = $this->getRandomAsset();
+ $destination = $this->getRandomRevenue(); // is revenue account.
+ $withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
+ $journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
@@ -246,39 +242,45 @@ class FixAccountTypesTest extends TestCase
'date' => '2019-01-01',
]
);
- $one = Transaction::create(
+ $one = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $source->id,
'amount' => '-10',
]
);
- $two = Transaction::create(
+ $two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
- 'account_id' => $destination->id,
+ 'account_id' => $destination->id, // revenue cannot be destination.
'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(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").
$this->artisan('firefly-iii:fix-account-types')
->expectsOutput(
- sprintf('Transaction journal #%d, destination account changed from #%d ("%s") to #%d ("%s").',
- $journal->id,
- $destination->id, $destination->name,
- $newDestination->id, $newDestination->name
- ))
+ sprintf(
+ 'Transaction journal #%d, destination account changed from #%d ("%s") to #%d ("%s").',
+ $journal->id,
+ $destination->id, $destination->name,
+ $newDestination->id, $newDestination->name
+ )
+ )
->expectsOutput('Acted on 1 transaction(s)!')
->assertExitCode(0);
@@ -296,12 +298,12 @@ class FixAccountTypesTest extends TestCase
*/
public function testHandleDepositAssetExpense(): void
{
- $source = $this->getRandomExpense();
- $newSource = $this->getRandomRevenue();
+ $source = $this->getRandomExpense(); // expense account
+ //$newSource = $this->getRandomRevenue();
$destination = $this->getRandomAsset();
- $deposit = TransactionType::where('type', TransactionType::DEPOSIT)->first();
- $journal = TransactionJournal::create(
+ $deposit = TransactionType::where('type', TransactionType::DEPOSIT)->first();
+ $journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
@@ -311,39 +313,44 @@ class FixAccountTypesTest extends TestCase
'date' => '2019-01-01',
]
);
- $one = Transaction::create(
+ $one = Transaction::create(
[
'transaction_journal_id' => $journal->id,
- 'account_id' => $source->id,
+ 'account_id' => $source->id, // expense account cannot be source.
'amount' => '-10',
]
);
- $two = Transaction::create(
+ $two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $destination->id,
'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(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").
$this->artisan('firefly-iii:fix-account-types')
->expectsOutput(
- sprintf('Transaction journal #%d, source account changed from #%d ("%s") to #%d ("%s").',
- $journal->id,
- $destination->id, $destination->name,
- $newSource->id, $newSource->name
- ))
+ sprintf(
+ 'Transaction journal #%d, source account changed from #%d ("%s") to #%d ("%s").',
+ $journal->id,
+ $destination->id, $destination->name,
+ $newSource->id, $newSource->name
+ )
+ )
->expectsOutput('Acted on 1 transaction(s)!')
->assertExitCode(0);
diff --git a/tests/Feature/Console/Commands/Correction/FixTransactionTypesTest.php b/tests/Feature/Console/Commands/Correction/FixTransactionTypesTest.php
index 043be055fa..3b6877cd7d 100644
--- a/tests/Feature/Console/Commands/Correction/FixTransactionTypesTest.php
+++ b/tests/Feature/Console/Commands/Correction/FixTransactionTypesTest.php
@@ -34,7 +34,9 @@ class FixTransactionTypesTest extends TestCase
*/
public function testHandle(): void
{
- $this->markTestIncomplete();
+ $this->artisan('firefly-iii:fix-transaction-types')
+ //->expectsOutput()
+ ->assertExitCode(0);
}
diff --git a/tests/TestCase.php b/tests/TestCase.php
index f292d1091f..fec7ab7732 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -23,18 +23,17 @@ declare(strict_types=1);
namespace Tests;
-use FireflyIII\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Tests\Traits\CollectsValues;
-use Tests\Traits\MocksDefaultValues;
-use Tests\Traits\TestHelpers;
/**
* Class TestCase
*/
abstract class TestCase extends BaseTestCase
{
- use CreatesApplication, CollectsValues; // MocksDefaultValues TestHelpers
+ use CreatesApplication, CollectsValues;
+
+ // MocksDefaultValues TestHelpers
/**
* @return array