Improve code coverage.

This commit is contained in:
James Cole 2018-09-07 20:12:22 +02:00
parent d5773ab5d0
commit b33f8b70d4
10 changed files with 299 additions and 130 deletions

View File

@ -29,6 +29,7 @@ use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Http\Request;
use Log;
/**
*
@ -132,6 +133,8 @@ class CreateController extends Controller
$request->session()->flash('success', (string)trans('firefly.stored_new_account', ['name' => $account->name]));
app('preferences')->mark();
// update preferences if necessary:
$frontPage = app('preferences')->get('frontPageAccounts', [])->data;
if (AccountType::ASSET === $account->accountType->type && \count($frontPage) > 0) {

View File

@ -161,6 +161,7 @@ class ConvertController extends Controller
$errors = $this->repository->convert($journal, $destinationType, $source, $destination);
if ($errors->count() > 0) {
Log::error('Errors while converting: ', $errors->toArray());
return redirect(route('transactions.convert.index', [strtolower($destinationType->type), $journal->id]))->withErrors($errors)->withInput();
}

View File

@ -33,6 +33,8 @@ use Log;
/**
* Class Installer
* @codeCoverageIgnore
*
*/
class Installer
{
@ -48,6 +50,7 @@ class Installer
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
*/
public function handle($request, Closure $next)
{

View File

@ -60,7 +60,7 @@ class IsDemoUser
return response()->redirectTo($previous);
}
return response()->redirectTo(route('index'));
return response()->redirectTo(route('index')); // @codeCoverageIgnore
}
return $next($request);

View File

@ -108,9 +108,11 @@ class Range
private function loseItAll(Request $request): void
{
if ('sqlite' === getenv('DB_CONNECTION') && true === getenv('IS_DOCKER')) {
// @codeCoverageIgnoreStart
$request->session()->flash(
'error', 'You seem to be using SQLite in a Docker container. Don\'t do this. If the container restarts all your data will be gone.'
);
// @codeCoverageIgnoreEnd
}
}

View File

