Clean up some tests

This commit is contained in:
James Cole
2019-09-04 21:05:50 +02:00
parent f9f1fa0fcb
commit 5623c3c43f
17 changed files with 320 additions and 699 deletions

View File

@@ -1,144 +0,0 @@
<?php
/**
* AmountControllerTest.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 Amount;
use Carbon\Carbon;
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Log;
use Preferences;
use Tests\TestCase;
/**
*
* Class AmountControllerTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class AmountControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Http\Controllers\Budget\AmountController
*/
public function testAmount(): void
{
// mock stuff
$repository = $this->mock(BudgetRepositoryInterface::class);
$budget = $this->getRandomBudget();
$repository->shouldReceive('updateLimitAmount')->andReturn(new BudgetLimit);
$repository->shouldReceive('spentInPeriod')->andReturn('0');
$repository->shouldReceive('budgetedPerDay')->andReturn('10');
$this->mockDefaultSession();
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('200');
Preferences::shouldReceive('mark')->atLeast()->once();
$data = ['amount' => 200, 'start' => '2017-01-01', 'end' => '2017-01-31'];
$this->be($this->user());
$response = $this->post(route('budgets.amount', [$budget->id]), $data);
$response->assertStatus(200);
// assert some reactions:
$response->assertSee($budget->name);
$response->assertSee('"amount":"200"');
}
/**
* @covers \FireflyIII\Http\Controllers\Budget\AmountController
*/
public function testAmountLargeDiff(): void
{
$repository = $this->mock(BudgetRepositoryInterface::class);
$budget = $this->getRandomBudget();
$repository->shouldReceive('updateLimitAmount')->andReturn(new BudgetLimit);
$repository->shouldReceive('spentInPeriod')->andReturn('0');
$repository->shouldReceive('budgetedPerDay')->andReturn('10');
$this->mockDefaultSession();
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('200');
Preferences::shouldReceive('mark')->atLeast()->once();
$data = ['amount' => 20000, 'start' => '2017-01-01', 'end' => '2017-01-31'];
$this->be($this->user());
$response = $this->post(route('budgets.amount', [$budget->id]), $data);
$response->assertStatus(200);
$response->assertSee('Usually you budget about 200 per day.');
$response->assertSee($budget->name);
}
/**
* @covers \FireflyIII\Http\Controllers\Budget\AmountController
*/
public function testPostUpdateIncome(): void
{
$repository = $this->mock(BudgetRepositoryInterface::class);
$repository->shouldReceive('setAvailableBudget');
$repository->shouldReceive('cleanupBudgets');
$this->mockDefaultSession();
Preferences::shouldReceive('mark')->atLeast()->once();
$data = ['amount' => '200', 'start' => '2017-01-01', 'end' => '2017-01-31'];
$this->be($this->user());
$response = $this->post(route('budgets.income.post'), $data);
$response->assertStatus(302);
}
/**
* @covers \FireflyIII\Http\Controllers\Budget\AmountController
*/
public function testUpdateIncome(): void
{
$repository = $this->mock(BudgetRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
$this->mockDefaultSession();
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
$repository->shouldReceive('getAvailableBudget')->andReturn('1');
$repository->shouldReceive('cleanupBudgets');
$this->be($this->user());
$response = $this->get(route('budgets.income', ['2017-01-01', '2017-01-31']));
$response->assertStatus(200);
}
}

View File

