Improve test coverage.

This commit is contained in:
James Cole
2019-06-10 20:14:00 +02:00
parent 8efb73694d
commit 2ab9d2e6ee
75 changed files with 4672 additions and 484 deletions

View File

@@ -0,0 +1,96 @@
<?php
/**
* CorrectDatabaseTest.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\Correction;
use FireflyIII\Models\Preference;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Mockery;
use Preferences;
use Tests\TestCase;
/**
* Class CreateAccessTokensTest
*/
class CreateAccessTokensTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\CreateAccessTokens
*/
public function testHandle(): void
{
$users = new Collection([$this->user()]);
$repository = $this->mock(UserRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('all')->atLeast()->once()->andReturn($users);
// mock preferences thing:
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token', null])
->once()->andReturn(null);
// null means user object will generate one and store it.
Preferences::shouldReceive('setForUser')->withArgs([Mockery::any(), 'access_token', Mockery::any()])
->once();
$this->artisan('firefly-iii:create-access-tokens')
->expectsOutput(sprintf('Generated access token for user %s', $this->user()->email))
->assertExitCode(0);
}
/**
* @covers \FireflyIII\Console\Commands\Correction\CreateAccessTokens
*/
public function testHandlePrefExists(): void
{
$users = new Collection([$this->user()]);
$repository = $this->mock(UserRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('all')->atLeast()->once()->andReturn($users);
// mock preferences thing:
$preference = new Preference;
$preference->data = '123';
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token', null])
->once()->andReturn($preference);
// null means user object will generate one and store it.
Preferences::shouldNotReceive('setForUser');
$this->artisan('firefly-iii:create-access-tokens')
->expectsOutput('All access tokens OK!')
->assertExitCode(0);
}
}

View File

@@ -0,0 +1,78 @@
<?php
/**
* CreateLinkTypesTest.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\Correction;
use FireflyIII\Models\LinkType;
use Log;
use Tests\TestCase;
/**
* Class CreateLinkTypesTest
*/
class CreateLinkTypesTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\CreateLinkTypes
*/
public function testHandle(): void
{
// delete all other link types:
LinkType::whereNotIn('name', ['Related', 'Refund', 'Paid', 'Reimbursement'])->forceDelete();
// delete link type:
LinkType::where('name', 'Reimbursement')->forceDelete();
$this->assertCount(3, LinkType::get());
// run command, expect output:
$this->artisan('firefly-iii:create-link-types')
->expectsOutput('Created missing link type "Reimbursement"')
->assertExitCode(0);
$this->assertCount(4, LinkType::get());
}
/**
* @covers \FireflyIII\Console\Commands\Correction\CreateLinkTypes
*/
public function testHandleNothing(): void
{
$this->assertCount(4, LinkType::get());
// run command, expect output:
$this->artisan('firefly-iii:create-link-types')
->expectsOutput('All link types OK!')
->assertExitCode(0);
$this->assertCount(4, LinkType::get());
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* DeleteEmptyGroupsTest.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\Correction;
use FireflyIII\Models\TransactionGroup;
use Log;
use Tests\TestCase;
/**
* Class DeleteEmptyGroupsTest
*/
class DeleteEmptyGroupsTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\DeleteEmptyGroups
*/
public function testHandle(): void
{
// assume there are no empty groups..
$this->artisan('firefly-iii:delete-empty-groups')
->expectsOutput('No empty transaction groups.')
->assertExitCode(0);
}
/**
* @covers \FireflyIII\Console\Commands\Correction\DeleteEmptyGroups
*/
public function testHandleWithGroup(): void
{
// create new group:
$group = TransactionGroup::create(['user_id' => 1]);
// command should delete it.
$this->artisan('firefly-iii:delete-empty-groups')
->expectsOutput('Deleted 1 empty transaction group(s).')
->assertExitCode(0);
// should not be able to find it:
$this->assertCount(0, TransactionGroup::where('id', $group->id)->whereNull('deleted_at')->get());
}
}

View File

@@ -0,0 +1,116 @@
<?php
/**
* DeleteEmptyJournalsTest.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\Correction;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Log;
use Tests\TestCase;
class DeleteEmptyJournalsTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\DeleteEmptyJournals
*/
public function testHandle(): void
{
// assume there are no empty journals or uneven journals
$this->artisan('firefly-iii:delete-empty-journals')
->expectsOutput('No uneven transaction journals.')
->expectsOutput('No empty transaction journals.')
->assertExitCode(0);
}
/**
* @covers \FireflyIII\Console\Commands\Correction\DeleteEmptyJournals
*/
public function testHandleEmptyJournals(): void
{
// create empty journal:
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => 1,
'description' => 'Hello',
'tag_count' => 0,
'date' => '2019-01-01',
]
);
$this->artisan('firefly-iii:delete-empty-journals')
->expectsOutput('No uneven transaction journals.')
->expectsOutput(sprintf('Deleted empty transaction journal #%d', $journal->id))
->assertExitCode(0);
// verify its indeed gone
$this->assertCount(0, TransactionJournal::where('id', $journal->id)->whereNull('deleted_at')->get());
}
/**
* @covers \FireflyIII\Console\Commands\Correction\DeleteEmptyJournals
*/
public function testHandleUnevenJournals(): void
{
// create empty journal:
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => 1,
'description' => 'Hello',
'tag_count' => 0,
'date' => '2019-01-01',
]
);
// link empty transaction
$transaction = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => 1,
'amount' => '5',
]
);
$this->artisan('firefly-iii:delete-empty-journals')
->expectsOutput(sprintf('Deleted transaction journal #%d because it had an uneven number of transactions.', $journal->id))
->expectsOutput('No empty transaction journals.')
->assertExitCode(0);
// verify both are gone
$this->assertCount(0, TransactionJournal::where('id', $journal->id)->whereNull('deleted_at')->get());
$this->assertCount(0, Transaction::where('id', $transaction->id)->whereNull('deleted_at')->get());
}
}

View File

@@ -0,0 +1,144 @@
<?php
/**
* DeleteOrphanedTransactionsTest.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\Correction;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Log;
use Tests\TestCase;
/**
* Class DeleteOrphanedTransactionsTest
*/
class DeleteOrphanedTransactionsTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\DeleteOrphanedTransactions
*/
public function testHandle(): void
{
// assume there are no orphaned transactions.
$this->artisan('firefly-iii:delete-orphaned-transactions')
->expectsOutput('No orphaned transactions.')
->expectsOutput('No orphaned accounts.')
->assertExitCode(0);
}
/**
*
*/
public function testHandleOrphanedAccounts(): void
{
// create deleted account:
$account = Account::create(
[
'user_id' => 1,
'name' => 'Some account',
'account_type_id' => 1,
]
);
$account->delete();
// create NOT deleted journal + transaction.
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => 1,
'description' => 'Hello',
'tag_count' => 0,
'date' => '2019-01-01',
]
);
$transaction = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $account->id,
'amount' => '5',
]
);
$this->artisan('firefly-iii:delete-orphaned-transactions')
->expectsOutput('No orphaned transactions.')
->expectsOutput(sprintf('Deleted transaction journal #%d because account #%d was already deleted.',
$journal->id, $account->id))
->assertExitCode(0);
// verify bad objects are gone.
$this->assertCount(0, Transaction::where('id', $transaction->id)->whereNull('deleted_at')->get());
$this->assertCount(0, TransactionJournal::where('id', $journal->id)->whereNull('deleted_at')->get());
$this->assertCount(0, Account::where('id', $account->id)->whereNull('deleted_at')->get());
}
/**
* @covers \FireflyIII\Console\Commands\Correction\DeleteOrphanedTransactions
*/
public function testHandleOrphanedTransactions(): void
{
// create deleted journal:
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => 1,
'description' => 'Hello',
'tag_count' => 0,
'date' => '2019-01-01',
]
);
$journal->delete();
// create NOT deleted transaction.
$transaction = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => 1,
'amount' => '5',
]
);
$this->artisan('firefly-iii:delete-orphaned-transactions')
->expectsOutput(sprintf('Transaction #%d (part of deleted transaction journal #%d) has been deleted as well.',
$transaction->id, $journal->id))
->expectsOutput('No orphaned accounts.')
->assertExitCode(0);
// verify objects are gone.
$this->assertCount(0, TransactionJournal::where('id', $journal->id)->whereNull('deleted_at')->get());
$this->assertCount(0, Transaction::where('id', $transaction->id)->whereNull('deleted_at')->get());
}
}

View File

@@ -0,0 +1,91 @@
<?php
/**
* DeleteZeroAmountTest.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\Correction;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Log;
use Tests\TestCase;
/**
* Class DeleteZeroAmountTest
*/
class DeleteZeroAmountTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\DeleteZeroAmount
*/
public function testHandle(): void
{
// assume there are no transactions with a zero amount.
$this->artisan('firefly-iii:delete-zero-amount')
->expectsOutput('No zero-amount transaction journals.')
->assertExitCode(0);
}
/**
* @covers \FireflyIII\Console\Commands\Correction\DeleteZeroAmount
*/
public function testHandleTransactions(): void
{
$account = $this->getRandomAsset();
// create NOT deleted journal + transaction.
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => 1,
'description' => 'Hello',
'tag_count' => 0,
'date' => '2019-01-01',
]
);
$transaction = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $account->id,
'amount' => '0',
]
);
// assume there are no transactions with a zero amount.
$this->artisan('firefly-iii:delete-zero-amount')
->expectsOutput(sprintf('Deleted transaction journal #%d because the amount is zero (0.00).', $journal->id))
->assertExitCode(0);
// verify objects are gone.
$this->assertCount(0, Transaction::where('id', $transaction->id)->whereNull('deleted_at')->get());
$this->assertCount(0, TransactionJournal::where('id', $journal->id)->whereNull('deleted_at')->get());
}
}

View File

