firefly-iii/tests/Feature/Controllers/Category/ShowControllerTest.php
2019-06-25 19:24:01 +02:00

157 lines
6.4 KiB
PHP

<?php
/**
* ShowControllerTest.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\Category;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Helpers\Filter\InternalTransferFilter;
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
use FireflyIII\Models\Preference;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Log;
use Mockery;
use Navigation;
use Preferences;
use Tests\TestCase;
/**
*
* Class ShowControllerTest
*/
class ShowControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Http\Controllers\Category\ShowController
*
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShow(string $range): void
{
$withdrawal = $this->getRandomWithdrawalAsArray();
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$this->mockDefaultSession();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$pref = new Preference;
$pref->data = 50;
Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
// mock stuff
$categoryRepos->shouldReceive('spentInPeriodCollection')->andReturn(new Collection);
$categoryRepos->shouldReceive('earnedInPeriodCollection')->andReturn(new Collection);
$categoryRepos->shouldReceive('firstUseDate')->andReturnNull();
$collector->shouldReceive('setPage')->andReturnSelf()->once();
$collector->shouldReceive('setLimit')->andReturnSelf()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->atLeast(2);
$collector->shouldReceive('withBudgetInformation')->andReturnSelf()->once();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf()->once();
$collector->shouldReceive('withAccountInformation')->andReturnSelf()->once();
$collector->shouldReceive('setCategory')->andReturnSelf()->atLeast(2);
$collector->shouldReceive('getPaginatedGroups')->andReturn(new LengthAwarePaginator([$withdrawal], 0, 10))->once();
$collector->shouldReceive('setTypes')->andReturnSelf()->atLeast(1);
$collector->shouldReceive('getExtractedJournals')->andReturn([])->atLeast()->once();
Navigation::shouldReceive('updateStartDate')->andReturn(new Carbon);
Navigation::shouldReceive('updateEndDate')->andReturn(new Carbon);
Navigation::shouldReceive('startOfPeriod')->andReturn(new Carbon);
Navigation::shouldReceive('endOfPeriod')->andReturn(new Carbon);
Navigation::shouldReceive('periodShow')->andReturn('Some date');
Navigation::shouldReceive('blockPeriods')->andReturn([['period' => '1M', 'start' => new Carbon, 'end' => new Carbon]])->once();
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('categories.show', [1]));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Category\ShowController
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShowAll(string $range): void
{
$withdrawal = $this->getRandomWithdrawalAsArray();
$repository = $this->mock(CategoryRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$this->mockDefaultSession();
$pref = new Preference;
$pref->data = 50;
Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$collector->shouldReceive('setPage')->andReturnSelf()->once();
$collector->shouldReceive('setLimit')->andReturnSelf()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->once();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf()->once();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf()->once();
$collector->shouldReceive('withAccountInformation')->andReturnSelf()->once();
$collector->shouldReceive('setCategory')->andReturnSelf()->atLeast(2);
$collector->shouldReceive('getPaginatedGroups')->andReturn(new LengthAwarePaginator([$withdrawal], 0, 10))->once();
$repository->shouldReceive('firstUseDate')->andReturn(new Carbon);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('categories.show', [1, 'all']));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
}