@@ -30,12 +30,17 @@ use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Preference;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Mockery;
use Preferences;
use Tests\Support\TestDataTrait;
use Tests\TestCase;
/**
@@ -47,6 +52,7 @@ use Tests\TestCase;
*/
class IndexControllerTest extends TestCase
{
use TestDataTrait;
/**
*
*/
@@ -69,35 +75,31 @@ class IndexControllerTest extends TestCase
$budgetLimit = $this->getRandomBudgetLimit();
$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);
$userRepos = $this->mock(UserRepositoryInterface::class);
$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);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$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]));
$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()]));
$this->mockDefaultSession();
$this->mockIntroPreference('shown_demo_budgets_index');
// list size
$pref = new Preference;
$pref->data = 50;
Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
Amount::shouldReceive('formatAnything')->andReturn('123');
$this->be($this->user());
@@ -113,6 +115,7 @@ class IndexControllerTest extends TestCase
* @dataProvider dateRangeProvider
*
* @param string $range
*
* @throws Exception
*/
public function testIndexOutOfRange(string $range): void
@@ -135,27 +138,29 @@ class IndexControllerTest extends TestCase
$repository = $this->mock(BudgetRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$abRepos = $this->mock(AvailableBudgetRepositoryInterface::class);
$blRepos = $this->mock(BudgetLimitRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$date = new Carbon;
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
$repository->shouldReceive('cleanupBudgets')->atLeast()->once();
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]))->atLeast()->once();
$repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection)->atLeast()->once();
$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);
$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()]));
$this->mockDefaultSession();
$this->mockIntroPreference('shown_demo_budgets_index');
// list size
$pref = new Preference;
$pref->data = 50;
Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
Amount::shouldReceive('formatAnything')->andReturn('123');
$this->be($this->user());
@@ -173,6 +178,7 @@ class IndexControllerTest extends TestCase
* @dataProvider dateRangeProvider
*
* @param string $range
*
* @throws Exception
*/
public function testIndexWithDate(string $range): void
@@ -195,25 +201,27 @@ class IndexControllerTest extends TestCase
$repository = $this->mock(BudgetRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$abRepos = $this->mock(AvailableBudgetRepositoryInterface::class);
$blRepos = $this->mock(BudgetLimitRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$date = new Carbon;
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$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);
$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()]));
$this->mockDefaultSession();
$this->mockIntroPreference('shown_demo_budgets_index');
// list size
$pref = new Preference;
$pref->data = 50;
Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
Amount::shouldReceive('formatAnything')->andReturn('123');
$this->be($this->user());
@@ -229,6 +237,7 @@ class IndexControllerTest extends TestCase
* @dataProvider dateRangeProvider
*
* @param string $range
*
* @throws Exception
*/
public function testIndexWithInvalidDate(string $range): void
@@ -240,6 +249,11 @@ class IndexControllerTest extends TestCase
$budgetLimit->end_date = Carbon::now()->endOfMonth();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$abRepos = $this->mock(AvailableBudgetRepositoryInterface::class);
$blRepos = $this->mock(BudgetLimitRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$this->mock(UserRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
@@ -264,12 +278,13 @@ class IndexControllerTest extends TestCase
{
$this->mockDefaultSession();
$pref = new Preference;
$pref->data = 50;
Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
$repository = $this->mock(BudgetRepositoryInterface::class);
$data = [
$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,
];

View File

@@ -30,7 +30,9 @@ use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
use FireflyIII\Models\Preference;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
@@ -96,6 +98,8 @@ class ShowControllerTest extends TestCase
$collector->shouldReceive('setTypes')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withoutBudget')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withAccountInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getPaginatedGroups')->andReturn(new LengthAwarePaginator([], 0, 10))->atLeast()->once();
$collector->shouldReceive('getExtractedJournals')->andReturn([])->atLeast()->once();
@@ -140,6 +144,7 @@ class ShowControllerTest extends TestCase
$collector->shouldReceive('setTypes')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withoutBudget')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withAccountInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getPaginatedGroups')->andReturn(new LengthAwarePaginator([], 0, 10))->atLeast()->once();
try {
@@ -170,10 +175,10 @@ class ShowControllerTest extends TestCase
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$blRepos = $this->mock(BudgetLimitRepositoryInterface::class);
$this->mockDefaultSession();
// mock calls
$pref = new Preference;
@@ -189,14 +194,11 @@ class ShowControllerTest extends TestCase
$collector->shouldReceive('setPage')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setBudget')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getPaginatedGroups')->andReturn(new LengthAwarePaginator([], 0, 10))->atLeast()->once();
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
$repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit]));
$repository->shouldReceive('spentInPeriod')->andReturn('-1');
$blRepos->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit]))->atLeast()->once();
$opsRepos->shouldReceive('spentInPeriod')->andReturn('-1')->atLeast()->once();
try {
$date = new Carbon;
@@ -226,7 +228,8 @@ class ShowControllerTest extends TestCase
$budgetRepository = $this->mock(BudgetRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$blRepos = $this->mock(BudgetLimitRepositoryInterface::class);
$this->mockDefaultSession();
@@ -238,15 +241,14 @@ class ShowControllerTest extends TestCase
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$accountRepository->shouldReceive('getAccountsByType')->andReturn(new Collection);
$budgetRepository->shouldReceive('spentInPeriod')->andReturn('1');
$budgetRepository->shouldReceive('getBudgetLimits')->andReturn(new Collection);
$blRepos->shouldReceive('getBudgetLimits')->andReturn(new Collection)->atLeast()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setLimit')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setPage')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setBudget')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getPaginatedGroups')->andReturn(new LengthAwarePaginator([], 0, 10))->atLeast()->once();
$this->be($this->user());