Skip to content

Firefly III: a personal finances manager

Expand some test code.

James Cole43fc4c5f521f
diff --git a/app/Factory/AccountFactory.php b/app/Factory/AccountFactory.php
index 59a7a1ada1..2b29ab5fb3 100644
--- a/app/Factory/AccountFactory.php
+++ b/app/Factory/AccountFactory.php
@@ -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);
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index 14dd528e30..790f3e851f 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -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
*/
diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php
index b0d3b951b0..324834988b 100644
--- a/app/Repositories/Category/CategoryRepository.php
+++ b/app/Repositories/Category/CategoryRepository.php
@@ -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
diff --git a/tests/Unit/Factory/AccountFactoryTest.php b/tests/Unit/Factory/AccountFactoryTest.php
index ffb63c4836..fef6d56b19 100644
--- a/tests/Unit/Factory/AccountFactoryTest.php
+++ b/tests/Unit/Factory/AccountFactoryTest.php
@@ -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);
+
+ }
}