@@ -0,0 +1,82 @@
<?php
/**
* EnableCurrenciesTest.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\Correction;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionCurrency;
use Log;
use Tests\TestCase;
/**
* Class EnableCurrenciesTest
*/
class EnableCurrenciesTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\EnableCurrencies
*/
public function testHandle(): void
{
// assume the current database is intact.
$count = TransactionCurrency::where('enabled', 1)->count();
$this->artisan('firefly-iii:enable-currencies')
->expectsOutput('All currencies are correctly enabled or disabled.')
->assertExitCode(0);
$this->assertCount($count, TransactionCurrency::where('enabled', 1)->get());
}
/**
* @covers \FireflyIII\Console\Commands\Correction\EnableCurrencies
*/
public function testHandleDisabled(): void
{
// find a disabled currency, update a budget limit with it.
$currency = TransactionCurrency::where('enabled', 0)->first();
/** @var BudgetLimit $budgetLimit */
$budgetLimit = BudgetLimit::inRandomOrder()->first();
$budgetLimit->transaction_currency_id = $currency->id;
$budgetLimit->save();
// assume the current database is intact.
$count = TransactionCurrency::where('enabled', 1)->count();
$this->artisan('firefly-iii:enable-currencies')
->expectsOutput(sprintf('%d were (was) still disabled. This has been corrected.', 1))
->assertExitCode(0);
// assume its been enabled.
$this->assertCount($count + 1, TransactionCurrency::where('enabled', 1)->get());
}
}

View File

@@ -0,0 +1,371 @@
<?php
/**
* FixAccountTypesTest.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\Correction;
use FireflyIII\Factory\AccountFactory;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Log;
use Tests\TestCase;
/**
* Class FixAccountTypesTest
*/
class FixAccountTypesTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\FixAccountTypes
*/
public function testHandleUneven(): void
{
$this->mock(AccountFactory::class);
$source = $this->user()->accounts()->where('name', 'Another DUO Student loans')->first();
$type = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
$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' => $source->id,
'amount' => '-10',
]
);
// assume there's nothing to fix.
$this->artisan('firefly-iii:fix-account-types')
->expectsOutput(sprintf('Cannot inspect transaction journal #%d because it has 1 transaction(s) instead of 2.', $journal->id))
->assertExitCode(0);
$one->forceDelete();
$journal->forceDelete();
}
/**
* @covers \FireflyIII\Console\Commands\Correction\FixAccountTypes
*/
public function testHandle(): void
{
$this->mock(AccountFactory::class);
// assume there's nothing to fix.
$this->artisan('firefly-iii:fix-account-types')
->expectsOutput('All account types are OK!')
->assertExitCode(0);
}
/**
* Try to fix a withdrawal that goes from a loan to another loan.
*
* @covers \FireflyIII\Console\Commands\Correction\FixAccountTypes
*/
public function testHandleWithdrawalLoanLoan(): void
{
$this->mock(AccountFactory::class);
$source = $this->user()->accounts()->where('name', 'Another DUO Student loans')->first();
$destination = $this->user()->accounts()->where('name', 'DUO Student loans')->first();
$type = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
$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' => $source->id,
'amount' => '-10',
]
);
$two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $destination->id,
'amount' => '10',
]
);
$this->artisan('firefly-iii:fix-account-types')
->expectsOutput(sprintf('The source account of %s #%d cannot be of type "%s".', $type->type, $journal->id, 'Loan'))
->expectsOutput(sprintf('The destination account of %s #%d cannot be of type "%s".', $type->type, $journal->id, 'Loan'))
->expectsOutput('Acted on 1 transaction(s)!')
->assertExitCode(0);
// since system cant handle this problem, dont look for changed transactions.
$one->forceDelete();
$two->forceDelete();
$journal->forceDelete();
}
/**
* Transferring from an asset to a loan should be a withdrawal, not a transfer
*/
public function testHandleTransferAssetLoan(): void
{
$this->mock(AccountFactory::class);
$source = $this->getRandomAsset();
$destination = $this->user()->accounts()->where('name', 'DUO Student loans')->first();
$type = TransactionType::where('type', TransactionType::TRANSFER)->first();
$withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
$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' => $source->id,
'amount' => '-10',
]
);
$two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $destination->id,
'amount' => '10',
]
);
$this->artisan('firefly-iii:fix-account-types')
->expectsOutput(sprintf('Converted transaction #%d from a transfer to a withdrawal.', $journal->id))
->expectsOutput('Acted on 1 transaction(s)!')
->assertExitCode(0);
// verify the change has been made.
$this->assertCount(1, TransactionJournal::where('id', $journal->id)->where('transaction_type_id', $withdrawal->id)->get());
$this->assertCount(0, TransactionJournal::where('id', $journal->id)->where('transaction_type_id', $type->id)->get());
$one->forceDelete();
$two->forceDelete();
$journal->forceDelete();
}
/**
* Transferring from a loan to an asset should be a deposit, not a transfer
*/
public function testHandleTransferLoanAsset(): void
{
$this->mock(AccountFactory::class);
$source = $this->user()->accounts()->where('name', 'DUO Student loans')->first();
$destination = $this->getRandomAsset();
$type = TransactionType::where('type', TransactionType::TRANSFER)->first();
$deposit = TransactionType::where('type', TransactionType::DEPOSIT)->first();
$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' => $source->id,
'amount' => '-10',
]
);
$two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $destination->id,
'amount' => '10',
]
);
$this->artisan('firefly-iii:fix-account-types')
->expectsOutput(sprintf('Converted transaction #%d from a transfer to a deposit.', $journal->id))
->expectsOutput('Acted on 1 transaction(s)!')
->assertExitCode(0);
// verify the change has been made.
$this->assertCount(1, TransactionJournal::where('id', $journal->id)->where('transaction_type_id', $deposit->id)->get());
$this->assertCount(0, TransactionJournal::where('id', $journal->id)->where('transaction_type_id', $type->id)->get());
$one->forceDelete();
$two->forceDelete();
$journal->forceDelete();
}
/**
* Withdrawal with a revenue account as a destination must be converted.
*/
public function testHandleWithdrawalAssetRevenue(): void
{
$source = $this->getRandomAsset();
$destination = $this->getRandomRevenue();
$newDestination = $this->getRandomExpense();
$withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => $withdrawal->id,
'description' => 'Test',
'tag_count' => 0,
'date' => '2019-01-01',
]
);
$one = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $source->id,
'amount' => '-10',
]
);
$two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $destination->id,
'amount' => '10',
]
);
$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
))
->expectsOutput('Acted on 1 transaction(s)!')
->assertExitCode(0);
// verify the change has been made
$this->assertCount(1, Transaction::where('id', $two->id)->where('account_id', $newDestination->id)->get());
$this->assertCount(0, Transaction::where('id', $two->id)->where('account_id', $destination->id)->get());
$one->forceDelete();
$two->forceDelete();
$journal->forceDelete();
}
/**
* Deposit with an expense account as a source instead of a revenue account must be converted.
*/
public function testHandleDepositAssetExpense(): void
{
$source = $this->getRandomExpense();
$newSource = $this->getRandomRevenue();
$destination = $this->getRandomAsset();
$withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
$deposit = TransactionType::where('type', TransactionType::DEPOSIT)->first();
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => $deposit->id,
'description' => 'Test',
'tag_count' => 0,
'date' => '2019-01-01',
]
);
$one = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $source->id,
'amount' => '-10',
]
);
$two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $destination->id,
'amount' => '10',
]
);
$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
))
->expectsOutput('Acted on 1 transaction(s)!')
->assertExitCode(0);
$this->assertCount(1, Transaction::where('id', $one->id)->where('account_id', $newSource->id)->get());
$this->assertCount(0, Transaction::where('id', $one->id)->where('account_id', $source->id)->get());
$one->forceDelete();
$two->forceDelete();
$journal->forceDelete();
}
}

View File

@@ -0,0 +1,130 @@
<?php
/**
* FixPiggiesTest.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\Correction;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
use Log;
use Tests\TestCase;
/**
* Class FixPiggiesTest
*/
class FixPiggiesTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* Null event.
*
* @covers \FireflyIII\Console\Commands\Correction\FixPiggies
*/
public function testHandleNull(): void
{
/** @var PiggyBank $piggy */
$piggy = $this->user()->piggyBanks()->inRandomOrder()->first();
// create event to trigger console commands.
$event = PiggyBankEvent::create(
[
'piggy_bank_id' => $piggy->id,
'date' => '2019-01-01',
'amount' => 5,
]
);
// assume there's nothing to fix.
$this->artisan('firefly-iii:fix-piggies')
->expectsOutput('All piggy bank events are correct.')
->assertExitCode(0);
$event->forceDelete();
}
/**
* Withdrawal instead of transfer
*
* @covers \FireflyIII\Console\Commands\Correction\FixPiggies
*/
public function testHandleBadJournal(): void
{
/** @var PiggyBank $piggy */
$piggy = $this->user()->piggyBanks()->inRandomOrder()->first();
$withdrawal = $this->getRandomWithdrawal();
// create event to trigger console commands.
$event = PiggyBankEvent::create(
[
'piggy_bank_id' => $piggy->id,
'date' => '2019-01-01',
'amount' => 5,
'transaction_journal_id' => $withdrawal->id,
]
);
// assume there's nothing to fix.
$this->artisan('firefly-iii:fix-piggies')
->expectsOutput(sprintf('Piggy bank #%d was referenced by an invalid event. This has been fixed.', $piggy->id))
->expectsOutput('Fixed 1 piggy bank event(s).')
->assertExitCode(0);
// verify update
$this->assertCount(0, PiggyBankEvent::where('id', $event->id)->where('transaction_journal_id', $withdrawal->id)->get());
}
/**
* Withdrawal instead of transfer
*
* @covers \FireflyIII\Console\Commands\Correction\FixPiggies
*/
public function testHandleDeletedJournal(): void
{
/** @var PiggyBank $piggy */
$piggy = $this->user()->piggyBanks()->inRandomOrder()->first();
$transfer = $this->getRandomTransfer();
$event = PiggyBankEvent::create(
[
'piggy_bank_id' => $piggy->id,
'date' => '2019-01-01',
'amount' => 5,
'transaction_journal_id' => $transfer->id,
]
);
$transfer->deleted_at = '2019-01-01 12:00:00';
$transfer->save();
$transfer->refresh();
$this->artisan('firefly-iii:fix-piggies')
->expectsOutput('Fixed 1 piggy bank event(s).')
->assertExitCode(0);
// verify update
$this->assertCount(0, PiggyBankEvent::where('id', $event->id)->where('transaction_journal_id', $transfer->id)->get());
$event->forceDelete();
}
}

View File

@@ -0,0 +1,101 @@
<?php
/**
* FixUnevenAmountTest.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\Correction;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Log;
use Tests\TestCase;
/**
* Class FixUnevenAmountTest
*/
class FixUnevenAmountTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\FixUnevenAmount
*/
public function testHandle(): void
{
// assume there's nothing to fix.
$this->artisan('firefly-iii:fix-uneven-amount')
->expectsOutput('Amount integrity OK!')
->assertExitCode(0);
// dont verify anything
}
/**
* Create uneven journal
* @covers \FireflyIII\Console\Commands\Correction\FixUnevenAmount
*/
public function testHandleUneven(): void
{
$asset = $this->getRandomAsset();
$expense = $this->getRandomExpense();
$withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
$journal = TransactionJournal::create(
[
'user_id' => 1,
'transaction_currency_id' => 1,
'transaction_type_id' => $withdrawal->id,
'description' => 'Test',
'tag_count' => 0,
'date' => '2019-01-01',
]
);
$one = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $asset->id,
'amount' => '-10',
]
);
$two = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $expense->id,
'amount' => '12',
]
);
$this->artisan('firefly-iii:fix-uneven-amount')
->expectsOutput(sprintf('Corrected amount in transaction journal #%d', $journal->id))
->assertExitCode(0);
// verify change.
$this->assertCount(1, Transaction::where('id', $one->id)->where('amount', '-10')->get());
$this->assertCount(1, Transaction::where('id', $two->id)->where('amount', '10')->get());
}
}

View File

@@ -0,0 +1,75 @@
<?php
/**
* RemoveBillsTest.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\Correction;
use FireflyIII\Models\TransactionJournal;
use Log;
use Tests\TestCase;
/**
* Class RemoveBillsTest
*/
class RemoveBillsTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\RemoveBills
*/
public function testHandle(): void
{
// assume there's nothing to fix.
$this->artisan('firefly-iii:remove-bills')
->expectsOutput('All transaction journals have correct bill information.')
->assertExitCode(0);
// dont verify anything
}
/**
* @covers \FireflyIII\Console\Commands\Correction\RemoveBills
*/
public function testHandleWithdrawal(): void
{
$bill = $this->user()->bills()->first();
$journal = $this->getRandomDeposit();
$journal->bill_id = $bill->id;
$journal->save();
$this->artisan('firefly-iii:remove-bills')
->expectsOutput(sprintf('Transaction journal #%d should not be linked to bill #%d.', $journal->id, $bill->id))
->assertExitCode(0);
// verify change
$this->assertCount(0, TransactionJournal::where('id', $journal->id)->whereNotNull('bill_id')->get());
}
}

View File

@@ -0,0 +1,76 @@
<?php
/**
* RenameMetaFieldsTest.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\Correction;
use FireflyIII\Models\TransactionJournalMeta;
use Log;
use Tests\TestCase;
/**
* Class RenameMetaFieldsTest
*/
class RenameMetaFieldsTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\RenameMetaFields
*/
public function testHandle(): void
{
$this->artisan('firefly-iii:rename-meta-fields')
->expectsOutput('All meta fields are correct.')
->assertExitCode(0);
}
/**
* @covers \FireflyIII\Console\Commands\Correction\RenameMetaFields
*/
public function testHandleFixed(): void
{
$withdrawal = $this->getRandomWithdrawal();
$entry = TransactionJournalMeta::create(
[
'transaction_journal_id' => $withdrawal->id,
'name' => 'importHashV2',
'data' => 'Fake data',
]
);
$this->artisan('firefly-iii:rename-meta-fields')
->expectsOutput('Renamed 1 meta field(s).')
->assertExitCode(0);
// verify update
$this->assertCount(1, TransactionJournalMeta::where('id', $entry->id)->where('name', 'import_hash_v2')->get());
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* TransferBudgetsTest.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\Correction;
use Log;
use Tests\TestCase;
/**
* Class TransferBudgetsTest
*/
class TransferBudgetsTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Correction\TransferBudgets
*/
public function testHandle(): void
{
$this->artisan('firefly-iii:fix-transfer-budgets')
->expectsOutput('No invalid budget/journal entries.')
->assertExitCode(0);
}
/**
* @covers \FireflyIII\Console\Commands\Correction\TransferBudgets
*/
public function testHandleBudget(): void
{
$deposit = $this->getRandomDeposit();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$deposit->budgets()->save($budget);
$this->artisan('firefly-iii:fix-transfer-budgets')
->expectsOutput(sprintf('Transaction journal #%d is a %s, so has no longer a budget.', $deposit->id, $deposit->transactionType->type))
->expectsOutput('Corrected 1 invalid budget/journal entries (entry).')
->assertExitCode(0);
// verify change
$this->assertCount(0, $deposit->budgets()->get());
}
}

View File

@@ -0,0 +1,140 @@
<?php
/**
* DecryptDatabaseTest.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;
use Crypt;
use FireflyConfig;
use FireflyIII\Models\Account;
use FireflyIII\Models\Configuration;
use Log;
use Mockery;
use Tests\TestCase;
/**
* Class DecryptDatabaseTest
*/
class DecryptDatabaseTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\DecryptDatabase
*/
public function testHandle(): void
{
// create encrypted account:
$name = 'Encrypted name';
$iban = 'HR1723600001101234565';
$account = Account::create(
[
'user_id' => 1,
'account_type_id' => 1,
'name' => Crypt::encrypt($name),
'iban' => Crypt::encrypt($iban),
]);
FireflyConfig::shouldReceive('get')->withArgs([Mockery::any(), false])->atLeast()->once()->andReturn(null);
FireflyConfig::shouldReceive('set')->withArgs([Mockery::any(), true])->atLeast()->once();
$this->artisan('firefly-iii:decrypt-all')
->expectsOutput('Done!')
->assertExitCode(0);
$this->assertCount(1, Account::where('id', $account->id)->where('name', $name)->get());
$this->assertCount(1, Account::where('id', $account->id)->where('iban', $iban)->get());
}
/**
* @covers \FireflyIII\Console\Commands\DecryptDatabase
*/
public function testHandleDecrypted(): void
{
// create encrypted account:
$name = 'Encrypted name';
$iban = 'HR1723600001101234565';
$encryptedName = Crypt::encrypt($name);
$encryptedIban = Crypt::encrypt($iban);
$account = Account::create(
[
'user_id' => 1,
'account_type_id' => 1,
'name' => $encryptedName,
'iban' => $encryptedIban,
]);
// pretend its not yet decrypted.
$true = new Configuration;
$true->data = true;
FireflyConfig::shouldReceive('get')->withArgs([Mockery::any(), false])->atLeast()->once()->andReturn($true);
$this->artisan('firefly-iii:decrypt-all')
->expectsOutput('Done!')
->assertExitCode(0);
$this->assertCount(1, Account::where('id', $account->id)->where('name', $encryptedName)->get());
$this->assertCount(1, Account::where('id', $account->id)->where('iban', $encryptedIban)->get());
}
/**
* Try to decrypt data that isn't actually encrypted.
*
* @covers \FireflyIII\Console\Commands\DecryptDatabase
*/
public function testHandleNotEncrypted(): void
{
// create encrypted account:
$name = 'Encrypted name';
$iban = 'HR1723600001101234565';
$account = Account::create(
[
'user_id' => 1,
'account_type_id' => 1,
'name' => $name,
'iban' => $iban,
]);
// pretend its not yet decrypted.
$true = new Configuration;
$true->data = true;
FireflyConfig::shouldReceive('get')->withArgs([Mockery::any(), false])->atLeast()->once()->andReturn($true);
$this->artisan('firefly-iii:decrypt-all')
->expectsOutput('Done!')
->assertExitCode(0);
$this->assertCount(1, Account::where('id', $account->id)->where('name', $name)->get());
$this->assertCount(1, Account::where('id', $account->id)->where('iban', $iban)->get());
}
}

View File

