firefly-iii/tests/Feature/Controllers/Report/CategoryControllerTest.php

154 lines
6.8 KiB
PHP
Raw Normal View History

2017-02-12 11:40:39 -06:00
<?php
/**
* CategoryControllerTest.php
2020-02-16 06:59:55 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2017-02-12 11:40:39 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 01:40:00 -05: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.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05: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.
2017-10-21 01:40:00 -05: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/>.
2017-02-12 11:40:39 -06:00
*/
2017-07-07 23:28:44 -05:00
declare(strict_types=1);
2017-02-12 11:40:39 -06:00
namespace Tests\Feature\Controllers\Report;
2019-07-24 12:02:41 -05:00
use Amount;
2018-12-12 13:30:25 -06:00
use Carbon\Carbon;
2019-06-23 03:40:46 -05:00
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
2017-03-12 15:24:34 -05:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2019-08-29 10:53:25 -05:00
use FireflyIII\Repositories\Category\NoCategoryRepositoryInterface;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
2017-03-12 15:24:34 -05:00
use Illuminate\Support\Collection;
2018-03-24 00:08:50 -05:00
use Log;
2019-07-24 12:02:41 -05:00
use Preferences;
2019-08-29 10:53:25 -05:00
use Tests\Support\TestDataTrait;
2017-02-12 11:40:39 -06:00
use Tests\TestCase;
2017-08-12 03:27:45 -05:00
/**
* Class CategoryControllerTest
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
2017-02-12 11:40:39 -06:00
class CategoryControllerTest extends TestCase
{
2019-08-29 10:53:25 -05:00
use TestDataTrait;
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();
2019-04-09 13:05:20 -05:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-03-24 00:08:50 -05:00
}
2017-02-12 11:40:39 -06:00
/**
2018-08-09 13:17:15 -05:00
* @covers \FireflyIII\Http\Controllers\Report\CategoryController
2017-02-12 11:40:39 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testExpenses(): void
2017-02-12 11:40:39 -06:00
{
2019-07-23 10:33:23 -05:00
$this->mockDefaultSession();
2019-08-29 10:53:25 -05:00
$first = [1 => ['entries' => ['1', '1']]];
$second = ['entries' => ['1', '1']];
$repository = $this->mock(CategoryRepositoryInterface::class);
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$noCatRepository = $this->mock(NoCategoryRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
$opsRepos->shouldReceive('listExpenses')->atLeast()->once()->andReturn($this->categoryListExpenses());
$noCatRepository->shouldReceive('listExpenses')->atLeast()->once()->andReturn($this->noCategoryListExpenses());
2019-07-24 12:02:41 -05:00
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('x');
2018-12-12 13:30:25 -06:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2017-03-12 15:24:34 -05:00
$repository->shouldReceive('getCategories')->andReturn(new Collection);
$repository->shouldReceive('periodExpenses')->andReturn($first);
$repository->shouldReceive('periodExpensesNoCategory')->andReturn($second);
2017-02-12 11:40:39 -06:00
$this->be($this->user());
$response = $this->get(route('report-data.category.expenses', ['1', '20120101', '20120131']));
$response->assertStatus(200);
}
/**
2018-08-09 13:17:15 -05:00
* @covers \FireflyIII\Http\Controllers\Report\CategoryController
2017-02-12 11:40:39 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testIncome(): void
2017-02-12 11:40:39 -06:00
{
2019-07-23 10:33:23 -05:00
$this->mockDefaultSession();
2019-08-29 10:53:25 -05:00
$first = [
2019-07-24 12:02:41 -05:00
1 => ['entries' => ['1', '1']],
2 => ['entries' => ['0']],
];
2019-08-29 10:53:25 -05:00
$second = ['entries' => ['1', '1']];
$repository = $this->mock(CategoryRepositoryInterface::class);
$opsRepository = $this->mock(OperationsRepositoryInterface::class);
$noCatRepository = $this->mock(NoCategoryRepositoryInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
$opsRepository->shouldReceive('listIncome')->atLeast()->once()->andReturn($this->categoryListIncome());
$noCatRepository->shouldReceive('listIncome')->atLeast()->once()->andReturn($this->noCategoryListIncome());
2019-07-24 12:02:41 -05:00
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
2019-08-29 10:53:25 -05:00
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('x');
2018-12-12 13:30:25 -06:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2017-03-12 15:24:34 -05:00
$repository->shouldReceive('getCategories')->andReturn(new Collection);
$repository->shouldReceive('periodIncome')->andReturn($first);
$repository->shouldReceive('periodIncomeNoCategory')->andReturn($second);
2017-02-12 11:40:39 -06:00
$this->be($this->user());
$response = $this->get(route('report-data.category.income', ['1', '20120101', '20120131']));
$response->assertStatus(200);
}
/**
2018-08-09 13:17:15 -05:00
* @covers \FireflyIII\Http\Controllers\Report\CategoryController
2017-02-12 11:40:39 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testOperations(): void
2017-02-12 11:40:39 -06:00
{
2019-07-23 10:33:23 -05:00
$this->mockDefaultSession();
2019-08-29 10:53:25 -05:00
$repository = $this->mock(CategoryRepositoryInterface::class);
$opsRepository = $this->mock(OperationsRepositoryInterface::class);
$noCatRepository = $this->mock(NoCategoryRepositoryInterface::class);
$category = $this->getRandomCategory();
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
$opsRepository->shouldReceive('listIncome')->atLeast()->once()->andReturn($this->categoryListIncome());
$noCatRepository->shouldReceive('listIncome')->atLeast()->once()->andReturn($this->noCategoryListIncome());
$opsRepository->shouldReceive('listExpenses')->atLeast()->once()->andReturn($this->categoryListExpenses());
$noCatRepository->shouldReceive('listExpenses')->atLeast()->once()->andReturn($this->noCategoryListExpenses());
2019-07-24 12:02:41 -05:00
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
2019-08-29 10:53:25 -05:00
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('x');
2018-12-12 13:30:25 -06:00
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2017-03-12 15:24:34 -05:00
$repository->shouldReceive('getCategories')->andReturn(new Collection([$category]));
2019-08-17 05:08:09 -05:00
$repository->shouldReceive('spentInPeriod')->andReturn([]);
$repository->shouldReceive('earnedInPeriod')->andReturn([]);
2018-01-17 05:29:00 -06:00
2017-03-12 15:24:34 -05:00
2017-02-12 11:40:39 -06:00
$this->be($this->user());
$response = $this->get(route('report-data.category.operations', ['1', '20120101', '20120131']));
$response->assertStatus(200);
2019-07-24 12:02:41 -05:00
$response->assertDontSee('An error prevented Firefly III from rendering: %s. Apologies.');
2017-02-12 11:40:39 -06:00
}
2017-02-16 15:33:32 -06:00
}