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

179 lines
6.5 KiB
PHP
Raw Normal View History

2018-07-01 14:02:39 -05:00
<?php
/**
* BudgetLimitControllerTest.php
2020-02-16 06:59:55 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-01 14:02:39 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-01 14:02:39 -05:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-07-01 14:02:39 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-07-01 14:02:39 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-07-01 14:02:39 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-01 14:02:39 -05:00
*/
declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
use FireflyIII\Models\BudgetLimit;
2019-09-04 10:39:39 -05:00
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
2018-07-01 14:02:39 -05:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2018-12-16 06:55:19 -06:00
use FireflyIII\Transformers\BudgetLimitTransformer;
2018-07-01 14:02:39 -05:00
use Laravel\Passport\Passport;
use Log;
use Tests\TestCase;
/**
*
* Class BudgetLimitControllerTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-07-01 14:02:39 -05:00
*/
class BudgetLimitControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
2020-03-15 09:32:09 -05:00
$this->mockDefaultConfiguration();
2019-04-09 13:05:20 -05:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-07-01 14:02:39 -05:00
}
/**
* Store new budget limit.
*
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
*/
public function testStore(): void
{
2019-09-04 10:39:39 -05:00
$budget = $this->user()->budgets()->first();
$transformer = $this->mock(BudgetLimitTransformer::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$blRepository = $this->mock(BudgetLimitRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
// 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-07-01 14:02:39 -05:00
$budgetLimit = BudgetLimit::create(
[
2018-12-10 14:45:44 -06:00
'budget_id' => $budget->id,
'start_date' => '2018-01-01',
'end_date' => '2018-01-31',
'amount' => 1,
2018-07-01 14:02:39 -05:00
]
);
$data
= [
2018-12-09 13:54:11 -06:00
'budget_id' => $budget->id,
'start' => '2018-01-01',
'end' => '2018-01-31',
'amount' => 1,
2018-07-01 14:02:39 -05:00
];
// mock stuff:
$repository->shouldReceive('findNull')->andReturn($budget)->once();
2019-09-04 10:39:39 -05:00
$blRepository->shouldReceive('storeBudgetLimit')->andReturn($budgetLimit)->once();
2018-07-01 14:02:39 -05:00
// mock calls:
$repository->shouldReceive('setUser')->once();
2019-09-04 10:39:39 -05:00
$blRepository->shouldReceive('setUser')->once();
2018-07-01 14:02:39 -05:00
// call API
2018-12-16 06:55:19 -06:00
$response = $this->post(route('api.v1.budget_limits.store'), $data, ['Accept' => 'application/json']);
2018-07-01 14:02:39 -05:00
$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
= [
2018-12-09 13:54:11 -06:00
'budget_id' => '1',
'start' => '2018-01-01',
'end' => '2018-01-31',
'amount' => 1,
2018-07-01 14:02:39 -05:00
];
// mock stuff:
$repository = $this->mock(BudgetRepositoryInterface::class);
2019-09-04 10:39:39 -05:00
//$repository->shouldReceive('findNull')->andReturn(null)->once();
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(BudgetLimitTransformer::class);
2019-09-04 10:39:39 -05:00
$blRepository = $this->mock(BudgetLimitRepositoryInterface::class);
2018-07-01 14:02:39 -05:00
// mock calls:
$repository->shouldReceive('setUser')->once();
// call API
2019-08-29 10:53:25 -05:00
Log::warning('The following error is part of a test.');
2018-12-16 06:55:19 -06:00
$response = $this->post(route('api.v1.budget_limits.store'), $data);
2018-07-01 14:02:39 -05:00
$response->assertStatus(500);
$response->assertSee('Unknown budget.');
2018-07-01 14:02:39 -05:00
}
/**
* Test update of budget limit.
*
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
*/
public function testUpdate(): void
{
$transformer = $this->mock(BudgetLimitTransformer::class);
2019-09-04 10:39:39 -05:00
$blRepository = $this->mock(BudgetLimitRepositoryInterface::class);
2018-07-01 14:02:39 -05:00
$budget = $this->user()->budgets()->first();
$budgetLimit = BudgetLimit::create(
[
2018-12-10 14:45:44 -06:00
'budget_id' => $budget->id,
'start_date' => '2018-01-01',
'end_date' => '2018-01-31',
'amount' => 1,
2018-07-01 14:02:39 -05:00
]
);
$data
= [
2018-12-09 13:54:11 -06:00
'budget_id' => $budget->id,
'start' => '2018-01-01',
'end' => '2018-01-31',
'amount' => 2,
2018-07-01 14:02:39 -05:00
];
// mock stuff:
$repository = $this->mock(BudgetRepositoryInterface::class);
2019-09-04 10:39:39 -05:00
$blRepository->shouldReceive('updateBudgetLimit')->andReturn($budgetLimit)->once();
2018-12-16 06:55:19 -06:00
// 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-07-01 14:02:39 -05:00
// mock calls:
$repository->shouldReceive('setUser')->once();
2019-09-04 10:39:39 -05:00
$blRepository->shouldReceive('setUser')->once();
2018-07-01 14:02:39 -05:00
// call API
2019-06-09 03:27:11 -05:00
$response = $this->put(route('api.v1.budget_limits.update', [$budgetLimit->id]), $data);
2018-07-01 14:02:39 -05:00
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
2018-07-22 13:33:17 -05:00
}