@@ -0,0 +1,433 @@
<?php
/**
* CreateCSVImportTest.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\Import;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Routine\FileRoutine;
use FireflyIII\Import\Storage\ImportArrayStorage;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\Preference;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\MessageBag;
use Log;
use Mockery;
use Preferences;
use Tests\TestCase;
/**
* Class CreateCSVImportTest
*/
class CreateCSVImportTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* Covers a default run with perfect arguments.
*
* @covers \FireflyIII\Console\Commands\Import\CreateCSVImport
*/
public function testHandle(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$jobRepos = $this->mock(ImportJobRepositoryInterface::class);
$fileRoutine = $this->mock(FileRoutine::class);
$storage = $this->mock(ImportArrayStorage::class);
$user = $this->user();
$token = new Preference;
$importJob = $this->user()->importJobs()->first();
$file = storage_path('build/test-upload.csv');
$config = storage_path('build/configuration.json');
// set preferences:
$token->data = 'token';
// mock calls to repository:
$userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($user);
$jobRepos->shouldReceive('setUser')->atLeast()->once();
$jobRepos->shouldReceive('create')->atLeast()->once()->andReturn($importJob);
$jobRepos->shouldReceive('storeCLIupload')->atLeast()->once()->andReturn(new MessageBag);
$jobRepos->shouldReceive('setConfiguration')->atLeast()->once();
// job is ready to run.
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'ready_to_run'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'provider_finished'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'storing_data'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'storage_finished'])->atLeast()->once();
// file routine gets called.
$fileRoutine->shouldReceive('setImportJob')->atLeast()->once();
$fileRoutine->shouldReceive('run')->atLeast()->once();
// store data thing gets called.
$storage->shouldReceive('setImportJob')->atLeast()->once();
$storage->shouldReceive('store')->atLeast()->once();
// mock Preferences.
Preferences::shouldReceive('setForUser')->atLeast()->once()->withArgs([Mockery::any(), 'lastActivity', Mockery::any()]);
Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'access_token', null])->andReturn($token);
$parameters = [
$file,
$config,
'--user=1',
'--token=token',
];
$this->artisan('firefly-iii:csv-import ' . implode(' ', $parameters))
->expectsOutput(sprintf('Import file : %s', $file))
->expectsOutput(sprintf('Configuration file : %s', $config))
->expectsOutput('User : #1 (thegrumpydictator@gmail.com)')
->expectsOutput(sprintf('Job : %s', $importJob->key))
->assertExitCode(0);
// this method imports nothing so there is nothing to verify.
}
/**
* Covers a default run with perfect arguments, but no import tag
*
* @covers \FireflyIII\Console\Commands\Import\CreateCSVImport
*/
public function testHandleNoTag(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$jobRepos = $this->mock(ImportJobRepositoryInterface::class);
$fileRoutine = $this->mock(FileRoutine::class);
$storage = $this->mock(ImportArrayStorage::class);
$user = $this->user();
$token = new Preference;
$importJob = ImportJob::create(
[
'key' => 'key-' . random_int(1, 100000),
'user_id' => 1,
'file_type' => 'csv',
'status' => 'new',
'errors' => [],
]
);
$file = storage_path('build/test-upload.csv');
$config = storage_path('build/configuration.json');
// set preferences:
$token->data = 'token';
// mock calls to repository:
$userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($user);
$jobRepos->shouldReceive('setUser')->atLeast()->once();
$jobRepos->shouldReceive('create')->atLeast()->once()->andReturn($importJob);
$jobRepos->shouldReceive('storeCLIupload')->atLeast()->once()->andReturn(new MessageBag);
$jobRepos->shouldReceive('setConfiguration')->atLeast()->once();
// job is ready to run.
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'ready_to_run'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'provider_finished'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'storing_data'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'storage_finished'])->atLeast()->once();
// file routine gets called.
$fileRoutine->shouldReceive('setImportJob')->atLeast()->once();
$fileRoutine->shouldReceive('run')->atLeast()->once();
// store data thing gets called.
$storage->shouldReceive('setImportJob')->atLeast()->once();
$storage->shouldReceive('store')->atLeast()->once();
// mock Preferences.
Preferences::shouldReceive('setForUser')->atLeast()->once()->withArgs([Mockery::any(), 'lastActivity', Mockery::any()]);
Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'access_token', null])->andReturn($token);
$parameters = [
$file,
$config,
'--user=1',
'--token=token',
];
$this->artisan('firefly-iii:csv-import ' . implode(' ', $parameters))
->expectsOutput(sprintf('Import file : %s', $file))
->expectsOutput(sprintf('Configuration file : %s', $config))
->expectsOutput('User : #1 (thegrumpydictator@gmail.com)')
->expectsOutput(sprintf('Job : %s', $importJob->key))
->expectsOutput('No transactions have been imported :(.')
->assertExitCode(0);
// this method imports nothing so there is nothing to verify.
}
/**
* Covers a default run with perfect arguments, but errors after importing.
*
* @covers \FireflyIII\Console\Commands\Import\CreateCSVImport
*/
public function testHandleErrors(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$jobRepos = $this->mock(ImportJobRepositoryInterface::class);
$fileRoutine = $this->mock(FileRoutine::class);
$storage = $this->mock(ImportArrayStorage::class);
$user = $this->user();
$token = new Preference;
$importJob = ImportJob::create(
[
'key' => 'key-' . random_int(1, 100000),
'user_id' => 1,
'file_type' => 'csv',
'status' => 'new',
'errors' => ['I am an error'],
]
);
$file = storage_path('build/test-upload.csv');
$config = storage_path('build/configuration.json');
// set preferences:
$token->data = 'token';
// mock calls to repository:
$userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($user);
$jobRepos->shouldReceive('setUser')->atLeast()->once();
$jobRepos->shouldReceive('create')->atLeast()->once()->andReturn($importJob);
$jobRepos->shouldReceive('storeCLIupload')->atLeast()->once()->andReturn(new MessageBag);
$jobRepos->shouldReceive('setConfiguration')->atLeast()->once();
// job is ready to run.
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'ready_to_run'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'provider_finished'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'storing_data'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'storage_finished'])->atLeast()->once();
// file routine gets called.
$fileRoutine->shouldReceive('setImportJob')->atLeast()->once();
$fileRoutine->shouldReceive('run')->atLeast()->once();
// store data thing gets called.
$storage->shouldReceive('setImportJob')->atLeast()->once();
$storage->shouldReceive('store')->atLeast()->once();
// mock Preferences.
Preferences::shouldReceive('setForUser')->atLeast()->once()->withArgs([Mockery::any(), 'lastActivity', Mockery::any()]);
Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'access_token', null])->andReturn($token);
$parameters = [
$file,
$config,
'--user=1',
'--token=token',
];
$this->artisan('firefly-iii:csv-import ' . implode(' ', $parameters))
->expectsOutput(sprintf('Import file : %s', $file))
->expectsOutput(sprintf('Configuration file : %s', $config))
->expectsOutput('User : #1 (thegrumpydictator@gmail.com)')
->expectsOutput(sprintf('Job : %s', $importJob->key))
->expectsOutput('- I am an error')
->assertExitCode(0);
// this method imports nothing so there is nothing to verify.
}
/**
* Crash while storing data.
*
* @covers \FireflyIII\Console\Commands\Import\CreateCSVImport
*/
public function testHandleCrashStorage(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$jobRepos = $this->mock(ImportJobRepositoryInterface::class);
$fileRoutine = $this->mock(FileRoutine::class);
$storage = $this->mock(ImportArrayStorage::class);
$user = $this->user();
$token = new Preference;
$importJob = $this->user()->importJobs()->first();
$file = storage_path('build/test-upload.csv');
$config = storage_path('build/configuration.json');
// set preferences:
$token->data = 'token';
// mock calls to repository:
$userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($user);
$jobRepos->shouldReceive('setUser')->atLeast()->once();
$jobRepos->shouldReceive('create')->atLeast()->once()->andReturn($importJob);
$jobRepos->shouldReceive('storeCLIupload')->atLeast()->once()->andReturn(new MessageBag);
$jobRepos->shouldReceive('setConfiguration')->atLeast()->once();
// job is ready to run.
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'ready_to_run'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'provider_finished'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'storing_data'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'error'])->atLeast()->once();
// file routine gets called.
$fileRoutine->shouldReceive('setImportJob')->atLeast()->once();
$fileRoutine->shouldReceive('run')->atLeast()->once();
// store data thing gets called.
$storage->shouldReceive('setImportJob')->atLeast()->once();
$storage->shouldReceive('store')->atLeast()->once()->andThrow(new FireflyException('I am storage error.'));
// mock Preferences.
Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'access_token', null])->andReturn($token);
$parameters = [
$file,
$config,
'--user=1',
'--token=token',
];
$this->artisan('firefly-iii:csv-import ' . implode(' ', $parameters))
->expectsOutput(sprintf('Import file : %s', $file))
->expectsOutput(sprintf('Configuration file : %s', $config))
->expectsOutput('User : #1 (thegrumpydictator@gmail.com)')
->expectsOutput(sprintf('Job : %s', $importJob->key))
->expectsOutput('The import routine crashed: I am storage error.')
->assertExitCode(1);
// this method imports nothing so there is nothing to verify.
}
/**
* The file processor crashes for some reason.
*
* @covers \FireflyIII\Console\Commands\Import\CreateCSVImport
*/
public function testHandleCrashProcess(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$jobRepos = $this->mock(ImportJobRepositoryInterface::class);
$fileRoutine = $this->mock(FileRoutine::class);
$storage = $this->mock(ImportArrayStorage::class);
$user = $this->user();
$token = new Preference;
$importJob = $this->user()->importJobs()->first();
$file = storage_path('build/test-upload.csv');
$config = storage_path('build/configuration.json');
// set preferences:
$token->data = 'token';
// mock calls to repository:
$userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($user);
$jobRepos->shouldReceive('setUser')->atLeast()->once();
$jobRepos->shouldReceive('create')->atLeast()->once()->andReturn($importJob);
$jobRepos->shouldReceive('storeCLIupload')->atLeast()->once()->andReturn(new MessageBag);
$jobRepos->shouldReceive('setConfiguration')->atLeast()->once();
// job is ready to run.
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'ready_to_run'])->atLeast()->once();
$jobRepos->shouldReceive('setStatus')->withArgs([Mockery::any(), 'error'])->atLeast()->once();
// file routine gets called.
$fileRoutine->shouldReceive('setImportJob')->atLeast()->once();
$fileRoutine->shouldReceive('run')->atLeast()->once()->andThrows(new FireflyException('I am big bad exception.'));
// mock Preferences.
Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'access_token', null])->andReturn($token);
$parameters = [
$file,
$config,
'--user=1',
'--token=token',
];
$this->artisan('firefly-iii:csv-import ' . implode(' ', $parameters))
->expectsOutput(sprintf('Import file : %s', $file))
->expectsOutput(sprintf('Configuration file : %s', $config))
->expectsOutput('User : #1 (thegrumpydictator@gmail.com)')
->expectsOutput(sprintf('Job : %s', $importJob->key))
->expectsOutput('The import routine crashed: I am big bad exception.')
->assertExitCode(1);
// this method imports nothing so there is nothing to verify.
}
/**
* Throw error when storing data.
*
* @covers \FireflyIII\Console\Commands\Import\CreateCSVImport
*/
public function testHandleFileStoreError(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$jobRepos = $this->mock(ImportJobRepositoryInterface::class);
$this->mock(FileRoutine::class);
$this->mock(ImportArrayStorage::class);
$user = $this->user();
$token = new Preference;
$importJob = $this->user()->importJobs()->first();
$file = storage_path('build/test-upload.csv');
$config = storage_path('build/configuration.json');
$messages = new MessageBag;
$messages->add('file', 'Some file error.');
// set preferences:
$token->data = 'token';
// mock calls to repository:
$userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($user);
$jobRepos->shouldReceive('setUser')->atLeast()->once();
$jobRepos->shouldReceive('create')->atLeast()->once()->andReturn($importJob);
$jobRepos->shouldReceive('storeCLIupload')->atLeast()->once()->andReturn($messages);
// mock Preferences.
Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'access_token', null])->andReturn($token);
$parameters = [
$file,
$config,
'--user=1',
'--token=token',
];
$this->artisan('firefly-iii:csv-import ' . implode(' ', $parameters))
->expectsOutput(sprintf('Import file : %s', $file))
->expectsOutput(sprintf('Configuration file : %s', $config))
->expectsOutput('User : #1 (thegrumpydictator@gmail.com)')
->expectsOutput(sprintf('Job : %s', $importJob->key))
->expectsOutput('Some file error.')
->assertExitCode(1);
// this method imports nothing so there is nothing to verify.
}
}

