2018-07-14 11:16:12 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CreateControllerTest.php
|
2020-02-16 13:59:55 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-07-14 11:16:12 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-07-14 11:16:12 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2018-07-14 11:16:12 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-07-14 11:16:12 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:45:03 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-07-14 11:16:12 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-07-14 11:16:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Tests\Feature\Controllers\Account;
|
|
|
|
|
|
|
|
|
2018-08-05 18:59:15 +02:00
|
|
|
use FireflyIII\Models\AccountType;
|
2019-06-22 10:25:34 +02:00
|
|
|
use FireflyIII\Models\Preference;
|
2018-07-14 11:16:12 +02:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2018-09-03 08:41:03 +02:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
2018-07-14 11:16:12 +02:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Log;
|
2018-09-03 18:52:46 +02:00
|
|
|
use Mockery;
|
2018-07-14 11:16:12 +02:00
|
|
|
use Preferences;
|
|
|
|
use Tests\TestCase;
|
2018-09-03 18:52:46 +02:00
|
|
|
|
2018-07-14 11:16:12 +02:00
|
|
|
/**
|
|
|
|
*
|
2019-06-22 10:25:34 +02:00
|
|
|
* Class CreateControllerTest.
|
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
2019-08-17 10:48:28 +02:00
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
2018-07-14 11:16:12 +02:00
|
|
|
*/
|
|
|
|
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)));
|
2018-07-14 11:16:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Account\CreateController
|
|
|
|
*/
|
|
|
|
public function testCreate(): void
|
|
|
|
{
|
|
|
|
// mock stuff
|
2019-06-23 11:13:36 +02:00
|
|
|
|
2018-07-14 11:16:12 +02:00
|
|
|
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
|
|
|
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
2018-09-03 08:41:03 +02:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-07-14 11:16:12 +02:00
|
|
|
$repository->shouldReceive('get')->andReturn(new Collection);
|
2019-06-22 10:25:34 +02:00
|
|
|
|
2018-09-03 08:41:03 +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();
|
2018-09-03 08:41:03 +02:00
|
|
|
|
2019-06-23 11:13:36 +02:00
|
|
|
// 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();
|
|
|
|
|
2018-07-14 11:16:12 +02:00
|
|
|
$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
|
2019-06-23 11:13:36 +02:00
|
|
|
|
2019-07-23 17:33:23 +02:00
|
|
|
$repository = $this->mock(AccountRepositoryInterface::class);
|
|
|
|
$asset = $this->getRandomAsset();
|
2019-06-23 11:13:36 +02:00
|
|
|
|
2019-06-22 10:25:34 +02:00
|
|
|
$repository->shouldReceive('store')->once()->andReturn($asset);
|
2018-07-14 11:16:12 +02:00
|
|
|
|
2019-06-23 11:13:36 +02:00
|
|
|
// mock default session stuff
|
|
|
|
$this->mockDefaultSession();
|
2019-06-22 10:25:34 +02:00
|
|
|
|
2018-07-14 11:16:12 +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']);
|
2018-07-14 11:16:12 +02:00
|
|
|
$this->be($this->user());
|
|
|
|
$data = [
|
2019-06-22 10:25:34 +02:00
|
|
|
'name' => 'new account ' . $this->randomInt(),
|
|
|
|
'objectType' => 'asset',
|
2018-07-14 11:16:12 +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/x');
|
2018-07-14 11:16:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Account\CreateController
|
|
|
|
* @covers \FireflyIII\Http\Requests\AccountFormRequest
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Controller
|
|
|
|
*/
|
|
|
|
public function testStoreAnother(): void
|
|
|
|
{
|
|
|
|
// mock stuff
|
2019-07-23 17:33:23 +02:00
|
|
|
$repository = $this->mock(AccountRepositoryInterface::class);
|
|
|
|
$asset = $this->getRandomAsset();
|
2018-09-03 08:41:03 +02:00
|
|
|
|
2019-06-22 10:25:34 +02:00
|
|
|
$repository->shouldReceive('store')->once()->andReturn($asset);
|
2018-07-14 11:16:12 +02:00
|
|
|
|
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]]);
|
|
|
|
|
|
|
|
|
2019-06-23 11:13:36 +02:00
|
|
|
// mock default session stuff
|
|
|
|
$this->mockDefaultSession();
|
2019-06-22 10:25:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
Preferences::shouldReceive('mark')->atLeast()->once()->withNoArgs();
|
|
|
|
|
2018-07-14 11:16:12 +02:00
|
|
|
$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',
|
2018-07-14 11:16:12 +02:00
|
|
|
'create_another' => 1,
|
|
|
|
];
|
|
|
|
|
2019-06-22 10:25:34 +02:00
|
|
|
|
2018-07-14 11:16:12 +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-07-14 11:16:12 +02:00
|
|
|
}
|
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
|
2019-07-23 17:33:23 +02:00
|
|
|
$repository = $this->mock(AccountRepositoryInterface::class);
|
|
|
|
$liability = $this->getRandomLoan();
|
|
|
|
$loan = AccountType::where('type', AccountType::LOAN)->first();
|
2019-06-22 10:25:34 +02:00
|
|
|
$repository->shouldReceive('store')->once()->andReturn($liability);
|
2018-09-07 20:12:22 +02:00
|
|
|
|
2019-06-23 11:13:36 +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
|
|
|
}
|