Files
firefly-iii/tests/Feature/Controllers/Chart/BudgetControllerTest.php

350 lines
15 KiB
PHP
Raw Normal View History

2017-02-12 17:58:16 +01:00
<?php
/**
* BudgetControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* 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
2017-12-17 14:42:21 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-02-12 17:58:16 +01:00
*/
2017-03-25 13:41:17 +01:00
declare(strict_types=1);
2017-02-12 17:58:16 +01:00
namespace Tests\Feature\Controllers\Chart;
use Carbon\Carbon;
2017-03-12 08:38:13 +01:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
2018-12-12 20:30:25 +01:00
use FireflyIII\Helpers\FiscalHelperInterface;
2017-04-23 18:53:00 +02:00
use FireflyIII\Models\Account;
2017-03-12 08:38:13 +01:00
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
2017-04-23 18:53:00 +02:00
use FireflyIII\Models\Category;
2017-03-25 13:41:17 +01:00
use FireflyIII\Models\Transaction;
2017-03-12 08:38:13 +01:00
use FireflyIII\Models\TransactionType;
2017-04-23 18:53:00 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-02-12 17:58:16 +01:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2017-04-23 18:53:00 +02:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2017-03-12 08:38:13 +01:00
use Illuminate\Support\Collection;
2018-03-24 06:08:50 +01:00
use Log;
2017-02-12 17:58:16 +01:00
use Tests\TestCase;
2017-03-05 11:19:06 +01:00
2017-03-12 09:22:33 +01:00
/**
* Class BudgetControllerTest
*/
2017-02-12 17:58:16 +01:00
class BudgetControllerTest 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();
2018-09-03 18:52:46 +02:00
Log::info(sprintf('Now in %s.', \get_class($this)));
2018-03-24 06:08:50 +01:00
}
2017-02-12 17:58:16 +01:00
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-02-12 17:58:16 +01:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 19:58:10 +02:00
public function testBudget(string $range): void
2017-02-12 17:58:16 +01:00
{
2017-03-12 08:38:13 +01:00
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$repository->shouldReceive('firstUseDate')->andReturn(new Carbon('2015-01-01'))->once();
$repository->shouldReceive('spentInPeriod')->andReturn('-100');
$generator->shouldReceive('singleSet')->andReturn([])->once();
2017-02-12 17:58:16 +01:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.budget.budget', [1]));
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-02-12 17:58:16 +01:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 19:58:10 +02:00
public function testBudgetLimit(string $range): void
2017-02-12 17:58:16 +01:00
{
2017-03-12 08:38:13 +01:00
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$repository->shouldReceive('spentInPeriod')->andReturn('-100');
$generator->shouldReceive('singleSet')->once()->andReturn([]);
2017-02-12 17:58:16 +01:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
2017-03-05 11:19:06 +01:00
$response = $this->get(route('chart.budget.budget-limit', [1, 1]));
2017-02-12 17:58:16 +01:00
$response->assertStatus(200);
}
2017-03-25 13:41:17 +01:00
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-03-25 13:41:17 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testBudgetLimitWrongLimit(): void
2017-03-25 13:41:17 +01:00
{
2018-02-28 15:50:00 +01:00
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
2017-03-25 13:41:17 +01:00
$this->be($this->user());
$response = $this->get(route('chart.budget.budget-limit', [1, 8]));
$response->assertStatus(500);
}
2017-04-23 18:53:00 +02:00
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-04-23 18:53:00 +02:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 19:58:10 +02:00
public function testExpenseAsset(string $range): void
2017-04-23 18:53:00 +02:00
{
2018-02-28 15:50:00 +01:00
$budgetRepository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$transactions = factory(Transaction::class, 10)->make();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2017-04-23 18:53:00 +02:00
2018-02-28 15:50:00 +01:00
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
2017-04-23 18:53:00 +02:00
$collector->shouldReceive('setAllAssetAccounts')->once()->andReturnSelf();
$collector->shouldReceive('setBudget')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('getTransactions')->andReturn($transactions);
2017-04-23 18:53:00 +02:00
$generator->shouldReceive('pieChart')->once()->andReturn([]);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.budget.expense-asset', [1, 1]));
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-04-23 18:53:00 +02:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 19:58:10 +02:00
public function testExpenseCategory(string $range): void
2017-04-23 18:53:00 +02:00
{
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$catRepos = $this->mock(CategoryRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
$repository = $this->mock(BudgetRepositoryInterface::class);
2017-04-23 18:53:00 +02:00
$transactions = factory(Transaction::class, 10)->make();
2017-07-08 06:28:44 +02:00
$categories = factory(Category::class, 10)->make();
2017-04-23 18:53:00 +02:00
$collector->shouldReceive('setAllAssetAccounts')->once()->andReturnSelf();
$collector->shouldReceive('setBudget')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('getTransactions')->andReturn($transactions);
2017-04-23 18:53:00 +02:00
$catRepos->shouldReceive('getCategories')->andReturn($categories)->once();
$generator->shouldReceive('pieChart')->once()->andReturn([]);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.budget.expense-category', [1, 1]));
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-04-23 18:53:00 +02:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 19:58:10 +02:00
public function testExpenseExpense(string $range): void
2017-04-23 18:53:00 +02:00
{
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
2017-04-23 18:53:00 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
2017-04-23 18:53:00 +02:00
$transactions = factory(Transaction::class, 10)->make();
$accounts = factory(Account::class, 10)->make();
$collector->shouldReceive('setAllAssetAccounts')->once()->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->once()->andReturnSelf();
$collector->shouldReceive('setBudget')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('getTransactions')->andReturn($transactions);
2017-04-23 18:53:00 +02:00
$accountRepos->shouldReceive('getAccountsByType')->andReturn($accounts)->once();
$generator->shouldReceive('pieChart')->once()->andReturn([]);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.budget.expense-expense', [1, 1]));
$response->assertStatus(200);
}
2017-02-12 17:58:16 +01:00
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-02-12 17:58:16 +01:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 19:58:10 +02:00
public function testFrontpage(string $range): void
2017-02-12 17:58:16 +01:00
{
2017-03-12 08:38:13 +01:00
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
2017-03-12 08:38:13 +01:00
$budget = factory(Budget::class)->make();
$budgetLimit = factory(BudgetLimit::class)->make();
$budgetLimit->budget_id = $budget->id;
2017-03-25 13:41:17 +01:00
$transaction = factory(Transaction::class)->make();
2017-03-12 08:38:13 +01:00
2017-03-25 13:41:17 +01:00
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]))->once();
2017-03-12 08:38:13 +01:00
$repository->shouldReceive('getBudgetLimits')->once()->andReturn(new Collection([$budgetLimit]));
$repository->shouldReceive('spentInPeriod')->andReturn('-100');
2017-03-25 13:41:17 +01:00
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->once();
$collector->shouldReceive('withoutBudget')->andReturnSelf()->once();
$collector->shouldReceive('getTransactions')->andReturn(new Collection([$transaction]))->once();
2017-03-25 13:41:17 +01:00
$generator->shouldReceive('multiSet')->once()->andReturn([]);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.budget.frontpage'));
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-03-25 13:41:17 +01:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 19:58:10 +02:00
public function testFrontpageMultiLimit(string $range): void
2017-03-25 13:41:17 +01:00
{
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
2017-03-25 13:41:17 +01:00
$budget = factory(Budget::class)->make();
$one = factory(BudgetLimit::class)->make();
$two = factory(BudgetLimit::class)->make();
$one->budget_id = $budget->id;
$two->budget_id = $budget->id;
$transaction = factory(Transaction::class)->make();
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]))->once();
$repository->shouldReceive('getBudgetLimits')->once()->andReturn(new Collection([$one, $two]));
$repository->shouldReceive('spentInPeriod')->andReturn('-100');
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->once();
$collector->shouldReceive('withoutBudget')->andReturnSelf()->once();
$collector->shouldReceive('getTransactions')->andReturn(new Collection([$transaction]))->once();
2017-03-25 13:41:17 +01:00
$generator->shouldReceive('multiSet')->once()->andReturn([]);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.budget.frontpage'));
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-03-25 13:41:17 +01:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 19:58:10 +02:00
public function testFrontpageNoLimits(string $range): void
2017-03-25 13:41:17 +01:00
{
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
2017-03-25 13:41:17 +01:00
$budget = factory(Budget::class)->make();
$transaction = factory(Transaction::class)->make();
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
$repository->shouldReceive('getBudgetLimits')->once()->andReturn(new Collection);
$repository->shouldReceive('spentInPeriod')->andReturn('-100');
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->once();
$collector->shouldReceive('withoutBudget')->andReturnSelf()->once();
$collector->shouldReceive('getTransactions')->andReturn(new Collection([$transaction]))->once();
2017-03-12 08:38:13 +01:00
$generator->shouldReceive('multiSet')->once()->andReturn([]);
2017-02-12 17:58:16 +01:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.budget.frontpage'));
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-02-12 17:58:16 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testPeriod(): void
2017-02-12 17:58:16 +01:00
{
2017-03-12 08:38:13 +01:00
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$budget = factory(Budget::class)->make();
$budgetLimit = factory(BudgetLimit::class)->make();
$budgetLimit->budget_id = $budget->id;
2018-12-12 20:30:25 +01:00
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2017-03-12 08:38:13 +01:00
$repository->shouldReceive('getBudgetPeriodReport')->andReturn([])->once();
$repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit]));
$generator->shouldReceive('multiSet')->once()->andReturn([]);
2017-02-12 17:58:16 +01:00
$this->be($this->user());
2017-03-05 11:19:06 +01:00
$response = $this->get(route('chart.budget.period', [1, '1', '20120101', '20120131']));
2017-02-12 17:58:16 +01:00
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController
2017-02-12 17:58:16 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testPeriodNoBudget(): void
2017-02-12 17:58:16 +01:00
{
2018-12-12 20:30:25 +01:00
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2017-03-12 08:38:13 +01:00
$repository->shouldReceive('getNoBudgetPeriodReport')->andReturn([])->once();
$generator->shouldReceive('singleSet')->once()->andReturn([]);
2017-02-12 17:58:16 +01:00
$this->be($this->user());
2017-03-05 11:19:06 +01:00
$response = $this->get(route('chart.budget.period.no-budget', ['1', '20120101', '20120131']));
2017-02-12 17:58:16 +01:00
$response->assertStatus(200);
}
2017-02-16 22:33:32 +01:00
}