View File

@@ -0,0 +1,148 @@
<?php
/**
* ReportEmptyObjectsTest.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\Integrity;
use FireflyIII\Models\Account;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\Tag;
use Log;
use Tests\TestCase;
/**
* Class ReportEmptyObjectsTest
*/
class ReportEmptyObjectsTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* Run basic test routine.
*
* @covers \FireflyIII\Console\Commands\Integrity\ReportEmptyObjects
*/
public function testHandleBudget(): void
{
$user = $this->user();
$budget = Budget::create(
[
'user_id' => $user->id,
'name' => 'Some budget',
]);
$budgetLine = sprintf('User #%d (%s) has budget #%d ("%s") which has no transaction journals.',
$user->id, $user->email, $budget->id, $budget->name);
$budgetLimitLine = sprintf('User #%d (%s) has budget #%d ("%s") which has no budget limits.',
$user->id, $user->email, $budget->id, $budget->name);
$this->artisan('firefly-iii:report-empty-objects')
->expectsOutput($budgetLine)
->expectsOutput($budgetLimitLine)
->assertExitCode(0);
$budget->forceDelete();
// this method changes no objects so there is nothing to verify.
}
/**
* Run basic test routine.
*
* @covers \FireflyIII\Console\Commands\Integrity\ReportEmptyObjects
*/
public function testHandleCategory(): void
{
$user = $this->user();
$category = Category::create(
[
'user_id' => $user->id,
'name' => 'Some category',
]);
$categoryLine = sprintf('User #%d (%s) has category #%d ("%s") which has no transaction journals.',
$user->id, $user->email, $category->id, $category->name);
$this->artisan('firefly-iii:report-empty-objects')
->expectsOutput($categoryLine)
->assertExitCode(0);
$category->forceDelete();
// this method changes no objects so there is nothing to verify.
}
/**
* Run basic test routine.
*
* @covers \FireflyIII\Console\Commands\Integrity\ReportEmptyObjects
*/
public function testHandleTag(): void
{
$user = $this->user();
$tag = Tag::create(
[
'user_id' => $user->id,
'tag' => 'Some tag',
'tagMode' => 'nothing',
]);
$tagLine = sprintf('User #%d (%s) has tag #%d ("%s") which has no transaction journals.',
$user->id, $user->email, $tag->id, $tag->tag);
$this->artisan('firefly-iii:report-empty-objects')
->expectsOutput($tagLine)
->assertExitCode(0);
$tag->forceDelete();
// this method changes no objects so there is nothing to verify.
}
/**
* Run basic test routine.
*
* @covers \FireflyIII\Console\Commands\Integrity\ReportEmptyObjects
*/
public function testHandleAccount(): void
{
$user = $this->user();
$account = Account::create(
[
'user_id' => $user->id,
'name' => 'Some account',
'account_type_id' => 1,
]);
$tagLine = sprintf('User #%d (%s) has account #%d ("%s") which has no transactions.',
$user->id, $user->email, $account->id, $account->name);
$this->artisan('firefly-iii:report-empty-objects')
->expectsOutput($tagLine)
->assertExitCode(0);
$account->forceDelete();
// this method changes no objects so there is nothing to verify.
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* ReportSumTest.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\Integrity;
use FireflyIII\Models\Transaction;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Tests\TestCase;
/**
* Class ReportSumTest
*/
class ReportSumTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Integrity\ReportSum
*/
public function testHandle(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('all')->atLeast()->once()->andReturn(new Collection([$this->user()]));
$this->artisan('firefly-iii:report-sum')
->expectsOutput(sprintf('Amount integrity OK for user #%d', $this->user()->id))
->assertExitCode(0);
// this method changes no objects so there is nothing to verify.
}
/**
* Create transaction to make balance uneven.
* @covers \FireflyIII\Console\Commands\Integrity\ReportSum
*/
public function testHandleUneven(): void
{
$transaction = Transaction::create(
[
'transaction_journal_id' => $this->getRandomWithdrawal()->id,
'user_id' => 1,
'account_id' => $this->getRandomAsset()->id,
'amount' => 10,
]
);
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('all')->atLeast()->once()->andReturn(new Collection([$this->user()]));
$this->artisan('firefly-iii:report-sum')
->expectsOutput(sprintf('Error: Transactions for user #%d (%s) are off by %s!', $this->user()->id, $this->user()->email, '10.0'))
->assertExitCode(0);
$transaction->forceDelete();
// this method changes no objects so there is nothing to verify.
}
}

View File