@ -87,6 +87,7 @@ class Sandstorm
* @param string $email
*
* @return User
* @codeCoverageIgnore
*/
private function createUser(string $email): User
{

View File

@ -47,7 +47,7 @@ class SecureHeaders
$google = '';
$analyticsId = env('ANALYTICS_ID', '');
if ('' !== $analyticsId) {
$google = 'https://www.google-analytics.com/analytics.js';
$google = 'https://www.google-analytics.com/analytics.js'; // @codeCoverageIgnore
}
$csp = [
"default-src 'none'",

View File

@ -82,7 +82,6 @@ class CreateControllerTest extends TestCase
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Account\CreateController
* @covers \FireflyIII\Http\Requests\AccountFormRequest
@ -138,4 +137,36 @@ class CreateControllerTest extends TestCase
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\Account\CreateController
* @covers \FireflyIII\Http\Requests\AccountFormRequest
* @covers \FireflyIII\Http\Controllers\Controller
*/
public function testStoreLiability(): void
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('store')->once()->andReturn(factory(Account::class)->make());
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
// change the preference:
Preferences::setForUser($this->user(), 'frontPageAccounts', [1]);
$this->session(['accounts.create.uri' => 'http://localhost']);
$this->be($this->user());
$data = [
'name' => 'new liability account ' . random_int(1000, 9999),
'what' => 'liabilities',
'liability_type_id' => AccountType::where('type', AccountType::LOAN)->first()->id,
'openingBalance' => '100',
'openingBalanceDate' => '2018-01-01',
];
$response = $this->post(route('accounts.store', ['liabilities']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
}

View File

@ -63,7 +63,7 @@ class ConvertControllerTest extends TestCase
{
// mock stuff:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
// find deposit:
@ -97,7 +97,7 @@ class ConvertControllerTest extends TestCase
{
// mock stuff:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
// find deposit:
@ -130,7 +130,7 @@ class ConvertControllerTest extends TestCase
// find deposit:
$deposit = $this->getRandomDeposit();
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn($deposit);
$journalRepos->shouldReceive('getJournalTotal')->andReturn('1')->once();
@ -149,7 +149,7 @@ class ConvertControllerTest extends TestCase
{
// mock stuff:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
$journalRepos->shouldReceive('getJournalTotal')->andReturn('1')->once();
@ -183,7 +183,7 @@ class ConvertControllerTest extends TestCase
// find transfer:
$transfer = $this->getRandomTransfer();
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->andReturn($transfer);
@ -206,7 +206,7 @@ class ConvertControllerTest extends TestCase
$transfer = $this->getRandomTransfer();
// mock stuff:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
@ -235,7 +235,7 @@ class ConvertControllerTest extends TestCase
$withdrawal = $this->getRandomWithdrawal();
// mock stuff:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
@ -263,7 +263,7 @@ class ConvertControllerTest extends TestCase
$withdrawal = $this->getRandomWithdrawal();
// mock stuff:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
@ -291,17 +291,25 @@ class ConvertControllerTest extends TestCase
*/
public function testPostIndexDepositTransfer(): void
{
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('find')->andReturn(new Account);
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$data = ['source_account_asset' => 1];
// get journal:
$deposit = $this->getRandomDeposit();
$source = $this->getRandomRevenue();
$destination = $this->getRandomAsset();
$repository->shouldReceive('convert')->andReturn(new MessageBag)->atLeast()->once();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal)->atLeast()->once();
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$source]))->atLeast()->once();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$destination]))->atLeast()->once();
$accountRepos->shouldReceive('findNull')->andReturn(new Account)->atLeast()->once();
$data = ['source_account_asset' => 1];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['transfer', $deposit->id]), $data);
$response->assertStatus(302);
@ -313,20 +321,25 @@ class ConvertControllerTest extends TestCase
*/
public function testPostIndexDepositWithdrawal(): void
{
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('store')->andReturn(new Account);
$account = $this->user()->accounts()->first();
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$data = ['destination_account_expense' => 'New expense name.'];
// get journal:
$deposit = $this->getRandomDeposit();
$source = $this->getRandomRevenue();
$destination = $this->getRandomAsset();
$expense = $this->getRandomExpense();
$repository->shouldReceive('convert')->andReturn(new MessageBag)->atLeast()->once();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal)->atLeast()->once();
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$source]))->atLeast()->once();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$destination]))->atLeast()->once();
$accountRepos->shouldReceive('store')->atLeast()->once()->andReturn($expense);
$data = ['destination_account_expense' => 'New expense name.'];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['withdrawal', $deposit->id]), $data);
$response->assertStatus(302);
@ -338,45 +351,58 @@ class ConvertControllerTest extends TestCase
*/
public function testPostIndexDepositWithdrawalEmptyName(): void
{
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$data = ['destination_account_expense' => ''];
// get journal:
$deposit = $this->getRandomDeposit();
$source = $this->getRandomRevenue();
$destination = $this->getRandomAsset();
$expense = $this->getRandomExpense();
$repository->shouldReceive('convert')->andReturn(new MessageBag)->atLeast()->once();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal)->atLeast()->once();
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$source]))->atLeast()->once();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$destination]))->atLeast()->once();
$accountRepos->shouldReceive('getCashAccount')->atLeast()->once()->andReturn($expense);
$data = ['destination_account_expense' => ''];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['withdrawal', $deposit->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$deposit->id]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController
*/
public function testPostIndexErrored(): void
{
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$account = $this->user()->accounts()->first();
// find withdrawal:
$withdrawal = $this->getRandomWithdrawal();
// mock stuff
$messageBag = new MessageBag;
$messageBag->add('fake', 'fake error');
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn($messageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$account]))->twice();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]))->twice();
$accountRepos->shouldReceive('findNull')->andReturn($account)->once();
// get journal:
$withdrawal = $this->getRandomWithdrawal();
$source = $this->getRandomRevenue();
$destination = $this->getRandomAsset();
$repository->shouldReceive('convert')->andReturn($messageBag)->atLeast()->once();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal)->atLeast()->once();
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$source]))->atLeast()->once();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$destination]))->atLeast()->once();
$accountRepos->shouldReceive('findNull')->andReturn(new Account)->atLeast()->once();
$data = [
'destination_account_asset' => 2,
];
@ -391,16 +417,17 @@ class ConvertControllerTest extends TestCase
*/
public function testPostIndexSameType(): void
{
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
// get journal:
$withdrawal = $this->getRandomWithdrawal();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal)->atLeast()->once();
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = [
$data = [
'destination_account_asset' => 2,
];
$this->be($this->user());
@ -414,21 +441,17 @@ class ConvertControllerTest extends TestCase
*/
public function testPostIndexSplit(): void
{
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
// get journal:
$withdrawal = $this->getRandomSplitWithdrawal();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal)->atLeast()->once();
$withdrawal = TransactionJournal::where('transaction_type_id', 1)
->whereNull('transaction_journals.deleted_at')
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->groupBy('transaction_journals.id')
->orderBy('ct', 'DESC')
->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')]);
$data = [
$data = [
'destination_account_asset' => 2,
];
$this->be($this->user());
@ -442,22 +465,25 @@ class ConvertControllerTest extends TestCase
*/
public function testPostIndexTransferDeposit(): void
{
// find transfer:
$transfer = $this->getRandomTransfer();
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
// get journal:
$transfer = $this->getRandomTransfer();
$source = $this->getRandomAsset();
$destination = $this->getRandomAsset();
$revenue = $this->getRandomRevenue();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('store')->andReturn(new Account)->once();
$account = $this->user()->accounts()->first();
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$account]))->twice();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]))->twice();
$repository->shouldReceive('convert')->andReturn(new MessageBag)->atLeast()->once();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal)->atLeast()->once();
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$source]))->atLeast()->once();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$destination]))->atLeast()->once();
$accountRepos->shouldReceive('store')->atLeast()->once()->andReturn($revenue);
$data = ['source_account_revenue' => 'New rev'];
$this->be($this->user());
@ -472,60 +498,82 @@ class ConvertControllerTest extends TestCase
public function testPostIndexWithdrawalDeposit(): void
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = ['source_account_revenue' => 'New revenue name.'];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['deposit', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController
*/
public function testPostIndexWithdrawalDepositEmptyName(): void
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = ['source_account_revenue' => ''];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['deposit', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController
*/
public function testPostIndexWithdrawalTransfer(): void
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('findNull')->andReturn(new Account);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = ['destination_account_asset' => 2,];
$withdrawal = $this->getRandomWithdrawal();
$source = $this->getRandomExpense();
$destination = $this->getRandomAsset();
$revenue = $this->getRandomRevenue();
$repository->shouldReceive('convert')->andReturn(new MessageBag)->atLeast()->once();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal)->atLeast()->once();
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$source]))->atLeast()->once();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$destination]))->atLeast()->once();
$accountRepos->shouldReceive('store')->atLeast()->once()->andReturn($revenue);
$data = ['source_account_revenue' => 'New revenue name.'];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['transfer', $withdrawal->id]), $data);
$response = $this->post(route('transactions.convert.index', ['deposit', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController
*/
public function testPostIndexWithdrawalDepositEmptyName(): void
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$withdrawal = $this->getRandomWithdrawal();
$source = $this->getRandomExpense();
$destination = $this->getRandomAsset();
$revenue = $this->getRandomRevenue();
$repository->shouldReceive('convert')->andReturn(new MessageBag)->atLeast()->once();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal)->atLeast()->once();
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$source]))->atLeast()->once();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$destination]))->atLeast()->once();
$accountRepos->shouldReceive('getCashAccount')->atLeast()->once()->andReturn($revenue);
$data = ['source_account_revenue' => ''];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['deposit', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController
*/
public function testPostIndexWithdrawalTransfer(): void
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$withdrawal = $this->getRandomWithdrawal();
$source = $this->getRandomExpense();
$destination = $this->getRandomAsset();
$newDest = $this->getRandomAsset();
$repository->shouldReceive('convert')->andReturn(new MessageBag)->atLeast()->once();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal)->atLeast()->once();
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$source]))->atLeast()->once();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$destination]))->atLeast()->once();
$accountRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($newDest);
$data = ['destination_account_asset' => 2,];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['transfer', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
}
}

