mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-28 18:51:55 +00:00
Improve account CRUD and tests.
This commit is contained in:
@@ -35,11 +35,15 @@ use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Mockery;
|
||||
use Preferences;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class EditControllerTest
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class EditControllerTest extends TestCase
|
||||
{
|
||||
@@ -54,6 +58,7 @@ class EditControllerTest extends TestCase
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Controllers\Account\EditController
|
||||
* @covers \FireflyIII\Http\Requests\AccountFormRequest
|
||||
*/
|
||||
public function testEdit(): void
|
||||
{
|
||||
@@ -61,36 +66,47 @@ class EditControllerTest extends TestCase
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$asset = $this->getRandomAsset();
|
||||
$euro = $this->getEuro();
|
||||
|
||||
$repository->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->atLeast()->once();
|
||||
Amount::shouldReceive('getDefaultCurrency')->atLeast()->once()->andReturn($euro);
|
||||
|
||||
// mock preferences:
|
||||
$this->mockDefaultPreferences();
|
||||
|
||||
$repository->shouldReceive('findNull')->withArgs([1])->andReturn($euro)->atLeast()->once();
|
||||
|
||||
// mock hasRole for user repository:
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
|
||||
|
||||
$repository->shouldReceive('get')->andReturn(new Collection);
|
||||
$repository->shouldReceive('get')->andReturn(new Collection)->atLeast()->once();
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
$accountRepos->shouldReceive('getNoteText')->andReturn('Some text')->once();
|
||||
$accountRepos->shouldReceive('getOpeningBalanceAmount')->andReturnNull();
|
||||
$accountRepos->shouldReceive('getOpeningBalanceDate')->andReturnNull();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('123');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'ccType'])->andReturn('');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'ccMonthlyPaymentDate'])->andReturn('');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('1');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('monthly');
|
||||
$accountRepos->shouldReceive('getOpeningBalanceAmount')->andReturnNull()->atLeast()->once();
|
||||
$accountRepos->shouldReceive('getOpeningBalanceDate')->andReturnNull()->atLeast()->once();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_number'])->andReturn('123')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_role'])->andReturn('defaultAsset')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'cc_type'])->andReturn('')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'cc_monthly_payment_date'])->andReturn('')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('1')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('monthly')->atLeast()->once();
|
||||
|
||||
// get all types:
|
||||
$accountRepos->shouldReceive('getAccountTypeByType')->withArgs(['Debt'])->andReturn(AccountType::find(11))->once();
|
||||
$accountRepos->shouldReceive('getAccountTypeByType')->withArgs(['Loan'])->andReturn(AccountType::find(9))->once();
|
||||
$accountRepos->shouldReceive('getAccountTypeByType')->withArgs(['Mortgage'])->andReturn(AccountType::find(12))->once();
|
||||
|
||||
// mock calls to Preferences:
|
||||
//$this->mockDefaultPreferences();
|
||||
|
||||
// mock calls to Configuration:
|
||||
$this->mockDefaultConfiguration();
|
||||
|
||||
$this->be($this->user());
|
||||
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
|
||||
$response = $this->get(route('accounts.edit', [$account->id]));
|
||||
$response = $this->get(route('accounts.edit', [$asset->id]));
|
||||
$response->assertStatus(200);
|
||||
// has bread crumb
|
||||
$response->assertSee('<ol class="breadcrumb">');
|
||||
@@ -99,6 +115,7 @@ class EditControllerTest extends TestCase
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Controllers\Account\EditController
|
||||
* @covers \FireflyIII\Http\Requests\AccountFormRequest
|
||||
*/
|
||||
public function testEditLiability(): void
|
||||
{
|
||||
@@ -106,22 +123,23 @@ class EditControllerTest extends TestCase
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
|
||||
$euro = $this->getEuro();
|
||||
$loan = $this->getRandomLoan();
|
||||
// mock hasRole for user repository:
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
|
||||
|
||||
$repository->shouldReceive('findNull')->once()->andReturn(TransactionCurrency::find(1));
|
||||
$repository->shouldReceive('findNull')->once()->andReturn($euro);
|
||||
$repository->shouldReceive('get')->andReturn(new Collection);
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
$accountRepos->shouldReceive('getNoteText')->andReturn('Some text')->once();
|
||||
$accountRepos->shouldReceive('getOpeningBalanceAmount')->andReturnNull();
|
||||
$accountRepos->shouldReceive('getOpeningBalanceDate')->andReturnNull();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('123');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'ccType'])->andReturn('');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_number'])->andReturn('123');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_role'])->andReturn('defaultAsset');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'cc_type'])->andReturn('');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'ccMonthlyPaymentDate'])->andReturn('');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'cc_monthly_payment_date'])->andReturn('');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('1');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('monthly');
|
||||
@@ -131,10 +149,19 @@ class EditControllerTest extends TestCase
|
||||
$accountRepos->shouldReceive('getAccountTypeByType')->withArgs(['Loan'])->andReturn(AccountType::find(9))->once();
|
||||
$accountRepos->shouldReceive('getAccountTypeByType')->withArgs(['Mortgage'])->andReturn(AccountType::find(12))->once();
|
||||
|
||||
// mock Amount
|
||||
$euro = $this->getEuro();
|
||||
Amount::shouldReceive('getDefaultCurrency')->atLeast()->once()->andReturn($euro);
|
||||
|
||||
// mock calls to Preferences:
|
||||
$this->mockDefaultPreferences();
|
||||
|
||||
// mock calls to Configuration:
|
||||
$this->mockDefaultConfiguration();
|
||||
|
||||
|
||||
$this->be($this->user());
|
||||
$account = $this->user()->accounts()->where('account_type_id', 12)->whereNull('deleted_at')->first();
|
||||
$response = $this->get(route('accounts.edit', [$account->id]));
|
||||
$response = $this->get(route('accounts.edit', [$loan->id]));
|
||||
$response->assertStatus(200);
|
||||
// has bread crumb
|
||||
$response->assertSee('<ol class="breadcrumb">');
|
||||
@@ -143,6 +170,7 @@ class EditControllerTest extends TestCase
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Controllers\Account\EditController
|
||||
* @covers \FireflyIII\Http\Requests\AccountFormRequest
|
||||
*/
|
||||
public function testEditNull(): void
|
||||
{
|
||||
@@ -163,15 +191,21 @@ class EditControllerTest extends TestCase
|
||||
$accountRepos->shouldReceive('getOpeningBalanceAmount')->andReturnNull();
|
||||
$accountRepos->shouldReceive('getOpeningBalanceDate')->andReturnNull();
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('123');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'ccType'])->andReturn('');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'ccMonthlyPaymentDate'])->andReturn('');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_number'])->andReturn('123');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_role'])->andReturn('defaultAsset');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'cc_type'])->andReturn('');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'cc_monthly_payment_date'])->andReturn('');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('1');
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('monthly');
|
||||
|
||||
// mock calls to Preferences:
|
||||
$this->mockDefaultPreferences();
|
||||
|
||||
// mock calls to Configuration:
|
||||
$this->mockDefaultConfiguration();
|
||||
|
||||
// get all types:
|
||||
$accountRepos->shouldReceive('getAccountTypeByType')->withArgs(['Debt'])->andReturn(AccountType::find(11))->once();
|
||||
$accountRepos->shouldReceive('getAccountTypeByType')->withArgs(['Loan'])->andReturn(AccountType::find(9))->once();
|
||||
@@ -195,13 +229,18 @@ class EditControllerTest extends TestCase
|
||||
public function testUpdate(): void
|
||||
{
|
||||
// mock stuff
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$this->mock(CurrencyRepositoryInterface::class);
|
||||
|
||||
$repository->shouldReceive('update')->once();
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
|
||||
|
||||
$euro = $this->getEuro();
|
||||
Amount::shouldReceive('getDefaultCurrency')->atLeast()->once()->andReturn($euro);
|
||||
Preferences::shouldReceive('mark')->atLeast()->once();
|
||||
|
||||
$this->session(['accounts.edit.uri' => 'http://localhost/javascript/account']);
|
||||
$this->be($this->user());
|
||||
$data = [
|
||||
@@ -210,6 +249,12 @@ class EditControllerTest extends TestCase
|
||||
'what' => 'asset',
|
||||
];
|
||||
|
||||
// mock calls to Preferences:
|
||||
$this->mockDefaultPreferences();
|
||||
|
||||
// mock calls to Configuration:
|
||||
$this->mockDefaultConfiguration();
|
||||
|
||||
$response = $this->post(route('accounts.update', [1]), $data);
|
||||
$response->assertStatus(302);
|
||||
$response->assertSessionHas('success');
|
||||
@@ -223,9 +268,9 @@ class EditControllerTest extends TestCase
|
||||
public function testUpdateAgain(): void
|
||||
{
|
||||
// mock stuff
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$repository = $this->mock(AccountRepositoryInterface::class);
|
||||
$this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('update')->once();
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
|
||||
@@ -238,6 +283,16 @@ class EditControllerTest extends TestCase
|
||||
'return_to_edit' => '1',
|
||||
];
|
||||
|
||||
$euro = $this->getEuro();
|
||||
Amount::shouldReceive('getDefaultCurrency')->atLeast()->once()->andReturn($euro);
|
||||
Preferences::shouldReceive('mark')->atLeast()->once();
|
||||
|
||||
// mock calls to Preferences:
|
||||
$this->mockDefaultPreferences();
|
||||
|
||||
// mock calls to Configuration:
|
||||
$this->mockDefaultConfiguration();
|
||||
|
||||
$response = $this->post(route('accounts.update', [1]), $data);
|
||||
$response->assertStatus(302);
|
||||
$response->assertSessionHas('success');
|
||||
|
Reference in New Issue
Block a user