firefly-iii/tests/Api/V1/Controllers/RuleGroupControllerTest.php

227 lines
9.0 KiB
PHP
Raw Normal View History

<?php
/**
* RuleGroupControllerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
2018-12-12 13:30:25 -06:00
use FireflyIII\Jobs\ExecuteRuleOnExistingTransactions;
use FireflyIII\Jobs\Job;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
2018-12-12 13:30:25 -06:00
use FireflyIII\TransactionRules\TransactionMatcher;
2018-12-16 06:55:19 -06:00
use FireflyIII\Transformers\RuleGroupTransformer;
2019-06-09 02:40:08 -05:00
use FireflyIII\Transformers\TransactionGroupTransformer;
2018-12-12 13:30:25 -06:00
use Illuminate\Support\Collection;
use Laravel\Passport\Passport;
use Log;
2018-12-12 13:30:25 -06:00
use Queue;
use Tests\TestCase;
/**
*
* Class RuleGroupControllerTest
2019-06-09 02:40:08 -05:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class RuleGroupControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
2019-04-09 13:05:20 -05:00
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Api\V1\Controllers\RuleGroupController
* @covers \FireflyIII\Api\V1\Requests\RuleGroupRequest
*/
public function testStore(): void
{
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
2018-12-12 13:30:25 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(RuleGroupTransformer::class);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
2018-12-12 13:30:25 -06:00
$accountRepos->shouldReceive('setUser')->once();
$ruleGroupRepos->shouldReceive('setUser')->once();
$ruleGroup = $this->user()->ruleGroups()->first();
$data = [
2019-06-09 02:40:08 -05:00
'title' => 'Store new rule group ' . random_int(1, 100000),
'active' => 1,
'description' => 'Hello',
];
$ruleGroupRepos->shouldReceive('store')->once()->andReturn($ruleGroup);
// test API
2019-06-09 02:40:08 -05:00
$response = $this->post(route('api.v1.rule_groups.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
}
2018-12-12 13:30:25 -06:00
/**
* @covers \FireflyIII\Api\V1\Controllers\RuleGroupController
*/
public function testTestGroupBasic(): void
{
$group = $this->user()->ruleGroups()->first();
$rule = $this->user()->rules()->first();
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$matcher = $this->mock(TransactionMatcher::class);
2019-06-09 02:40:08 -05:00
$transformer = $this->mock(TransactionGroupTransformer::class);
2018-12-16 06:55:19 -06:00
2019-06-09 02:40:08 -05:00
// mock calls
$asset = $this->getRandomAsset();
$expense = $this->getRandomExpense();
2018-12-12 13:30:25 -06:00
2019-06-09 02:40:08 -05:00
$ruleGroupRepos->shouldReceive('setUser')->once();
2018-12-12 13:30:25 -06:00
$repository->shouldReceive('setUser')->once();
2019-06-09 02:40:08 -05:00
$ruleGroupRepos->shouldReceive('getActiveRules')->once()->andReturn(new Collection([$rule]));
2018-12-12 13:30:25 -06:00
$repository->shouldReceive('findNull')->withArgs([1])->andReturn($asset);
2019-06-09 02:40:08 -05:00
$repository->shouldReceive('findNull')->withArgs([2])->andReturn($expense);
2018-12-12 13:30:25 -06:00
$repository->shouldReceive('findNull')->withArgs([3])->andReturn(null);
2019-06-09 02:40:08 -05:00
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
2018-12-12 13:30:25 -06:00
$matcher->shouldReceive('setRule')->once();
$matcher->shouldReceive('setEndDate')->once();
$matcher->shouldReceive('setStartDate')->once();
$matcher->shouldReceive('setSearchLimit')->once();
$matcher->shouldReceive('setTriggeredLimit')->once();
$matcher->shouldReceive('setAccounts')->once();
2019-06-09 02:40:08 -05:00
$matcher->shouldReceive('findTransactionsByRule')->once()->andReturn([]);
2018-12-12 13:30:25 -06:00
// call API
$response = $this->get(route('api.v1.rule_groups.test', [$group->id]) . '?accounts=1,2,3');
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Api\V1\Controllers\RuleGroupController
*/
public function testTestGroupEmpty(): void
{
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('setUser')->once();
$ruleGroupRepos->shouldReceive('setUser')->once();
$ruleGroupRepos->shouldReceive('getActiveRules')->once()->andReturn(new Collection);
// call API
$group = $this->user()->ruleGroups()->first();
2019-06-09 02:40:08 -05:00
$response = $this->get(route('api.v1.rule_groups.test', [$group->id]), ['Accept' => 'application/json']);
2018-12-12 13:30:25 -06:00
$response->assertStatus(500);
2019-06-09 02:40:08 -05:00
$response->assertSee('{"message":"No rules in this rule group.","exception":"FireflyIII\\\\Exceptions\\\\FireflyException"');
2018-12-12 13:30:25 -06:00
}
/**
* @covers \FireflyIII\Api\V1\Controllers\RuleGroupController
*/
public function testTrigger(): void
{
$group = $this->user()->ruleGroups()->first();
$rule = $this->user()->rules()->first();
$asset = $this->getRandomAsset();
2019-06-09 02:40:08 -05:00
$expense = $this->getRandomExpense();
2018-12-12 13:30:25 -06:00
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$matcher = $this->mock(TransactionMatcher::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$ruleGroupRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('setUser')->once();
$ruleGroupRepos->shouldReceive('getActiveRules')->once()->andReturn(new Collection([$rule]));
$repository->shouldReceive('findNull')->withArgs([1])->andReturn($asset);
2019-06-09 02:40:08 -05:00
$repository->shouldReceive('findNull')->withArgs([2])->andReturn($expense);
2018-12-12 13:30:25 -06:00
$repository->shouldReceive('findNull')->withArgs([3])->andReturn(null);
$repository->shouldReceive('isAsset')->withArgs([1])->andReturn(true);
$repository->shouldReceive('isAsset')->withArgs([2])->andReturn(false);
Queue::fake();
2019-06-09 02:40:08 -05:00
$response = $this->post(route('api.v1.rule_groups.trigger', [$group->id]) . '?accounts=1,2,3&start_date=2019-01-01&end_date=2019-01-02');
2018-12-12 13:30:25 -06:00
$response->assertStatus(204);
Queue::assertPushed(
ExecuteRuleOnExistingTransactions::class, function (Job $job) use ($rule) {
return $job->getRule()->id === $rule->id;
}
);
}
/**
* @covers \FireflyIII\Api\V1\Controllers\RuleGroupController
* @covers \FireflyIII\Api\V1\Requests\RuleGroupRequest
*/
public function testUpdate(): void
{
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
2018-12-12 13:30:25 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(RuleGroupTransformer::class);
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
2018-12-12 13:30:25 -06:00
$accountRepos->shouldReceive('setUser')->once();
$ruleGroupRepos->shouldReceive('setUser')->once();
$ruleGroup = $this->user()->ruleGroups()->first();
$data = [
'title' => 'Store new rule ' . random_int(1, 100000),
'active' => 1,
'description' => 'Hello',
];
$ruleGroupRepos->shouldReceive('update')->once()->andReturn($ruleGroup);
// test API
$response = $this->put('/api/v1/rule_groups/' . $ruleGroup->id, $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
}
2018-12-31 00:48:23 -06:00
}