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

278 lines
12 KiB
PHP
Raw Normal View History

2019-06-09 15:28:54 +02:00
<?php
/**
* SummaryControllerTest.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program 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 Affero General Public License for more details.
*
* 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/>.
*/
2019-08-17 12:09:03 +02:00
declare(strict_types=1);
2019-06-09 15:28:54 +02:00
namespace Tests\Api\V1\Controllers;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Report\NetWorthInterface;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2019-09-04 17:39:39 +02:00
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
2019-06-09 15:28:54 +02:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2019-09-04 17:39:39 +02:00
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
2019-06-09 15:28:54 +02:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection;
use Laravel\Passport\Passport;
use Log;
use Mockery;
use Tests\TestCase;
/**
* Class SummaryControllerTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class SummaryControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
2019-09-04 17:39:39 +02:00
* Also includes NULL currencies for better coverage.
*
2019-06-09 15:28:54 +02:00
* @covers \FireflyIII\Api\V1\Controllers\SummaryController
*/
2019-09-04 17:39:39 +02:00
public function testBasicInTheFuture(): void
2019-06-09 15:28:54 +02:00
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$billRepos = $this->mock(BillRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$netWorth = $this->mock(NetWorthInterface::class);
2019-09-04 17:39:39 +02:00
$abRepos = $this->mock(AvailableBudgetRepositoryInterface::class);
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$date = new Carbon();
$date->addWeek();
2019-06-09 15:28:54 +02:00
// data
2019-07-27 13:54:06 +02:00
$euro = $this->getEuro();
2019-06-09 15:28:54 +02:00
$budget = $this->user()->budgets()->inRandomOrder()->first();
$account = $this->getRandomAsset();
$journals = [
[
'amount' => '10',
'currency_id' => 1,
],
2019-09-04 17:39:39 +02:00
[
'amount' => '10',
'currency_id' => 2,
],
2019-06-09 15:28:54 +02:00
];
$netWorthData = [
[
'currency' => $euro,
'balance' => '232',
],
2019-09-04 17:39:39 +02:00
[
'currency' => $euro,
'balance' => '0',
],
2019-06-09 15:28:54 +02:00
];
// mock calls.
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$billRepos->shouldReceive('setUser')->atLeast()->once();
$budgetRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$netWorth->shouldReceive('setUser')->atLeast()->once();
2019-09-04 17:39:39 +02:00
$opsRepos->shouldReceive('setUser')->atLeast()->once();
$abRepos->shouldReceive('setUser')->atLeast()->once();
2019-06-09 15:28:54 +02:00
// mock collector calls:
$collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setPage')->atLeast()->once()->andReturnSelf();
// used to get balance information (deposits)
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT]])->atLeast()->once()->andReturnSelf();
// same, but for withdrawals
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->atLeast()->once()->andReturnSelf();
// system always returns one basic transaction (see above)
$collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn($journals);
// currency repos does some basic collecting
$currencyRepos->shouldReceive('findNull')->withArgs([1])->atLeast()->once()->andReturn($euro);
2019-09-04 17:39:39 +02:00
$currencyRepos->shouldReceive('findNull')->withArgs([2])->atLeast()->once()->andReturn(null);
2019-06-09 15:28:54 +02:00
// bill repository return value
2019-09-04 17:39:39 +02:00
$billRepos->shouldReceive('getBillsPaidInRangePerCurrency')->atLeast()->once()->andReturn([1 => '123', 2 => '456']);
$billRepos->shouldReceive('getBillsUnpaidInRangePerCurrency')->atLeast()->once()->andReturn([1 => '123', 2 => '456']);
2019-06-09 15:28:54 +02:00
// budget repos
2019-09-04 17:39:39 +02:00
$abRepos->shouldReceive('getAvailableBudgetWithCurrency')->atLeast()->once()->andReturn([1 => '123', 2 => '456']);
2019-06-09 15:28:54 +02:00
$budgetRepos->shouldReceive('getActiveBudgets')->atLeast()->once()->andReturn(new Collection([$budget]));
2019-12-22 07:50:40 +01:00
// new stuff
$opsRepos->shouldReceive('sumExpenses')->atLeast()->once()->andReturn([]);
//$opsRepos->shouldReceive('spentInPeriodMc')->atLeast()->once()->andReturn(
// [
// [
// 'currency_id' => 3,
// 'currency_code' => 'EUR',
// 'currency_symbol' => 'x',
// 'currency_decimal_places' => 2,
// 'amount' => 321.21,
// ],
// [
// 'currency_id' => 1,
// 'currency_code' => 'EUR',
// 'currency_symbol' => 'x',
// 'currency_decimal_places' => 2,
// 'amount' => 321.21,
// ],
//
// ]
// );
2019-06-09 15:28:54 +02:00
// account repos:
2019-12-22 07:50:40 +01:00
$accountRepos->shouldReceive('getActiveAccountsByType')->atLeast()->once()->withArgs([[AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE]])->andReturn(new Collection([$account]));
2019-06-09 15:28:54 +02:00
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'include_net_worth'])->andReturn(true);
// net worth calculator
$netWorth->shouldReceive('getNetWorthByCurrency')->atLeast()->once()->andReturn($netWorthData);
$parameters = [
2019-09-04 17:39:39 +02:00
'start' => $date->format('Y-m-d'),
'end' => $date->addWeek()->format('Y-m-d'),
2019-06-09 15:28:54 +02:00
];
2019-12-22 07:50:40 +01:00
// TODO AFTER 4.8,0: check if JSON is correct
2019-06-09 15:28:54 +02:00
$response = $this->get(route('api.v1.summary.basic') . '?' . http_build_query($parameters));
$response->assertStatus(200);
2019-12-22 07:50:40 +01:00
//$response->assertSee('hi there');
2019-06-09 15:28:54 +02:00
}
/**
* @covers \FireflyIII\Api\V1\Controllers\SummaryController
*/
2019-09-04 17:39:39 +02:00
public function testBasicInThePast(): void
2019-06-09 15:28:54 +02:00
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$billRepos = $this->mock(BillRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$netWorth = $this->mock(NetWorthInterface::class);
2019-09-04 17:39:39 +02:00
$abRepos = $this->mock(AvailableBudgetRepositoryInterface::class);
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
2019-06-09 15:28:54 +02:00
// data
2019-07-27 13:54:06 +02:00
$euro = $this->getEuro();
2019-06-09 15:28:54 +02:00
$budget = $this->user()->budgets()->inRandomOrder()->first();
$account = $this->getRandomAsset();
$journals = [
[
'amount' => '10',
'currency_id' => 1,
],
];
$netWorthData = [
[
'currency' => $euro,
'balance' => '232',
],
];
// mock calls.
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$billRepos->shouldReceive('setUser')->atLeast()->once();
$budgetRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$netWorth->shouldReceive('setUser')->atLeast()->once();
2019-09-04 17:39:39 +02:00
$abRepos->shouldReceive('setUser')->atLeast()->once();
$opsRepos->shouldReceive('setUser')->atLeast()->once();
2019-06-09 15:28:54 +02:00
// mock collector calls:
$collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setPage')->atLeast()->once()->andReturnSelf();
// used to get balance information (deposits)
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT]])->atLeast()->once()->andReturnSelf();
// same, but for withdrawals
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->atLeast()->once()->andReturnSelf();
// system always returns one basic transaction (see above)
$collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn($journals);
// currency repos does some basic collecting
$currencyRepos->shouldReceive('findNull')->withArgs([1])->atLeast()->once()->andReturn($euro);
// bill repository return value
2019-09-04 17:39:39 +02:00
$billRepos->shouldReceive('getBillsPaidInRangePerCurrency')->atLeast()->once()->andReturn([1 => '123']);
$billRepos->shouldReceive('getBillsUnpaidInRangePerCurrency')->atLeast()->once()->andReturn([1 => '123']);
2019-06-09 15:28:54 +02:00
// budget repos
2019-09-04 17:39:39 +02:00
$abRepos->shouldReceive('getAvailableBudgetWithCurrency')->atLeast()->once()->andReturn([1 => '123']);
2019-06-09 15:28:54 +02:00
$budgetRepos->shouldReceive('getActiveBudgets')->atLeast()->once()->andReturn(new Collection([$budget]));
2019-12-22 07:50:40 +01:00
// new stuff
$opsRepos->shouldReceive('sumExpenses')->atLeast()->once()->andReturn([]);
// $opsRepos->shouldReceive('spentInPeriodMc')->atLeast()->once()->andReturn(
// [
// [
// 'currency_id' => 1,
// 'currency_code' => 'EUR',
// 'currency_symbol' => 'x',
// 'currency_decimal_places' => 2,
// 'amount' => 321.21,
// ],
// ]
// );
2019-06-09 15:28:54 +02:00
// account repos:
2019-12-22 07:50:40 +01:00
$accountRepos->shouldReceive('getActiveAccountsByType')->atLeast()->once()->withArgs([[AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE]])->andReturn(new Collection([$account]));
2019-06-09 15:28:54 +02:00
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'include_net_worth'])->andReturn(true);
// net worth calculator
$netWorth->shouldReceive('getNetWorthByCurrency')->atLeast()->once()->andReturn($netWorthData);
$parameters = [
2019-09-04 17:39:39 +02:00
'start' => '2019-01-01',
'end' => '2019-01-31',
2019-06-09 15:28:54 +02:00
];
$response = $this->get(route('api.v1.summary.basic') . '?' . http_build_query($parameters));
$response->assertStatus(200);
2019-08-12 17:12:11 +02:00
// TODO AFTER 4.8,0: check if JSON is correct
2019-06-09 15:28:54 +02:00
}
2019-08-17 12:09:03 +02:00
}