@@ -0,0 +1,404 @@
<?php
/**
* ApplyRulesTest.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\Tools;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\TransactionRules\Engine\RuleEngine;
use Illuminate\Support\Collection;
use Log;
use Tests\TestCase;
/**
* Class ApplyRulesTest
*/
class ApplyRulesTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* Basic call with everything perfect (and ALL rules).
*
* @covers \FireflyIII\Console\Commands\Tools\ApplyRules
*/
public function testHandle(): void
{
$ruleRepos = $this->mock(RuleRepositoryInterface::class);
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$ruleEngine = $this->mock(RuleEngine::class);
// data
$asset = $this->getRandomAsset();
$journal = $this->getRandomWithdrawal();
$group = $this->user()->ruleGroups()->first();
$rule = $this->user()->rules()->first();
$groups = new Collection([$group]);
$rules = new Collection([$rule]);
// expected calls:
$ruleRepos->shouldReceive('setUser')->atLeast()->once();
$ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($asset);
$journalRepos->shouldReceive('firstNull')->atLeast()->once()->andReturn($journal);
$ruleGroupRepos->shouldReceive('getActiveGroups')->atLeast()->once()->andReturn($groups);
$ruleGroupRepos->shouldReceive('getActiveStoreRules')->atLeast()->once()->andReturn($rules);
$collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([[], [], []]);
$ruleEngine->shouldReceive('setUser')->atLeast()->once();
$ruleEngine->shouldReceive('setRulesToApply')->atLeast()->once();
$ruleEngine->shouldReceive('processJournalArray')->times(3);
$parameters = [
'--user=1',
'--token=token',
'--accounts=1',
'--all_rules',
];
$this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
->expectsOutput('Will apply 1 rule(s) to 3 transaction(s).')
->expectsOutput('Done!')
->assertExitCode(0);
// this method changes no objects so there is nothing to verify.
}
/**
* Basic call with everything perfect (and ALL rules), but no rules will be selected.
*
* @covers \FireflyIII\Console\Commands\Tools\ApplyRules
*/
public function testHandEmptye(): void
{
$ruleRepos = $this->mock(RuleRepositoryInterface::class);
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$ruleEngine = $this->mock(RuleEngine::class);
// data
$asset = $this->getRandomAsset();
$journal = $this->getRandomWithdrawal();
$group = $this->user()->ruleGroups()->first();
$groups = new Collection([$group]);
// expected calls:
$ruleRepos->shouldReceive('setUser')->atLeast()->once();
$ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($asset);
$journalRepos->shouldReceive('firstNull')->atLeast()->once()->andReturn($journal);
$ruleGroupRepos->shouldReceive('getActiveGroups')->atLeast()->once()->andReturn($groups);
$ruleGroupRepos->shouldReceive('getActiveStoreRules')->atLeast()->once()->andReturn(new Collection);
$collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([[], [], []]);
$ruleEngine->shouldReceive('setUser')->atLeast()->once();
$ruleEngine->shouldReceive('setRulesToApply')->atLeast()->once();
$ruleEngine->shouldReceive('processJournalArray')->times(3);
$parameters = [
'--user=1',
'--token=token',
'--accounts=1',
'--all_rules',
];
$this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
->expectsOutput('No rules or rule groups have been included.')
->expectsOutput('Done!')
->assertExitCode(0);
}
/**
* Basic call with everything perfect (and ALL rules) and dates.
*
* @covers \FireflyIII\Console\Commands\Tools\ApplyRules
*/
public function testHandleDate(): void
{
$ruleRepos = $this->mock(RuleRepositoryInterface::class);
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
$this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$ruleEngine = $this->mock(RuleEngine::class);
// data
$asset = $this->getRandomAsset();
$group = $this->user()->ruleGroups()->first();
$rule = $this->user()->rules()->first();
$groups = new Collection([$group]);
$rules = new Collection([$rule]);
// expected calls:
$ruleRepos->shouldReceive('setUser')->atLeast()->once();
$ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($asset);
$ruleGroupRepos->shouldReceive('getActiveGroups')->atLeast()->once()->andReturn($groups);
$ruleGroupRepos->shouldReceive('getActiveStoreRules')->atLeast()->once()->andReturn($rules);
$collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([[], [], []]);
$ruleEngine->shouldReceive('setUser')->atLeast()->once();
$ruleEngine->shouldReceive('setRulesToApply')->atLeast()->once();
$ruleEngine->shouldReceive('processJournalArray')->times(3);
$parameters = [
'--user=1',
'--token=token',
'--accounts=1',
'--all_rules',
'--start_date=2019-01-31',
'--end_date=2019-01-01',
];
$this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
->expectsOutput('Will apply 1 rule(s) to 3 transaction(s).')
->expectsOutput('Done!')
->assertExitCode(0);
}
/**
* Will submit some rules to apply.
*
* @covers \FireflyIII\Console\Commands\Tools\ApplyRules
*/
public function testHandleRules(): void
{
$ruleRepos = $this->mock(RuleRepositoryInterface::class);
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$ruleEngine = $this->mock(RuleEngine::class);
// data
$asset = $this->getRandomAsset();
$journal = $this->getRandomWithdrawal();
$group = $this->user()->ruleGroups()->first();
$groups = new Collection([$group]);
$activeRule = $this->user()->rules()->where('active', 1)->inRandomOrder()->first();
$inactiveRule = $this->user()->rules()->where('active', 0)->inRandomOrder()->first();
$rules = new Collection([$activeRule]);
// expected calls:
$ruleRepos->shouldReceive('setUser')->atLeast()->once();
$ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($asset);
$journalRepos->shouldReceive('firstNull')->atLeast()->once()->andReturn($journal);
$ruleRepos->shouldReceive('find')->atLeast()->once()->withArgs([$activeRule->id])->andReturn($activeRule);
$ruleRepos->shouldReceive('find')->atLeast()->once()->withArgs([$inactiveRule->id])->andReturn($inactiveRule);
$ruleGroupRepos->shouldReceive('getActiveGroups')->atLeast()->once()->andReturn($groups);
$ruleGroupRepos->shouldReceive('getActiveStoreRules')->atLeast()->once()->andReturn($rules);
$collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([[], [], []]);
$ruleEngine->shouldReceive('setUser')->atLeast()->once();
$ruleEngine->shouldReceive('setRulesToApply')->atLeast()->once();
$ruleEngine->shouldReceive('processJournalArray')->times(3);
$parameters = [
'--user=1',
'--token=token',
'--accounts=1',
sprintf('--rules=%d,%d', $activeRule->id, $inactiveRule->id),
];
$this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
->expectsOutput('Will apply 1 rule(s) to 3 transaction(s).')
->expectsOutput('Done!')
->assertExitCode(0);
}
/**
* Basic call with two rule groups. One active, one inactive.
*
* @covers \FireflyIII\Console\Commands\Tools\ApplyRules
*/
public function testHandleRuleGroups(): void
{
$ruleRepos = $this->mock(RuleRepositoryInterface::class);
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$ruleEngine = $this->mock(RuleEngine::class);
$activeGroup = $this->user()->ruleGroups()->where('active', 1)->inRandomOrder()->first();
$inactiveGroup = $this->user()->ruleGroups()->where('active', 0)->inRandomOrder()->first();
// data
$asset = $this->getRandomAsset();
$journal = $this->getRandomWithdrawal();
$rule = $this->user()->rules()->first();
$groups = new Collection([$activeGroup]);
$rules = new Collection([$rule]);
// expected calls:
$ruleRepos->shouldReceive('setUser')->atLeast()->once();
$ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($asset);
$journalRepos->shouldReceive('firstNull')->atLeast()->once()->andReturn($journal);
$ruleGroupRepos->shouldReceive('getActiveGroups')->atLeast()->once()->andReturn($groups);
$ruleGroupRepos->shouldReceive('getActiveStoreRules')->atLeast()->once()->andReturn($rules);
$ruleGroupRepos->shouldReceive('find')->atLeast()->once()->withArgs([$activeGroup->id])->andReturn($activeGroup);
$ruleGroupRepos->shouldReceive('find')->atLeast()->once()->withArgs([$inactiveGroup->id])->andReturn($inactiveGroup);
$collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([[], [], []]);
$ruleEngine->shouldReceive('setUser')->atLeast()->once();
$ruleEngine->shouldReceive('setRulesToApply')->atLeast()->once();
$ruleEngine->shouldReceive('processJournalArray')->times(3);
$parameters = [
'--user=1',
'--token=token',
'--accounts=1',
sprintf('--rule_groups=%d,%d', $activeGroup->id, $inactiveGroup->id),
];
$this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
->expectsOutput(sprintf('Will ignore inactive rule group #%d ("%s")', $inactiveGroup->id, $inactiveGroup->title))
// one rule out of 2 groups:
->expectsOutput('Will apply 1 rule(s) to 3 transaction(s).')
->expectsOutput('Done!')
->assertExitCode(0);
}
/**
* Basic call but no accounts submitted.
*
* @covers \FireflyIII\Console\Commands\Tools\ApplyRules
*/
public function testHandleNoAccounts(): void
{
$ruleRepos = $this->mock(RuleRepositoryInterface::class);
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
$this->mock(JournalRepositoryInterface::class);
$this->mock(GroupCollectorInterface::class);
$this->mock(AccountRepositoryInterface::class);
$this->mock(RuleEngine::class);
// expected calls:
$ruleRepos->shouldReceive('setUser')->atLeast()->once();
$ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
$parameters = [
'--user=1',
'--token=token',
'--accounts=',
'--all_rules',
];
$this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
->expectsOutput('Please use the --accounts option to indicate the accounts to apply rules to.')
->assertExitCode(1);
}
/**
* Basic call but only one expense account submitted
*
* @covers \FireflyIII\Console\Commands\Tools\ApplyRules
*/
public function testHandleExpenseAccounts(): void
{
$ruleRepos = $this->mock(RuleRepositoryInterface::class);
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
$this->mock(JournalRepositoryInterface::class);
$this->mock(GroupCollectorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$this->mock(RuleEngine::class);
// data
$expense = $this->getRandomExpense();
// expected calls:
$ruleRepos->shouldReceive('setUser')->atLeast()->once();
$ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([$expense->id])->andReturn($expense);
$parameters = [
'--user=1',
'--token=token',
'--accounts=' . $expense->id,
'--all_rules',
];
$this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
->expectsOutput('Please make sure all accounts in --accounts are asset accounts or liabilities.')
->assertExitCode(1);
}
}

View File

@@ -0,0 +1,341 @@
<?php
/**
* AccountCurrenciesTest.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\AccountMeta;
use FireflyIII\Models\Configuration;
use FireflyIII\Models\Preference;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Mockery;
use Preferences;
use Tests\TestCase;
/**
* Class AccountCurrenciesTest
*/
class AccountCurrenciesTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* Perfect run without opening balance.
*
* @covers \FireflyIII\Console\Commands\Upgrade\AccountCurrencies
*/
public function testHandle(): void
{
$false = new Configuration;
$false->data = false;
$pref = new Preference;
$pref->data = 'EUR';
$account = $this->getRandomAsset();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
// mock calls
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->andReturn('1');
$accountRepos->shouldReceive('getOpeningBalance')->atLeast()->once()->andReturn(null);
$accountRepos->shouldReceive('getAccountsByType')->atLeast()->once()->andReturn(new Collection([$account]));
$userRepos->shouldReceive('all')->atLeast()->once()->andReturn(new Collection([$this->user()]));
// check config
FireflyConfig::shouldReceive('get')->withArgs(['4780_account_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_account_currencies', true]);
// check preferences:
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'currencyPreference', 'EUR'])->andReturn($pref);
$this->artisan('firefly-iii:account-currencies')
->expectsOutput('All accounts are OK.')
->assertExitCode(0);
// nothing changed, so nothing to verify.
}
/**
* Perfect run without opening balance.
*
* @covers \FireflyIII\Console\Commands\Upgrade\AccountCurrencies
*/
public function testHandleNotNull(): void
{
$false = new Configuration;
$false->data = false;
$pref = new Preference;
$pref->data = 'EUR';
$account = $this->getRandomAsset();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$journal = $this->getRandomWithdrawal();
// mock calls
$accountRepos->shouldReceive('setUser')->atLeast()->once();
// account reports USD
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->andReturn('2');
// journal is EUR.
$accountRepos->shouldReceive('getOpeningBalance')->atLeast()->once()->andReturn($journal);
$accountRepos->shouldReceive('getAccountsByType')->atLeast()->once()->andReturn(new Collection([$account]));
$userRepos->shouldReceive('all')->atLeast()->once()->andReturn(new Collection([$this->user()]));
// check config
FireflyConfig::shouldReceive('get')->withArgs(['4780_account_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_account_currencies', true]);
// check preferences:
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'currencyPreference', 'EUR'])->andReturn($pref);
$this->artisan('firefly-iii:account-currencies')
->expectsOutput(sprintf('Account #%d ("%s") now has a correct currency for opening balance.', $account->id, $account->name))
->assertExitCode(0);
// check if currency has been changed for the journal + transactions.
$this->assertCount(1, TransactionJournal::where('id', $journal->id)->where('transaction_currency_id', 2)->get());
$this->assertCount(2, Transaction::where('transaction_journal_id', $journal->id)->where('transaction_currency_id', 2)->get());
}
/**
* Perfect run with opening balance.
*
* @covers \FireflyIII\Console\Commands\Upgrade\AccountCurrencies
*/
public function testHandleOpeningBalance(): void
{
$false = new Configuration;
$false->data = false;
$pref = new Preference;
$pref->data = 'EUR';
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$journal = $this->getRandomWithdrawal();
$account = $this->getRandomAsset();
// mock calls
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->andReturn('1');
$accountRepos->shouldReceive('getOpeningBalance')->atLeast()->once()->andReturn($journal);
$userRepos->shouldReceive('all')->atLeast()->once()->andReturn(new Collection([$this->user()]));
$accountRepos->shouldReceive('getAccountsByType')->atLeast()->once()->andReturn(new Collection([$account]));
// check config
FireflyConfig::shouldReceive('get')->withArgs(['4780_account_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_account_currencies', true]);
// check preferences:
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'currencyPreference', 'EUR'])->andReturn($pref);
$this->artisan('firefly-iii:account-currencies')
->expectsOutput('All accounts are OK.')
->assertExitCode(0);
// nothing changed, dont check output.
}
/**
* Perfect run with opening balance with different currencies
*
* @covers \FireflyIII\Console\Commands\Upgrade\AccountCurrencies
*/
public function testHandleDifferent(): void
{
$false = new Configuration;
$false->data = false;
$pref = new Preference;
$pref->data = 'USD';
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$journal = $this->getRandomWithdrawal();
$account = $this->getRandomAsset();
$euro = TransactionCurrency::where('code', 'EUR')->first();
// delete meta data of account just in case:
AccountMeta::where('account_id', $account->id)->where('name', 'currency_id')->forceDelete();
// mock calls
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->andReturn('0');
$accountRepos->shouldReceive('getOpeningBalance')->atLeast()->once()->andReturn($journal);
$userRepos->shouldReceive('all')->atLeast()->once()->andReturn(new Collection([$this->user()]));
$accountRepos->shouldReceive('getAccountsByType')->atLeast()->once()->andReturn(new Collection([$account]));
// check config
FireflyConfig::shouldReceive('get')->withArgs(['4780_account_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_account_currencies', true]);
// check preferences:
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'currencyPreference', 'EUR'])->andReturn($pref);
$this->artisan('firefly-iii:account-currencies')
->expectsOutput(sprintf('Account #%d ("%s") now has a currency setting (#%d).', $account->id, $account->name, $euro->id))
->assertExitCode(0);
// verify account meta data change.
$this->assertCount(1,
AccountMeta::where('account_id', $account->id)
->where('name', 'currency_id')
->where('data', $euro->id)->get());
}
/**
* No known currency preferences.
*
* @covers \FireflyIII\Console\Commands\Upgrade\AccountCurrencies
*/
public function testHandleZeroPreference(): void
{
$false = new Configuration;
$false->data = false;
$pref = new Preference;
$pref->data = 'EUR';
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$account = $this->getRandomAsset();
$euro = TransactionCurrency::where('code', 'EUR')->first();
// mock calls
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->andReturn('0');
$accountRepos->shouldReceive('getOpeningBalance')->atLeast()->once()->andReturn(null);
$userRepos->shouldReceive('all')->atLeast()->once()->andReturn(new Collection([$this->user()]));
$accountRepos->shouldReceive('getAccountsByType')->atLeast()->once()->andReturn(new Collection([$account]));
// check config
FireflyConfig::shouldReceive('get')->withArgs(['4780_account_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_account_currencies', true]);
// check preferences:
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'currencyPreference', 'EUR'])->andReturn($pref);
$this->artisan('firefly-iii:account-currencies')
->expectsOutput(sprintf('Account #%d ("%s") now has a currency setting (%s).',
$account->id, $account->name, $euro->code
))
->expectsOutput('Corrected 1 account(s).')
->assertExitCode(0);
$this->assertCount(1, AccountMeta::where('account_id', $account->id)
->where('name', 'currency_id')
->where('data', $euro->id)->get());
}
/**
* @covers \FireflyIII\Console\Commands\Upgrade\AccountCurrencies
*/
public function testHandleNoPreference(): void
{
$false = new Configuration;
$false->data = false;
$pref = new Preference;
$pref->data = false;
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$account = $this->getRandomAsset();
// mock calls
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->andReturn('1');
$accountRepos->shouldReceive('getOpeningBalance')->atLeast()->once()->andReturn(null);
$userRepos->shouldReceive('all')->atLeast()->once()->andReturn(new Collection([$this->user()]));
$accountRepos->shouldReceive('getAccountsByType')->atLeast()->once()->andReturn(new Collection([$account]));
// check config
FireflyConfig::shouldReceive('get')->withArgs(['4780_account_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_account_currencies', true]);
// check preferences:
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'currencyPreference', 'EUR'])->andReturn($pref);
$this->artisan('firefly-iii:account-currencies')
->expectsOutput('All accounts are OK.')
->assertExitCode(0);
// nothing changed, so nothing to verify.
}
/**
* @covers \FireflyIII\Console\Commands\Upgrade\AccountCurrencies
*/
public function testHandleInvalidPreference(): void
{
$false = new Configuration;
$false->data = false;
$pref = new Preference;
$pref->data = 'ABC';
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$account = $this->getRandomAsset();
// mock calls
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getAccountsByType')->atLeast()->once()->andReturn(new Collection([$account]));
$userRepos->shouldReceive('all')->atLeast()->once()->andReturn(new Collection([$this->user()]));
// check config
FireflyConfig::shouldReceive('get')->withArgs(['4780_account_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_account_currencies', true]);
// check preferences:
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'currencyPreference', 'EUR'])->andReturn($pref);
$this->artisan('firefly-iii:account-currencies')
->expectsOutput('User has a preference for "ABC", but this currency does not exist.')
->assertExitCode(0);
// nothing changed, so nothing to verify.
}
/**
* @covers \FireflyIII\Console\Commands\Upgrade\AccountCurrencies
*/
public function testHandleAlreadyExecuted(): void
{
$true = new Configuration;
$true->data = true;
$pref = new Preference;
$pref->data = 'EUR';
$this->mock(AccountRepositoryInterface::class);
$this->mock(UserRepositoryInterface::class);
// check config
FireflyConfig::shouldReceive('get')->withArgs(['4780_account_currencies', false])->andReturn($true);
FireflyConfig::shouldReceive('set')->withArgs(['4780_account_currencies', true]);
// check preferences:
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'currencyPreference', 'EUR'])->andReturn($pref);
$this->artisan('firefly-iii:account-currencies')
->expectsOutput('This command has already been executed.')
->assertExitCode(0);
// nothing changed, so nothing to verify.
}
}

