Expand tests for updated triggers / actions.

This commit is contained in:
James Cole 2018-03-04 17:10:36 +01:00
parent d22fb9f438
commit 72314e2d9f
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
9 changed files with 70 additions and 21 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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());
}
}
}

View File

@ -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());
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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);