firefly-iii/tests/Feature/Controllers/Account/ShowControllerTest.php

333 lines
14 KiB
PHP
Raw Normal View History

<?php
/**
2018-07-14 04:45:05 -05:00
* ShowControllerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:42:21 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2018-07-14 04:45:05 -05:00
declare(strict_types=1);
2018-07-14 04:45:05 -05:00
namespace Tests\Feature\Controllers\Account;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
2018-12-12 13:30:25 -06:00
use FireflyIII\Helpers\FiscalHelperInterface;
2017-03-04 12:14:36 -06:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
2017-03-05 04:18:34 -06:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Account\AccountTaskerInterface;
2017-03-04 12:14:36 -06:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-03-05 04:18:34 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
2018-03-24 00:08:50 -05:00
use Log;
use Mockery;
use Tests\TestCase;
2018-03-31 14:05:06 -05:00
2017-03-05 04:18:34 -06:00
/**
*
2018-07-14 04:45:05 -05:00
* Class ShowControllerTest
2017-03-05 04:18:34 -06:00
*/
2018-07-14 04:45:05 -05:00
class ShowControllerTest extends TestCase
{
2018-03-24 00:08:50 -05:00
/**
*
*/
2018-07-14 04:45:05 -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
}
/**
2018-07-14 04:45:05 -05:00
* @covers \FireflyIII\Http\Controllers\Account\ShowController
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 12:58:10 -05:00
public function testShow(string $range): void
{
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2018-12-12 13:30:25 -06:00
Log::info(sprintf('testShow(%s)', $range));
$date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]);
2017-03-04 12:14:36 -06:00
// mock stuff:
2018-09-03 11:52:46 -05:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-09-03 11:52:46 -05:00
$tasker = $this->mock(AccountTaskerInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-09-03 11:52:46 -05:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
// mock hasRole for user repository:
2018-09-03 11:52:46 -05:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
2018-02-28 08:50:00 -06:00
$currencyRepos->shouldReceive('findNull')->andReturn(TransactionCurrency::find(1));
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1');
$tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1');
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('oldestJournalDate')->andReturn(clone $date)->once();
2018-03-22 23:31:30 -05:00
$repository->shouldReceive('getMetaValue')->andReturn('');
$repository->shouldReceive('isLiability')->andReturn(false);
2018-03-22 23:31:30 -05:00
2017-03-04 12:14:36 -06:00
$transaction = factory(Transaction::class)->make();
$collector = $this->mock(TransactionCollectorInterface::class);
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('getTransactions')->andReturn(new Collection([$transaction]));
$collector->shouldReceive('getPaginatedTransactions')->andReturn(new LengthAwarePaginator([$transaction], 0, 10));
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.show', [1]));
$response->assertStatus(200);
2018-06-01 15:04:52 -05:00
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-07-14 04:45:05 -05:00
* @covers \FireflyIII\Http\Controllers\Account\ShowController
2018-06-01 15:04:52 -05:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShowAll(string $range): void
{
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2018-12-12 13:30:25 -06:00
Log::info(sprintf('testShowAll(%s)', $range));
2018-06-01 15:04:52 -05:00
$date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]);
// mock stuff:
$tasker = $this->mock(AccountTaskerInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-09-03 11:52:46 -05:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
// mock hasRole for user repository:
2018-09-03 11:52:46 -05:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
2018-06-01 15:04:52 -05:00
$currencyRepos->shouldReceive('findNull')->andReturn(TransactionCurrency::find(1));
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1');
$tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1');
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('oldestJournalDate')->andReturn(clone $date)->once();
$repository->shouldReceive('getMetaValue')->andReturn('');
$repository->shouldReceive('isLiability')->andReturn(false);
2018-06-01 15:04:52 -05:00
$transaction = factory(Transaction::class)->make();
$collector = $this->mock(TransactionCollectorInterface::class);
2018-06-01 15:04:52 -05:00
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('getTransactions')->andReturn(new Collection([$transaction]));
$collector->shouldReceive('getPaginatedTransactions')->andReturn(new LengthAwarePaginator([$transaction], 0, 10));
2018-06-01 15:04:52 -05:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.show.all', [1]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2018-03-03 07:24:06 -06:00
/**
2018-08-06 12:14:30 -05:00
* @covers \FireflyIII\Http\Controllers\Account\ShowController
2018-03-03 07:24:06 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testShowBrokenBadDates(): void
2018-03-03 07:24:06 -06:00
{
2018-12-12 13:30:25 -06:00
Log::info(sprintf('testShowBrokenBadDates(%s)', ''));
2018-03-03 07:24:06 -06:00
// mock
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-09-03 11:52:46 -05:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-12-12 13:30:25 -06:00
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
2018-09-03 11:52:46 -05:00
$accountRepos->shouldReceive('isLiability')->atLeast()->once()->andReturn(false);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-03-03 07:24:06 -06:00
$this->session(['start' => '2018-01-01', 'end' => '2017-12-01']);
$this->be($this->user());
$account = $this->user()->accounts()->where('account_type_id', 3)->orderBy('id', 'ASC')->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.show', [$account->id, '2018-01-01', '2017-12-01']));
$response->assertStatus(500);
2018-09-03 11:52:46 -05:00
$response->assertSee('End is after start!');
2018-03-03 07:24:06 -06:00
}
2017-03-04 12:14:36 -06:00
/**
2018-08-06 12:14:30 -05:00
* @covers \FireflyIII\Http\Controllers\Account\ShowController
2017-03-04 12:14:36 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testShowBrokenInitial(): void
2017-03-04 12:14:36 -06:00
{
2018-12-12 13:30:25 -06:00
Log::info(sprintf('testShowBrokenInitial(%s)', ''));
2017-03-12 15:24:34 -05:00
// mock
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-09-03 11:52:46 -05:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-05 04:19:06 -06:00
$date = new Carbon;
2017-03-04 12:14:36 -06:00
$this->session(['start' => $date, 'end' => clone $date]);
$this->be($this->user());
$account = $this->user()->accounts()->where('account_type_id', 6)->orderBy('id', 'ASC')->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.show', [$account->id]));
2018-08-09 12:44:36 -05:00
$response->assertStatus(302);
$response->assertRedirect(route('index'));
$response->assertSessionHas('error');
2017-03-04 12:14:36 -06:00
}
/**
2018-07-14 04:45:05 -05:00
* @covers \FireflyIII\Http\Controllers\Account\ShowController
2017-03-04 12:14:36 -06:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 12:58:10 -05:00
public function testShowByDateEmpty(string $range): void
2017-03-04 12:14:36 -06:00
{
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2018-12-12 13:30:25 -06:00
Log::info(sprintf('testShowByDateEmpty(%s)', $range));
2017-03-04 12:14:36 -06:00
// mock stuff
$collector = $this->mock(TransactionCollectorInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-02-28 08:50:00 -06:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
2018-12-12 13:30:25 -06:00
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$date = new Carbon;
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
// mock hasRole for user repository:
2018-09-03 11:52:46 -05:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-04 12:14:36 -06:00
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('getPaginatedTransactions')->andReturn(new LengthAwarePaginator([], 0, 10));
2017-03-04 12:14:36 -06:00
2017-03-04 12:14:36 -06:00
$repository->shouldReceive('oldestJournalDate')->andReturn(new Carbon);
2018-03-22 23:31:30 -05:00
$repository->shouldReceive('getMetaValue')->andReturn('');
$repository->shouldReceive('isLiability')->andReturn(false);
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('getTransactions')->andReturn(new Collection);
2017-03-04 12:14:36 -06:00
2018-02-28 08:50:00 -06:00
$currencyRepos->shouldReceive('findNull')->andReturn(TransactionCurrency::find(1));
2017-03-04 12:14:36 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.show', [1, '2016-01-01']));
2017-03-04 12:14:36 -06:00
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-07-14 04:45:05 -05:00
* @covers \FireflyIII\Http\Controllers\Account\ShowController
2017-03-04 12:14:36 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testShowInitial(): void
2017-03-04 12:14:36 -06:00
{
2018-12-12 13:30:25 -06:00
Log::info(sprintf('testShowInitial(%s)', ''));
2017-03-12 15:24:34 -05:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-09-03 11:52:46 -05:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-05 04:19:06 -06:00
$date = new Carbon;
2017-03-04 12:14:36 -06:00
$this->session(['start' => $date, 'end' => clone $date]);
$this->be($this->user());
$account = $this->user()->accounts()->where('account_type_id', 6)->orderBy('id', 'DESC')->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.show', [$account->id]));
$response->assertStatus(302);
}
2018-09-02 13:27:26 -05:00
/**
* @covers \FireflyIII\Http\Controllers\Account\ShowController
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShowLiability(string $range): void
{
2018-12-12 13:30:25 -06:00
Log::info(sprintf('testShowLiability(%s)', $range));
2018-09-02 13:27:26 -05:00
$date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]);
$account = $this->user()->accounts()->where('account_type_id', 12)->whereNull('deleted_at')->first();
// mock stuff:
$tasker = $this->mock(AccountTaskerInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
2018-09-02 13:27:26 -05:00
$currencyRepos->shouldReceive('findNull')->andReturn(TransactionCurrency::find(1));
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1');
$tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1');
2018-09-02 13:27:26 -05:00
$repository->shouldReceive('getMetaValue')->andReturn('');
$repository->shouldReceive('isLiability')->andReturn(true);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.show', [$account->id]));
$response->assertStatus(302);
$response->assertRedirect(route('accounts.show.all', [$account->id]));
}
2018-07-22 13:33:17 -05:00
}