Files
firefly-iii/tests/Feature/Controllers/Budget/IndexControllerTest.php

304 lines
13 KiB
PHP
Raw Normal View History

2018-07-14 15:22:21 +02:00
<?php
/**
* IndexControllerTest.php
2020-02-16 13:59:55 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-14 15:22:21 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-14 15:22:21 +02: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.
2018-07-14 15:22:21 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-07-14 15:22:21 +02: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.
2018-07-14 15:22:21 +02: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/>.
2018-07-14 15:22:21 +02:00
*/
declare(strict_types=1);
namespace Tests\Feature\Controllers\Budget;
2019-07-24 19:02:41 +02:00
use Amount;
2018-07-14 15:22:21 +02:00
use Carbon\Carbon;
2019-07-24 19:02:41 +02:00
use Exception;
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
2018-07-14 15:22:21 +02:00
use FireflyIII\Models\Budget;
use FireflyIII\Models\Preference;
2018-07-14 15:22:21 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2019-09-04 21:05:50 +02:00
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
2018-07-14 15:22:21 +02:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2019-09-04 21:05:50 +02:00
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2018-09-03 18:52:46 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-07-14 15:22:21 +02:00
use Illuminate\Support\Collection;
use Log;
2018-09-03 18:52:46 +02:00
use Mockery;
use Preferences;
2019-09-04 21:05:50 +02:00
use Tests\Support\TestDataTrait;
2018-07-14 15:22:21 +02:00
use Tests\TestCase;
/**
*
* Class IndexControllerTest
2019-07-24 19:02:41 +02:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2018-07-14 15:22:21 +02:00
*/
class IndexControllerTest extends TestCase
{
2019-09-04 21:05:50 +02:00
use TestDataTrait;
2018-07-14 15:22:21 +02:00
/**
*
*/
public function setUp(): void
{
2020-07-30 20:49:40 +02:00
self::markTestIncomplete('Incomplete for refactor.');
return;
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-07-14 15:22:21 +02:00
}
/**
* @covers \FireflyIII\Http\Controllers\Budget\IndexController
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testIndex(string $range): void
{
// mock stuff
2019-07-25 14:19:49 +02:00
$budget = $this->getRandomBudget();
$budgetLimit = $this->getRandomBudgetLimit();
2018-07-14 15:22:21 +02:00
$budgetLimit->start_date = Carbon::now()->startOfMonth();
$budgetLimit->end_date = Carbon::now()->endOfMonth();
2018-09-03 18:52:46 +02:00
2019-09-04 21:05:50 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$abRepos = $this->mock(AvailableBudgetRepositoryInterface::class);
$blRepos = $this->mock(BudgetLimitRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
2018-07-14 15:22:21 +02:00
2019-09-04 21:05:50 +02:00
$repository->shouldReceive('cleanupBudgets')->atLeast()->once();
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]))->atLeast()->once();
$repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection)->atLeast()->once();
$abRepos->shouldReceive('get')->atLeast()->once()->andReturn(new Collection);
$blRepos->shouldReceive('budgeted')->andReturn('1')->atLeast()->once();
$blRepos->shouldReceive('getBudgetLimits')->andReturn(new Collection)->atLeast()->once();
$opsRepos->shouldReceive('sumExpenses')->atLeast()->once()->andReturn($this->budgetSumExpenses());
$currencyRepos->shouldReceive('getEnabled')->atLeast()->once()->andReturn(new Collection([$this->getEuro()]));
2018-07-14 15:22:21 +02:00
$this->mockDefaultSession();
$this->mockIntroPreference('shown_demo_budgets_index');
Amount::shouldReceive('formatAnything')->andReturn('123');
2018-07-14 15:22:21 +02:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
2019-07-25 14:19:49 +02:00
$response = $this->get(route('budgets.index'));
2018-07-14 15:22:21 +02:00
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Budget\IndexController
* @dataProvider dateRangeProvider
*
* @param string $range
2019-09-04 21:05:50 +02:00
*
2019-07-24 19:02:41 +02:00
* @throws Exception
2018-07-14 15:22:21 +02:00
*/
public function testIndexOutOfRange(string $range): void
{
$budget = $this->getRandomBudget();
$budgetLimit = $this->getRandomBudgetLimit();
2018-07-14 15:22:21 +02:00
$budgetInfo = [
$budget->id => [
'spent' => '0',
'budgeted' => '0',
'currentRep' => false,
],
];
// set budget limit to current month:
$budgetLimit->start_date = Carbon::now()->startOfMonth();
$budgetLimit->end_date = Carbon::now()->endOfMonth();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-12-18 07:08:35 +01:00
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
2019-09-04 21:05:50 +02:00
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$abRepos = $this->mock(AvailableBudgetRepositoryInterface::class);
$blRepos = $this->mock(BudgetLimitRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$date = new Carbon;
2018-12-12 20:30:25 +01:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
2019-09-04 21:05:50 +02:00
$repository->shouldReceive('cleanupBudgets')->atLeast()->once();
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]))->atLeast()->once();
$repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection)->atLeast()->once();
2018-07-14 15:22:21 +02:00
2019-09-04 21:05:50 +02:00
$abRepos->shouldReceive('get')->atLeast()->once()->andReturn(new Collection);
$blRepos->shouldReceive('budgeted')->andReturn('1')->atLeast()->once();
$blRepos->shouldReceive('getBudgetLimits')->andReturn(new Collection)->atLeast()->once();
$opsRepos->shouldReceive('sumExpenses')->atLeast()->once()->andReturn($this->budgetSumExpenses());
$currencyRepos->shouldReceive('getEnabled')->atLeast()->once()->andReturn(new Collection([$this->getEuro()]));
2018-07-14 15:22:21 +02:00
$this->mockDefaultSession();
$this->mockIntroPreference('shown_demo_budgets_index');
Amount::shouldReceive('formatAnything')->andReturn('123');
2018-07-14 15:22:21 +02:00
$this->be($this->user());
$today = new Carbon;
$today->startOfMonth();
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.index', [$today->format('Y-m-d')]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Budget\IndexController
* @dataProvider dateRangeProvider
*
* @param string $range
2019-09-04 21:05:50 +02:00
*
2019-07-24 19:02:41 +02:00
* @throws Exception
2018-07-14 15:22:21 +02:00
*/
public function testIndexWithDate(string $range): void
{
$budget = $this->getRandomBudget();
$budgetLimit = $this->getRandomBudgetLimit();
2018-07-14 15:22:21 +02:00
$budgetInfo = [
$budget->id => [
'spent' => '0',
'budgeted' => '0',
'currentRep' => false,
],
];
// set budget limit to current month:
$budgetLimit->start_date = Carbon::now()->startOfMonth();
$budgetLimit->end_date = Carbon::now()->endOfMonth();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-12-18 07:08:35 +01:00
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
2019-09-04 21:05:50 +02:00
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$abRepos = $this->mock(AvailableBudgetRepositoryInterface::class);
$blRepos = $this->mock(BudgetLimitRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$date = new Carbon;
2018-12-12 20:30:25 +01:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
2019-09-04 21:05:50 +02:00
$repository->shouldReceive('cleanupBudgets')->atLeast()->once();
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]))->atLeast()->once();
$repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection)->atLeast()->once();
$abRepos->shouldReceive('get')->atLeast()->once()->andReturn(new Collection);
$blRepos->shouldReceive('budgeted')->andReturn('1')->atLeast()->once();
$blRepos->shouldReceive('getBudgetLimits')->andReturn(new Collection)->atLeast()->once();
$opsRepos->shouldReceive('sumExpenses')->atLeast()->once()->andReturn($this->budgetSumExpenses());
$currencyRepos->shouldReceive('getEnabled')->atLeast()->once()->andReturn(new Collection([$this->getEuro()]));
2018-07-14 15:22:21 +02:00
$this->mockDefaultSession();
$this->mockIntroPreference('shown_demo_budgets_index');
Amount::shouldReceive('formatAnything')->andReturn('123');
2018-07-14 15:22:21 +02:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.index', ['2017-01-01']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Budget\IndexController
* @dataProvider dateRangeProvider
*
* @param string $range
2019-09-04 21:05:50 +02:00
*
2019-07-24 19:02:41 +02:00
* @throws Exception
2018-07-14 15:22:21 +02:00
*/
public function testIndexWithInvalidDate(string $range): void
{
$budgetLimit = $this->getRandomBudgetLimit();
2018-07-14 15:22:21 +02:00
// set budget limit to current month:
$budgetLimit->start_date = Carbon::now()->startOfMonth();
$budgetLimit->end_date = Carbon::now()->endOfMonth();
2019-07-24 19:02:41 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
2019-09-04 21:05:50 +02:00
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$abRepos = $this->mock(AvailableBudgetRepositoryInterface::class);
$blRepos = $this->mock(BudgetLimitRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$this->mock(UserRepositoryInterface::class);
2018-12-18 07:08:35 +01:00
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
2018-12-12 20:30:25 +01:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2018-07-14 15:22:21 +02:00
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
$repository->shouldReceive('cleanupBudgets');
$this->mockDefaultSession();
Amount::shouldReceive('formatAnything')->andReturn('123');
2018-07-14 15:22:21 +02:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.index', ['Hello-there']));
2018-08-09 19:44:36 +02:00
$response->assertStatus(404);
2018-07-14 15:22:21 +02:00
}
2018-12-18 07:08:35 +01:00
/**
* @covers \FireflyIII\Http\Controllers\Budget\IndexController
*/
public function testReorder(): void
{
2019-07-23 17:33:23 +02:00
$this->mockDefaultSession();
$repository = $this->mock(BudgetRepositoryInterface::class);
2019-09-04 21:05:50 +02:00
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$abRepos = $this->mock(AvailableBudgetRepositoryInterface::class);
$blRepos = $this->mock(BudgetLimitRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$data = [
'budgetIds' => [1, 2],
'page' => 1,
2018-12-18 07:08:35 +01:00
];
$repository->shouldReceive('cleanupBudgets')->atLeast()->once();
$repository->shouldReceive('findNull')->atLeast()->once()->andReturn(new Budget);
$repository->shouldReceive('setBudgetOrder')->atLeast()->once();
$this->be($this->user());
$response = $this->post(route('budgets.reorder', $data));
$response->assertStatus(200);
}
2018-07-22 20:33:17 +02:00
}