Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Improve code coverage.
James Coleb33f8b70d410
diff --git a/app/Http/Controllers/Account/CreateController.php b/app/Http/Controllers/Account/CreateController.phpindex 11848ca120..6440aae26b 100644--- a/app/Http/Controllers/Account/CreateController.php+++ b/app/Http/Controllers/Account/CreateController.php@@ -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) {diff --git a/app/Http/Controllers/Transaction/ConvertController.php b/app/Http/Controllers/Transaction/ConvertController.phpindex d1b41f4fe7..726f42eda2 100644--- a/app/Http/Controllers/Transaction/ConvertController.php+++ b/app/Http/Controllers/Transaction/ConvertController.php@@ -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();}diff --git a/app/Http/Middleware/Installer.php b/app/Http/Middleware/Installer.phpindex d246c1b02d..e34473b446 100644--- a/app/Http/Middleware/Installer.php+++ b/app/Http/Middleware/Installer.php@@ -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){diff --git a/app/Http/Middleware/IsDemoUser.php b/app/Http/Middleware/IsDemoUser.phpindex adf759708e..35e63971d8 100644--- a/app/Http/Middleware/IsDemoUser.php+++ b/app/Http/Middleware/IsDemoUser.php@@ -60,7 +60,7 @@ class IsDemoUserreturn response()->redirectTo($previous);}- return response()->redirectTo(route('index'));+ return response()->redirectTo(route('index')); // @codeCoverageIgnore}return $next($request);diff --git a/app/Http/Middleware/Range.php b/app/Http/Middleware/Range.phpindex 70acc493f0..ad88b040d1 100644--- a/app/Http/Middleware/Range.php+++ b/app/Http/Middleware/Range.php@@ -108,9 +108,11 @@ class Rangeprivate 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}}diff --git a/app/Http/Middleware/Sandstorm.php b/app/Http/Middleware/Sandstorm.phpindex 905e840716..65ec654e89 100644--- a/app/Http/Middleware/Sandstorm.php+++ b/app/Http/Middleware/Sandstorm.php@@ -87,6 +87,7 @@ class Sandstorm* @param string $email** @return User+ * @codeCoverageIgnore*/private function createUser(string $email): User{diff --git a/app/Http/Middleware/SecureHeaders.php b/app/Http/Middleware/SecureHeaders.phpindex 5a7ff83eb9..cf3ca4fa19 100644--- a/app/Http/Middleware/SecureHeaders.php+++ b/app/Http/Middleware/SecureHeaders.php@@ -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'",diff --git a/tests/Feature/Controllers/Account/CreateControllerTest.php b/tests/Feature/Controllers/Account/CreateControllerTest.phpindex 674b156821..33879f295f 100644--- a/tests/Feature/Controllers/Account/CreateControllerTest.php+++ b/tests/Feature/Controllers/Account/CreateControllerTest.php@@ -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');+ }}diff --git a/tests/Feature/Controllers/Transaction/ConvertControllerTest.php b/tests/Feature/Controllers/Transaction/ConvertControllerTest.phpindex 58b107a9e9..6dbb3d89c0 100644--- a/tests/Feature/Controllers/Transaction/ConvertControllerTest.php+++ b/tests/Feature/Controllers/Transaction/ConvertControllerTest.php@@ -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);+ $accountRepos = $this->mock(AccountRepositoryInterface::class);- $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:+ $deposit = $this->getRandomDeposit();+ $source = $this->getRandomRevenue();+ $destination = $this->getRandomAsset();- $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];+ $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);+ $accountRepos = $this->mock(AccountRepositoryInterface::class);- $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:+ $deposit = $this->getRandomDeposit();+ $source = $this->getRandomRevenue();+ $destination = $this->getRandomAsset();+ $expense = $this->getRandomExpense();- $accountRepos = $this->mock(AccountRepositoryInterface::class);- $accountRepos->shouldReceive('store')->andReturn(new Account);- $account = $this->user()->accounts()->first();+ $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);- $deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();- $data = ['destination_account_expense' => 'New expense name.'];+ $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 = $this->mock(JournalRepositoryInterface::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:+ $deposit = $this->getRandomDeposit();+ $source = $this->getRandomRevenue();+ $destination = $this->getRandomAsset();+ $expense = $this->getRandomExpense();- $accountRepos = $this->mock(AccountRepositoryInterface::class);- $deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();- $data = ['destination_account_expense' => ''];+ $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);+ $repository = $this->mock(JournalRepositoryInterface::class);+ $userRepos = $this->mock(UserRepositoryInterface::class);+ $accountRepos = $this->mock(AccountRepositoryInterface::class);- $repository->shouldReceive('convert')->andReturn(new MessageBag);- $repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);+ // mock stuff- $accountRepos = $this->mock(AccountRepositoryInterface::class);- $accountRepos->shouldReceive('store')->andReturn(new Account)->once();+ // get journal:+ $transfer = $this->getRandomTransfer();+ $source = $this->getRandomAsset();+ $destination = $this->getRandomAsset();+ $revenue = $this->getRandomRevenue();- $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 TestCasepublic 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);+ $repository = $this->mock(JournalRepositoryInterface::class);+ $userRepos = $this->mock(UserRepositoryInterface::class);+ $accountRepos = $this->mock(AccountRepositoryInterface::class);- $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]));- }+ $withdrawal = $this->getRandomWithdrawal();+ $source = $this->getRandomExpense();+ $destination = $this->getRandomAsset();+ $revenue = $this->getRandomRevenue();- /**- * @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)->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' => ''];+ $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 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);-- $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,];- $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]));- }+ /**+ * @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]));+ }}diff --git a/tests/TestCase.php b/tests/TestCase.phpindex 1192df583a..8d7d7255ca 100644--- a/tests/TestCase.php+++ b/tests/TestCase.php@@ -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 BaseTestCasereturn User::find(2);}+ /**+ * @return Account+ */+ public function getRandomAsset(): Account+ {+ return $this->getRandomAccount(AccountType::ASSET);+ }+/*** @return TransactionJournal*/@@ -119,6 +129,30 @@ abstract class TestCase extends BaseTestCasereturn $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 BaseTestCasereturn 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 BaseTestCasereturn 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);+ }}