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

341 lines
14 KiB
PHP
Raw Normal View History

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