firefly-iii/tests/Feature/Controllers/AccountControllerTest.php

290 lines
12 KiB
PHP
Raw Normal View History

<?php
/**
* AccountControllerTest.php
* Copyright (c) 2017 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/>.
*/
declare(strict_types=1);
2017-02-12 05:00:11 -06:00
namespace Tests\Feature\Controllers;
2018-06-01 15:04:52 -05:00
use Amount;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Account;
2017-03-04 12:14:36 -06:00
use FireflyIII\Models\AccountType;
2017-12-31 03:40:27 -06:00
use FireflyIII\Models\Note;
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 Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
2018-03-24 00:08:50 -05:00
use Log;
2018-03-31 14:05:06 -05:00
use Mockery;
2017-11-26 02:54:09 -06:00
use Preferences;
2017-03-04 12:14:36 -06:00
use Steam;
use Tests\TestCase;
2018-03-31 14:05:06 -05:00
2017-03-05 04:18:34 -06:00
/**
* Class AccountControllerTest
*
2017-08-12 03:27:45 -05:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-03-05 04:18:34 -06:00
*/
class AccountControllerTest extends TestCase
{
2018-03-24 00:08:50 -05:00
/**
*
*/
public function setUp()
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
2018-03-24 00:08:50 -05:00
}
2018-06-01 15:04:52 -05:00
/**
* @covers \FireflyIII\Http\Controllers\AccountController::index
2017-02-17 13:14:38 -06:00
* @covers \FireflyIII\Http\Controllers\AccountController::__construct
* @covers \FireflyIII\Http\Controllers\AccountController::isInArray
* @dataProvider dateRangeProvider
*
* @param string $range
2018-02-28 08:50:00 -06:00
*
*/
2018-05-11 12:58:10 -05:00
public function testIndex(string $range): void
{
2017-03-04 12:14:36 -06:00
// mock stuff
2018-02-28 08:50:00 -06:00
$account = factory(Account::class)->make();
$repository = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2017-03-04 12:14:36 -06:00
$repository->shouldReceive('getAccountsByType')->andReturn(new Collection([$account]));
2018-03-31 14:05:06 -05:00
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-03-31 14:05:06 -05:00
$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1));
Steam::shouldReceive('balancesByAccounts')->andReturn([$account->id => '100']);
2017-03-04 12:14:36 -06:00
Steam::shouldReceive('getLastActivities')->andReturn([]);
2018-06-01 15:04:52 -05:00
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('123');
2018-04-27 04:29:09 -05:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.index', ['asset']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @covers \FireflyIII\Http\Controllers\AccountController::getPeriodOverview
* @dataProvider dateRangeProvider
*
* @param string $range
*/
2018-05-11 12:58:10 -05:00
public function testShow(string $range): void
{
$date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]);
2017-03-04 12:14:36 -06:00
// mock stuff:
$tasker = $this->mock(AccountTaskerInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-02-28 08:50:00 -06:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$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('');
2017-03-04 12:14:36 -06:00
$transaction = factory(Transaction::class)->make();
$collector = $this->mock(JournalCollectorInterface::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('getJournals')->andReturn(new Collection([$transaction]));
2017-03-04 12:14:36 -06:00
$collector->shouldReceive('getPaginatedJournals')->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">');
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController
* @covers \FireflyIII\Http\Controllers\AccountController
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShowAll(string $range): void
{
$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);
$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('');
$transaction = factory(Transaction::class)->make();
$collector = $this->mock(JournalCollectorInterface::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('getJournals')->andReturn(new Collection([$transaction]));
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([$transaction], 0, 10));
$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
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @expectedExceptionMessage End is after start!
*/
2018-05-11 12:58:10 -05:00
public function testShowBrokenBadDates(): void
2018-03-03 07:24:06 -06:00
{
// mock
2018-03-31 14:05:06 -05:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-03-03 07:24:06 -06:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$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);
}
2017-03-04 12:14:36 -06:00
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @covers \FireflyIII\Http\Controllers\AccountController::redirectToOriginalAccount
* @expectedExceptionMessage Expected a transaction
*/
2018-05-11 12:58:10 -05:00
public function testShowBrokenInitial(): void
2017-03-04 12:14:36 -06:00
{
2017-03-12 15:24:34 -05:00
// mock
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-02-28 08:50:00 -06:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::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]));
$response->assertStatus(500);
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @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
{
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-02-28 08:50:00 -06:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$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('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10));
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('oldestJournalDate')->andReturn(new Carbon);
2018-03-22 23:31:30 -05:00
$repository->shouldReceive('getMetaValue')->andReturn('');
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('getJournals')->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">');
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @covers \FireflyIII\Http\Controllers\AccountController::redirectToOriginalAccount
*/
2018-05-11 12:58:10 -05:00
public function testShowInitial(): void
2017-03-04 12:14:36 -06:00
{
2017-03-12 15:24:34 -05:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-02-28 08:50:00 -06: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);
}
2017-02-16 15:33:32 -06:00
}