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\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Http\Request; 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])); $request->session()->flash('success', (string)trans('firefly.stored_new_account', ['name' => $account->name]));
app('preferences')->mark(); app('preferences')->mark();
// update preferences if necessary: // update preferences if necessary:
$frontPage = app('preferences')->get('frontPageAccounts', [])->data; $frontPage = app('preferences')->get('frontPageAccounts', [])->data;
if (AccountType::ASSET === $account->accountType->type && \count($frontPage) > 0) { 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); $errors = $this->repository->convert($journal, $destinationType, $source, $destination);
if ($errors->count() > 0) { 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(); return redirect(route('transactions.convert.index', [strtolower($destinationType->type), $journal->id]))->withErrors($errors)->withInput();
} }

View File

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

View File

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

View File

@@ -108,9 +108,11 @@ class Range
private function loseItAll(Request $request): void private function loseItAll(Request $request): void
{ {
if ('sqlite' === getenv('DB_CONNECTION') && true === getenv('IS_DOCKER')) { if ('sqlite' === getenv('DB_CONNECTION') && true === getenv('IS_DOCKER')) {
// @codeCoverageIgnoreStart
$request->session()->flash( $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.' '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 * @param string $email
* *
* @return User * @return User
* @codeCoverageIgnore
*/ */
private function createUser(string $email): User private function createUser(string $email): User
{ {

View File

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

View File

@@ -82,7 +82,6 @@ class CreateControllerTest extends TestCase
$response->assertSee('<ol class="breadcrumb">'); $response->assertSee('<ol class="breadcrumb">');
} }
/** /**
* @covers \FireflyIII\Http\Controllers\Account\CreateController * @covers \FireflyIII\Http\Controllers\Account\CreateController
* @covers \FireflyIII\Http\Requests\AccountFormRequest * @covers \FireflyIII\Http\Requests\AccountFormRequest
@@ -138,4 +137,36 @@ class CreateControllerTest extends TestCase
$response->assertStatus(302); $response->assertStatus(302);
$response->assertSessionHas('success'); $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

@@ -291,16 +291,24 @@ class ConvertControllerTest extends TestCase
*/ */
public function testPostIndexDepositTransfer(): void public function testPostIndexDepositTransfer(): void
{ {
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff // mock stuff
$repository = $this->mock(JournalRepositoryInterface::class); $repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::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(); // 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]; $data = ['source_account_asset' => 1];
$this->be($this->user()); $this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['transfer', $deposit->id]), $data); $response = $this->post(route('transactions.convert.index', ['transfer', $deposit->id]), $data);
@@ -313,19 +321,24 @@ class ConvertControllerTest extends TestCase
*/ */
public function testPostIndexDepositWithdrawal(): void public function testPostIndexDepositWithdrawal(): void
{ {
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff // mock stuff
$repository = $this->mock(JournalRepositoryInterface::class); $repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::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(); // 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.']; $data = ['destination_account_expense' => 'New expense name.'];
$this->be($this->user()); $this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['withdrawal', $deposit->id]), $data); $response = $this->post(route('transactions.convert.index', ['withdrawal', $deposit->id]), $data);
@@ -338,15 +351,24 @@ class ConvertControllerTest extends TestCase
*/ */
public function testPostIndexDepositWithdrawalEmptyName(): void public function testPostIndexDepositWithdrawalEmptyName(): void
{ {
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff // mock stuff
$repository = $this->mock(JournalRepositoryInterface::class); $repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class);
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
// 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' => '']; $data = ['destination_account_expense' => ''];
$this->be($this->user()); $this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['withdrawal', $deposit->id]), $data); $response = $this->post(route('transactions.convert.index', ['withdrawal', $deposit->id]), $data);
@@ -354,29 +376,33 @@ class ConvertControllerTest extends TestCase
$response->assertRedirect(route('transactions.show', [$deposit->id])); $response->assertRedirect(route('transactions.show', [$deposit->id]));
} }
/** /**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController * @covers \FireflyIII\Http\Controllers\Transaction\ConvertController
*/ */
public function testPostIndexErrored(): void public function testPostIndexErrored(): void
{ {
$accountRepos = $this->mock(AccountRepositoryInterface::class); Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$account = $this->user()->accounts()->first();
// find withdrawal:
$withdrawal = $this->getRandomWithdrawal();
// mock stuff // mock stuff
$messageBag = new MessageBag; $messageBag = new MessageBag;
$messageBag->add('fake', 'fake error'); $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(); // get journal:
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]))->twice(); $withdrawal = $this->getRandomWithdrawal();
$accountRepos->shouldReceive('findNull')->andReturn($account)->once(); $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 = [ $data = [
'destination_account_asset' => 2, 'destination_account_asset' => 2,
]; ];
@@ -391,15 +417,16 @@ class ConvertControllerTest extends TestCase
*/ */
public function testPostIndexSameType(): void public function testPostIndexSameType(): void
{ {
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff // mock stuff
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(JournalRepositoryInterface::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); // get journal:
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); $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, 'destination_account_asset' => 2,
]; ];
@@ -414,20 +441,16 @@ class ConvertControllerTest extends TestCase
*/ */
public function testPostIndexSplit(): void public function testPostIndexSplit(): void
{ {
Log::info(sprintf('Now in test %s', __METHOD__));
// mock stuff // mock stuff
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(JournalRepositoryInterface::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); // get journal:
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); $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, 'destination_account_asset' => 2,
]; ];
@@ -442,22 +465,25 @@ class ConvertControllerTest extends TestCase
*/ */
public function testPostIndexTransferDeposit(): void public function testPostIndexTransferDeposit(): void
{ {
// find transfer: Log::info(sprintf('Now in test %s', __METHOD__));
$transfer = $this->getRandomTransfer();
// mock stuff // mock stuff
$repository = $this->mock(JournalRepositoryInterface::class); $repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('store')->andReturn(new Account)->once();
$account = $this->user()->accounts()->first(); // mock stuff
$repository->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$account]))->twice();
$repository->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]))->twice(); // get journal:
$transfer = $this->getRandomTransfer();
$source = $this->getRandomAsset();
$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 rev']; $data = ['source_account_revenue' => 'New rev'];
$this->be($this->user()); $this->be($this->user());
@@ -474,11 +500,20 @@ class ConvertControllerTest extends TestCase
// mock stuff // mock stuff
$repository = $this->mock(JournalRepositoryInterface::class); $repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::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('store')->atLeast()->once()->andReturn($revenue);
$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.']; $data = ['source_account_revenue' => 'New revenue name.'];
$this->be($this->user()); $this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['deposit', $withdrawal->id]), $data); $response = $this->post(route('transactions.convert.index', ['deposit', $withdrawal->id]), $data);
@@ -494,11 +529,19 @@ class ConvertControllerTest extends TestCase
// mock stuff // mock stuff
$repository = $this->mock(JournalRepositoryInterface::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); $withdrawal = $this->getRandomWithdrawal();
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); $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);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = ['source_account_revenue' => '']; $data = ['source_account_revenue' => ''];
$this->be($this->user()); $this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['deposit', $withdrawal->id]), $data); $response = $this->post(route('transactions.convert.index', ['deposit', $withdrawal->id]), $data);
@@ -514,14 +557,19 @@ class ConvertControllerTest extends TestCase
// mock stuff // mock stuff
$repository = $this->mock(JournalRepositoryInterface::class); $repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::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(); $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,]; $data = ['destination_account_asset' => 2,];
$this->be($this->user()); $this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['transfer', $withdrawal->id]), $data); $response = $this->post(route('transactions.convert.index', ['transfer', $withdrawal->id]), $data);

View File

@@ -26,6 +26,8 @@ namespace Tests;
use Carbon\Carbon; use Carbon\Carbon;
use DB; use DB;
use Exception; use Exception;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Preference; use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
@@ -111,6 +113,14 @@ abstract class TestCase extends BaseTestCase
return User::find(2); return User::find(2);
} }
/**
* @return Account
*/
public function getRandomAsset(): Account
{
return $this->getRandomAccount(AccountType::ASSET);
}
/** /**
* @return TransactionJournal * @return TransactionJournal
*/ */
@@ -119,6 +129,30 @@ abstract class TestCase extends BaseTestCase
return $this->getRandomJournal(TransactionType::DEPOSIT); 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 * @return TransactionJournal
*/ */
@@ -178,6 +212,24 @@ abstract class TestCase extends BaseTestCase
return Mockery::mock('overload:' . $class); 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 * @param string $type
* *
@@ -205,4 +257,32 @@ abstract class TestCase extends BaseTestCase
return TransactionJournal::find((int)$result->transaction_journal_id); 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);
}
} }