. */ declare(strict_types=1); namespace Tests\Unit\TransactionRules\Triggers; use FireflyIII\Models\TransactionJournal; use FireflyIII\TransactionRules\Triggers\FromAccountIs; use Tests\TestCase; use Log; /** * Class FromAccountIsTest */ class FromAccountIsTest extends TestCase { /** * @covers \FireflyIII\TransactionRules\Triggers\FromAccountIs::triggered */ public function testTriggered(): void { $loops = 0; // FINAL LOOP METHOD. do { /** @var TransactionJournal $journal */ $journal = $this->user()->transactionJournals()->inRandomOrder()->whereNull('deleted_at')->first(); $transaction = $journal->transactions()->where('amount', '<', 0)->first(); $account = $transaction->account; $count = $journal->transactions()->count(); Log::debug(sprintf('Loop: %d, transaction count: %d, account is null: %d', $loops, $count, (int)null===$account)); $loops++; // do this until: account is not null, journal has two transactions, loops is below 30 } while (!(null !== $account && 2 === $count && $loops < 30)); $trigger = FromAccountIs::makeFromStrings($account->name, false); $result = $trigger->triggered($journal); $this->assertTrue($result); } /** * @covers \FireflyIII\TransactionRules\Triggers\FromAccountIs::triggered */ public function testTriggeredNot(): void { $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); $trigger = FromAccountIs::makeFromStrings('some name' . random_int(1, 234), false); $result = $trigger->triggered($journal); $this->assertFalse($result); } /** * @covers \FireflyIII\TransactionRules\Triggers\FromAccountIs::willMatchEverything */ public function testWillMatchEverythingEmpty(): void { $value = ''; $result = FromAccountIs::willMatchEverything($value); $this->assertTrue($result); } /** * @covers \FireflyIII\TransactionRules\Triggers\FromAccountIs::willMatchEverything */ public function testWillMatchEverythingNotNull(): void { $value = 'x'; $result = FromAccountIs::willMatchEverything($value); $this->assertFalse($result); } /** * @covers \FireflyIII\TransactionRules\Triggers\FromAccountIs::willMatchEverything */ public function testWillMatchEverythingNull(): void { $value = null; $result = FromAccountIs::willMatchEverything($value); $this->assertTrue($result); } }