. */ declare(strict_types=1); namespace Tests\Unit\TransactionRules\Actions; use FireflyIII\Factory\CategoryFactory; use FireflyIII\Models\RuleAction; use FireflyIII\TransactionRules\Actions\SetCategory; use Tests\TestCase; /** * Class SetCategoryTest * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ class SetCategoryTest extends TestCase { /** * @covers \FireflyIII\TransactionRules\Actions\SetCategory */ public function testAct(): void { // get journal, remove all budgets $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()); // fire the action: $ruleAction = new RuleAction; $ruleAction->action_value = $category->name; $action = new SetCategory($ruleAction); $result = $action->act($journal); $this->assertTrue($result); $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()); } }