mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-16 22:58:09 +00:00
Fix test cases.
This commit is contained in:
@@ -24,7 +24,9 @@ namespace Tests\Unit\TransactionRules\Actions;
|
||||
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Actions\SetBudget;
|
||||
use Illuminate\Support\Collection;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
@@ -39,8 +41,12 @@ class SetBudgetTest extends TestCase
|
||||
public function testAct()
|
||||
{
|
||||
// get journal, remove all budgets
|
||||
$journal = TransactionJournal::find(12);
|
||||
$budget = $journal->user->budgets()->first();
|
||||
$journal = TransactionJournal::find(12);
|
||||
$budget = $journal->user->budgets()->first();
|
||||
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
|
||||
$budgetRepos->shouldReceive('setUser');
|
||||
$budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
|
||||
|
||||
$journal->budgets()->detach();
|
||||
$this->assertEquals(0, $journal->budgets()->count());
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ namespace Tests\Unit\TransactionRules\Actions;
|
||||
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Actions\SetCategory;
|
||||
use Tests\TestCase;
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Actions\SetDestinationAccount;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -47,6 +48,7 @@ class SetDestinationAccountTest extends TestCase
|
||||
*/
|
||||
public function testActDepositExisting()
|
||||
{
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$type = TransactionType::whereType(TransactionType::DEPOSIT)->first();
|
||||
$journal = TransactionJournal::where('transaction_type_id', $type->id)->first();
|
||||
$destinationTr = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
@@ -56,6 +58,10 @@ class SetDestinationAccountTest extends TestCase
|
||||
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $destination->id)->first();
|
||||
$this->assertNotEquals($destination->id, $account->id);
|
||||
|
||||
// find account? Return account:
|
||||
$accountRepos->shouldReceive('setUser');
|
||||
$accountRepos->shouldReceive('findByName')->andReturn($account);
|
||||
|
||||
// fire the action:
|
||||
$ruleAction = new RuleAction;
|
||||
$ruleAction->action_value = $account->name;
|
||||
@@ -71,34 +77,6 @@ class SetDestinationAccountTest extends TestCase
|
||||
$this->assertEquals($newDestination->id, $account->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give deposit new asset account (will fail)
|
||||
*
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct()
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act()
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::findAssetAccount()
|
||||
*/
|
||||
public function testActDepositNew()
|
||||
{
|
||||
$type = TransactionType::whereType(TransactionType::DEPOSIT)->first();
|
||||
$journal = TransactionJournal::where('transaction_type_id', $type->id)->first();
|
||||
$destinationTr = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
$destination = $destinationTr->account;
|
||||
|
||||
// fire the action:
|
||||
$ruleAction = new RuleAction;
|
||||
$ruleAction->action_value = 'Some new asset ' . rand(1, 1000);
|
||||
$action = new SetDestinationAccount($ruleAction);
|
||||
$result = $action->act($journal);
|
||||
$this->assertFalse($result);
|
||||
|
||||
// test journal for still having old account
|
||||
$journal = TransactionJournal::find($journal->id);
|
||||
$destinationTr = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
$newDestination = $destinationTr->account;
|
||||
$this->assertEquals($destination->id, $newDestination->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give withdrawal existing expense account.
|
||||
*
|
||||
@@ -108,6 +86,7 @@ class SetDestinationAccountTest extends TestCase
|
||||
*/
|
||||
public function testActWithdrawalExisting()
|
||||
{
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||
$journal = TransactionJournal::where('transaction_type_id', $type->id)->first();
|
||||
$destinationTr = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
@@ -117,6 +96,10 @@ class SetDestinationAccountTest extends TestCase
|
||||
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $destination->id)->first();
|
||||
$this->assertNotEquals($destination->id, $account->id);
|
||||
|
||||
// find account? Return account:
|
||||
$accountRepos->shouldReceive('setUser');
|
||||
$accountRepos->shouldReceive('findByName')->andReturn($account);
|
||||
|
||||
// fire the action:
|
||||
$ruleAction = new RuleAction;
|
||||
$ruleAction->action_value = $account->name;
|
||||
@@ -132,34 +115,6 @@ class SetDestinationAccountTest extends TestCase
|
||||
$this->assertEquals($newDestination->id, $account->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give withdrawal new expense account.
|
||||
*
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct()
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act()
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::findExpenseAccount
|
||||
*/
|
||||
public function testActWithdrawalNew()
|
||||
{
|
||||
$type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||
$journal = TransactionJournal::where('transaction_type_id', $type->id)->first();
|
||||
$destinationTr = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
$destination = $destinationTr->account;
|
||||
|
||||
// fire the action:
|
||||
$ruleAction = new RuleAction;
|
||||
$ruleAction->action_value = 'Some new expense ' . rand(1, 1000);
|
||||
$action = new SetDestinationAccount($ruleAction);
|
||||
$result = $action->act($journal);
|
||||
$this->assertTrue($result);
|
||||
|
||||
// test journal for new account
|
||||
$journal = TransactionJournal::find($journal->id);
|
||||
$destinationTr = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
$newDestination = $destinationTr->account;
|
||||
$this->assertNotEquals($destination->id, $newDestination->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test this on a split journal.
|
||||
*
|
||||
@@ -168,10 +123,15 @@ class SetDestinationAccountTest extends TestCase
|
||||
*/
|
||||
public function testSplitJournal()
|
||||
{
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$transaction = Transaction::orderBy('count', 'DESC')->groupBy('transaction_journal_id')
|
||||
->get(['transaction_journal_id', DB::raw('COUNT(transaction_journal_id) as count')])
|
||||
->first();
|
||||
$journal = TransactionJournal::find($transaction->transaction_journal_id);
|
||||
|
||||
// mock
|
||||
$accountRepos->shouldReceive('setUser');
|
||||
|
||||
// fire the action:
|
||||
$ruleAction = new RuleAction;
|
||||
$ruleAction->action_value = 'Some new asset ' . rand(1, 1000);
|
||||
|
||||
@@ -28,6 +28,7 @@ use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Actions\SetSourceAccount;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -45,6 +46,9 @@ class SetSourceAccountTest extends TestCase
|
||||
*/
|
||||
public function testActDepositExistingUpdated()
|
||||
{
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
|
||||
|
||||
$type = TransactionType::whereType(TransactionType::DEPOSIT)->first();
|
||||
$journal = TransactionJournal::where('transaction_type_id', $type->id)->first();
|
||||
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
@@ -54,6 +58,10 @@ class SetSourceAccountTest extends TestCase
|
||||
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first();
|
||||
$this->assertNotEquals($source->id, $account->id);
|
||||
|
||||
// find account? Return account:
|
||||
$accountRepos->shouldReceive('setUser');
|
||||
$accountRepos->shouldReceive('findByName')->andReturn($account);
|
||||
|
||||
// fire the action:
|
||||
$ruleAction = new RuleAction;
|
||||
$ruleAction->action_value = $account->name;
|
||||
@@ -69,34 +77,6 @@ class SetSourceAccountTest extends TestCase
|
||||
$this->assertEquals($newSource->id, $account->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give deposit new revenue account.
|
||||
*
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct()
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act()
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findRevenueAccount
|
||||
*/
|
||||
public function testActDepositNewUpdated()
|
||||
{
|
||||
$type = TransactionType::whereType(TransactionType::DEPOSIT)->first();
|
||||
$journal = TransactionJournal::where('transaction_type_id', $type->id)->first();
|
||||
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$source = $sourceTr->account;
|
||||
|
||||
// fire the action:
|
||||
$ruleAction = new RuleAction;
|
||||
$ruleAction->action_value = 'Some new revenue ' . rand(1, 1000);
|
||||
$action = new SetSourceAccount($ruleAction);
|
||||
$result = $action->act($journal);
|
||||
$this->assertTrue($result);
|
||||
|
||||
// test journal for new account
|
||||
$journal = TransactionJournal::find($journal->id);
|
||||
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$newSource = $sourceTr->account;
|
||||
$this->assertNotEquals($source->id, $newSource->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give withdrawal existing asset account.
|
||||
*
|
||||
@@ -106,15 +86,20 @@ class SetSourceAccountTest extends TestCase
|
||||
*/
|
||||
public function testActWithdrawalExistingUpdated()
|
||||
{
|
||||
$type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||
$journal = TransactionJournal::where('transaction_type_id', $type->id)->first();
|
||||
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$source = $sourceTr->account;
|
||||
$user = $journal->user;
|
||||
$accountType = AccountType::whereType(AccountType::ASSET)->first();
|
||||
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first();
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||
$journal = TransactionJournal::where('transaction_type_id', $type->id)->first();
|
||||
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$source = $sourceTr->account;
|
||||
$user = $journal->user;
|
||||
$accountType = AccountType::whereType(AccountType::ASSET)->first();
|
||||
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first();
|
||||
$this->assertNotEquals($source->id, $account->id);
|
||||
|
||||
|
||||
$accountRepos->shouldReceive('setUser');
|
||||
$accountRepos->shouldReceive('findByName')->andReturn($account);
|
||||
|
||||
// fire the action:
|
||||
$ruleAction = new RuleAction;
|
||||
$ruleAction->action_value = $account->name;
|
||||
@@ -130,34 +115,6 @@ class SetSourceAccountTest extends TestCase
|
||||
$this->assertEquals($newSource->id, $account->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give withdrawal new asset account (will fail)
|
||||
*
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct()
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act()
|
||||
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findAssetAccount()
|
||||
*/
|
||||
public function testActWithdrawalNew()
|
||||
{
|
||||
$type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||
$journal = TransactionJournal::where('transaction_type_id', $type->id)->first();
|
||||
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$source = $sourceTr->account;
|
||||
|
||||
// fire the action:
|
||||
$ruleAction = new RuleAction;
|
||||
$ruleAction->action_value = 'Some new asset ' . rand(1, 1000);
|
||||
$action = new SetSourceAccount($ruleAction);
|
||||
$result = $action->act($journal);
|
||||
$this->assertFalse($result);
|
||||
|
||||
// test journal for still having old account
|
||||
$journal = TransactionJournal::find($journal->id);
|
||||
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$newSource = $sourceTr->account;
|
||||
$this->assertEquals($source->id, $newSource->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test this on a split journal.
|
||||
*
|
||||
@@ -166,10 +123,14 @@ class SetSourceAccountTest extends TestCase
|
||||
*/
|
||||
public function testSplitJournal()
|
||||
{
|
||||
$transaction = Transaction::orderBy('count', 'DESC')->groupBy('transaction_journal_id')
|
||||
->get(['transaction_journal_id', DB::raw('COUNT(transaction_journal_id) as count')])
|
||||
->first();
|
||||
$journal = TransactionJournal::find($transaction->transaction_journal_id);
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$transaction = Transaction::orderBy('count', 'DESC')->groupBy('transaction_journal_id')
|
||||
->get(['transaction_journal_id', DB::raw('COUNT(transaction_journal_id) as count')])
|
||||
->first();
|
||||
$journal = TransactionJournal::find($transaction->transaction_journal_id);
|
||||
|
||||
// mock
|
||||
$accountRepos->shouldReceive('setUser');
|
||||
// fire the action:
|
||||
$ruleAction = new RuleAction;
|
||||
$ruleAction->action_value = 'Some new asset ' . rand(1, 1000);
|
||||
|
||||
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace Tests\Unit\TransactionRules\Triggers;
|
||||
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Triggers\AmountExactly;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -36,7 +37,11 @@ class AmountExactlyTest extends TestCase
|
||||
*/
|
||||
public function testTriggeredExact()
|
||||
{
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$journalRepos->shouldReceive('setUser');
|
||||
$journalRepos->shouldReceive('getJournalTotal')->andReturn('12.34');
|
||||
$journal = new TransactionJournal;
|
||||
$journal->user = $this->user();
|
||||
$journal->destination_amount = '12.34';
|
||||
$trigger = AmountExactly::makeFromStrings('12.340', false);
|
||||
$result = $trigger->triggered($journal);
|
||||
@@ -48,7 +53,11 @@ class AmountExactlyTest extends TestCase
|
||||
*/
|
||||
public function testTriggeredNotExact()
|
||||
{
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$journalRepos->shouldReceive('setUser');
|
||||
$journalRepos->shouldReceive('getJournalTotal')->andReturn('12.34');
|
||||
$journal = new TransactionJournal;
|
||||
$journal->user = $this->user();
|
||||
$journal->destination_amount = '12.35';
|
||||
$trigger = AmountExactly::makeFromStrings('12.340', false);
|
||||
$result = $trigger->triggered($journal);
|
||||
|
||||
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace Tests\Unit\TransactionRules\Triggers;
|
||||
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Triggers\AmountLess;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -36,8 +37,13 @@ class AmountLessTest extends TestCase
|
||||
*/
|
||||
public function testTriggeredExact()
|
||||
{
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$journalRepos->shouldReceive('setUser');
|
||||
$journalRepos->shouldReceive('getJournalTotal')->andReturn('12.35');
|
||||
|
||||
$journal = new TransactionJournal;
|
||||
$journal->destination_amount = '12.35';
|
||||
$journal->user = $this->user();
|
||||
$trigger = AmountLess::makeFromStrings('12.35', false);
|
||||
$result = $trigger->triggered($journal);
|
||||
$this->assertFalse($result);
|
||||
@@ -48,8 +54,13 @@ class AmountLessTest extends TestCase
|
||||
*/
|
||||
public function testTriggeredLess()
|
||||
{
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$journalRepos->shouldReceive('setUser');
|
||||
$journalRepos->shouldReceive('getJournalTotal')->andReturn('12.34');
|
||||
|
||||
$journal = new TransactionJournal;
|
||||
$journal->destination_amount = '12.34';
|
||||
$journal->user = $this->user();
|
||||
$trigger = AmountLess::makeFromStrings('12.50', false);
|
||||
$result = $trigger->triggered($journal);
|
||||
$this->assertTrue($result);
|
||||
@@ -60,8 +71,13 @@ class AmountLessTest extends TestCase
|
||||
*/
|
||||
public function testTriggeredNotLess()
|
||||
{
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$journalRepos->shouldReceive('setUser');
|
||||
$journalRepos->shouldReceive('getJournalTotal')->andReturn('12.35');
|
||||
|
||||
$journal = new TransactionJournal;
|
||||
$journal->destination_amount = '12.35';
|
||||
$journal->user = $this->user();
|
||||
$trigger = AmountLess::makeFromStrings('12.00', false);
|
||||
$result = $trigger->triggered($journal);
|
||||
$this->assertFalse($result);
|
||||
|
||||
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace Tests\Unit\TransactionRules\Triggers;
|
||||
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Triggers\AmountMore;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -36,7 +37,12 @@ class AmountMoreTest extends TestCase
|
||||
*/
|
||||
public function testTriggeredExact()
|
||||
{
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$journalRepos->shouldReceive('setUser');
|
||||
$journalRepos->shouldReceive('getJournalTotal')->andReturn('12.35');
|
||||
|
||||
$journal = new TransactionJournal;
|
||||
$journal->user = $this->user();
|
||||
$journal->destination_amount = '12.35';
|
||||
$trigger = AmountMore::makeFromStrings('12.35', false);
|
||||
$result = $trigger->triggered($journal);
|
||||
@@ -48,7 +54,12 @@ class AmountMoreTest extends TestCase
|
||||
*/
|
||||
public function testTriggeredMore()
|
||||
{
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$journalRepos->shouldReceive('setUser');
|
||||
$journalRepos->shouldReceive('getJournalTotal')->andReturn('12.34');
|
||||
|
||||
$journal = new TransactionJournal;
|
||||
$journal->user = $this->user();
|
||||
$journal->destination_amount = '12.34';
|
||||
$trigger = AmountMore::makeFromStrings('12.10', false);
|
||||
$result = $trigger->triggered($journal);
|
||||
@@ -60,7 +71,12 @@ class AmountMoreTest extends TestCase
|
||||
*/
|
||||
public function testTriggeredNotMore()
|
||||
{
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$journalRepos->shouldReceive('setUser');
|
||||
$journalRepos->shouldReceive('getJournalTotal')->andReturn('12.35');
|
||||
|
||||
$journal = new TransactionJournal;
|
||||
$journal->user = $this->user();
|
||||
$journal->destination_amount = '12.35';
|
||||
$trigger = AmountMore::makeFromStrings('12.50', false);
|
||||
$result = $trigger->triggered($journal);
|
||||
|
||||
Reference in New Issue
Block a user