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

434 lines
18 KiB
PHP
Raw Normal View History

<?php
/**
* AccountControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
2017-02-12 05:00:11 -06:00
namespace Tests\Feature\Controllers;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Account;
2017-03-04 12:14:36 -06:00
use FireflyIII\Models\AccountType;
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;
2017-03-04 12:14:36 -06:00
use Steam;
use Tests\TestCase;
2017-03-05 04:18:34 -06:00
/**
* Class AccountControllerTest
*
* @package Tests\Feature\Controllers
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
{
/**
* @covers \FireflyIII\Http\Controllers\AccountController::create
*/
public function testCreate()
{
2017-03-04 12:14:36 -06:00
// mock stuff
2017-03-05 04:18:34 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(CurrencyRepositoryInterface::class);
2017-03-04 12:14:36 -06:00
$repository->shouldReceive('get')->andReturn(new Collection);
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-04 12:14:36 -06:00
$this->be($this->user());
$response = $this->get(route('accounts.create', ['asset']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController::delete
2017-03-18 14:53:44 -05:00
* @covers \FireflyIII\Http\Controllers\Controller::rememberPreviousUri
*/
public function testDelete()
{
2017-03-04 12:14:36 -06:00
// mock stuff
2017-03-05 04:18:34 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
2017-03-04 12:14:36 -06:00
$repository->shouldReceive('getAccountsByType')->withArgs([[AccountType::ASSET]])->andReturn(new Collection);
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-04 12:14:36 -06:00
$this->be($this->user());
2017-02-22 13:35:31 -06:00
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.delete', [$account->id]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController::destroy
2017-03-18 14:53:44 -05:00
* @covers \FireflyIII\Http\Controllers\Controller::__construct
* @covers \FireflyIII\Http\Controllers\Controller::getPreviousUri
*/
public function testDestroy()
{
2017-03-04 12:14:36 -06:00
// mock stuff
2017-03-05 04:18:34 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('find')->withArgs([0])->once()->andReturn(new Account);
$repository->shouldReceive('destroy')->andReturn(true);
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-18 14:53:44 -05:00
$this->session(['accounts.delete.uri' => 'http://localhost/accounts/show/1']);
2017-03-04 12:14:36 -06:00
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
$this->be($this->user());
2017-02-22 13:35:31 -06:00
$response = $this->post(route('accounts.destroy', [$account->id]));
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController::edit
*/
public function testEdit()
{
2017-03-04 12:14:36 -06:00
// mock stuff
2017-03-05 04:18:34 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(CurrencyRepositoryInterface::class);
2017-03-04 12:14:36 -06:00
$repository->shouldReceive('get')->andReturn(new Collection);
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('find')->once()->andReturn(new TransactionCurrency());
2017-03-04 12:14:36 -06:00
$this->be($this->user());
2017-02-22 13:35:31 -06:00
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.edit', [$account->id]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @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
*/
public function testIndex(string $range)
{
2017-03-04 12:14:36 -06:00
// mock stuff
2017-03-05 04:18:34 -06:00
$account = factory(Account::class)->make();
$repository = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2017-03-04 12:14:36 -06:00
$repository->shouldReceive('getAccountsByType')->andReturn(new Collection([$account]));
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
Steam::shouldReceive('balancesByAccounts')->andReturn([$account->id => '100']);
2017-03-04 12:14:36 -06:00
Steam::shouldReceive('getLastActivities')->andReturn([]);
$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
*/
public function testShow(string $range)
{
$date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]);
2017-03-04 12:14:36 -06:00
// mock stuff:
2017-03-05 04:18:34 -06:00
$tasker = $this->mock(AccountTaskerInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->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();
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);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2017-03-03 23:53:46 -06:00
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShowAll(string $range)
{
2017-03-04 12:14:36 -06:00
// mock stuff
2017-03-05 04:18:34 -06:00
$transaction = factory(Transaction::class)->make();
$collector = $this->mock(JournalCollectorInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->twice()->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('setTypes')->andReturnSelf();
2017-03-04 12:14:36 -06:00
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([$transaction], 0, 10));
$tasker = $this->mock(AccountTaskerInterface::class);
$tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1');
$tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1');
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
2017-02-25 06:34:44 -06:00
$response = $this->get(route('accounts.show', [1, 'all']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-03-04 12:14:36 -06:00
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @covers \FireflyIII\Http\Controllers\AccountController::redirectToOriginalAccount
* @expectedExceptionMessage Expected a transaction
*/
public function testShowBrokenInitial()
{
2017-03-12 15:24:34 -05:00
// mock
2017-03-05 04:18:34 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->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);
}
/**
2017-03-03 23:53:46 -06:00
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShowByDate(string $range)
{
2017-03-04 12:14:36 -06:00
// mock stuff
2017-03-05 04:18:34 -06:00
$transaction = factory(Transaction::class)->make();
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-05 04:19:06 -06:00
$collector = $this->mock(JournalCollectorInterface::class);
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('setTypes')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->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));
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('oldestJournalDate')->andReturn(new Carbon)->once();
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']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-03-04 12:14:36 -06:00
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShowByDateEmpty(string $range)
{
// mock stuff
2017-03-05 04:18:34 -06:00
$collector = $this->mock(JournalCollectorInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->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);
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection);
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
*/
public function testShowInitial()
{
2017-03-12 15:24:34 -05:00
// mock stuff
2017-03-05 04:18:34 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->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);
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController::store
2017-03-18 14:53:44 -05:00
* @covers \FireflyIII\Http\Controllers\Controller::getPreviousUri
*/
public function testStore()
{
2017-03-12 15:24:34 -05:00
// mock stuff
2017-03-18 05:02:02 -05:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
2017-03-04 12:14:36 -06:00
$repository->shouldReceive('find')->andReturn(new Account)->once();
$repository->shouldReceive('store')->once()->andReturn(factory(Account::class)->make());
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-04 12:14:36 -06:00
2017-03-18 14:53:44 -05:00
$this->session(['accounts.create.uri' => 'http://localhost']);
$this->be($this->user());
$data = [
'name' => 'new account ' . rand(1000, 9999),
'what' => 'asset',
];
$response = $this->post(route('accounts.store', ['asset']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
2017-03-18 05:02:02 -05:00
/**
* @covers \FireflyIII\Http\Controllers\AccountController::store
2017-03-18 14:53:44 -05:00
* @covers \FireflyIII\Http\Controllers\Controller::getPreviousUri
2017-03-18 05:02:02 -05:00
*/
public function testStoreAnother()
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('find')->andReturn(new Account)->once();
$repository->shouldReceive('store')->once()->andReturn(factory(Account::class)->make());
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-18 14:53:44 -05:00
$this->session(['accounts.create.uri' => 'http://localhost']);
2017-03-18 05:02:02 -05:00
$this->be($this->user());
$data = [
'name' => 'new account ' . rand(1000, 9999),
'what' => 'asset',
'create_another' => 1,
];
$response = $this->post(route('accounts.store', ['asset']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController::update
2017-03-18 14:53:44 -05:00
* @covers \FireflyIII\Http\Controllers\Controller::getPreviousUri
*/
public function testUpdate()
{
2017-03-12 15:24:34 -05:00
// mock stuff
2017-03-05 04:18:34 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
2017-03-04 12:14:36 -06:00
$repository->shouldReceive('find')->andReturn(new Account)->once();
$repository->shouldReceive('update')->once();
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-04 12:14:36 -06:00
2017-03-18 14:53:44 -05:00
$this->session(['accounts.edit.uri' => 'http://localhost/javascript/account']);
$this->be($this->user());
$data = [
'name' => 'updated account ' . rand(1000, 9999),
'active' => 1,
'what' => 'asset',
];
$response = $this->post(route('accounts.update', [1]), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
2017-03-18 05:02:02 -05:00
/**
* @covers \FireflyIII\Http\Controllers\AccountController::update
2017-03-18 14:53:44 -05:00
* @covers \FireflyIII\Http\Controllers\Controller::getPreviousUri
2017-03-18 05:02:02 -05:00
*/
public function testUpdateAgain()
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('find')->andReturn(new Account)->once();
$repository->shouldReceive('update')->once();
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-18 14:53:44 -05:00
$this->session(['accounts.edit.uri' => 'http://localhost']);
2017-03-18 05:02:02 -05:00
$this->be($this->user());
$data = [
'name' => 'updated account ' . rand(1000, 9999),
'active' => 1,
'what' => 'asset',
'return_to_edit' => '1',
];
$response = $this->post(route('accounts.update', [1]), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
2017-02-16 15:33:32 -06:00
}