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

340 lines
11 KiB
PHP
Raw Normal View History

2018-03-01 20:54:50 +01:00
<?php
/**
* AccountFactoryTest.php
2020-02-16 13:59:55 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-03-01 20:54:50 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-03-01 20:54:50 +01: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-03-01 20:54:50 +01:00
*
* This program is distributed in the hope that it will be useful,
2018-03-01 20:54:50 +01:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-03-01 20:54:50 +01: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-03-01 20:54:50 +01:00
*/
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
*/
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
2020-08-01 05:32:38 +02:00
* @covers \FireflyIII\Services\Internal\Support\LocationServiceTrait
2018-03-01 20:54:50 +01:00
*/
2020-08-01 07:55:55 +02:00
public function testCreateAsset(): void
2018-03-01 20:54:50 +01:00
{
2020-08-01 07:55:55 +02:00
$data = [
2018-03-01 20:54:50 +01:00
'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
];
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);
2020-08-01 05:32:38 +02:00
$this->assertEquals(0, $account->order);
$this->assertNull($account->virtual_balance);
2019-08-29 17:53:25 +02:00
$account->forceDelete();
2018-03-01 20:54:50 +01:00
}
2020-08-01 07:55:55 +02:00
/**
* Submit invalid IBAN, so assume NULL on final result.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
* @covers \FireflyIII\Services\Internal\Support\LocationServiceTrait
*/
public function testCreateInvalidIBAN(): void
{
$data = [
'account_type_id' => null,
'account_type' => 'asset',
'iban' => 'IAMINVALID',
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
'active' => true,
'account_role' => 'defaultAsset',
];
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->assertTrue(false, $e->getMessage());
return;
}
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertTrue($account->active);
$this->assertEquals(0, $account->order);
$this->assertNull($account->virtual_balance);
$this->assertNull($account->iban);
$account->forceDelete();
}
/**
* Submit invalid IBAN, so assume NULL on final result.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
* @covers \FireflyIII\Services\Internal\Support\LocationServiceTrait
*/
public function testCreateValidIBAN(): void
{
$data = [
'account_type_id' => null,
'account_type' => 'asset',
'iban' => 'NL83ABNA8548609842', // fake IBAN, ABN AMRO IBAN's are always "ABNA0".
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
'active' => true,
'account_role' => 'defaultAsset',
];
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->assertTrue(false, $e->getMessage());
return;
}
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
$this->assertTrue($account->active);
$this->assertEquals(0, $account->order);
$this->assertNull($account->virtual_balance);
$this->assertEquals($data['iban'], $account->iban);
$account->forceDelete();
}
/**
* Create asset, include opening balance.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
* @covers \FireflyIII\Services\Internal\Support\LocationServiceTrait
*/
public function testCreateAssetOpeningBalance(): void
{
$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' => '1234.56',
'opening_balance_date' => today(),
];
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$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->order);
$this->assertNull($account->virtual_balance);
$this->assertCount(1, $account->transactions()->get());
$account->forceDelete();
}
/**
* Create expense account.
*
* - Virtual balance must become NULL despite being set.
* - Account type is found by ID
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
* @covers \FireflyIII\Services\Internal\Support\LocationServiceTrait
*/
public function testCreateExpense(): void
{
$expense = AccountType::where('type', AccountType::EXPENSE)->first();
$data = [
'account_type_id' => $expense->id ?? null,
'iban' => null,
'name' => sprintf('Basic expense account #%d', $this->randomInt()),
'virtual_balance' => '1234.56',
'active' => true,
];
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
try {
$account = $factory->create($data);
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->assertTrue(false, $e->getMessage());
return;
}
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::EXPENSE, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals(0, $account->order);
$this->assertNull($account->virtual_balance);
$account->forceDelete();
}
/**
* Unknown type.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
* @covers \FireflyIII\Services\Internal\Support\LocationServiceTrait
*/
public function testCreateErrorType(): void
{
// mock repositories
$data = [
'account_type_id' => null,
'account_type' => 'bad-type',
'iban' => null,
'name' => sprintf('Basic asset account #%d', $this->randomInt()),
'virtual_balance' => null,
'active' => true,
'account_role' => 'defaultAsset',
];
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
try {
$factory->create($data);
} catch (FireflyException $e) {
$this->assertEquals($e->getMessage(), 'AccountFactory::create() was unable to find account type #0 ("bad-type").');
return;
}
$this->assertTrue(false, 'Should not reach here.');
}
/**
* Find expense account we know doesn't exist.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
* @covers \FireflyIII\Services\Internal\Support\LocationServiceTrait
*/
public function testFindOrCreate(): void
{
$name = sprintf('Basic account #%d', $this->randomInt());
$type = AccountType::EXPENSE;
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
try {
$account = $factory->findOrCreate($name, $type);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
return;
}
$this->assertEquals($name, $account->name);
$this->assertEquals($type, $account->accountType->type);
}
2018-03-04 15:14:29 +01:00
}