. */ declare(strict_types=1); namespace Tests\Api\V1\Controllers; use FireflyIII\Models\BudgetLimit; use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Transformers\BudgetLimitTransformer; use Laravel\Passport\Passport; use Log; use Tests\TestCase; /** * * Class BudgetLimitControllerTest * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ class BudgetLimitControllerTest extends TestCase { /** * */ public function setUp(): void { parent::setUp(); Passport::actingAs($this->user()); $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } /** * Store new budget limit. * * @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController */ public function testStore(): void { $budget = $this->user()->budgets()->first(); $transformer = $this->mock(BudgetLimitTransformer::class); $repository = $this->mock(BudgetRepositoryInterface::class); $blRepository = $this->mock(BudgetLimitRepositoryInterface::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]); $budgetLimit = BudgetLimit::create( [ 'budget_id' => $budget->id, 'start_date' => '2018-01-01', 'end_date' => '2018-01-31', 'amount' => 1, ] ); $data = [ 'budget_id' => $budget->id, 'start' => '2018-01-01', 'end' => '2018-01-31', 'amount' => 1, ]; // mock stuff: $repository->shouldReceive('findNull')->andReturn($budget)->once(); $blRepository->shouldReceive('storeBudgetLimit')->andReturn($budgetLimit)->once(); // mock calls: $repository->shouldReceive('setUser')->once(); $blRepository->shouldReceive('setUser')->once(); // call API $response = $this->post(route('api.v1.budget_limits.store'), $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } /** * Store new budget limit, but give error * * @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController */ public function testStoreBadBudget(): void { $data = [ 'budget_id' => '1', 'start' => '2018-01-01', 'end' => '2018-01-31', 'amount' => 1, ]; // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); //$repository->shouldReceive('findNull')->andReturn(null)->once(); $transformer = $this->mock(BudgetLimitTransformer::class); $blRepository = $this->mock(BudgetLimitRepositoryInterface::class); // mock calls: $repository->shouldReceive('setUser')->once(); // call API Log::warning('The following error is part of a test.'); $response = $this->post(route('api.v1.budget_limits.store'), $data); $response->assertStatus(500); $response->assertSee('Unknown budget.'); } /** * Test update of budget limit. * * @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController */ public function testUpdate(): void { $transformer = $this->mock(BudgetLimitTransformer::class); $blRepository = $this->mock(BudgetLimitRepositoryInterface::class); $budget = $this->user()->budgets()->first(); $budgetLimit = BudgetLimit::create( [ 'budget_id' => $budget->id, 'start_date' => '2018-01-01', 'end_date' => '2018-01-31', 'amount' => 1, ] ); $data = [ 'budget_id' => $budget->id, 'start' => '2018-01-01', 'end' => '2018-01-31', 'amount' => 2, ]; // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); $blRepository->shouldReceive('updateBudgetLimit')->andReturn($budgetLimit)->once(); // 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]); // mock calls: $repository->shouldReceive('setUser')->once(); $blRepository->shouldReceive('setUser')->once(); // call API $response = $this->put(route('api.v1.budget_limits.update', [$budgetLimit->id]), $data); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } }