Files
firefly-iii/tests/Feature/Controllers/Json/BoxControllerTest.php

397 lines
16 KiB
PHP
Raw Normal View History

2017-12-23 17:42:07 +01:00
<?php
/**
* BoxControllerTest.php
2020-02-16 13:59:55 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2017-12-23 17:42:07 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-12-23 17:42:07 +01: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.
2017-12-23 17:42:07 +01:00
*
* This program is distributed in the hope that it will be useful,
2017-12-23 17:42:07 +01: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.
2017-12-23 17:42:07 +01: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/>.
2017-12-23 17:42:07 +01:00
*/
declare(strict_types=1);
namespace Tests\Feature\Controllers\Json;
2019-06-29 19:47:31 +02:00
use Amount;
2017-12-23 17:42:07 +01:00
use Carbon\Carbon;
2019-06-29 19:47:31 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Report\NetWorthInterface;
2018-02-28 15:50:00 +01:00
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2019-09-06 17:19:21 +02:00
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
2017-12-23 17:42:07 +01:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2019-09-06 17:19:21 +02:00
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
2018-02-28 15:50:00 +01:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-12-23 17:42:07 +01:00
use Illuminate\Support\Collection;
2018-03-24 06:08:50 +01:00
use Log;
2018-03-28 19:38:20 +02:00
use Mockery;
2019-06-29 19:47:31 +02:00
use Preferences;
2017-12-23 17:42:07 +01:00
use Tests\TestCase;
/**
* Class BoxControllerTest
2019-08-17 10:48:28 +02:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2017-12-23 17:42:07 +01:00
*/
class BoxControllerTest extends TestCase
{
2018-03-24 06:08:50 +01:00
/**
*
*/
2018-09-02 20:27:26 +02:00
public function setUp(): void
2018-03-24 06:08:50 +01:00
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-03-24 06:08:50 +01:00
}
2017-12-23 17:42:07 +01:00
/**
2018-06-01 22:04:52 +02:00
* @covers \FireflyIII\Http\Controllers\Json\BoxController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testAvailable(): void
2017-12-23 17:42:07 +01:00
{
2019-07-24 19:02:41 +02:00
$this->mockDefaultSession();
$return = [
2017-12-23 17:42:07 +01:00
0 => [
'spent' => '-1200', // more than budgeted.
],
];
$repository = $this->mock(BudgetRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2019-09-06 17:19:21 +02:00
$opsRepository = $this->mock(OperationsRepositoryInterface::class);
$abRepository = $this->mock(AvailableBudgetRepositoryInterface::class);
2019-07-24 19:02:41 +02:00
2019-06-29 19:47:31 +02:00
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('-100');
2019-09-06 17:19:21 +02:00
$abRepository->shouldReceive('getAvailableBudget')->andReturn('1000');
2017-12-23 17:42:07 +01:00
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection);
2019-09-06 17:19:21 +02:00
$opsRepository->shouldReceive('collectBudgetInformation')->andReturn($return);
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('json.box.available'));
$response->assertStatus(200);
}
2018-06-01 22:04:52 +02:00
/**
* @covers \FireflyIII\Http\Controllers\Json\BoxController
*/
public function testAvailableDays(): void
{
2019-07-24 19:02:41 +02:00
$this->mockDefaultSession();
$return = [
2018-06-01 22:04:52 +02:00
0 => [
'spent' => '-800', // more than budgeted.
],
];
$repository = $this->mock(BudgetRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2019-09-06 17:19:21 +02:00
$opsRepository = $this->mock(OperationsRepositoryInterface::class);
$abRepository = $this->mock(AvailableBudgetRepositoryInterface::class);
2019-06-29 19:47:31 +02:00
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('-100');
2019-09-06 17:19:21 +02:00
$abRepository->shouldReceive('getAvailableBudget')->andReturn('1000');
2018-06-01 22:04:52 +02:00
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection);
2019-09-06 17:19:21 +02:00
$opsRepository->shouldReceive('collectBudgetInformation')->andReturn($return);
2018-06-01 22:04:52 +02:00
$this->be($this->user());
$response = $this->get(route('json.box.available'));
$response->assertStatus(200);
}
2017-12-23 17:42:07 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Json\BoxController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testBalance(): void
2019-07-25 14:19:49 +02:00
{
$this->mockDefaultSession();
2019-07-24 19:02:41 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2019-06-29 19:47:31 +02:00
$collector = $this->mock(GroupCollectorInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2019-06-29 19:47:31 +02:00
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('-100');
2018-02-28 15:50:00 +01:00
// try a collector for income:
2019-06-29 19:47:31 +02:00
//$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setTypes')->andReturnSelf()->atLeast()->once();
//$collector->shouldReceive('withOpposingAccount')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getExtractedJournals')->andReturn([])->atLeast()->once();
2018-02-28 15:50:00 +01:00
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('json.box.balance'));
$response->assertStatus(200);
}
2018-06-01 22:04:52 +02:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Json\BoxController
2018-06-01 22:04:52 +02:00
*/
public function testBalanceTransactions(): void
{
2019-07-25 14:19:49 +02:00
$withdrawal = $this->getRandomWithdrawalAsArray();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2019-06-29 19:47:31 +02:00
$collector = $this->mock(GroupCollectorInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2019-07-25 14:19:49 +02:00
$euro = $this->getEuro();
2019-06-29 19:47:31 +02:00
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('-100');
2018-06-01 22:04:52 +02:00
// try a collector for income:
2019-06-29 19:47:31 +02:00
//$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setTypes')->andReturnSelf()->atLeast()->once();
//$collector->shouldReceive('withOpposingAccount')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getExtractedJournals')->andReturn([$withdrawal])->atLeast()->once();
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro);
2018-06-01 22:04:52 +02:00
$this->be($this->user());
$response = $this->get(route('json.box.balance'));
$response->assertStatus(200);
}
2017-12-23 17:42:07 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Json\BoxController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testBills(): void
2017-12-23 17:42:07 +01:00
{
2019-07-24 19:02:41 +02:00
$this->mockDefaultSession();
$billRepos = $this->mock(BillRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2019-07-24 19:02:41 +02:00
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
Amount::shouldReceive('formatAnything')->andReturn('-100');
2018-02-28 15:50:00 +01:00
$billRepos->shouldReceive('getBillsPaidInRange')->andReturn('0');
$billRepos->shouldReceive('getBillsUnpaidInRange')->andReturn('0');
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('json.box.bills'));
$response->assertStatus(200);
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Json\BoxController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testNetWorth(): void
2017-12-23 17:42:07 +01:00
{
2019-07-23 17:33:23 +02:00
$this->mockDefaultSession();
$result = [
[
2019-08-03 04:46:47 +02:00
'currency' => $this->getEuro(),
'balance' => '3',
],
];
$netWorthHelper = $this->mock(NetWorthInterface::class);
2019-07-24 19:02:41 +02:00
Amount::shouldReceive('formatAnything')->andReturn('-100');
$netWorthHelper->shouldReceive('setUser')->once();
$netWorthHelper->shouldReceive('getNetWorthByCurrency')->once()->andReturn($result);
2018-02-28 15:50:00 +01:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$accountRepos->shouldReceive('getActiveAccountsByType')->andReturn(new Collection([$this->user()->accounts()->first()]));
2019-08-03 04:46:47 +02:00
$currencyRepos->shouldReceive('findNull')->andReturn($this->getEuro());
2018-03-28 19:38:20 +02:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('ccAsset');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1');
2018-03-28 19:38:20 +02:00
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('json.box.net-worth'));
$response->assertStatus(200);
}
2018-08-31 21:12:53 +02:00
/**
* @covers \FireflyIII\Http\Controllers\Json\BoxController
*/
2018-09-02 20:27:26 +02:00
public function testNetWorthFuture(): void
2018-08-31 21:12:53 +02:00
{
2019-07-23 17:33:23 +02:00
$this->mockDefaultSession();
2018-08-31 21:12:53 +02:00
$result = [
[
2019-08-03 04:46:47 +02:00
'currency' => $this->getEuro(),
2018-08-31 21:12:53 +02:00
'balance' => '3',
],
];
2018-09-02 20:27:26 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2019-07-24 19:02:41 +02:00
Amount::shouldReceive('formatAnything')->andReturn('-100');
2018-08-31 21:12:53 +02:00
$netWorthHelper = $this->mock(NetWorthInterface::class);
$netWorthHelper->shouldReceive('setUser')->once();
$netWorthHelper->shouldReceive('getNetWorthByCurrency')->once()->andReturn($result);
$accountRepos->shouldReceive('getActiveAccountsByType')->andReturn(new Collection([$this->user()->accounts()->first()]));
2019-08-03 04:46:47 +02:00
$currencyRepos->shouldReceive('findNull')->andReturn($this->getEuro());
2018-08-31 21:12:53 +02:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('ccAsset');
2018-09-02 20:27:26 +02:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1');
2018-08-31 21:12:53 +02:00
2018-09-02 20:27:26 +02:00
$start = new Carbon;
$start->addMonths(6)->startOfMonth();
$end = clone $start;
$end->endOfMonth();
$this->session(['start' => $start, 'end' => $end]);
2018-08-31 21:12:53 +02:00
$this->be($this->user());
$response = $this->get(route('json.box.net-worth'));
$response->assertStatus(200);
}
2019-07-26 17:48:24 +02:00
/**
* @covers \FireflyIII\Http\Controllers\Json\BoxController
*/
public function testNetWorthPast(): void
{
$this->mockDefaultSession();
$result = [
[
2019-08-03 04:46:47 +02:00
'currency' => $this->getEuro(),
2019-07-26 17:48:24 +02:00
'balance' => '3',
],
];
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
Amount::shouldReceive('formatAnything')->andReturn('-100');
$netWorthHelper = $this->mock(NetWorthInterface::class);
$netWorthHelper->shouldReceive('setUser')->once();
$netWorthHelper->shouldReceive('getNetWorthByCurrency')->once()->andReturn($result);
$accountRepos->shouldReceive('getActiveAccountsByType')->andReturn(new Collection([$this->user()->accounts()->first()]));
2019-08-03 04:46:47 +02:00
$currencyRepos->shouldReceive('findNull')->andReturn($this->getEuro());
2019-07-26 17:48:24 +02:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('ccAsset');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1');
$start = new Carbon;
$start->subMonths(6)->startOfMonth();
$end = clone $start;
$end->endOfMonth();
$this->session(['start' => $start, 'end' => $end]);
$this->be($this->user());
$response = $this->get(route('json.box.net-worth'));
$response->assertStatus(200);
}
2017-12-23 17:42:07 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Json\BoxController
2018-06-01 22:04:52 +02:00
*/
2018-09-02 20:27:26 +02:00
public function testNetWorthNoCurrency(): void
2018-06-01 22:04:52 +02:00
{
2019-07-23 17:33:23 +02:00
$this->mockDefaultSession();
$result = [
[
2019-08-03 04:46:47 +02:00
'currency' => $this->getEuro(),
'balance' => '3',
],
];
2018-06-01 22:04:52 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2019-07-24 19:02:41 +02:00
Amount::shouldReceive('formatAnything')->andReturn('-100');
$netWorthHelper = $this->mock(NetWorthInterface::class);
$netWorthHelper->shouldReceive('setUser')->once();
$netWorthHelper->shouldReceive('getNetWorthByCurrency')->once()->andReturn($result);
2018-08-06 19:14:30 +02:00
$accountRepos->shouldReceive('getActiveAccountsByType')->andReturn(new Collection([$this->user()->accounts()->first()]));
2018-09-02 20:27:26 +02:00
$currencyRepos->shouldReceive('findNull')->andReturn(null);
2018-06-01 22:04:52 +02:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('ccAsset');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1');
2018-06-01 22:04:52 +02:00
$this->be($this->user());
$response = $this->get(route('json.box.net-worth'));
$response->assertStatus(200);
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Json\BoxController
2018-06-01 22:04:52 +02:00
*/
2018-09-02 20:27:26 +02:00
public function testNetWorthNoInclude(): void
2018-06-01 22:04:52 +02:00
{
2019-07-23 17:33:23 +02:00
$this->mockDefaultSession();
$result = [
[
2019-08-03 04:46:47 +02:00
'currency' => $this->getEuro(),
'balance' => '3',
],
];
$netWorthHelper = $this->mock(NetWorthInterface::class);
$netWorthHelper->shouldReceive('setUser')->once();
$netWorthHelper->shouldReceive('getNetWorthByCurrency')->once()->andReturn($result);
2019-07-24 19:02:41 +02:00
Amount::shouldReceive('formatAnything')->andReturn('-100');
2018-09-02 20:27:26 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-06-01 22:04:52 +02:00
$accountRepos->shouldReceive('getActiveAccountsByType')->andReturn(new Collection([$this->user()->accounts()->first()]));
2019-08-03 04:46:47 +02:00
$currencyRepos->shouldReceive('findNull')->andReturn($this->getEuro());
2018-06-01 22:04:52 +02:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('ccAsset');
2018-09-02 20:27:26 +02:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('0');
2018-06-01 22:04:52 +02:00
$this->be($this->user());
$response = $this->get(route('json.box.net-worth'));
$response->assertStatus(200);
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Json\BoxController
2017-12-23 17:42:07 +01:00
*/
2018-08-06 19:14:30 +02:00
public function testNetWorthVirtual(): void
2017-12-23 17:42:07 +01:00
{
2019-07-23 17:33:23 +02:00
$this->mockDefaultSession();
$result = [
[
2019-08-03 04:46:47 +02:00
'currency' => $this->getEuro(),
'balance' => '3',
],
];
2018-08-06 19:14:30 +02:00
$account = $this->user()->accounts()->first();
$account->virtual_balance = '1000';
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2019-07-24 19:02:41 +02:00
Amount::shouldReceive('formatAnything')->andReturn('-100');
$netWorthHelper = $this->mock(NetWorthInterface::class);
$netWorthHelper->shouldReceive('setUser')->once();
$netWorthHelper->shouldReceive('getNetWorthByCurrency')->once()->andReturn($result);
2018-08-06 19:14:30 +02:00
$accountRepos->shouldReceive('getActiveAccountsByType')->andReturn(new Collection([$account]));
2019-08-03 04:46:47 +02:00
$currencyRepos->shouldReceive('findNull')->andReturn($this->getEuro());
2018-03-28 19:38:20 +02:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('ccAsset');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1');
2018-02-28 15:50:00 +01:00
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('json.box.net-worth'));
$response->assertStatus(200);
}
}