Files
firefly-iii/tests/Unit/Factory/AccountFactoryTest.php

1044 lines
45 KiB
PHP
Raw Normal View History

2018-03-01 20:54:50 +01:00
<?php
/**
* AccountFactoryTest.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\Unit\Factory;
2019-08-03 14:45:37 +02:00
use Amount;
2018-03-01 20:54:50 +01:00
use Carbon\Carbon;
2018-08-24 07:18:33 +02:00
use FireflyIII\Exceptions\FireflyException;
2018-03-01 20:54:50 +01:00
use FireflyIII\Factory\AccountFactory;
2019-06-16 13:16:46 +02:00
use FireflyIII\Factory\AccountMetaFactory;
2019-08-03 14:45:37 +02:00
use FireflyIII\Factory\TransactionCurrencyFactory;
2019-06-16 13:16:46 +02:00
use FireflyIII\Factory\TransactionGroupFactory;
2018-08-24 07:18:33 +02:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
2018-03-01 20:54:50 +01:00
use FireflyIII\Models\AccountType;
2019-08-29 17:53:25 +02:00
use FireflyIII\Models\Preference;
2019-06-16 13:16:46 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-08-24 07:18:33 +02:00
use Log;
2019-06-16 13:16:46 +02:00
use Mockery;
2019-08-29 17:53:25 +02:00
use Preferences;
2018-03-01 20:54:50 +01:00
use Tests\TestCase;
/**
* Class AccountFactoryTest
2019-08-17 10:48:28 +02:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2019-06-16 13:16:46 +02:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2019-08-17 10:48:28 +02:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-03-01 20:54:50 +01:00
*/
class AccountFactoryTest extends TestCase
{
2018-08-24 07:18:33 +02:00
/**
*
*/
public function setUp(): void
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-08-24 07:18:33 +02:00
}
2018-03-01 20:54:50 +01:00
/**
* Test minimal set of data to make factory work (asset account).
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-06-16 13:16:46 +02:00
public function testCreate(): void
2018-03-01 20:54:50 +01:00
{
2019-08-03 14:45:37 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-06-16 13:16:46 +02:00
$this->mock(TransactionGroupFactory::class);
2019-08-03 14:45:37 +02:00
$euro = $this->getEuro();
2018-03-01 20:54:50 +01:00
$data = [
'account_type_id' => null,
2019-06-16 13:16:46 +02:00
'account_type' => 'asset',
2018-03-01 20:54:50 +01:00
'iban' => null,
2019-06-16 13:16:46 +02:00
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
2018-03-01 20:54:50 +01:00
'active' => true,
2019-06-16 13:16:46 +02:00
'account_role' => 'defaultAsset',
2018-03-01 20:54:50 +01:00
];
2019-06-16 13:16:46 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
////$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
////$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
2019-08-03 14:45:37 +02:00
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn($euro);
2018-12-21 16:38:10 +01:00
2018-03-01 20:54:50 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
2019-08-03 14:45:37 +02:00
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2019-06-16 13:16:46 +02:00
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-01 20:54:50 +01:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2019-06-16 13:16:46 +02:00
$account->forceDelete();
2018-03-01 20:54:50 +01:00
}
2018-04-03 19:15:06 +02:00
/**
2018-08-06 19:14:30 +02:00
* Test creation of CC asset.
2018-04-03 19:15:06 +02:00
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-06-16 13:16:46 +02:00
public function testCreateCC(): void
2018-04-03 19:15:06 +02:00
{
2019-08-03 14:45:37 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-06-16 13:16:46 +02:00
$this->mock(TransactionGroupFactory::class);
2019-08-03 14:45:37 +02:00
2018-04-03 19:15:06 +02:00
$data = [
2019-06-16 13:16:46 +02:00
'account_type_id' => null,
'account_type' => 'asset',
'iban' => null,
'name' => sprintf('Basic CC account #%d', $this->randomInt()),
'virtual_balance' => null,
'active' => true,
'account_role' => 'ccAsset',
'cc_monthly_payment_date' => '2018-01-01',
2018-04-03 19:15:06 +02:00
];
2019-06-16 13:16:46 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'ccAsset'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
////$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
////$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'cc_monthly_payment_date', '2018-01-01'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'cc_type', ''])->atLeast()->once()->andReturnNull();
2019-08-03 14:45:37 +02:00
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2018-04-03 19:15:06 +02:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
2019-08-03 14:45:37 +02:00
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2019-06-16 13:16:46 +02:00
$this->assertTrue(false, $e->getMessage());
return;
}
2018-04-03 19:15:06 +02:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2019-06-16 13:16:46 +02:00
$account->forceDelete();
2018-04-03 19:15:06 +02:00
}
2018-03-01 20:54:50 +01:00
/**
2019-08-29 17:53:25 +02:00
* Test minimal set of data to make factory work (asset account).
2018-03-01 20:54:50 +01:00
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-08-29 17:53:25 +02:00
public function testCreateCurrencyCode(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
$this->mock(TransactionGroupFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
$currency = $this->getDollar();
$data = [
'account_type_id' => null,
'account_type' => 'asset',
'iban' => null,
'currency_code' => $currency->code,
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
'active' => true,
'account_role' => 'defaultAsset',
];
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', $currency->id])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
$currencyFactory->shouldReceive('find')->withArgs([0, 'USD'])->atLeast()->once()->andReturn($currency);
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
return;
}
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
}
/**
* Test minimal set of data to make factory work (asset account).
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
public function testCreateCurrencyId(): void
2018-03-01 20:54:50 +01:00
{
2019-08-03 14:45:37 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-06-16 13:16:46 +02:00
$this->mock(TransactionGroupFactory::class);
2019-08-29 17:53:25 +02:00
$currency = $this->getDollar();
$data = [
2018-08-06 19:14:30 +02:00
'account_type_id' => null,
2019-06-16 13:16:46 +02:00
'account_type' => 'asset',
2018-08-06 19:14:30 +02:00
'iban' => null,
2019-08-29 17:53:25 +02:00
'currency_id' => $currency->id,
2019-06-16 13:16:46 +02:00
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
2019-08-29 17:53:25 +02:00
'virtual_balance' => null,
2018-08-06 19:14:30 +02:00
'active' => true,
2019-06-16 13:16:46 +02:00
'account_role' => 'defaultAsset',
2018-03-01 20:54:50 +01:00
];
2019-06-16 13:16:46 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
2019-08-29 17:53:25 +02:00
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', $currency->id])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
$currencyFactory->shouldReceive('find')->withArgs([7, ''])->atLeast()->once()->andReturn($currency);
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2019-06-16 13:16:46 +02:00
2018-03-01 20:54:50 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-08-29 17:53:25 +02:00
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-01 20:54:50 +01:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2018-03-01 20:54:50 +01:00
}
/**
2019-08-29 17:53:25 +02:00
* Leave virtual balance empty.
2018-03-01 20:54:50 +01:00
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-08-29 17:53:25 +02:00
public function testCreateEmptyVb(): void
2018-03-01 20:54:50 +01:00
{
2019-08-29 17:53:25 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
2019-08-03 14:45:37 +02:00
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-08-29 17:53:25 +02:00
$this->mock(TransactionGroupFactory::class);
$data = [
2018-03-01 20:54:50 +01:00
'account_type_id' => null,
2019-08-29 17:53:25 +02:00
'account_type' => 'asset',
2018-03-01 20:54:50 +01:00
'iban' => null,
2019-08-29 17:53:25 +02:00
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => '',
2018-03-30 22:40:20 +02:00
'active' => true,
2019-06-16 13:16:46 +02:00
'account_role' => 'defaultAsset',
2018-03-30 22:40:20 +02:00
];
2019-08-29 17:53:25 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
////$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
////$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
2019-08-03 14:45:37 +02:00
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2018-03-30 22:40:20 +02:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
2019-08-03 14:45:37 +02:00
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2019-06-16 13:16:46 +02:00
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-30 22:40:20 +02:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
2019-08-29 17:53:25 +02:00
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
2018-03-30 22:40:20 +02:00
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2019-06-16 13:16:46 +02:00
$account->forceDelete();
2018-03-30 22:40:20 +02:00
}
/**
2019-08-29 17:53:25 +02:00
* Should return existing account.
2018-03-30 22:40:20 +02:00
*
* @covers \FireflyIII\Factory\AccountFactory
*/
2019-08-29 17:53:25 +02:00
public function testCreateExisting(): void
2018-03-30 22:40:20 +02:00
{
2019-06-16 13:16:46 +02:00
$this->mock(AccountRepositoryInterface::class);
$this->mock(TransactionGroupFactory::class);
2019-08-29 17:53:25 +02:00
$this->mock(AccountMetaFactory::class);
$existing = $this->getRandomAsset();
$data = [
2018-03-30 22:40:20 +02:00
'account_type_id' => null,
2019-08-29 17:53:25 +02:00
'account_type' => 'asset',
'name' => $existing->name,
'virtual_balance' => null,
2018-03-30 22:40:20 +02:00
'iban' => null,
2018-03-01 20:54:50 +01:00
'active' => true,
2019-06-16 13:16:46 +02:00
'account_role' => 'defaultAsset',
2018-03-01 20:54:50 +01:00
];
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2018-03-01 20:54:50 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-01 20:54:50 +01:00
// assert stuff about account:
2019-08-29 17:53:25 +02:00
$this->assertEquals($account->id, $existing->id);
2018-03-01 20:54:50 +01:00
}
/**
2019-08-29 17:53:25 +02:00
* Create an expense account. This overrules the virtual balance.
* Role should not be set.
2018-03-01 20:54:50 +01:00
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-08-29 17:53:25 +02:00
public function testCreateExpense(): void
2018-03-01 20:54:50 +01:00
{
2019-08-29 17:53:25 +02:00
$this->mock(AccountRepositoryInterface::class);
$this->mock(TransactionGroupFactory::class);
2019-08-03 14:45:37 +02:00
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2018-03-01 20:54:50 +01:00
2019-08-29 17:53:25 +02:00
$metaFactory = $this->mock(AccountMetaFactory::class);
$data = [
'account_type_id' => null,
'account_type' => 'expense',
'iban' => null,
'name' => sprintf('Basic expense account #%d', $this->randomInt()),
'virtual_balance' => '1243',
'active' => true,
'account_role' => 'defaultAsset',
];
2019-06-16 13:16:46 +02:00
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'interest', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'interest_period', ''])->atLeast()->once()->andReturnNull();
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2019-06-16 13:16:46 +02:00
2018-03-01 20:54:50 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
2019-08-29 17:53:25 +02:00
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2019-06-16 13:16:46 +02:00
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-01 20:54:50 +01:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
2019-08-29 17:53:25 +02:00
$this->assertEquals(AccountType::EXPENSE, $account->accountType->type);
2018-03-01 20:54:50 +01:00
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2019-08-29 17:53:25 +02:00
// get the role:
/** @var AccountMeta $meta */
$meta = $account->accountMeta()->where('name', 'accountRole')->first();
$this->assertNull($meta);
$account->forceDelete();
2018-03-01 20:54:50 +01:00
}
/**
2019-08-29 17:53:25 +02:00
* Create an expense account. This overrules the virtual balance.
* Role should not be set. This time set type name, not ID.
2018-03-01 20:54:50 +01:00
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-08-29 17:53:25 +02:00
public function testCreateExpenseFullType(): void
2018-03-01 20:54:50 +01:00
{
2019-08-29 17:53:25 +02:00
$this->mock(AccountRepositoryInterface::class);
$this->mock(TransactionGroupFactory::class);
2019-08-03 14:45:37 +02:00
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-08-29 17:53:25 +02:00
$metaFactory = $this->mock(AccountMetaFactory::class);
2019-08-03 14:45:37 +02:00
$data = [
2019-08-29 17:53:25 +02:00
'account_type_id' => null,
'account_type' => 'Expense account',
'iban' => null,
'name' => sprintf('Basic expense account #%d', $this->randomInt()),
'virtual_balance' => '1243',
'active' => true,
'account_role' => 'defaultAsset',
2018-03-01 20:54:50 +01:00
];
2019-06-16 13:16:46 +02:00
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2019-06-16 13:16:46 +02:00
2018-03-01 20:54:50 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
2019-08-29 17:53:25 +02:00
////$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
////$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
////$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'interest', ''])->atLeast()->once()->andReturnNull();
////$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'interest_period', ''])->atLeast()->once()->andReturnNull();
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
2019-08-29 17:53:25 +02:00
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2019-06-16 13:16:46 +02:00
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-01 20:54:50 +01:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
2019-08-29 17:53:25 +02:00
$this->assertEquals(AccountType::EXPENSE, $account->accountType->type);
2018-03-01 20:54:50 +01:00
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2019-08-29 17:53:25 +02:00
// get the role:
/** @var AccountMeta $meta */
$meta = $account->accountMeta()->where('name', 'accountRole')->first();
$this->assertNull($meta);
2019-06-16 13:16:46 +02:00
$account->forceDelete();
2018-03-01 20:54:50 +01:00
}
/**
* Add valid IBAN.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-06-16 13:16:46 +02:00
public function testCreateIban(): void
2018-03-01 20:54:50 +01:00
{
2019-08-29 17:53:25 +02:00
Log::info(sprintf('Now in test %s', __METHOD__));
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
2019-08-03 14:45:37 +02:00
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-06-16 13:16:46 +02:00
$this->mock(TransactionGroupFactory::class);
2018-03-01 20:54:50 +01:00
$data = [
'account_type_id' => null,
2019-06-16 13:16:46 +02:00
'account_type' => 'asset',
'iban' => 'NL02ABNA0870809585',
2019-06-16 13:16:46 +02:00
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
2018-03-01 20:54:50 +01:00
'active' => true,
2019-06-16 13:16:46 +02:00
'account_role' => 'defaultAsset',
2018-03-01 20:54:50 +01:00
];
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2019-06-16 13:16:46 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
2019-08-03 14:45:37 +02:00
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
2018-03-01 20:54:50 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-01 20:54:50 +01:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('NL02ABNA0870809585', $account->iban);
2018-03-01 20:54:50 +01:00
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2019-06-16 13:16:46 +02:00
$account->forceDelete();
2018-03-01 20:54:50 +01:00
}
/**
* Add invalid IBAN.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-06-16 13:16:46 +02:00
public function testCreateInvalidIban(): void
2018-03-01 20:54:50 +01:00
{
2019-08-29 17:53:25 +02:00
Log::info(sprintf('Now in test %s', __METHOD__));
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
2019-08-03 14:45:37 +02:00
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-06-16 13:16:46 +02:00
$this->mock(TransactionGroupFactory::class);
2018-03-01 20:54:50 +01:00
$data = [
'account_type_id' => null,
2019-06-16 13:16:46 +02:00
'account_type' => 'asset',
2018-03-01 20:54:50 +01:00
'iban' => 'NL1XRABO032674X238',
2019-06-16 13:16:46 +02:00
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
2018-03-01 20:54:50 +01:00
'active' => true,
2019-06-16 13:16:46 +02:00
'account_role' => 'defaultAsset',
2018-03-01 20:54:50 +01:00
];
2019-06-16 13:16:46 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
2019-08-03 14:45:37 +02:00
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2018-03-01 20:54:50 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-01 20:54:50 +01:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2018-03-01 20:54:50 +01:00
}
/**
* Submit IB data for asset account.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-06-16 13:16:46 +02:00
public function testCreateNegativeIB(): void
2018-03-01 20:54:50 +01:00
{
2019-08-29 17:53:25 +02:00
Log::info(sprintf('Now in test %s', __METHOD__));
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$groupFactory = $this->mock(TransactionGroupFactory::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
2019-08-03 14:45:37 +02:00
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
$data = [
2019-06-16 13:16:46 +02:00
'account_type_id' => null,
'account_type' => 'asset',
'iban' => null,
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
'active' => true,
'account_role' => 'defaultAsset',
'opening_balance' => '-100',
'opening_balance_date' => new Carbon('2018-01-01'),
'currency_id' => 1,
2018-03-01 20:54:50 +01:00
];
2019-08-29 17:53:25 +02:00
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
$langPreference = new Preference;
$langPreference->data = 'en_US';
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($langPreference);
2019-06-16 13:16:46 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
$groupFactory->shouldReceive('setUser')->atLeast()->once();
$groupFactory->shouldReceive('create')->atLeast()->once();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
2019-08-03 14:45:37 +02:00
$currencyFactory->shouldReceive('find')->withArgs([1, ''])->atLeast()->once()->andReturn($euro);
2019-06-16 13:16:46 +02:00
2018-03-01 20:54:50 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-01 20:54:50 +01:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2019-06-16 13:16:46 +02:00
$account->forceDelete();
2018-03-01 20:54:50 +01:00
}
2019-08-29 17:53:25 +02:00
/**
* Can't find account type.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
public function testCreateNoType(): void
{
Log::info(sprintf('Now in test %s', __METHOD__));
$this->mock(AccountRepositoryInterface::class);
$this->mock(TransactionGroupFactory::class);
$this->mock(AccountMetaFactory::class);
$data = [
'account_type_id' => null,
'account_type' => 'bla-bla',
'iban' => null,
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
'active' => true,
'account_role' => 'defaultAsset',
];
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
try {
$factory->create($data);
} catch (FireflyException $e) {
$this->assertStringContainsString('AccountFactory::create() was unable to find account type #0 ("bla-bla").', $e->getMessage());
}
}
2018-03-01 20:54:50 +01:00
/**
* Add some notes to asset account.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-06-16 13:16:46 +02:00
public function testCreateNotes(): void
2018-03-01 20:54:50 +01:00
{
2019-08-29 17:53:25 +02:00
Log::info(sprintf('Now in test %s', __METHOD__));
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
2019-08-03 14:45:37 +02:00
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-06-16 13:16:46 +02:00
$this->mock(TransactionGroupFactory::class);
2018-03-01 20:54:50 +01:00
$data = [
'account_type_id' => null,
2019-06-16 13:16:46 +02:00
'account_type' => 'asset',
2018-03-01 20:54:50 +01:00
'iban' => null,
2019-06-16 13:16:46 +02:00
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
2018-03-01 20:54:50 +01:00
'active' => true,
2019-06-16 13:16:46 +02:00
'account_role' => 'defaultAsset',
2018-03-01 20:54:50 +01:00
'notes' => 'Hello!',
];
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2018-03-01 20:54:50 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
2019-08-03 14:45:37 +02:00
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-01 20:54:50 +01:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2018-03-01 20:54:50 +01:00
$note = $account->notes()->first();
$this->assertEquals('Hello!', $note->text);
}
2018-12-21 16:38:10 +01:00
/**
2019-08-29 17:53:25 +02:00
* Submit valid opening balance data for asset account.
2018-12-21 16:38:10 +01:00
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-08-29 17:53:25 +02:00
public function testCreateOB(): void
2018-12-21 16:38:10 +01:00
{
2019-08-29 17:53:25 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$groupFactory = $this->mock(TransactionGroupFactory::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
2019-08-03 14:45:37 +02:00
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
$data = [
'account_type_id' => null,
'account_type' => 'asset',
'iban' => null,
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
'active' => true,
'account_role' => 'defaultAsset',
'opening_balance' => '100',
'opening_balance_date' => new Carbon('2018-01-01'),
'currency_id' => 1,
2018-12-21 16:38:10 +01:00
];
2019-06-16 13:16:46 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
2019-08-29 17:53:25 +02:00
$groupFactory->shouldReceive('setUser')->atLeast()->once();
$groupFactory->shouldReceive('create')->atLeast()->once();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
$currencyFactory->shouldReceive('find')->withArgs([1, ''])->atLeast()->once()->andReturn($euro);
$langPreference = new Preference;
$langPreference->data = 'en_US';
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($langPreference);
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2018-12-21 16:38:10 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
return;
}
2018-12-21 16:38:10 +01:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
}
/**
2019-08-29 17:53:25 +02:00
* Submit empty (amount = 0) IB data for asset account.
2018-12-21 16:38:10 +01:00
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
2019-08-29 17:53:25 +02:00
public function testCreateOBZero(): void
2018-12-21 16:38:10 +01:00
{
2019-08-29 17:53:25 +02:00
// mock repositories:
2019-06-16 13:16:46 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$this->mock(TransactionGroupFactory::class);
2019-08-29 17:53:25 +02:00
$metaFactory = $this->mock(AccountMetaFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
$euro = $this->getEuro();
$data = [
'account_type_id' => null,
'account_type' => 'asset',
'iban' => null,
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
'active' => true,
'account_role' => 'defaultAsset',
'opening_balance' => '0.0',
'opening_balance_date' => new Carbon('2018-01-01'),
'currency_id' => 1,
2018-12-21 16:38:10 +01:00
];
2019-06-16 13:16:46 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
$currencyFactory->shouldReceive('find')->withArgs([1, ''])->atLeast()->once()->andReturn($euro);
$langPreference = new Preference;
$langPreference->data = 'en_US';
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($langPreference);
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2018-12-21 16:38:10 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
return;
}
2018-12-21 16:38:10 +01:00
2019-08-29 17:53:25 +02:00
2018-12-21 16:38:10 +01:00
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2019-08-29 17:53:25 +02:00
$account->forceDelete();
2018-12-21 16:38:10 +01:00
}
2018-03-01 20:54:50 +01:00
/**
2019-08-29 17:53:25 +02:00
* Test minimal set of data to make factory work (asset account).
*
* Add some details
2018-03-01 20:54:50 +01:00
*
* @covers \FireflyIII\Factory\AccountFactory
2019-08-29 17:53:25 +02:00
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
2018-03-01 20:54:50 +01:00
*/
2019-08-29 17:53:25 +02:00
public function testCreateWithNumbers(): void
2018-03-01 20:54:50 +01:00
{
2019-08-29 17:53:25 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-06-16 13:16:46 +02:00
$this->mock(TransactionGroupFactory::class);
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
$data = [
'account_type_id' => null,
'account_type' => 'asset',
'iban' => null,
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
'active' => true,
'account_role' => 'defaultAsset',
'BIC' => 'BIC',
'include_net_worth' => false,
'account_number' => '1234',
2018-03-01 20:54:50 +01:00
];
2019-08-29 17:53:25 +02:00
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', '1234'])->atLeast()->once()->andReturnNull();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', 'BIC'])->atLeast()->once()->andReturnNull();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', false])->atLeast()->once()->andReturnNull();
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn($euro);
2019-06-16 13:16:46 +02:00
2018-03-01 20:54:50 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2019-06-16 13:16:46 +02:00
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
2019-08-29 17:53:25 +02:00
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2019-06-16 13:16:46 +02:00
$this->assertTrue(false, $e->getMessage());
return;
}
2018-03-01 20:54:50 +01:00
// assert stuff about account:
2019-08-29 17:53:25 +02:00
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
2018-08-24 07:18:33 +02:00
2019-08-29 17:53:25 +02:00
$account->forceDelete();
2018-08-24 07:18:33 +02:00
}
/**
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
public function testFindOrCreate(): void
{
2019-06-16 13:16:46 +02:00
$this->mock(AccountRepositoryInterface::class);
$this->mock(TransactionGroupFactory::class);
$this->mock(AccountMetaFactory::class);
2018-08-24 07:18:33 +02:00
/** @var Account $account */
2018-09-27 06:26:03 +02:00
$account = $this->getRandomRevenue();
2019-06-16 13:16:46 +02:00
2018-08-24 07:18:33 +02:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
2018-09-27 06:26:03 +02:00
Log::debug(sprintf('Searching for account #%d with name "%s"', $account->id, $account->name));
2018-08-24 07:18:33 +02:00
$result = $factory->findOrCreate($account->name, $account->accountType->type);
$this->assertEquals($result->id, $account->id);
}
2019-06-16 13:16:46 +02:00
/**
* Test only for existing account because the rest has been covered by other tests.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
public function testFindOrCreateNew(): void
{
$this->mock(AccountRepositoryInterface::class);
$this->mock(TransactionGroupFactory::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
/** @var Account $account */
2019-08-29 17:53:25 +02:00
$account = $this->getRandomRevenue();
2019-08-03 14:45:37 +02:00
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2019-06-16 13:16:46 +02:00
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'BIC', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'include_net_worth', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'interest', ''])->atLeast()->once()->andReturnNull();
//$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'interest_period', ''])->atLeast()->once()->andReturnNull();
2019-08-03 14:45:37 +02:00
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro);
2019-06-16 13:16:46 +02:00
$name = sprintf('New %s', $account->name);
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
Log::debug(sprintf('Searching for account #%d with name "%s"', $account->id, $account->name));
$result = $factory->findOrCreate($name, $account->accountType->type);
$this->assertNotEquals($result->id, $account->id);
$result->forceDelete();
}
2018-03-04 15:14:29 +01:00
}