View File

@ -26,6 +26,8 @@ namespace Tests;
use Carbon\Carbon;
use DB;
use Exception;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
@ -111,6 +113,14 @@ abstract class TestCase extends BaseTestCase
return User::find(2);
}
/**
* @return Account
*/
public function getRandomAsset(): Account
{
return $this->getRandomAccount(AccountType::ASSET);
}
/**
* @return TransactionJournal
*/
@ -119,6 +129,30 @@ abstract class TestCase extends BaseTestCase
return $this->getRandomJournal(TransactionType::DEPOSIT);
}
/**
* @return Account
*/
public function getRandomExpense(): Account
{
return $this->getRandomAccount(AccountType::EXPENSE);
}
/**
* @return Account
*/
public function getRandomRevenue(): Account
{
return $this->getRandomAccount(AccountType::REVENUE);
}
/**
* @return TransactionJournal
*/
public function getRandomSplitWithdrawal(): TransactionJournal
{
return $this->getRandomSplitJournal(TransactionType::WITHDRAWAL);
}
/**
* @return TransactionJournal
*/
@ -178,6 +212,24 @@ abstract class TestCase extends BaseTestCase
return Mockery::mock('overload:' . $class);
}
/**
* @param string $type
*
* @return Account
*/
private function getRandomAccount(string $type): Account
{
$query = Account::
leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->whereNull('accounts.deleted_at')
->where('accounts.user_id', $this->user()->id)
->where('account_types.type', $type)
->inRandomOrder()->take(1);
$result = $query->first();
return $result;
}
/**
* @param string $type
*
@ -205,4 +257,32 @@ abstract class TestCase extends BaseTestCase
return TransactionJournal::find((int)$result->transaction_journal_id);
}
/**
* @param string $type
*
* @return TransactionJournal
*/
private function getRandomSplitJournal(string $type): TransactionJournal
{
$query = DB::table('transactions')
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->where('transaction_journals.user_id', $this->user()->id)
->whereNull('transaction_journals.deleted_at')
->whereNull('transactions.deleted_at')
->where('transaction_types.type', $type)
->groupBy('transactions.transaction_journal_id')
->having('ct', '>', 2)
->inRandomOrder()->take(1);
$result = $query->get(
[
'transactions.transaction_journal_id',
'transaction_journalstransaction_type_id',
DB::raw('COUNT(transaction_journal_id) as ct'),
]
)->first();
return TransactionJournal::find((int)$result->transaction_journal_id);
}
}