Files
firefly-iii/tests/Feature/Controllers/Account/CreateControllerTest.php

207 lines
7.3 KiB
PHP
Raw Normal View History

<?php
/**
* CreateControllerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Feature\Controllers\Account;
2019-06-22 10:25:34 +02:00
use Amount;
2018-08-05 18:59:15 +02:00
use FireflyIII\Models\AccountType;
2019-06-22 10:25:34 +02:00
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
2018-09-03 18:52:46 +02:00
use Mockery;
use Preferences;
use Tests\TestCase;
2018-09-03 18:52:46 +02:00
/**
*
2019-06-22 10:25:34 +02:00
* Class CreateControllerTest.
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CreateControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Http\Controllers\Account\CreateController
*/
public function testCreate(): void
{
// mock stuff
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('get')->andReturn(new Collection);
2019-06-22 10:25:34 +02:00
// mock hasRole for user repository:
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
// mock default calls
$this->mockDefaultSession();
2019-06-22 10:25:34 +02:00
$this->mockIntroPreference('shown_demo_accounts_create_asset');
2018-08-05 18:59:15 +02:00
// 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();
$this->be($this->user());
$response = $this->get(route('accounts.create', ['asset']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Account\CreateController
* @covers \FireflyIII\Http\Requests\AccountFormRequest
* @covers \FireflyIII\Http\Controllers\Controller
*/
public function testStore(): void
{
// mock stuff
2018-09-03 18:52:46 +02:00
$repository = $this->mock(AccountRepositoryInterface::class);
2019-06-22 10:25:34 +02:00
$asset = $this->getRandomAsset();
2019-06-22 10:25:34 +02:00
$repository->shouldReceive('store')->once()->andReturn($asset);
// mock default session stuff
$this->mockDefaultSession();
2019-06-22 10:25:34 +02:00
// change the preference:
2019-06-22 10:25:34 +02:00
$emptyPref = new Preference;
$emptyPref->data = [];
Preferences::shouldReceive('get')->atLeast()->once()->withArgs(['frontPageAccounts', []])->andReturn($emptyPref);
Preferences::shouldReceive('set')->atLeast()->once()->withArgs(['frontPageAccounts', [$asset->id]]);
Preferences::shouldReceive('mark')->atLeast()->once()->withNoArgs();
$this->session(['accounts.create.uri' => 'http://localhost/x']);
$this->be($this->user());
$data = [
2019-06-22 10:25:34 +02:00
'name' => 'new account ' . $this->randomInt(),
'objectType' => 'asset',
];
$response = $this->post(route('accounts.store', ['asset']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
2019-06-22 10:25:34 +02:00
$response->assertRedirect('http://localhost/x');
}
/**
* @covers \FireflyIII\Http\Controllers\Account\CreateController
* @covers \FireflyIII\Http\Requests\AccountFormRequest
* @covers \FireflyIII\Http\Controllers\Controller
*/
public function testStoreAnother(): void
{
// mock stuff
2018-09-03 18:52:46 +02:00
$repository = $this->mock(AccountRepositoryInterface::class);
2019-06-22 10:25:34 +02:00
$asset = $this->getRandomAsset();
2019-06-22 10:25:34 +02:00
$repository->shouldReceive('store')->once()->andReturn($asset);
2019-06-22 10:25:34 +02:00
// change the preference:
$emptyPref = new Preference;
$emptyPref->data = [];
Preferences::shouldReceive('get')->atLeast()->once()->withArgs(['frontPageAccounts', []])->andReturn($emptyPref);
Preferences::shouldReceive('set')->atLeast()->once()->withArgs(['frontPageAccounts', [$asset->id]]);
// mock default session stuff
$this->mockDefaultSession();
2019-06-22 10:25:34 +02:00
Preferences::shouldReceive('mark')->atLeast()->once()->withNoArgs();
$this->session(['accounts.create.uri' => 'http://localhost']);
$this->be($this->user());
$data = [
2019-06-16 13:16:46 +02:00
'name' => 'new account ' . $this->randomInt(),
2019-06-22 10:25:34 +02:00
'objectType' => 'asset',
'create_another' => 1,
];
2019-06-22 10:25:34 +02:00
$response = $this->post(route('accounts.store', ['asset']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
2019-06-22 10:25:34 +02:00
$response->assertRedirect('http://localhost/accounts/create/asset');
}
2018-09-07 20:12:22 +02:00
/**
* @covers \FireflyIII\Http\Controllers\Account\CreateController
* @covers \FireflyIII\Http\Requests\AccountFormRequest
* @covers \FireflyIII\Http\Controllers\Controller
*/
public function testStoreLiability(): void
{
// mock stuff
$repository = $this->mock(AccountRepositoryInterface::class);
2019-06-22 10:25:34 +02:00
$liability = $this->getRandomLoan();
$loan = AccountType::where('type', AccountType::LOAN)->first();
$repository->shouldReceive('store')->once()->andReturn($liability);
2018-09-07 20:12:22 +02:00
// mock default session stuff
$this->mockDefaultSession();
2019-06-22 10:25:34 +02:00
2018-09-07 20:12:22 +02:00
// change the preference:
2019-06-22 10:25:34 +02:00
$emptyPref = new Preference;
$emptyPref->data = [];
Preferences::shouldReceive('get')->atLeast()->once()->withArgs(['frontPageAccounts', []])->andReturn($emptyPref);
Preferences::shouldReceive('mark')->atLeast()->once()->withNoArgs();
2018-09-07 20:12:22 +02:00
$this->session(['accounts.create.uri' => 'http://localhost']);
$this->be($this->user());
$data = [
2019-06-22 10:25:34 +02:00
'name' => 'new liability account ' . $this->randomInt(),
'objectType' => 'liabilities',
'liability_type_id' => $loan->id,
'opening_balance' => '-100',
'opening_balance_date' => '2018-01-01',
2018-09-07 20:12:22 +02:00
];
$response = $this->post(route('accounts.store', ['liabilities']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
2018-07-22 20:33:17 +02:00
}