Files
firefly-iii/tests/Feature/Controllers/Report/ExpenseControllerTest.php

233 lines
10 KiB
PHP
Raw Normal View History

2017-12-23 17:42:07 +01:00
<?php
/**
* ExpenseControllerTest.php
* Copyright (c) 2017 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\Report;
use Amount;
2018-12-12 20:30:25 +01:00
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2019-06-23 10:40:46 +02:00
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
2017-12-23 17:42:07 +01:00
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-03-24 06:08:50 +01:00
use Log;
use Preferences;
2017-12-23 17:42:07 +01:00
use Tests\TestCase;
/**
* Class ExpenseControllerTest
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ExpenseControllerTest 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();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-03-24 06:08:50 +01:00
}
2017-12-23 17:42:07 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Report\ExpenseController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testBudget(): void
2017-12-23 17:42:07 +01:00
{
$repository = $this->mock(AccountRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$expense = $this->getRandomExpense();
$revenue = $this->getRandomRevenue();
$date = new Carbon;
$transactions = [$this->getRandomWithdrawalAsArray()];
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once();
2018-12-12 20:30:25 +01:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2017-12-23 17:42:07 +01:00
$repository->shouldReceive('findByName')->once()->withArgs([$expense->name, [AccountType::REVENUE]])->andReturn($revenue);
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('-100');
2017-12-23 17:42:07 +01:00
// dont care about any calls, just return a default set of fake transactions:
$collector->shouldReceive('setRange')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setTypes')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setAccounts')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getExtractedJournals')->andReturn($transactions)->atLeast()->once();
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('report-data.expense.budget', ['1', $expense->id, '20170101', '20170131']));
$response->assertStatus(200);
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Report\ExpenseController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testCategory(): void
2017-12-23 17:42:07 +01:00
{
$repository = $this->mock(AccountRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$expense = $this->getRandomExpense();
$revenue = $this->getRandomRevenue();
$date = new Carbon;
$one = $this->getRandomWithdrawalAsArray();
$two = $this->getRandomWithdrawalAsArray();
// two categories
$oneCat = $this->getRandomCategory();
$twoCat = $this->user()->categories()->where('id', '!=', $oneCat->id)->inRandomOrder()->first();
$one['category_id'] = $oneCat->id;
$one['category_name'] = $oneCat->name;
$two['category_id'] = $twoCat->id;
$two['category_name'] = $twoCat->name;
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once();
2018-12-12 20:30:25 +01:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2017-12-23 17:42:07 +01:00
$repository->shouldReceive('findByName')->once()->withArgs([$expense->name, [AccountType::REVENUE]])->andReturn($revenue);
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('-100');
2017-12-23 17:42:07 +01:00
$collector->shouldReceive('setRange')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setTypes')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setAccounts')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getExtractedJournals')->andReturn([$one], [$two])->atLeast()->once();
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('report-data.expense.category', ['1', $expense->id, '20170101', '20170131']));
$response->assertStatus(200);
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Report\ExpenseController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testSpent(): void
2017-12-23 17:42:07 +01:00
{
$repository = $this->mock(AccountRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$expense = $this->getRandomExpense();
$revenue = $this->getRandomRevenue();
$date = new Carbon;
$transactions = [$this->getRandomWithdrawalAsArray()];
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once();
2018-12-12 20:30:25 +01:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2017-12-23 17:42:07 +01:00
$repository->shouldReceive('findByName')->once()->withArgs([$expense->name, [AccountType::REVENUE]])->andReturn($revenue);
$collector->shouldReceive('setRange')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setTypes')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setAccounts')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getExtractedJournals')->andReturn($transactions)->atLeast()->once();
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('-100');
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('report-data.expense.spent', ['1', $expense->id, '20170101', '20170131']));
$response->assertStatus(200);
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Report\ExpenseController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testTopExpense(): void
2017-12-23 17:42:07 +01:00
{
Log::debug(sprintf('Now in test %s', __METHOD__));
$repository = $this->mock(AccountRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$expense = $this->getRandomExpense();
$revenue = $this->getRandomRevenue();
$date = new Carbon;
$transactions = [$this->getRandomWithdrawalAsArray()];
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once();
2018-12-12 20:30:25 +01:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2017-12-23 17:42:07 +01:00
$repository->shouldReceive('findByName')->once()->withArgs([$expense->name, [AccountType::REVENUE]])->andReturn($revenue);
$collector->shouldReceive('setRange')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setTypes')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withAccountInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setAccounts')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getExtractedJournals')->andReturn($transactions)->atLeast()->once();
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('report-data.expense.expenses', ['1', $expense->id, '20170101', '20170131']));
$response->assertStatus(200);
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Report\ExpenseController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testTopIncome(): void
2017-12-23 17:42:07 +01:00
{
Log::debug(sprintf('Now in test %s', __METHOD__));
$repository = $this->mock(AccountRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$expense = $this->getRandomExpense();
$revenue = $this->getRandomRevenue();
$date = new Carbon;
$transactions = [$this->getRandomWithdrawalAsArray()];
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once();
2018-12-12 20:30:25 +01:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2017-12-23 17:42:07 +01:00
$repository->shouldReceive('findByName')->once()->withArgs([$expense->name, [AccountType::REVENUE]])->andReturn($revenue);
$collector->shouldReceive('setRange')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setTypes')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('setAccounts')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('withAccountInformation')->andReturnSelf()->atLeast()->once();
$collector->shouldReceive('getExtractedJournals')->andReturn($transactions)->atLeast()->once();
2018-09-02 20:13:25 +02:00
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('report-data.expense.income', ['1', $expense->id, '20170101', '20170131']));
$response->assertStatus(200);
}
}