From b049ca27f1c2a2ac67498b3506e7b81917a4bcbf Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 1 Aug 2019 06:21:44 +0200 Subject: [PATCH] Improve test coverage. --- app/Repositories/Bill/BillRepository.php | 6 +- app/TransactionRules/Actions/AddTag.php | 1 - app/TransactionRules/Actions/ClearBudget.php | 2 +- app/TransactionRules/Actions/LinkToBill.php | 10 +- app/TransactionRules/Actions/RemoveTag.php | 2 +- app/TransactionRules/Actions/SetBudget.php | 14 +-- app/TransactionRules/Actions/SetCategory.php | 11 +-- .../Actions/SetDestinationAccount.php | 9 +- .../Actions/SetSourceAccount.php | 9 +- phpunit.coverage.specific.xml | 2 + phpunit.coverage.xml | 1 + phpunit.xml | 1 + .../TransactionRules/Actions/AddTagTest.php | 76 +++++++++++---- .../Actions/AppendDescriptionTest.php | 9 +- .../Actions/AppendNotesTest.php | 2 +- .../Actions/ClearBudgetTest.php | 4 +- .../Actions/ClearCategoryTest.php | 4 +- .../Actions/ClearNotesTest.php | 2 +- .../Actions/ConvertToDepositTest.php | 10 +- .../Actions/ConvertToTransferTest.php | 27 +++--- .../Actions/ConvertToWithdrawalTest.php | 9 -- .../Actions/LinkToBillTest.php | 95 +++++++++++++++++++ .../Actions/PrependDescriptionTest.php | 4 +- .../Actions/PrependNotesTest.php | 5 +- .../Actions/RemoveTagTest.php | 5 +- .../Actions/SetBudgetTest.php | 60 ++++++++++-- .../Actions/SetCategoryTest.php | 41 ++++++-- .../Actions/SetDescriptionTest.php | 4 +- .../Actions/SetDestinationAccountTest.php | 40 +------- .../TransactionRules/Actions/SetNotesTest.php | 8 +- .../Actions/SetSourceAccountTest.php | 40 +------- .../Triggers/HasAnyCategoryTest.php | 6 +- .../Triggers/HasAnyTagTest.php | 4 +- .../Triggers/HasNoBudgetTest.php | 2 +- .../Triggers/HasNoCategoryTest.php | 6 +- .../Triggers/HasNoTagTest.php | 4 +- .../Triggers/NotesAnyTest.php | 6 +- .../Triggers/NotesAreTest.php | 8 +- .../Triggers/NotesContainTest.php | 10 +- .../Triggers/NotesEmptyTest.php | 6 +- .../Triggers/NotesEndTest.php | 6 +- .../Triggers/NotesStartTest.php | 6 +- .../TransactionRules/Triggers/TagIsTest.php | 4 +- .../Triggers/ToAccountEndsTest.php | 2 +- .../Triggers/TransactionTypeTest.php | 4 +- 45 files changed, 336 insertions(+), 251 deletions(-) create mode 100644 tests/Unit/TransactionRules/Actions/LinkToBillTest.php diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index de033da43c..2bf4680ea7 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -576,12 +576,12 @@ class BillRepository implements BillRepositoryInterface } // find the most recent date for this bill NOT in the future. Cache this date: $start = clone $bill->date; - Log::debug('nextDateMatch: Start is ' . $start->format('Y-m-d')); + //Log::debug('nextDateMatch: Start is ' . $start->format('Y-m-d')); while ($start < $date) { - Log::debug(sprintf('$start (%s) < $date (%s)', $start->format('Y-m-d'), $date->format('Y-m-d'))); + //Log::debug(sprintf('$start (%s) < $date (%s)', $start->format('Y-m-d'), $date->format('Y-m-d'))); $start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip); - Log::debug('Start is now ' . $start->format('Y-m-d')); + //Log::debug('Start is now ' . $start->format('Y-m-d')); } $end = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip); diff --git a/app/TransactionRules/Actions/AddTag.php b/app/TransactionRules/Actions/AddTag.php index 4da72dcf74..252afd1f8e 100644 --- a/app/TransactionRules/Actions/AddTag.php +++ b/app/TransactionRules/Actions/AddTag.php @@ -73,7 +73,6 @@ class AddTag implements ActionInterface return true; } - Log::debug(sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal->id)); return false; diff --git a/app/TransactionRules/Actions/ClearBudget.php b/app/TransactionRules/Actions/ClearBudget.php index 0047f0903f..4d0ae7f241 100644 --- a/app/TransactionRules/Actions/ClearBudget.php +++ b/app/TransactionRules/Actions/ClearBudget.php @@ -53,7 +53,7 @@ class ClearBudget implements ActionInterface $journal->budgets()->detach(); $journal->touch(); - // also remove categories from transactions: + // also remove budgets from transactions (although no longer necessary) /** @var Transaction $transaction */ foreach ($journal->transactions as $transaction) { $transaction->budgets()->detach(); diff --git a/app/TransactionRules/Actions/LinkToBill.php b/app/TransactionRules/Actions/LinkToBill.php index d0bd43240c..e51fb72f34 100644 --- a/app/TransactionRules/Actions/LinkToBill.php +++ b/app/TransactionRules/Actions/LinkToBill.php @@ -39,6 +39,8 @@ class LinkToBill implements ActionInterface /** * TriggerInterface constructor. * + * @codeCoverageIgnore + * * @param RuleAction $action */ public function __construct(RuleAction $action) @@ -65,13 +67,13 @@ class LinkToBill implements ActionInterface $journal->bill()->associate($bill); $journal->save(); Log::debug(sprintf('RuleAction LinkToBill set the bill of journal #%d to bill #%d ("%s").', $journal->id, $bill->id, $bill->name)); + + return true; } - if (null === $bill) { - Log::error(sprintf('RuleAction LinkToBill could not set the bill of journal #%d to bill "%s": no such bill found!', $journal->id, $billName)); - } + Log::error(sprintf('RuleAction LinkToBill could not set the bill of journal #%d to bill "%s": no such bill found!', $journal->id, $billName)); - return true; + return false; } } diff --git a/app/TransactionRules/Actions/RemoveTag.php b/app/TransactionRules/Actions/RemoveTag.php index dd81dedfd5..bb20c0453f 100644 --- a/app/TransactionRules/Actions/RemoveTag.php +++ b/app/TransactionRules/Actions/RemoveTag.php @@ -47,7 +47,7 @@ class RemoveTag implements ActionInterface /** * Remove tag X - * + * TODO the filter is no longer necessary. * @param TransactionJournal $journal * * @return bool diff --git a/app/TransactionRules/Actions/SetBudget.php b/app/TransactionRules/Actions/SetBudget.php index 693ec72308..ca9b317af0 100644 --- a/app/TransactionRules/Actions/SetBudget.php +++ b/app/TransactionRules/Actions/SetBudget.php @@ -49,7 +49,8 @@ class SetBudget implements ActionInterface } /** - * Set budget X + * Set budget. + * TODO the filter is no longer necessary. * * @param TransactionJournal $journal * @@ -63,14 +64,14 @@ class SetBudget implements ActionInterface $search = $this->action->action_value; $budgets = $repository->getActiveBudgets(); $budget = $budgets->filter( - function (Budget $current) use ($search) { + static function (Budget $current) use ($search) { return $current->name === $search; } )->first(); if (null === $budget) { Log::debug(sprintf('RuleAction SetBudget could not set budget of journal #%d to "%s" because no such budget exists.', $journal->id, $search)); - return true; + return false; } if (TransactionType::WITHDRAWAL !== $journal->transactionType->type) { @@ -88,12 +89,7 @@ class SetBudget implements ActionInterface Log::debug(sprintf('RuleAction SetBudget set the budget of journal #%d to budget #%d ("%s").', $journal->id, $budget->id, $budget->name)); - $journal->budgets()->detach(); - // set budget on transactions: - /** @var Transaction $transaction */ - foreach ($journal->transactions as $transaction) { - $transaction->budgets()->sync([$budget->id]); - } + $journal->budgets()->sync([$budget->id]); $journal->touch(); return true; diff --git a/app/TransactionRules/Actions/SetCategory.php b/app/TransactionRules/Actions/SetCategory.php index 500d7bd559..04b1c77d23 100644 --- a/app/TransactionRules/Actions/SetCategory.php +++ b/app/TransactionRules/Actions/SetCategory.php @@ -64,18 +64,11 @@ class SetCategory implements ActionInterface if (null === $category) { Log::error(sprintf('Action SetCategory did not fire because "%s" did not result in a valid category.', $name)); - return true; + return false; } - $journal->categories()->detach(); - // set category on transactions: - /** @var Transaction $transaction */ - foreach ($journal->transactions as $transaction) { - $transaction->categories()->sync([$category->id]); - } - $journal->touch(); + $journal->categories()->sync([$category->id]); - $journal->touch(); Log::debug(sprintf('RuleAction SetCategory set the category of journal #%d to category #%d ("%s").', $journal->id, $category->id, $category->name)); return true; diff --git a/app/TransactionRules/Actions/SetDestinationAccount.php b/app/TransactionRules/Actions/SetDestinationAccount.php index 3737a01ab6..885fc6e2ff 100644 --- a/app/TransactionRules/Actions/SetDestinationAccount.php +++ b/app/TransactionRules/Actions/SetDestinationAccount.php @@ -69,13 +69,6 @@ class SetDestinationAccount implements ActionInterface $this->journal = $journal; $this->repository = app(AccountRepositoryInterface::class); $this->repository->setUser($journal->user); - $count = $journal->transactions()->count(); - if ($count > 2) { - Log::error(sprintf('Cannot change destination account of journal #%d because it is a split journal.', $journal->id)); - - return false; - } - // journal type: $type = $journal->transactionType->type; @@ -103,7 +96,7 @@ class SetDestinationAccount implements ActionInterface // get destination transaction: $transaction = $journal->transactions()->where('amount', '>', 0)->first(); if (null === $transaction) { - return true; + return true; // @codeCoverageIgnore } $transaction->account_id = $this->newDestinationAccount->id; $transaction->save(); diff --git a/app/TransactionRules/Actions/SetSourceAccount.php b/app/TransactionRules/Actions/SetSourceAccount.php index fd23cba980..9622f4ea4e 100644 --- a/app/TransactionRules/Actions/SetSourceAccount.php +++ b/app/TransactionRules/Actions/SetSourceAccount.php @@ -69,13 +69,6 @@ class SetSourceAccount implements ActionInterface $this->journal = $journal; $this->repository = app(AccountRepositoryInterface::class); $this->repository->setUser($journal->user); - $count = $journal->transactions()->count(); - if ($count > 2) { - Log::error(sprintf('Cannot change source account of journal #%d because it is a split journal.', $journal->id)); - - return false; - } - // journal type: $type = $journal->transactionType->type; // if this is a transfer or a withdrawal, the new source account must be an asset account or a default account, and it MUST exist: @@ -102,9 +95,11 @@ class SetSourceAccount implements ActionInterface // get source transaction: $transaction = $journal->transactions()->where('amount', '<', 0)->first(); if (null === $transaction) { + // @codeCoverageIgnoreStart Log::error(sprintf('Cannot change source account of journal #%d because no source transaction exists.', $journal->id)); return false; + // @codeCoverageIgnoreEnd } $transaction->account_id = $this->newSourceAccount->id; $transaction->save(); diff --git a/phpunit.coverage.specific.xml b/phpunit.coverage.specific.xml index 34a20e5a32..f956f821d8 100644 --- a/phpunit.coverage.specific.xml +++ b/phpunit.coverage.specific.xml @@ -49,6 +49,8 @@ ./tests/Unit/Rules ./tests/Unit/Services ./tests/Unit/Support + ./tests/Unit/TransactionRules + ./tests/Feature diff --git a/phpunit.coverage.xml b/phpunit.coverage.xml index 0541833e2c..f2cbdc495f 100644 --- a/phpunit.coverage.xml +++ b/phpunit.coverage.xml @@ -49,6 +49,7 @@ ./tests/Unit/Rules ./tests/Unit/Services ./tests/Unit/Support + ./tests/Unit/TransactionRules ./tests/Feature diff --git a/phpunit.xml b/phpunit.xml index 85e0cb367c..e5c11e216a 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -49,6 +49,7 @@ ./tests/Unit/Rules ./tests/Unit/Services ./tests/Unit/Support + ./tests/Unit/TransactionRules ./tests/Feature diff --git a/tests/Unit/TransactionRules/Actions/AddTagTest.php b/tests/Unit/TransactionRules/Actions/AddTagTest.php index bb95a0be36..e59f426e0c 100644 --- a/tests/Unit/TransactionRules/Actions/AddTagTest.php +++ b/tests/Unit/TransactionRules/Actions/AddTagTest.php @@ -24,8 +24,6 @@ namespace Tests\Unit\TransactionRules\Actions; use FireflyIII\Factory\TagFactory; use FireflyIII\Models\RuleAction; -use FireflyIII\Models\Tag; -use FireflyIII\Models\TransactionJournal; use FireflyIII\TransactionRules\Actions\AddTag; use Log; use Tests\TestCase; @@ -49,42 +47,84 @@ class AddTagTest extends TestCase */ public function testActExistingTag(): void { - $tag = $this->user()->tags()->inRandomOrder()->whereNull('deleted_at')->first(); - $tagFactory = $this->mock(TagFactory::class); + $tag = $this->getRandomTag(); + $journal = $this->getRandomWithdrawal(); + + // make sure journal has no tags: + $journal->tags()->sync([]); + $journal->save(); + + // add single existing tag: + $journal->tags()->sync([$tag->id]); + + $tagFactory->shouldReceive('setUser')->once(); $tagFactory->shouldReceive('findOrCreate')->once()->withArgs([$tag->tag])->andReturn($tag); - - /** @var TransactionJournal $journal */ - $journal = $this->user()->transactionJournals()->inRandomOrder()->whereNull('deleted_at')->first(); - $journal->tags()->sync([]); - $journal->tags()->sync([$tag->id]); + // assert connection exists. $this->assertDatabaseHas('tag_transaction_journal', ['tag_id' => $tag->id, 'transaction_journal_id' => $journal->id]); + + // file action $ruleAction = new RuleAction; $ruleAction->action_value = $tag->tag; - $action = new AddTag($ruleAction); $result = $action->act($journal); $this->assertFalse($result); + + // assert DB is unchanged. + $this->assertDatabaseHas('tag_transaction_journal', ['tag_id' => $tag->id, 'transaction_journal_id' => $journal->id]); + } + + + /** + * @covers \FireflyIII\TransactionRules\Actions\AddTag + */ + public function testActNewTag(): void + { + $tagFactory = $this->mock(TagFactory::class); + $tag = $this->getRandomTag(); + $journal = $this->getRandomWithdrawal(); + + // make sure journal has no tags: + $journal->tags()->sync([]); + $journal->save(); + + $tagFactory->shouldReceive('setUser')->once(); + $tagFactory->shouldReceive('findOrCreate')->once()->withArgs([$tag->tag])->andReturn($tag); + + // assert connection does not exist. + $this->assertDatabaseMissing('tag_transaction_journal', ['tag_id' => $tag->id, 'transaction_journal_id' => $journal->id]); + + // file action + $ruleAction = new RuleAction; + $ruleAction->action_value = $tag->tag; + $action = new AddTag($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // assert DB is unchanged. $this->assertDatabaseHas('tag_transaction_journal', ['tag_id' => $tag->id, 'transaction_journal_id' => $journal->id]); } /** * @covers \FireflyIII\TransactionRules\Actions\AddTag */ - public function testActNoTag(): void + public function testActNullTag(): void { - $newTagName = 'TestTag-' . $th; - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + // try to add non-existing tag + $tagFactory = $this->mock(TagFactory::class); + $newTagName = 'TestTag-' . $this->randomInt(); + + // should return null: + $tagFactory->shouldReceive('setUser')->once(); + $tagFactory->shouldReceive('findOrCreate')->once()->withArgs([$newTagName])->andReturnNull(); + + $journal = $this->getRandomWithdrawal(); $ruleAction = new RuleAction; $ruleAction->action_value = $newTagName; $action = new AddTag($ruleAction); $result = $action->act($journal); - $this->assertTrue($result); - - // find newly created tag: - $tag = Tag::orderBy('id', 'DESC')->first(); - $this->assertDatabaseHas('tag_transaction_journal', ['tag_id' => $tag->id, 'transaction_journal_id' => $journal->id]); + $this->assertFalse($result); } } diff --git a/tests/Unit/TransactionRules/Actions/AppendDescriptionTest.php b/tests/Unit/TransactionRules/Actions/AppendDescriptionTest.php index 35c2f0d846..ccd90bf204 100644 --- a/tests/Unit/TransactionRules/Actions/AppendDescriptionTest.php +++ b/tests/Unit/TransactionRules/Actions/AppendDescriptionTest.php @@ -49,11 +49,10 @@ class AppendDescriptionTest extends TestCase { $ruleAction = new RuleAction; $ruleAction->action_value = 'APPEND'; - - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); - $oldDescription = $journal->description; - $action = new AppendDescription($ruleAction); - $result = $action->act($journal); + $journal = $this->getRandomWithdrawal(); + $oldDescription = $journal->description; + $action = new AppendDescription($ruleAction); + $result = $action->act($journal); $this->assertTrue($result); $journal = TransactionJournal::find($journal->id); diff --git a/tests/Unit/TransactionRules/Actions/AppendNotesTest.php b/tests/Unit/TransactionRules/Actions/AppendNotesTest.php index 4f211d1af8..35cb34f279 100644 --- a/tests/Unit/TransactionRules/Actions/AppendNotesTest.php +++ b/tests/Unit/TransactionRules/Actions/AppendNotesTest.php @@ -49,7 +49,7 @@ class AppendNotesTest extends TestCase public function testAct(): void { // give journal some notes. - $journal = TransactionJournal::find(3); + $journal = $this->getRandomWithdrawal(); $note = $journal->notes()->first(); $start = 'Default note text'; $toAppend = 'This is appended'; diff --git a/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php b/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php index cbe7b3fb82..eb44a61107 100644 --- a/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php +++ b/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php @@ -49,8 +49,8 @@ class ClearBudgetTest extends TestCase public function testAct(): void { // associate budget with journal: - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); - $budget = $journal->user->budgets()->first(); + $journal = $this->getRandomWithdrawal(); + $budget = $this->getRandomBudget(); $journal->budgets()->save($budget); $this->assertGreaterThan(0, $journal->budgets()->count()); diff --git a/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php b/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php index a07f7849a2..dfbfd8904d 100644 --- a/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php +++ b/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php @@ -49,8 +49,8 @@ class ClearCategoryTest extends TestCase public function testAct(): void { // associate budget with journal: - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); - $category = $journal->user->categories()->first(); + $journal = $this->getRandomWithdrawal(); + $category = $this->getRandomCategory();; $journal->categories()->save($category); $this->assertGreaterThan(0, $journal->categories()->count()); diff --git a/tests/Unit/TransactionRules/Actions/ClearNotesTest.php b/tests/Unit/TransactionRules/Actions/ClearNotesTest.php index aed570d0da..7312c451ba 100644 --- a/tests/Unit/TransactionRules/Actions/ClearNotesTest.php +++ b/tests/Unit/TransactionRules/Actions/ClearNotesTest.php @@ -51,7 +51,7 @@ class ClearNotesTest extends TestCase public function testAct(): void { // give journal a note: - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $note = $journal->notes()->first(); if (null === $note) { $note = new Note; diff --git a/tests/Unit/TransactionRules/Actions/ConvertToDepositTest.php b/tests/Unit/TransactionRules/Actions/ConvertToDepositTest.php index 8e55fb94d2..1aa250a2b9 100644 --- a/tests/Unit/TransactionRules/Actions/ConvertToDepositTest.php +++ b/tests/Unit/TransactionRules/Actions/ConvertToDepositTest.php @@ -53,11 +53,8 @@ class ConvertToDepositTest extends TestCase * * @covers \FireflyIII\TransactionRules\Actions\ConvertToDeposit */ - public function testActTransfer() + public function testActTransfer(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $revenue = $this->getRandomRevenue(); $name = 'Random revenue #' . $this->randomInt(); $journal = $this->getRandomTransfer(); @@ -92,11 +89,8 @@ class ConvertToDepositTest extends TestCase * * @covers \FireflyIII\TransactionRules\Actions\ConvertToDeposit */ - public function testActWithdrawal() + public function testActWithdrawal(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $revenue = $this->getRandomRevenue(); $name = 'Random revenue #' . $this->randomInt(); $journal = $this->getRandomWithdrawal(); diff --git a/tests/Unit/TransactionRules/Actions/ConvertToTransferTest.php b/tests/Unit/TransactionRules/Actions/ConvertToTransferTest.php index 92d8c027c5..2f7f03f6de 100644 --- a/tests/Unit/TransactionRules/Actions/ConvertToTransferTest.php +++ b/tests/Unit/TransactionRules/Actions/ConvertToTransferTest.php @@ -27,6 +27,7 @@ namespace Tests\Unit\TransactionRules\Actions; use Exception; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; +use FireflyIII\Models\Rule; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; @@ -56,24 +57,21 @@ class ConvertToTransferTest extends TestCase */ public function testActDeposit(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $deposit = $this->getRandomDeposit(); /** @var Account $asset */ - $asset = $this->user()->accounts()->where('name', 'Bitcoin Account')->first(); - // journal is a withdrawal: - $this->assertEquals(TransactionType::DEPOSIT, $deposit->transactionType->type); + $asset = $this->getRandomAsset(); // mock used stuff: $accountRepos = $this->mock(AccountRepositoryInterface::class); $accountRepos->shouldReceive('setUser')->once(); - $accountRepos->shouldReceive('findByName')->withArgs([$asset->name, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]]) - ->andReturn($asset); + $accountRepos->shouldReceive('findByName')->withArgs([$asset->name, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]])->andReturn($asset); // fire the action: + $rule = new Rule; + $rule->title = 'OK'; $ruleAction = new RuleAction; $ruleAction->action_value = $asset->name; + $ruleAction->rule = $rule; $action = new ConvertToTransfer($ruleAction); try { @@ -95,24 +93,21 @@ class ConvertToTransferTest extends TestCase */ public function testActWithdrawal(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $withdrawal = $this->getRandomWithdrawal(); /** @var Account $asset */ - $asset = $this->user()->accounts()->where('name', 'Bitcoin Account')->first(); - // journal is a withdrawal: - $this->assertEquals(TransactionType::WITHDRAWAL, $withdrawal->transactionType->type); + $asset = $this->getRandomAsset(); // mock used stuff: $accountRepos = $this->mock(AccountRepositoryInterface::class); $accountRepos->shouldReceive('setUser')->once(); - $accountRepos->shouldReceive('findByName')->withArgs([$asset->name, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]]) - ->andReturn($asset); + $accountRepos->shouldReceive('findByName')->withArgs([$asset->name, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]])->andReturn($asset); // fire the action: + $rule = new Rule; + $rule->title = 'OK'; $ruleAction = new RuleAction; $ruleAction->action_value = $asset->name; + $ruleAction->rule = $rule; $action = new ConvertToTransfer($ruleAction); try { diff --git a/tests/Unit/TransactionRules/Actions/ConvertToWithdrawalTest.php b/tests/Unit/TransactionRules/Actions/ConvertToWithdrawalTest.php index b13c52c013..5260327622 100644 --- a/tests/Unit/TransactionRules/Actions/ConvertToWithdrawalTest.php +++ b/tests/Unit/TransactionRules/Actions/ConvertToWithdrawalTest.php @@ -55,9 +55,6 @@ class ConvertToWithdrawalTest extends TestCase */ public function testActDeposit() { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $expense = $this->getRandomExpense(); $name = 'Random expense #' . $this->randomInt(); $deposit = $this->getRandomDeposit(); @@ -94,16 +91,10 @@ class ConvertToWithdrawalTest extends TestCase */ public function testActTransfer() { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $expense = $this->getRandomExpense(); $name = 'Random expense #' . $this->randomInt(); $transfer = $this->getRandomTransfer(); - // journal is a transfer: - $this->assertEquals(TransactionType::TRANSFER, $transfer->transactionType->type); - // mock used stuff: $factory = $this->mock(AccountFactory::class); $factory->shouldReceive('setUser')->once(); diff --git a/tests/Unit/TransactionRules/Actions/LinkToBillTest.php b/tests/Unit/TransactionRules/Actions/LinkToBillTest.php new file mode 100644 index 0000000000..92d0e4d80c --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/LinkToBillTest.php @@ -0,0 +1,95 @@ +. + */ + +namespace Tests\Unit\TransactionRules\Actions; + + +use FireflyIII\Models\RuleAction; +use FireflyIII\Repositories\Bill\BillRepositoryInterface; +use FireflyIII\TransactionRules\Actions\LinkToBill; +use Log; +use Tests\TestCase; + +/** + * Class LinkToBillTest + */ +class LinkToBillTest extends TestCase +{ + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::info(sprintf('Now in %s.', get_class($this))); + } + + /** + * @covers \FireflyIII\TransactionRules\Actions\LinkToBill + */ + public function testBasic(): void + { + $repos = $this->mock(BillRepositoryInterface::class); + $withdrawal = $this->getRandomWithdrawal(); + $rule = $this->getRandomRule(); + $bill = $this->getRandomBill(); + $ruleAction = new RuleAction; + $ruleAction->rule = $rule; + $ruleAction->action_type = 'link_to_bill'; + $ruleAction->action_value = $bill->name; + + $repos->shouldReceive('setUser'); + $repos->shouldReceive('findByName')->withArgs([$bill->name])->andReturn($bill); + + $action = new LinkToBill($ruleAction); + $result = $action->act($withdrawal); + + $this->assertTrue($result); + + + } + + + /** + * @covers \FireflyIII\TransactionRules\Actions\LinkToBill + */ + public function testNoBill(): void + { + $repos = $this->mock(BillRepositoryInterface::class); + $withdrawal = $this->getRandomWithdrawal(); + $rule = $this->getRandomRule(); + $bill = $this->getRandomBill(); + $ruleAction = new RuleAction; + $ruleAction->rule = $rule; + $ruleAction->action_type = 'link_to_bill'; + $ruleAction->action_value = $bill->name; + + $repos->shouldReceive('setUser'); + $repos->shouldReceive('findByName')->withArgs([$bill->name])->andReturnNull(); + + $action = new LinkToBill($ruleAction); + $result = $action->act($withdrawal); + + $this->assertFalse($result); + + + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/PrependDescriptionTest.php b/tests/Unit/TransactionRules/Actions/PrependDescriptionTest.php index 03f3ed9a63..2fecc0b909 100644 --- a/tests/Unit/TransactionRules/Actions/PrependDescriptionTest.php +++ b/tests/Unit/TransactionRules/Actions/PrependDescriptionTest.php @@ -39,8 +39,8 @@ class PrependDescriptionTest extends TestCase { // get journal, give fixed description $description = 'text' . $this->randomInt(); - $prepend = 'prepend' . random_int(1, 1234); - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $prepend = 'prepend' . $this->randomInt(); + $journal = $this->getRandomWithdrawal(); $journal->description = $description; $journal->save(); diff --git a/tests/Unit/TransactionRules/Actions/PrependNotesTest.php b/tests/Unit/TransactionRules/Actions/PrependNotesTest.php index 0d031ab25b..1cd0d7430e 100644 --- a/tests/Unit/TransactionRules/Actions/PrependNotesTest.php +++ b/tests/Unit/TransactionRules/Actions/PrependNotesTest.php @@ -39,7 +39,7 @@ class PrependNotesTest extends TestCase public function testAct(): void { // give journal some notes. - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $note = $journal->notes()->first(); $start = 'Default note text'; $toPrepend = 'This is prepended'; @@ -62,13 +62,12 @@ class PrependNotesTest extends TestCase } /** - * @covers \FireflyIII\TransactionRules\Actions\PrependNotes() * @covers \FireflyIII\TransactionRules\Actions\PrependNotes */ public function testActNewNote(): void { // give journal some notes. - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $note = $journal->notes()->first(); if (null !== $note) { $note->forceDelete(); diff --git a/tests/Unit/TransactionRules/Actions/RemoveTagTest.php b/tests/Unit/TransactionRules/Actions/RemoveTagTest.php index c74bb1f9f8..5cff507acb 100644 --- a/tests/Unit/TransactionRules/Actions/RemoveTagTest.php +++ b/tests/Unit/TransactionRules/Actions/RemoveTagTest.php @@ -60,21 +60,20 @@ class RemoveTagTest extends TestCase } /** - * @covers \FireflyIII\TransactionRules\Actions\RemoveTag() * @covers \FireflyIII\TransactionRules\Actions\RemoveTag */ public function testActNoTag(): void { // get journal, link al tags: /** @var TransactionJournal $journal */ - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $tags = $journal->user->tags()->get(); $journal->tags()->sync($tags->pluck('id')->toArray()); $this->assertEquals($tags->count(), $journal->tags()->get()->count()); // fire the action: $ruleAction = new RuleAction; - $ruleAction->action_value = random_int(1, 1234) . 'nosuchtag'; + $ruleAction->action_value = $this->randomInt() . 'nosuchtag'; $action = new RemoveTag($ruleAction); $result = $action->act($journal); $this->assertTrue($result); diff --git a/tests/Unit/TransactionRules/Actions/SetBudgetTest.php b/tests/Unit/TransactionRules/Actions/SetBudgetTest.php index 90c211368c..b31dc936b1 100644 --- a/tests/Unit/TransactionRules/Actions/SetBudgetTest.php +++ b/tests/Unit/TransactionRules/Actions/SetBudgetTest.php @@ -24,7 +24,6 @@ namespace Tests\Unit\TransactionRules\Actions; use FireflyIII\Models\RuleAction; use FireflyIII\Models\Transaction; -use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\TransactionRules\Actions\SetBudget; use Illuminate\Support\Collection; @@ -41,8 +40,57 @@ class SetBudgetTest extends TestCase public function testAct(): void { // get journal, remove all budgets - $journal = TransactionJournal::inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first(); - $budget = $journal->user->budgets()->first(); + $journal = $this->getRandomWithdrawal(); + $budget = $this->getRandomBudget(); + $budgetRepos = $this->mock(BudgetRepositoryInterface::class); + $budgetRepos->shouldReceive('setUser'); + $budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget])); + + $journal->budgets()->sync([]); + $this->assertEquals(0, $journal->budgets()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $budget->name; + $action = new SetBudget($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + $this->assertEquals(1, $journal->budgets()->count()); + } + + /** + * @covers \FireflyIII\TransactionRules\Actions\SetBudget + */ + public function testActNull(): void + { + // get journal, remove all budgets + $journal = $this->getRandomWithdrawal(); + $budget = $this->getRandomBudget(); + $budgetRepos = $this->mock(BudgetRepositoryInterface::class); + $budgetRepos->shouldReceive('setUser'); + $budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection); + + $journal->budgets()->sync([]); + $this->assertEquals(0, $journal->budgets()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $budget->name; + $action = new SetBudget($ruleAction); + $result = $action->act($journal); + $this->assertFalse($result); + $this->assertEquals(0, $journal->budgets()->count()); + } + + + /** + * @covers \FireflyIII\TransactionRules\Actions\SetBudget + */ + public function testActDeposit(): void + { + // get journal, remove all budgets + $journal = $this->getRandomDeposit(); + $budget = $this->getRandomBudget(); $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $budgetRepos->shouldReceive('setUser'); $budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget])); @@ -56,10 +104,6 @@ class SetBudgetTest extends TestCase $action = new SetBudget($ruleAction); $result = $action->act($journal); $this->assertTrue($result); - /** @var Transaction $transaction */ - foreach ($journal->transactions as $transaction) { - $this->assertEquals(1, $transaction->budgets()->count()); - $this->assertEquals($budget->name, $transaction->budgets()->first()->name); - } + $this->assertEquals(0, $journal->budgets()->count()); } } diff --git a/tests/Unit/TransactionRules/Actions/SetCategoryTest.php b/tests/Unit/TransactionRules/Actions/SetCategoryTest.php index 81fe994f1d..fd5a8e4508 100644 --- a/tests/Unit/TransactionRules/Actions/SetCategoryTest.php +++ b/tests/Unit/TransactionRules/Actions/SetCategoryTest.php @@ -22,9 +22,8 @@ declare(strict_types=1); namespace Tests\Unit\TransactionRules\Actions; +use FireflyIII\Factory\CategoryFactory; use FireflyIII\Models\RuleAction; -use FireflyIII\Models\Transaction; -use FireflyIII\Models\TransactionJournal; use FireflyIII\TransactionRules\Actions\SetCategory; use Tests\TestCase; @@ -39,8 +38,13 @@ class SetCategoryTest extends TestCase public function testAct(): void { // get journal, remove all budgets - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); - $category = $journal->user->categories()->first(); + $journal = $this->getRandomWithdrawal(); + $category = $this->getRandomCategory(); + + $factory = $this->mock(CategoryFactory::class); + $factory->shouldReceive('setUser'); + $factory->shouldReceive('findOrCreate')->andReturn($category); + $journal->categories()->detach(); $this->assertEquals(0, $journal->categories()->count()); @@ -51,12 +55,31 @@ class SetCategoryTest extends TestCase $result = $action->act($journal); $this->assertTrue($result); - /** @var Transaction $transaction */ - foreach ($journal->transactions as $transaction) { - $this->assertEquals(1, $transaction->categories()->count()); - $this->assertEquals($category->name, $transaction->categories()->first()->name); - } + $this->assertEquals(1, $journal->categories()->count()); + } + /** + * @covers \FireflyIII\TransactionRules\Actions\SetCategory + */ + public function testActNull(): void + { + $factory = $this->mock(CategoryFactory::class); + $factory->shouldReceive('setUser'); + $factory->shouldReceive('findOrCreate')->andReturnNull(); + // get journal, remove all budgets + $journal = $this->getRandomWithdrawal(); + $category = $this->getRandomCategory(); + $journal->categories()->detach(); + $this->assertEquals(0, $journal->categories()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $category->name; + $action = new SetCategory($ruleAction); + $result = $action->act($journal); + $this->assertFalse($result); + + $this->assertEquals(0, $journal->categories()->count()); } } diff --git a/tests/Unit/TransactionRules/Actions/SetDescriptionTest.php b/tests/Unit/TransactionRules/Actions/SetDescriptionTest.php index 71de5fedb4..18fb81b894 100644 --- a/tests/Unit/TransactionRules/Actions/SetDescriptionTest.php +++ b/tests/Unit/TransactionRules/Actions/SetDescriptionTest.php @@ -39,8 +39,8 @@ class SetDescriptionTest extends TestCase { // get journal, give fixed description $description = 'text' . $this->randomInt(); - $newDescription = 'new description' . random_int(1, 1234); - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $newDescription = 'new description' . $this->randomInt(); + $journal = $this->getRandomWithdrawal(); $journal->description = $description; $journal->save(); diff --git a/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php b/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php index 5ad3610c92..b0e2f80384 100644 --- a/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php +++ b/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php @@ -45,9 +45,6 @@ class SetDestinationAccountTest extends TestCase */ public function testActDepositExisting(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $accountRepos = $this->mock(AccountRepositoryInterface::class); $deposit = $this->getRandomDeposit(); $destinationTr = $deposit->transactions()->where('amount', '>', 0)->first(); @@ -82,9 +79,6 @@ class SetDestinationAccountTest extends TestCase */ public function testActDepositNotExisting(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $accountRepos = $this->mock(AccountRepositoryInterface::class); $deposit = $this->getRandomDeposit(); @@ -107,11 +101,8 @@ class SetDestinationAccountTest extends TestCase */ public function testActWithDrawalNotExisting(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $accountRepos = $this->mock(AccountRepositoryInterface::class); - $account = $this->user()->accounts()->inRandomOrder()->where('account_type_id', 4)->first(); + $account = $this->getRandomExpense(); $withdrawal = $this->getRandomWithdrawal(); // find account? Return account: @@ -135,9 +126,6 @@ class SetDestinationAccountTest extends TestCase */ public function testActWithdrawalExisting(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $accountRepos = $this->mock(AccountRepositoryInterface::class); $withdrawal = $this->getRandomWithdrawal(); $destinationTr = $withdrawal->transactions()->where('amount', '>', 0)->first(); @@ -165,30 +153,4 @@ class SetDestinationAccountTest extends TestCase $this->assertEquals($newDestination->id, $account->id); } - /** - * Test this on a split journal. - * - * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount - */ - public function testSplitJournal(): void - { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; - $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 ' . $this->randomInt(); - $action = new SetDestinationAccount($ruleAction); - $result = $action->act($journal); - $this->assertFalse($result); - } } diff --git a/tests/Unit/TransactionRules/Actions/SetNotesTest.php b/tests/Unit/TransactionRules/Actions/SetNotesTest.php index cea8af371f..e72b25a79f 100644 --- a/tests/Unit/TransactionRules/Actions/SetNotesTest.php +++ b/tests/Unit/TransactionRules/Actions/SetNotesTest.php @@ -39,7 +39,7 @@ class SetNotesTest extends TestCase public function testAct(): void { // give journal a note: - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $note = $journal->notes()->first(); if (null === $note) { $note = new Note; @@ -51,7 +51,7 @@ class SetNotesTest extends TestCase // fire the action: $ruleAction = new RuleAction; - $ruleAction->action_value = 'These are new notes ' . random_int(1, 1234); + $ruleAction->action_value = 'These are new notes ' . $this->randomInt(); $action = new SetNotes($ruleAction); $result = $action->act($journal); $this->assertTrue($result); @@ -68,13 +68,13 @@ class SetNotesTest extends TestCase public function testActNoNotes(): void { // give journal a note: - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->forceDelete(); $this->assertEquals(0, $journal->notes()->count()); // fire the action: $ruleAction = new RuleAction; - $ruleAction->action_value = 'These are new notes ' . random_int(1, 1234); + $ruleAction->action_value = 'These are new notes ' . $this->randomInt(); $action = new SetNotes($ruleAction); $result = $action->act($journal); $this->assertTrue($result); diff --git a/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php b/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php index 61982826b1..4ec0b63ecb 100644 --- a/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php +++ b/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php @@ -43,9 +43,6 @@ class SetSourceAccountTest extends TestCase */ public function testActDepositExistingUpdated(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $accountRepos = $this->mock(AccountRepositoryInterface::class); $deposit = $this->getRandomDeposit(); $sourceTr = $deposit->transactions()->where('amount', '<', 0)->first(); @@ -80,11 +77,8 @@ class SetSourceAccountTest extends TestCase */ public function testActDepositRevenue(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $accountRepos = $this->mock(AccountRepositoryInterface::class); - $account = $this->user()->accounts()->inRandomOrder()->where('account_type_id', 5)->first(); + $account = $this->getRandomRevenue(); $deposit = $this->getRandomDeposit(); $accountRepos->shouldReceive('setUser'); @@ -106,9 +100,6 @@ class SetSourceAccountTest extends TestCase */ public function testActWithdrawalExistingUpdated(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $accountRepos = $this->mock(AccountRepositoryInterface::class); $withdrawal = $this->getRandomWithdrawal(); @@ -144,9 +135,6 @@ class SetSourceAccountTest extends TestCase */ public function testActWithdrawalNotExisting(): void { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; $accountRepos = $this->mock(AccountRepositoryInterface::class); $withdrawal = $this->getRandomWithdrawal(); @@ -160,30 +148,4 @@ class SetSourceAccountTest extends TestCase $result = $action->act($withdrawal); $this->assertFalse($result); } - - /** - * Test this on a split journal. - * - * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount - */ - public function testSplitJournal(): void - { - $this->markTestIncomplete('Needs to be rewritten for v4.8.0'); - - return; - $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 ' . $this->randomInt(); - $action = new SetSourceAccount($ruleAction); - $result = $action->act($journal); - $this->assertFalse($result); - } } diff --git a/tests/Unit/TransactionRules/Triggers/HasAnyCategoryTest.php b/tests/Unit/TransactionRules/Triggers/HasAnyCategoryTest.php index 9043523267..b00f0cb502 100644 --- a/tests/Unit/TransactionRules/Triggers/HasAnyCategoryTest.php +++ b/tests/Unit/TransactionRules/Triggers/HasAnyCategoryTest.php @@ -37,8 +37,8 @@ class HasAnyCategoryTest extends TestCase */ public function testTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); - $category = $journal->user->categories()->first(); + $journal = $this->getRandomWithdrawal(); + $category = $this->getRandomCategory();; $journal->categories()->detach(); $journal->categories()->save($category); @@ -53,7 +53,7 @@ class HasAnyCategoryTest extends TestCase */ public function testTriggeredNot(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->categories()->detach(); // also detach transactions: diff --git a/tests/Unit/TransactionRules/Triggers/HasAnyTagTest.php b/tests/Unit/TransactionRules/Triggers/HasAnyTagTest.php index c2c0c7d1e6..2ae4c2b9d2 100644 --- a/tests/Unit/TransactionRules/Triggers/HasAnyTagTest.php +++ b/tests/Unit/TransactionRules/Triggers/HasAnyTagTest.php @@ -36,7 +36,7 @@ class HasAnyTagTest extends TestCase */ public function testTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $tag = $journal->user->tags()->first(); $journal->tags()->detach(); $journal->tags()->save($tag); @@ -52,7 +52,7 @@ class HasAnyTagTest extends TestCase */ public function testTriggeredNot(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->tags()->detach(); $this->assertEquals(0, $journal->tags()->count()); $trigger = HasAnyTag::makeFromStrings('', false); diff --git a/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php b/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php index 511779aa0b..3caa876dca 100644 --- a/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php +++ b/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php @@ -38,7 +38,7 @@ class HasNoBudgetTest extends TestCase public function testTriggeredBudget(): void { $journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first(); - $budget = $journal->user->budgets()->first(); + $budget = $this->getRandomBudget(); $journal->budgets()->detach(); $journal->budgets()->save($budget); $this->assertEquals(1, $journal->budgets()->count()); diff --git a/tests/Unit/TransactionRules/Triggers/HasNoCategoryTest.php b/tests/Unit/TransactionRules/Triggers/HasNoCategoryTest.php index fb48439231..0787df027d 100644 --- a/tests/Unit/TransactionRules/Triggers/HasNoCategoryTest.php +++ b/tests/Unit/TransactionRules/Triggers/HasNoCategoryTest.php @@ -37,8 +37,8 @@ class HasNoCategoryTest extends TestCase */ public function testTriggeredCategory(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); - $category = $journal->user->categories()->first(); + $journal = $this->getRandomWithdrawal(); + $category = $this->getRandomCategory();; $journal->categories()->detach(); $journal->categories()->save($category); $this->assertEquals(1, $journal->categories()->count()); @@ -53,7 +53,7 @@ class HasNoCategoryTest extends TestCase */ public function testTriggeredNoCategory(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->categories()->detach(); // also detach transactions: diff --git a/tests/Unit/TransactionRules/Triggers/HasNoTagTest.php b/tests/Unit/TransactionRules/Triggers/HasNoTagTest.php index 0e6997c174..771cd818ff 100644 --- a/tests/Unit/TransactionRules/Triggers/HasNoTagTest.php +++ b/tests/Unit/TransactionRules/Triggers/HasNoTagTest.php @@ -36,7 +36,7 @@ class HasNoTagTest extends TestCase */ public function testTriggeredNoTag(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->tags()->detach(); $this->assertEquals(0, $journal->tags()->count()); @@ -50,7 +50,7 @@ class HasNoTagTest extends TestCase */ public function testTriggeredTag(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $tag = $journal->user->tags()->first(); $journal->tags()->detach(); $journal->tags()->save($tag); diff --git a/tests/Unit/TransactionRules/Triggers/NotesAnyTest.php b/tests/Unit/TransactionRules/Triggers/NotesAnyTest.php index 2aebc4f2d6..3ceed437c4 100644 --- a/tests/Unit/TransactionRules/Triggers/NotesAnyTest.php +++ b/tests/Unit/TransactionRules/Triggers/NotesAnyTest.php @@ -37,7 +37,7 @@ class NotesAnyTest extends TestCase */ public function testTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -53,7 +53,7 @@ class NotesAnyTest extends TestCase */ public function testTriggeredEmpty(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -69,7 +69,7 @@ class NotesAnyTest extends TestCase */ public function testTriggeredNone(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $trigger = NotesAny::makeFromStrings('', false); $result = $trigger->triggered($journal); diff --git a/tests/Unit/TransactionRules/Triggers/NotesAreTest.php b/tests/Unit/TransactionRules/Triggers/NotesAreTest.php index b5eb7d9be9..8160f5ebff 100644 --- a/tests/Unit/TransactionRules/Triggers/NotesAreTest.php +++ b/tests/Unit/TransactionRules/Triggers/NotesAreTest.php @@ -37,7 +37,7 @@ class NotesAreTest extends TestCase */ public function testTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -53,7 +53,7 @@ class NotesAreTest extends TestCase */ public function testTriggeredDifferent(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -69,7 +69,7 @@ class NotesAreTest extends TestCase */ public function testTriggeredEmpty(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -85,7 +85,7 @@ class NotesAreTest extends TestCase */ public function testTriggeredNone(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $trigger = NotesAre::makeFromStrings('Bla bla', false); $result = $trigger->triggered($journal); diff --git a/tests/Unit/TransactionRules/Triggers/NotesContainTest.php b/tests/Unit/TransactionRules/Triggers/NotesContainTest.php index e84e20a321..2af0854b47 100644 --- a/tests/Unit/TransactionRules/Triggers/NotesContainTest.php +++ b/tests/Unit/TransactionRules/Triggers/NotesContainTest.php @@ -37,7 +37,7 @@ class NotesContainTest extends TestCase */ public function testTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -53,7 +53,7 @@ class NotesContainTest extends TestCase */ public function testTriggeredDifferent(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -69,7 +69,7 @@ class NotesContainTest extends TestCase */ public function testTriggeredEmpty(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -85,7 +85,7 @@ class NotesContainTest extends TestCase */ public function testTriggeredNone(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $trigger = NotesContain::makeFromStrings('Bla bla', false); $result = $trigger->triggered($journal); @@ -97,7 +97,7 @@ class NotesContainTest extends TestCase */ public function testTriggeredPartial(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); diff --git a/tests/Unit/TransactionRules/Triggers/NotesEmptyTest.php b/tests/Unit/TransactionRules/Triggers/NotesEmptyTest.php index f46a9ad19d..6eb2950148 100644 --- a/tests/Unit/TransactionRules/Triggers/NotesEmptyTest.php +++ b/tests/Unit/TransactionRules/Triggers/NotesEmptyTest.php @@ -37,7 +37,7 @@ class NotesEmptyTest extends TestCase */ public function testTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $trigger = NotesEmpty::makeFromStrings('', false); $result = $trigger->triggered($journal); @@ -49,7 +49,7 @@ class NotesEmptyTest extends TestCase */ public function testTriggeredEmpty(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -65,7 +65,7 @@ class NotesEmptyTest extends TestCase */ public function testTriggeredPartial(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); diff --git a/tests/Unit/TransactionRules/Triggers/NotesEndTest.php b/tests/Unit/TransactionRules/Triggers/NotesEndTest.php index e39550c6bd..ab2e602dc3 100644 --- a/tests/Unit/TransactionRules/Triggers/NotesEndTest.php +++ b/tests/Unit/TransactionRules/Triggers/NotesEndTest.php @@ -37,7 +37,7 @@ class NotesEndTest extends TestCase */ public function testTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -53,7 +53,7 @@ class NotesEndTest extends TestCase */ public function testTriggeredLonger(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -69,7 +69,7 @@ class NotesEndTest extends TestCase */ public function testTriggeredNoMatch(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); diff --git a/tests/Unit/TransactionRules/Triggers/NotesStartTest.php b/tests/Unit/TransactionRules/Triggers/NotesStartTest.php index abc5377454..02f516206a 100644 --- a/tests/Unit/TransactionRules/Triggers/NotesStartTest.php +++ b/tests/Unit/TransactionRules/Triggers/NotesStartTest.php @@ -37,7 +37,7 @@ class NotesStartTest extends TestCase */ public function testTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -53,7 +53,7 @@ class NotesStartTest extends TestCase */ public function testTriggeredLonger(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); @@ -69,7 +69,7 @@ class NotesStartTest extends TestCase */ public function testTriggeredNoMatch(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->notes()->delete(); $note = new Note(); $note->noteable()->associate($journal); diff --git a/tests/Unit/TransactionRules/Triggers/TagIsTest.php b/tests/Unit/TransactionRules/Triggers/TagIsTest.php index 139d29ed2f..7c0c386aa8 100644 --- a/tests/Unit/TransactionRules/Triggers/TagIsTest.php +++ b/tests/Unit/TransactionRules/Triggers/TagIsTest.php @@ -37,7 +37,7 @@ class TagIsTest extends TestCase */ public function testNotTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->tags()->detach(); $this->assertEquals(0, $journal->tags()->count()); @@ -51,7 +51,7 @@ class TagIsTest extends TestCase */ public function testTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $journal->tags()->detach(); /** @var Collection $tags */ $tags = $journal->user->tags()->take(3)->get(); diff --git a/tests/Unit/TransactionRules/Triggers/ToAccountEndsTest.php b/tests/Unit/TransactionRules/Triggers/ToAccountEndsTest.php index 87d4943f3c..ca048e696a 100644 --- a/tests/Unit/TransactionRules/Triggers/ToAccountEndsTest.php +++ b/tests/Unit/TransactionRules/Triggers/ToAccountEndsTest.php @@ -82,7 +82,7 @@ class ToAccountEndsTest extends TestCase $collection = new Collection([$account]); $repository->shouldReceive('getJournalDestinationAccounts')->once()->andReturn($collection); - $trigger = ToAccountEnds::makeFromStrings((string)random_int(1, 1234), false); + $trigger = ToAccountEnds::makeFromStrings((string)$this->randomInt(), false); $result = $trigger->triggered($journal); $this->assertFalse($result); } diff --git a/tests/Unit/TransactionRules/Triggers/TransactionTypeTest.php b/tests/Unit/TransactionRules/Triggers/TransactionTypeTest.php index 342db6a5d0..d3bf8f8476 100644 --- a/tests/Unit/TransactionRules/Triggers/TransactionTypeTest.php +++ b/tests/Unit/TransactionRules/Triggers/TransactionTypeTest.php @@ -36,7 +36,7 @@ class TransactionTypeTest extends TestCase */ public function testTriggered(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $type = $journal->transactionType->type; $trigger = TransactionType::makeFromStrings($type, false); $result = $trigger->triggered($journal); @@ -48,7 +48,7 @@ class TransactionTypeTest extends TestCase */ public function testTriggeredFalse(): void { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = $this->getRandomWithdrawal(); $trigger = TransactionType::makeFromStrings('NonExisting', false); $result = $trigger->triggered($journal); $this->assertFalse($result);