2018-07-01 20:19:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* BudgetControllerTest.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-10 21:45:44 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Helpers\Collector\TransactionCollector;
|
|
|
|
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
2018-07-01 20:19:34 +02:00
|
|
|
use FireflyIII\Models\Budget;
|
2018-12-10 21:45:44 +01:00
|
|
|
use FireflyIII\Models\BudgetLimit;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
2018-07-01 20:19:34 +02:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
2018-12-10 21:45:44 +01:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
2018-07-01 20:19:34 +02:00
|
|
|
use Laravel\Passport\Passport;
|
|
|
|
use Log;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class BudgetControllerTest
|
|
|
|
*/
|
|
|
|
class BudgetControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
Passport::actingAs($this->user());
|
2018-09-03 18:52:46 +02:00
|
|
|
Log::info(sprintf('Now in %s.', \get_class($this)));
|
2018-07-01 20:19:34 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 21:45:44 +01:00
|
|
|
/**
|
|
|
|
* Show all budgets
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
|
|
|
*/
|
|
|
|
public function testBudgetLimits(): void
|
|
|
|
{
|
|
|
|
$budget = $this->user()->budgets()->first();
|
|
|
|
$budgetLimits = BudgetLimit::get();
|
|
|
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
|
|
|
|
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('getBudgetLimits')->once()->andReturn($budgetLimits);
|
|
|
|
|
|
|
|
// call API
|
|
|
|
$response = $this->get(route('api.v1.budgets.budget_limits', [$budget->id]));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee('budget_limits');
|
|
|
|
}
|
|
|
|
|
2018-07-01 20:19:34 +02:00
|
|
|
/**
|
|
|
|
* Delete a budget.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
|
|
|
*/
|
|
|
|
public function testDelete(): void
|
|
|
|
{
|
|
|
|
// mock stuff:
|
|
|
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
|
|
|
|
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('destroy')->once()->andReturn(true);
|
|
|
|
|
|
|
|
// get budget:
|
|
|
|
$budget = $this->user()->budgets()->first();
|
|
|
|
|
|
|
|
// call API
|
|
|
|
$response = $this->delete('/api/v1/budgets/' . $budget->id);
|
|
|
|
$response->assertStatus(204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show all budgets
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
|
|
|
*/
|
|
|
|
public function testIndex(): void
|
|
|
|
{
|
|
|
|
$budgets = $this->user()->budgets()->get();
|
|
|
|
// mock stuff:
|
|
|
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
|
|
|
|
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('getBudgets')->once()->andReturn($budgets);
|
|
|
|
|
|
|
|
// call API
|
|
|
|
$response = $this->get('/api/v1/budgets');
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee($budgets->first()->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a single budget.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
|
|
|
*/
|
|
|
|
public function testShow(): void
|
|
|
|
{
|
|
|
|
$budget = $this->user()->budgets()->first();
|
|
|
|
// mock stuff:
|
|
|
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
|
|
|
|
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
|
|
|
|
// call API
|
|
|
|
$response = $this->get('/api/v1/budgets/' . $budget->id);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee($budget->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a new budget.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
2018-08-23 18:33:39 +02:00
|
|
|
* @covers \FireflyIII\Api\V1\Requests\BudgetRequest
|
2018-07-01 20:19:34 +02:00
|
|
|
*/
|
|
|
|
public function testStore(): void
|
|
|
|
{
|
|
|
|
/** @var Budget $budget */
|
|
|
|
$budget = $this->user()->budgets()->first();
|
|
|
|
|
|
|
|
// mock stuff:
|
|
|
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
|
|
|
|
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('store')->once()->andReturn($budget);
|
|
|
|
|
|
|
|
// data to submit
|
|
|
|
$data = [
|
|
|
|
'name' => 'Some budget',
|
|
|
|
'active' => '1',
|
|
|
|
];
|
|
|
|
|
|
|
|
// test API
|
|
|
|
$response = $this->post('/api/v1/budgets', $data);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJson(['data' => ['type' => 'budgets', 'links' => true],]);
|
|
|
|
$response->assertSee($budget->name); // the amount
|
|
|
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
|
|
|
}
|
|
|
|
|
2018-12-10 21:45:44 +01:00
|
|
|
/**
|
|
|
|
* Store new budget limit.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
|
|
|
* @covers \FireflyIII\Api\V1\Requests\BudgetLimitRequest
|
|
|
|
*/
|
|
|
|
public function testStoreBudgetLimit(): void
|
|
|
|
{
|
|
|
|
$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' => 1,
|
|
|
|
];
|
|
|
|
// mock stuff:
|
|
|
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
|
|
|
$repository->shouldReceive('storeBudgetLimit')->andReturn($budgetLimit)->once();
|
|
|
|
|
|
|
|
|
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('storeBudgetLimit')->andReturn($budgetLimit);
|
|
|
|
|
|
|
|
// call API
|
|
|
|
$response = $this->post(route('api.v1.budgets.store_budget_limit', [$budget->id]), $data, ['Accept' => 'application/json']);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee('budget_limits');
|
|
|
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show index.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
|
|
|
*/
|
|
|
|
public function testTransactionsBasic(): void
|
|
|
|
{
|
|
|
|
$budget = $this->user()->budgets()->first();
|
|
|
|
|
|
|
|
// get some transactions using the collector:
|
|
|
|
Log::info('This transaction collector is OK, because it is used in a test:');
|
|
|
|
$collector = new TransactionCollector;
|
|
|
|
$collector->setUser($this->user());
|
|
|
|
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
|
|
|
|
$collector->setAllAssetAccounts();
|
|
|
|
$collector->setLimit(5)->setPage(1);
|
|
|
|
try {
|
|
|
|
$paginator = $collector->getPaginatedTransactions();
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
$this->assertTrue(false, $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
// mock stuff:
|
|
|
|
$repository = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$collector = $this->mock(TransactionCollectorInterface::class);
|
|
|
|
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
|
|
|
$billRepos = $this->mock(BillRepositoryInterface::class);
|
|
|
|
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
|
|
|
|
$billRepos->shouldReceive('setUser');
|
|
|
|
$repository->shouldReceive('setUser');
|
|
|
|
$currencyRepository->shouldReceive('setUser');
|
|
|
|
$budgetRepos->shouldReceive('setUser');
|
|
|
|
|
|
|
|
|
|
|
|
$repository->shouldReceive('getNoteText')->atLeast()->once()->andReturn('Note');
|
|
|
|
$repository->shouldReceive('getMetaField')->atLeast()->once()->andReturn(null);
|
|
|
|
$repository->shouldReceive('getMetaDateString')->atLeast()->once()->andReturn('2018-01-01');
|
|
|
|
|
|
|
|
$collector->shouldReceive('setUser')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setBudget')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('removeFilter')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setLimit')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setPage')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setTypes')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator);
|
|
|
|
|
|
|
|
|
|
|
|
// mock some calls:
|
|
|
|
|
|
|
|
// test API
|
|
|
|
$response = $this->get(route('api.v1.budgets.transactions', [$budget->id]));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJson(['data' => [],]);
|
|
|
|
$response->assertJson(['meta' => ['pagination' => ['total' => true, 'count' => true, 'per_page' => 5, 'current_page' => 1, 'total_pages' => true]],]);
|
|
|
|
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
|
|
|
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show index.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
|
|
|
*/
|
|
|
|
public function testTransactionsRange(): void
|
|
|
|
{
|
|
|
|
$budget = $this->user()->budgets()->first();
|
|
|
|
|
|
|
|
// get some transactions using the collector:
|
|
|
|
Log::info('This transaction collector is OK, because it is used in a test:');
|
|
|
|
$collector = new TransactionCollector;
|
|
|
|
$collector->setUser($this->user());
|
|
|
|
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
|
|
|
|
$collector->setAllAssetAccounts();
|
|
|
|
$collector->setLimit(5)->setPage(1);
|
|
|
|
try {
|
|
|
|
$paginator = $collector->getPaginatedTransactions();
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
$this->assertTrue(false, $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
// mock stuff:
|
|
|
|
$repository = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$collector = $this->mock(TransactionCollectorInterface::class);
|
|
|
|
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
$billRepos = $this->mock(BillRepositoryInterface::class);
|
|
|
|
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
|
|
|
|
$billRepos->shouldReceive('setUser');
|
|
|
|
$repository->shouldReceive('setUser');
|
|
|
|
$currencyRepository->shouldReceive('setUser');
|
|
|
|
$budgetRepos->shouldReceive('setUser');
|
|
|
|
|
|
|
|
$repository->shouldReceive('getNoteText')->atLeast()->once()->andReturn('Note');
|
|
|
|
$repository->shouldReceive('getMetaField')->atLeast()->once()->andReturn(null);
|
|
|
|
$repository->shouldReceive('getMetaDateString')->atLeast()->once()->andReturn('2018-01-01');
|
|
|
|
|
|
|
|
$collector->shouldReceive('setUser')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setBudget')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('removeFilter')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setLimit')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setPage')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setTypes')->andReturnSelf();
|
|
|
|
$collector->shouldReceive('setRange')->andReturnSelf();
|
|
|
|
|
|
|
|
|
|
|
|
$collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator);
|
|
|
|
|
|
|
|
|
|
|
|
// mock some calls:
|
|
|
|
|
|
|
|
// test API
|
|
|
|
$response = $this->get(route('api.v1.budgets.transactions', [$budget->id]) . '?' . http_build_query(['start' => '2018-01-01', 'end' => '2018-01-31']));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJson(['data' => [],]);
|
|
|
|
$response->assertJson(['meta' => ['pagination' => ['total' => true, 'count' => true, 'per_page' => 5, 'current_page' => 1, 'total_pages' => true]],]);
|
|
|
|
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
|
|
|
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
|
|
|
}
|
|
|
|
|
2018-07-01 20:19:34 +02:00
|
|
|
/**
|
|
|
|
* Update a budget.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
2018-08-23 18:33:39 +02:00
|
|
|
* @covers \FireflyIII\Api\V1\Requests\BudgetRequest
|
2018-07-01 20:19:34 +02:00
|
|
|
*/
|
|
|
|
public function testUpdate(): void
|
|
|
|
{
|
|
|
|
// mock repositories
|
|
|
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
|
|
|
|
|
|
|
/** @var Budget $budget */
|
|
|
|
$budget = $this->user()->budgets()->first();
|
|
|
|
|
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser');
|
|
|
|
$repository->shouldReceive('update')->once()->andReturn($budget);
|
|
|
|
|
|
|
|
// data to submit
|
|
|
|
$data = [
|
|
|
|
'name' => 'Some new budget',
|
|
|
|
'active' => '1',
|
|
|
|
];
|
|
|
|
|
|
|
|
// test API
|
|
|
|
$response = $this->put('/api/v1/budgets/' . $budget->id, $data, ['Accept' => 'application/json']);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJson(['data' => ['type' => 'budgets', 'links' => true],]);
|
|
|
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
|
|
|
$response->assertSee($budget->name);
|
|
|
|
}
|
|
|
|
|
2018-07-22 20:33:17 +02:00
|
|
|
}
|