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

116 lines
4.1 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
*/
2019-06-16 13:16:46 +02:00
public function testCreate(): void
2018-03-01 20:54:50 +01:00
{
2020-08-01 05:32:38 +02:00
// mock repositories
2019-08-03 14:45:37 +02:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
2020-08-01 05:32:38 +02:00
$euro = $this->getEuro();
$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
];
2020-08-01 05:32:38 +02:00
// no currency submitted means: find the EURO:
2019-08-03 14:45:37 +02:00
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn($euro);
2018-12-21 16:38:10 +01:00
2019-08-03 14:45:37 +02:00
$currencyFactory->shouldReceive('find')->withArgs([0, ''])->atLeast()->once()->andReturnNull();
2019-08-29 17:53:25 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
2019-06-16 13:16:46 +02:00
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'currency_id', '1'])->atLeast()->once()->andReturnNull();
2020-08-01 05:32:38 +02:00
// get opening balance group (null for new accounts)
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
2019-08-29 17:53:25 +02: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
}
2018-03-04 15:14:29 +01:00
}