View File

@@ -0,0 +1,244 @@
<?php
/**
* BackToJournalsTest.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\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\Configuration;
use FireflyIII\Models\Transaction;
use Log;
use Tests\TestCase;
/**
* Class BackToJournalsTest
*/
class BackToJournalsTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* Perfect run. Will report on nothing.
*
* @covers \FireflyIII\Console\Commands\Upgrade\BackToJournals
*/
public function testHandle(): void
{
// verify preference:
$false = new Configuration;
$false->data = false;
$true = new Configuration;
$true->data = true;
FireflyConfig::shouldReceive('get')->withArgs(['4780_back_to_journals', false])->andReturn($false);
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($true);
// set new preference after running:
FireflyConfig::shouldReceive('set')->withArgs(['4780_back_to_journals', true]);
$this->artisan('firefly-iii:back-to-journals')
->expectsOutput('Check 0 transaction journal(s) for budget info.')
->expectsOutput('Check 0 transaction journal(s) for category info.')
->assertExitCode(0);
}
/**
* Transaction has a budget, journal doesn't.
*
* @covers \FireflyIII\Console\Commands\Upgrade\BackToJournals
*/
public function testHandleBudget(): void
{
$journal = $this->getRandomWithdrawal();
/** @var Transaction $transaction */
$transaction = $journal->transactions()->first();
/** @var Budget $budget */
$budget = $this->user()->budgets()->first();
$transaction->budgets()->sync([$budget->id]);
$journal->budgets()->sync([]);
$journal->save();
$transaction->save();
// verify preference:
$false = new Configuration;
$false->data = false;
$true = new Configuration;
$true->data = true;
FireflyConfig::shouldReceive('get')->withArgs(['4780_back_to_journals', false])->andReturn($false);
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($true);
// set new preference after running:
FireflyConfig::shouldReceive('set')->withArgs(['4780_back_to_journals', true]);
$this->artisan('firefly-iii:back-to-journals')
->expectsOutput('Check 1 transaction journal(s) for budget info.')
->expectsOutput('Check 0 transaction journal(s) for category info.')
->assertExitCode(0);
// transaction should have no budget:
$this->assertEquals(0, $transaction->budgets()->count());
// journal should have one.
$this->assertEquals(1, $journal->budgets()->count());
// should be $budget:
$this->assertEquals($budget->id, $journal->budgets()->first()->id);
}
/**
* Transaction has a category, journal doesn't.
*
* @covers \FireflyIII\Console\Commands\Upgrade\BackToJournals
*/
public function testHandleCategory(): void
{
$journal = $this->getRandomWithdrawal();
/** @var Transaction $transaction */
$transaction = $journal->transactions()->first();
/** @var Category $category */
$category = $this->user()->categories()->first();
$transaction->categories()->sync([$category->id]);
$journal->categories()->sync([]);
$journal->save();
$transaction->save();
// verify preference:
$false = new Configuration;
$false->data = false;
$true = new Configuration;
$true->data = true;
FireflyConfig::shouldReceive('get')->withArgs(['4780_back_to_journals', false])->andReturn($false);
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($true);
// set new preference after running:
FireflyConfig::shouldReceive('set')->withArgs(['4780_back_to_journals', true]);
$this->artisan('firefly-iii:back-to-journals')
->expectsOutput('Check 0 transaction journal(s) for budget info.')
->expectsOutput('Check 1 transaction journal(s) for category info.')
->assertExitCode(0);
// transaction should have no category:
$this->assertEquals(0, $transaction->categories()->count());
// journal should have one.
$this->assertEquals(1, $journal->categories()->count());
// should be $category:
$this->assertEquals($category->id, $journal->categories()->first()->id);
}
/**
* Transaction has a budget, journal has another
*
* @covers \FireflyIII\Console\Commands\Upgrade\BackToJournals
*/
public function testHandleDifferentBudget(): void
{
$journal = $this->getRandomWithdrawal();
/** @var Transaction $transaction */
$transaction = $journal->transactions()->first();
/** @var Budget $budget */
$budget = $this->user()->budgets()->first();
$otherBudget = $this->user()->budgets()->where('id', '!=', $budget->id)->first();
$transaction->budgets()->sync([$budget->id]);
$journal->budgets()->sync([$otherBudget->id]);
$journal->save();
$transaction->save();
// verify preference:
$false = new Configuration;
$false->data = false;
$true = new Configuration;
$true->data = true;
FireflyConfig::shouldReceive('get')->withArgs(['4780_back_to_journals', false])->andReturn($false);
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($true);
// set new preference after running:
FireflyConfig::shouldReceive('set')->withArgs(['4780_back_to_journals', true]);
$this->artisan('firefly-iii:back-to-journals')
->expectsOutput('Check 1 transaction journal(s) for budget info.')
->expectsOutput('Check 0 transaction journal(s) for category info.')
->assertExitCode(0);
// transaction should have no budget:
$this->assertEquals(0, $transaction->budgets()->count());
// journal should have one.
$this->assertEquals(1, $journal->budgets()->count());
// should be $budget:
$this->assertEquals($budget->id, $journal->budgets()->first()->id);
}
/**
* Transaction has a category, journal has another
*
* @covers \FireflyIII\Console\Commands\Upgrade\BackToJournals
*/
public function testHandleDifferentCategory(): void
{
$journal = $this->getRandomWithdrawal();
/** @var Transaction $transaction */
$transaction = $journal->transactions()->first();
/** @var Category $category */
$category = $this->user()->categories()->first();
$otherCategory = $this->user()->categories()->where('id', '!=', $category->id)->first();
$transaction->categories()->sync([$category->id]);
$journal->categories()->sync([$otherCategory->id]);
$journal->save();
$transaction->save();
// verify preference:
$false = new Configuration;
$false->data = false;
$true = new Configuration;
$true->data = true;
FireflyConfig::shouldReceive('get')->withArgs(['4780_back_to_journals', false])->andReturn($false);
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($true);
// set new preference after running:
FireflyConfig::shouldReceive('set')->withArgs(['4780_back_to_journals', true]);
$this->artisan('firefly-iii:back-to-journals')
->expectsOutput('Check 0 transaction journal(s) for budget info.')
->expectsOutput('Check 1 transaction journal(s) for category info.')
->assertExitCode(0);
// transaction should have no category:
$this->assertEquals(0, $transaction->categories()->count());
// journal should have one.
$this->assertEquals(1, $journal->categories()->count());
// should be $category:
$this->assertEquals($category->id, $journal->categories()->first()->id);
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* BudgetLimitCurrencyTest.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\BudgetLimit;
use FireflyIII\Models\Configuration;
use Log;
use Tests\TestCase;
/**
* Class BudgetLimitCurrencyTest
*/
class BudgetLimitCurrencyTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Upgrade\BudgetLimitCurrency
*/
public function testHandle(): void
{
$this->artisan('firefly-iii:bl-currency')
->assertExitCode(0);
}
/**
* Create a bad budget limit.
* @covers \FireflyIII\Console\Commands\Upgrade\BudgetLimitCurrency
*/
public function testHandleBadLimit(): void
{
$false = new Configuration;
$false->data = false;
$budget = $this->user()->budgets()->first();
$limit = BudgetLimit::create(
[
'budget_id' => $budget->id,
'amount' => '10',
'start_date' => '2019-01-01',
'end_date' => '2019-01-31',
]);
FireflyConfig::shouldReceive('get')->withArgs(['4780_bl_currency', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_bl_currency', true]);
$this->artisan('firefly-iii:bl-currency')
->expectsOutput(
sprintf('Budget limit #%d (part of budget "%s") now has a currency setting (%s).',
$limit->id, $budget->name, 'Euro'
)
)
->assertExitCode(0);
// assume currency is filled in.
$this->assertCount(1, BudgetLimit::where('id', $limit->id)->where('transaction_currency_id', 1)->get());
}
}

View File

@@ -0,0 +1,129 @@
<?php
/**
* CCLiabilitiesTest.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\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Configuration;
use Log;
use Tests\TestCase;
/**
* Class CCLiabilitiesTest
*/
class CCLiabilitiesTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Console\Commands\Upgrade\CCLiabilities
*/
public function testHandle(): void
{
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_cc_liabilities', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_cc_liabilities', true]);
$this->artisan('firefly-iii:cc-liabilities')
->expectsOutput('No incorrectly stored credit card liabilities.')
->assertExitCode(0);
// nothing changed, so nothing to verify.
}
/**
* Add type to make the script run.
*
* @covers \FireflyIII\Console\Commands\Upgrade\CCLiabilities
*/
public function testHandleEmpty(): void
{
$type = AccountType::create(
[
'type' => AccountType::CREDITCARD,
]
);
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_cc_liabilities', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_cc_liabilities', true]);
$this->artisan('firefly-iii:cc-liabilities')
->expectsOutput('No incorrectly stored credit card liabilities.')
->assertExitCode(0);
$type->forceDelete();
// nothing changed, so nothing to verify.
}
/**
* Add some things to make it trigger.
*
* @covers \FireflyIII\Console\Commands\Upgrade\CCLiabilities
*/
public function testHandleCase(): void
{
$type = AccountType::create(
[
'type' => AccountType::CREDITCARD,
]
);
$account = Account::create(
[
'name' => 'CC',
'user_id' => 1,
'account_type_id' => $type->id,
]
);
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_cc_liabilities', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_cc_liabilities', true]);
$this->artisan('firefly-iii:cc-liabilities')
->expectsOutput(sprintf('Converted credit card liability account "%s" (#%d) to generic debt liability.', $account->name, $account->id))
->expectsOutput('Credit card liability types are no longer supported and have been converted to generic debts. See: http://bit.ly/FF3-credit-cards')
->assertExitCode(0);
// verify new type.
$this->assertCount(0, Account::where('id', $account->id)->where('account_type_id', $type->id)->get());
$account->forceDelete();
$type->forceDelete();
}
}

