Expand some test code.

This commit is contained in:
James Cole 2020-08-01 07:55:55 +02:00
parent c26de3bdd2
commit 43fc4c5f52
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
4 changed files with 244 additions and 29 deletions

View File

@ -75,10 +75,10 @@ class AccountFactory
*/
public function create(array $data): Account
{
$type = $this->getAccountType($data['account_type_id'], $data['account_type']);
$type = $this->getAccountType($data['account_type_id'] ?? null, $data['account_type'] ?? null);
if (null === $type) {
throw new FireflyException(sprintf('AccountFactory::create() was unable to find account type #%d ("%s").', $data['account_type_id'], $data['account_type']));
throw new FireflyException(sprintf('AccountFactory::create() was unable to find account type #%d ("%s").', $data['account_type_id'] ?? null, $data['account_type'] ?? null));
}
$data['iban'] = $this->filterIban($data['iban'] ?? null);

View File

@ -50,17 +50,6 @@ class BudgetRepository implements BudgetRepositoryInterface
/** @var User */
private $user;
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
die(get_class($this));
}
}
/**
* @return bool
*/

View File

@ -42,19 +42,7 @@ use Storage;
*/
class CategoryRepository implements CategoryRepositoryInterface
{
/** @var User */
private $user;
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
die(__METHOD__);
}
}
private User $user;
/**
* @param Category $category

View File

@ -62,10 +62,9 @@ class AccountFactoryTest extends TestCase
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
* @covers \FireflyIII\Services\Internal\Support\LocationServiceTrait
*/
public function testCreateNoMockery(): void
public function testCreateAsset(): void
{
// mock repositories
$data = [
$data = [
'account_type_id' => null,
'account_type' => 'asset',
'iban' => null,
@ -98,4 +97,243 @@ class AccountFactoryTest extends TestCase
$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 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);
}
}