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

173 lines
6.0 KiB
PHP
Raw Normal View History

2018-07-01 21:02:39 +02:00
<?php
/**
* BudgetLimitControllerTest.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;
2019-06-29 19:47:31 +02:00
2018-07-01 21:02:39 +02:00
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2018-12-16 13:55:19 +01:00
use FireflyIII\Transformers\BudgetLimitTransformer;
2018-07-01 21:02:39 +02:00
use Laravel\Passport\Passport;
use Log;
use Tests\TestCase;
/**
*
* Class BudgetLimitControllerTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-07-01 21:02:39 +02:00
*/
class BudgetLimitControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-07-01 21:02:39 +02:00
}
/**
* Store new budget limit.
*
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
*/
public function testStore(): void
{
$budget = $this->user()->budgets()->first();
2018-12-16 13:55:19 +01:00
$transformer = $this->mock(BudgetLimitTransformer::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-07-01 21:02:39 +02:00
$budgetLimit = BudgetLimit::create(
[
2018-12-10 21:45:44 +01:00
'budget_id' => $budget->id,
'start_date' => '2018-01-01',
'end_date' => '2018-01-31',
'amount' => 1,
2018-07-01 21:02:39 +02:00
]
);
$data
= [
2018-12-09 20:54:11 +01:00
'budget_id' => $budget->id,
'start' => '2018-01-01',
'end' => '2018-01-31',
'amount' => 1,
2018-07-01 21:02:39 +02:00
];
// mock stuff:
$repository = $this->mock(BudgetRepositoryInterface::class);
$repository->shouldReceive('findNull')->andReturn($budget)->once();
$repository->shouldReceive('storeBudgetLimit')->andReturn($budgetLimit)->once();
// mock calls:
$repository->shouldReceive('setUser')->once();
// call API
2018-12-16 13:55:19 +01:00
$response = $this->post(route('api.v1.budget_limits.store'), $data, ['Accept' => 'application/json']);
2018-07-01 21:02:39 +02: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 20:54:11 +01:00
'budget_id' => '1',
'start' => '2018-01-01',
'end' => '2018-01-31',
'amount' => 1,
2018-07-01 21:02:39 +02:00
];
// mock stuff:
$repository = $this->mock(BudgetRepositoryInterface::class);
$repository->shouldReceive('findNull')->andReturn(null)->once();
2018-12-16 13:55:19 +01:00
$transformer = $this->mock(BudgetLimitTransformer::class);
2018-07-01 21:02:39 +02:00
// mock calls:
$repository->shouldReceive('setUser')->once();
// call API
2019-08-29 17:53:25 +02:00
Log::warning('The following error is part of a test.');
2018-12-16 13:55:19 +01:00
$response = $this->post(route('api.v1.budget_limits.store'), $data);
2018-07-01 21:02:39 +02:00
$response->assertStatus(500);
$response->assertSee('Unknown budget.');
2018-07-01 21:02:39 +02:00
}
/**
* Test update of budget limit.
*
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
*/
public function testUpdate(): void
{
$transformer = $this->mock(BudgetLimitTransformer::class);
2018-07-01 21:02:39 +02:00
$budget = $this->user()->budgets()->first();
$budgetLimit = BudgetLimit::create(
[
2018-12-10 21:45:44 +01:00
'budget_id' => $budget->id,
'start_date' => '2018-01-01',
'end_date' => '2018-01-31',
'amount' => 1,
2018-07-01 21:02:39 +02:00
]
);
$data
= [
2018-12-09 20:54:11 +01:00
'budget_id' => $budget->id,
'start' => '2018-01-01',
'end' => '2018-01-31',
'amount' => 2,
2018-07-01 21:02:39 +02:00
];
// mock stuff:
$repository = $this->mock(BudgetRepositoryInterface::class);
$repository->shouldReceive('updateBudgetLimit')->andReturn($budgetLimit)->once();
2018-12-16 13:55:19 +01: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 21:02:39 +02:00
// mock calls:
$repository->shouldReceive('setUser')->once();
// call API
2019-06-09 10:27:11 +02:00
$response = $this->put(route('api.v1.budget_limits.update', [$budgetLimit->id]), $data);
2018-07-01 21:02:39 +02:00
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
2018-07-22 20:33:17 +02:00
}