View File

@@ -0,0 +1,354 @@
<?php
/**
* JournalCurrenciesTest.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\Account;
use FireflyIII\Models\Configuration;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
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 JournalCurrenciesTest
*/
class JournalCurrenciesTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* Basic run. Would not change anything.
*
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
*/
public function testHandle(): void
{
// mock classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$euro = TransactionCurrency::find(1);
// update transfer if necessary for the test:
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
// mock stuff
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::TRANSFER]])
->andReturn(new Collection);
// for the "other journals" check, return nothing.
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
->andReturn(new Collection);
// transaction would be verified, nothing more.
$this->artisan('firefly-iii:journal-currencies')
->expectsOutput('All transactions are correct.')
->assertExitCode(0);
// nothing changed, so no verification.
}
/**
* Submit a single transfer which has no issues.
*
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
*/
public function testHandleTransfer(): void
{
// mock classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$euro = TransactionCurrency::find(1);
$transfer = $this->getRandomTransfer();
// update transfer if necessary for the test:
$collection = new Collection([$transfer]);
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
// mock stuff
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// return single tranfer
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::TRANSFER]])
->andReturn($collection);
// for the "other journals" check, return nothing.
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
->andReturn(new Collection);
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id);
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro);
// transaction would be verified, nothing more.
$this->artisan('firefly-iii:journal-currencies')
->expectsOutput('Verified 1 transaction(s) and journal(s).')
->assertExitCode(0);
// nothing changed, so no verification.
}
/**
* Submit a single transfer where the source account has no currency preference.
*
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
*/
public function testHandleTransferSourceNoPref(): void
{
// mock classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$euro = TransactionCurrency::find(1);
$transfer = $this->getRandomTransfer();
// edit source to remove currency preference:
/** @var Account $source */
$source = $transfer->transactions()->where('amount', '<', 0)->first()->account;
// AccountMeta::where('account_id', $source->id)->where('name', 'currency_id')->delete();
// update transfer if necessary for the test:
$collection = new Collection([$transfer]);
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
// mock stuff
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// return single transfer
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::TRANSFER]])
->andReturn($collection);
// for the "other journals" check, return nothing.
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
->andReturn(new Collection);
// return NULL for first currency ID and currency.
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturnNull();
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturnNull();
// transaction would be verified, nothing more.
$this->artisan('firefly-iii:journal-currencies')
->expectsOutput(sprintf('Account #%d ("%s") must have currency preference but has none.', $source->id, $source->name))
->assertExitCode(0);
// nothing changed, so no verification.
}
/**
* Submit a single transfer where the source transaction has no currency set.
* Because this is not done over repositories, we must edit the DB.
*
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
*/
public function testHandleTransferSourceNoCurrency(): void
{
// mock classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$euro = TransactionCurrency::find(1);
$transfer = $this->getRandomTransfer();
/** @var Transaction $source */
$source = $transfer->transactions()->where('amount', '<', 0)->first();
$source->transaction_currency_id = null;
$source->save();
// update transfer if necessary for the test:
$collection = new Collection([$transfer]);
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
// mock stuff
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// return single tranfer
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::TRANSFER]])
->andReturn($collection);
// for the "other journals" check, return nothing.
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
->andReturn(new Collection);
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id);
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro);
// transaction would be verified, nothing more.
$this->artisan('firefly-iii:journal-currencies')
->expectsOutput(sprintf('Transaction #%d has no currency setting, now set to %s.', $source->id, $euro->code))
->expectsOutput('Verified 2 transaction(s) and journal(s).')
->assertExitCode(0);
// check transaction
$this->assertCount(1, Transaction::where('id', $source->id)->where('transaction_currency_id', $euro->id)->get());
}
/**
* Submit a single transfer where the source transaction has a different currency than the source account does.
*
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
*/
public function testHandleMismatchedTransfer(): void
{
// mock classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$euro = TransactionCurrency::find(1);
$usd = TransactionCurrency::where('code', 'USD')->first();
$transfer = $this->getRandomTransfer();
/** @var Transaction $source */
$source = $transfer->transactions()->where('amount', '<', 0)->first();
$source->transaction_currency_id = $usd->id;
$source->save();
// update transfer if necessary for the test:
$collection = new Collection([$transfer]);
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
// mock stuff
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// return single tranfer
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::TRANSFER]])
->andReturn($collection);
// for the "other journals" check, return nothing.
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
->andReturn(new Collection);
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id);
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro);
// transaction would be verified, nothing more.
$this->artisan('firefly-iii:journal-currencies')
->expectsOutput(
sprintf(
'Transaction #%d has a currency setting #%d that should be #%d. Amount remains %s, currency is changed.',
$source->id,
$source->transaction_currency_id,
$euro->id,
$source->amount
)
)
->expectsOutput('Verified 2 transaction(s) and journal(s).')
->assertExitCode(0);
// nothing changed, so no verification.
}
/**
* Submit a single transfer where the destination account has no currency preference.
*
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
*/
public function testHandleTransferNoDestinationCurrency(): void
{
// mock classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$euro = TransactionCurrency::find(1);
$transfer = $this->getRandomTransfer();
/** @var Account $destination */
$destination = $transfer->transactions()->where('amount', '>', 0)->first()->account;
// update transfer if necessary for the test:
$collection = new Collection([$transfer]);
$false = new Configuration;
$false->data = false;
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
// mock stuff
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// return single tranfer
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::TRANSFER]])
->andReturn($collection);
// for the "other journals" check, return nothing.
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
->andReturn(new Collection);
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id, 0);
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro, null);
// transaction would be verified, nothing more.
$this->artisan('firefly-iii:journal-currencies')
->expectsOutput(sprintf('Account #%d ("%s") must have currency preference but has none.', $destination->id, $destination->name))
->assertExitCode(0);
// nothing changed, so no verification.
}
}