From 72314e2d9fca0e3e4a1291c7e8e6bebd7d6fc436 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 4 Mar 2018 17:10:36 +0100 Subject: [PATCH] Expand tests for updated triggers / actions. --- app/TransactionRules/Actions/SetBudget.php | 2 +- app/TransactionRules/Actions/SetCategory.php | 2 +- .../Actions/ClearBudgetTest.php | 8 +++++ .../Actions/ClearCategoryTest.php | 6 ++++ .../Actions/SetBudgetTest.php | 8 +++-- .../Actions/SetCategoryTest.php | 11 +++++-- .../Triggers/BudgetIsTest.php | 29 ++++++++++++++----- .../Triggers/HasAttachmentTest.php | 1 - .../Triggers/HasNoBudgetTest.php | 24 ++++++++++----- 9 files changed, 70 insertions(+), 21 deletions(-) diff --git a/app/TransactionRules/Actions/SetBudget.php b/app/TransactionRules/Actions/SetBudget.php index a015d8a1d9..693ec72308 100644 --- a/app/TransactionRules/Actions/SetBudget.php +++ b/app/TransactionRules/Actions/SetBudget.php @@ -91,7 +91,7 @@ class SetBudget implements ActionInterface $journal->budgets()->detach(); // set budget on transactions: /** @var Transaction $transaction */ - foreach ($journal->transaction as $transaction) { + foreach ($journal->transactions as $transaction) { $transaction->budgets()->sync([$budget->id]); } $journal->touch(); diff --git a/app/TransactionRules/Actions/SetCategory.php b/app/TransactionRules/Actions/SetCategory.php index ea1953c603..567cde9f47 100644 --- a/app/TransactionRules/Actions/SetCategory.php +++ b/app/TransactionRules/Actions/SetCategory.php @@ -61,7 +61,7 @@ class SetCategory implements ActionInterface $journal->categories()->detach(); // set category on transactions: /** @var Transaction $transaction */ - foreach ($journal->transaction as $transaction) { + foreach ($journal->transactions as $transaction) { $transaction->categories()->sync([$category->id]); } $journal->touch(); diff --git a/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php b/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php index ddf93eb788..b597ae3992 100644 --- a/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php +++ b/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace Tests\Unit\TransactionRules\Actions; use FireflyIII\Models\RuleAction; +use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\TransactionRules\Actions\ClearBudget; use Tests\TestCase; @@ -53,5 +54,12 @@ class ClearBudgetTest extends TestCase // assert result $this->assertEquals(0, $journal->budgets()->count()); + + /** @var Transaction $transaction */ + foreach($journal->transactions as $transaction) { + $this->assertEquals(0, $transaction->budgets()->count()); + } + + } } diff --git a/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php b/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php index aedee511dd..fa96275bde 100644 --- a/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php +++ b/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace Tests\Unit\TransactionRules\Actions; use FireflyIII\Models\RuleAction; +use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\TransactionRules\Actions\ClearCategory; use Tests\TestCase; @@ -53,5 +54,10 @@ class ClearCategoryTest extends TestCase // assert result $this->assertEquals(0, $journal->categories()->count()); + + /** @var Transaction $transaction */ + foreach($journal->transactions as $transaction) { + $this->assertEquals(0, $transaction->categories()->count()); + } } } diff --git a/tests/Unit/TransactionRules/Actions/SetBudgetTest.php b/tests/Unit/TransactionRules/Actions/SetBudgetTest.php index 6deb1e4a61..1e087f571e 100644 --- a/tests/Unit/TransactionRules/Actions/SetBudgetTest.php +++ b/tests/Unit/TransactionRules/Actions/SetBudgetTest.php @@ -23,6 +23,7 @@ declare(strict_types=1); 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; @@ -56,7 +57,10 @@ class SetBudgetTest extends TestCase $action = new SetBudget($ruleAction); $result = $action->act($journal); $this->assertTrue($result); - $this->assertEquals(1, $journal->budgets()->count()); - $this->assertEquals($budget->name, $journal->budgets()->first()->name); + /** @var Transaction $transaction */ + foreach ($journal->transactions as $transaction) { + $this->assertEquals(1, $transaction->budgets()->count()); + $this->assertEquals($budget->name, $transaction->budgets()->first()->name); + } } } diff --git a/tests/Unit/TransactionRules/Actions/SetCategoryTest.php b/tests/Unit/TransactionRules/Actions/SetCategoryTest.php index b9180f91f6..c49f74df33 100644 --- a/tests/Unit/TransactionRules/Actions/SetCategoryTest.php +++ b/tests/Unit/TransactionRules/Actions/SetCategoryTest.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace Tests\Unit\TransactionRules\Actions; use FireflyIII\Models\RuleAction; +use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\TransactionRules\Actions\SetCategory; use Tests\TestCase; @@ -50,7 +51,13 @@ class SetCategoryTest extends TestCase $action = new SetCategory($ruleAction); $result = $action->act($journal); $this->assertTrue($result); - $this->assertEquals(1, $journal->categories()->count()); - $this->assertEquals($category->name, $journal->categories()->first()->name); + + /** @var Transaction $transaction */ + foreach ($journal->transactions as $transaction) { + $this->assertEquals(1, $transaction->categories()->count()); + $this->assertEquals($category->name, $transaction->categories()->first()->name); + } + + } } diff --git a/tests/Unit/TransactionRules/Triggers/BudgetIsTest.php b/tests/Unit/TransactionRules/Triggers/BudgetIsTest.php index 326d8912a1..06af062137 100644 --- a/tests/Unit/TransactionRules/Triggers/BudgetIsTest.php +++ b/tests/Unit/TransactionRules/Triggers/BudgetIsTest.php @@ -36,8 +36,12 @@ class BudgetIsTest extends TestCase */ public function testTriggeredJournal() { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); - $budget = $journal->user->budgets()->first(); + do { + $journal = TransactionJournal::inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); + + $budget = $journal->user->budgets()->first(); $journal->budgets()->detach(); $journal->budgets()->save($budget); $this->assertEquals(1, $journal->budgets()->count()); @@ -52,7 +56,11 @@ class BudgetIsTest extends TestCase */ public function testTriggeredNotJournal() { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + do { + $journal = TransactionJournal::inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); + $budget = $journal->user->budgets()->first(); $otherBudget = $journal->user->budgets()->where('id', '!=', $budget->id)->first(); $journal->budgets()->detach(); @@ -69,14 +77,21 @@ class BudgetIsTest extends TestCase */ public function testTriggeredTransaction() { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); - $transaction = $journal->transactions()->first(); + do { + $journal = TransactionJournal::inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); + + $transactions = $journal->transactions()->get(); $budget = $journal->user->budgets()->first(); $journal->budgets()->detach(); - $transaction->budgets()->save($budget); + foreach($transactions as $transaction) { + $transaction->budgets()->save($budget); + $this->assertEquals(1, $transaction->budgets()->count()); + } + $this->assertEquals(0, $journal->budgets()->count()); - $this->assertEquals(1, $transaction->budgets()->count()); $trigger = BudgetIs::makeFromStrings($budget->name, false); $result = $trigger->triggered($journal); diff --git a/tests/Unit/TransactionRules/Triggers/HasAttachmentTest.php b/tests/Unit/TransactionRules/Triggers/HasAttachmentTest.php index 5fa574641c..9b2a4316c9 100644 --- a/tests/Unit/TransactionRules/Triggers/HasAttachmentTest.php +++ b/tests/Unit/TransactionRules/Triggers/HasAttachmentTest.php @@ -37,7 +37,6 @@ class HasAttachmentTest extends TestCase public function testTriggered() { do { - // this is kind of cheating but OK. $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); $count = $journal->attachments()->count(); } while($count !== 0); diff --git a/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php b/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php index d1d516094b..b0df151e08 100644 --- a/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php +++ b/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace Tests\Unit\TransactionRules\Triggers; +use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\TransactionRules\Triggers\HasNoBudget; use Tests\TestCase; @@ -36,7 +37,7 @@ class HasNoBudgetTest extends TestCase */ public function testTriggeredBudget() { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + $journal = TransactionJournal::inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first(); $budget = $journal->user->budgets()->first(); $journal->budgets()->detach(); $journal->budgets()->save($budget); @@ -52,8 +53,13 @@ class HasNoBudgetTest extends TestCase */ public function testTriggeredNoBudget() { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); + /** @var TransactionJournal $journal */ + $journal = TransactionJournal::inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first(); $journal->budgets()->detach(); + /** @var Transaction $transaction */ + foreach ($journal->transactions as $transaction) { + $transaction->budgets()->detach(); + } $this->assertEquals(0, $journal->budgets()->count()); $trigger = HasNoBudget::makeFromStrings('', false); @@ -66,14 +72,18 @@ class HasNoBudgetTest extends TestCase */ public function testTriggeredTransaction() { - $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); - $transaction = $journal->transactions()->first(); - $budget = $journal->user->budgets()->first(); + /** @var TransactionJournal $journal */ + $journal = TransactionJournal::inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first(); + $transactions = $journal->transactions()->get(); + $budget = $journal->user->budgets()->first(); $journal->budgets()->detach(); - $transaction->budgets()->save($budget); + /** @var Transaction $transaction */ + foreach ($transactions as $transaction) { + $transaction->budgets()->sync([$budget->id]); + $this->assertEquals(1, $transaction->budgets()->count()); + } $this->assertEquals(0, $journal->budgets()->count()); - $this->assertEquals(1, $transaction->budgets()->count()); $trigger = HasNoBudget::makeFromStrings('', false); $result = $trigger->triggered($journal);