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

279 lines
11 KiB
PHP
Raw Normal View History

2018-07-14 15:22:21 +02:00
<?php
/**
* IndexControllerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Feature\Controllers\Budget;
use Carbon\Carbon;
2018-12-12 20:30:25 +01:00
use FireflyIII\Helpers\FiscalHelperInterface;
2018-07-14 15:22:21 +02:00
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
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;
2018-07-14 15:22:21 +02:00
use Tests\TestCase;
/**
*
* Class IndexControllerTest
*/
class IndexControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
2018-09-03 18:52:46 +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
{
2018-09-03 18:52:46 +02:00
2018-12-12 20:30:25 +01:00
Log::info(sprintf('Now in testIndex(%s)', $range));
2018-07-14 15:22:21 +02:00
// mock stuff
$budget = factory(Budget::class)->make();
$budgetLimit = factory(BudgetLimit::class)->make();
// set budget limit to current month:
$budgetLimit->start_date = Carbon::now()->startOfMonth();
$budgetLimit->end_date = Carbon::now()->endOfMonth();
$budgetInfo = [
$budget->id => [
'spent' => '0',
'budgeted' => '0',
'currentRep' => false,
],
];
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
2018-07-14 15:22:21 +02:00
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
$repository->shouldReceive('cleanupBudgets');
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
$repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection);
$repository->shouldReceive('getAvailableBudget')->andReturn('100.123');
$repository->shouldReceive('spentInPeriod')->andReturn('-1');
$repository->shouldReceive('collectBudgetInformation')->andReturn($budgetInfo);
$repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit]));
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.index'));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Budget\IndexController
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testIndexOutOfRange(string $range): void
{
2018-12-12 20:30:25 +01:00
Log::info(sprintf('Now in testIndexOutOfRange(%s)', $range));
2018-07-14 15:22:21 +02:00
// mock stuff
$budget = factory(Budget::class)->make();
$budgetLimit = factory(BudgetLimit::class)->make();
$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);
$journalRepos = $this->mock(JournalRepositoryInterface::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);
$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();
2018-07-14 15:22:21 +02:00
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
$repository->shouldReceive('cleanupBudgets');
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
$repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection);
$repository->shouldReceive('getAvailableBudget')->andReturn('100.123');
$repository->shouldReceive('spentInPeriod')->andReturn('-1');
$repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit]));
$repository->shouldReceive('collectBudgetInformation')->andReturn($budgetInfo);
$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
*/
public function testIndexWithDate(string $range): void
{
2018-12-12 20:30:25 +01:00
Log::info(sprintf('Now in testIndexWithDate(%s)', $range));
2018-07-14 15:22:21 +02:00
// mock stuff
$budget = factory(Budget::class)->make();
$budgetLimit = factory(BudgetLimit::class)->make();
$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);
$journalRepos = $this->mock(JournalRepositoryInterface::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);
$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();
2018-07-14 15:22:21 +02:00
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
$repository->shouldReceive('cleanupBudgets');
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
$repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection);
$repository->shouldReceive('getAvailableBudget')->andReturn('100.123');
$repository->shouldReceive('spentInPeriod')->andReturn('-1');
$repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit]));
$repository->shouldReceive('collectBudgetInformation')->andReturn($budgetInfo);
$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
*/
public function testIndexWithInvalidDate(string $range): void
{
2018-12-12 20:30:25 +01:00
Log::info(sprintf('Now in testIndexWithInvalidDate(%s)', $range));
2018-07-14 15:22:21 +02:00
// mock stuff
$budget = factory(Budget::class)->make();
$budgetLimit = factory(BudgetLimit::class)->make();
// set budget limit to current month:
$budgetLimit->start_date = Carbon::now()->startOfMonth();
$budgetLimit->end_date = Carbon::now()->endOfMonth();
$budgetInfo = [
$budget->id => [
'spent' => '0',
'budgeted' => '0',
'currentRep' => false,
],
];
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::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);
$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
2018-07-14 15:22:21 +02:00
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
$repository->shouldReceive('cleanupBudgets');
$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
{
$repository = $this->mock(BudgetRepositoryInterface::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
}