Skip to content

Commit

Permalink
Refactor tests (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jul 14, 2023
1 parent 1605df8 commit 5682ef8
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 245 deletions.
124 changes: 124 additions & 0 deletions tests/Account/AbstractAccountManagerTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace PhpFinance\DoubleEntry\Tests\Account;

use PhpFinance\DoubleEntry\Domain\Account\Account;
use PhpFinance\DoubleEntry\Domain\Account\AccountId;
use PhpFinance\DoubleEntry\Domain\Account\AccountManager;
use PhpFinance\DoubleEntry\Domain\Account\AccountRepositoryInterface;
use PhpFinance\DoubleEntry\Domain\Account\Exception\AccountDeletionNotPossibleException;
use PhpFinance\DoubleEntry\Domain\Posting\Factory\EntryData;
use PhpFinance\DoubleEntry\Domain\Posting\Posting;
use PhpFinance\DoubleEntry\Domain\Posting\PostingRepositoryInterface;
use PhpFinance\DoubleEntry\Tests\Support\TestFactory;
use PHPUnit\Framework\TestCase;

abstract class AbstractAccountManagerTestCase extends TestCase
{
public function testGet(): void
{
$account = TestFactory::createAccount('incomes');
$accountManager = $this->createAccountManager(
accountRepository: $this->createAccountRepository($account),
);

$result = $accountManager->get(new AccountId('incomes'));

$this->assertSame($result, $account);
}

public function testSave(): void
{
$accountRepository = $this->createAccountRepository();
$accountManager = $this->createAccountManager(
accountRepository: $accountRepository,
);

$account = TestFactory::createAccount('incomes');
$accountManager->save($account);

$this->assertSame([$account], $accountRepository->getAll());
}

public function testCreate(): void
{
$accountManager = $this->createAccountManager(
newAccountIds: ['7'],
);

$account = $accountManager->create();

$this->assertSame('7', $account->id->value);
}

public function testDelete(): void
{
$account1 = TestFactory::createAccount('1');
$account2 = TestFactory::createAccount('2');
$accountRepository = $this->createAccountRepository($account1, $account2);
$accountManager = $this->createAccountManager(
accountRepository: $accountRepository,
);

$accountManager->delete($account1);

$this->assertSame([$account2], $accountRepository->getAll());
}

public function testDeleteWithChildren(): void
{
$accountIncomes = TestFactory::createAccount('incomes');
$accountSalary = TestFactory::createAccount(
'salary',
parent: $accountIncomes
);
$accountRepository = $this->createAccountRepository($accountIncomes, $accountSalary);
$accountManager = $this->createAccountManager(
accountRepository: $accountRepository,
);

$this->expectException(AccountDeletionNotPossibleException::class);
$this->expectExceptionMessage('Deletion not possible, account has children.');
$accountManager->delete($accountIncomes);
}

public function testDeleteWithPostings(): void
{
$account = TestFactory::createAccount('incomes');
$accountRepository = $this->createAccountRepository($account);
$accountManager = $this->createAccountManager(
accountRepository: $accountRepository,
postingRepository: $this->createPostingRepository(
TestFactory::createPosting(
new EntryData($account),
new EntryData($account),
),
),
);

$this->expectException(AccountDeletionNotPossibleException::class);
$this->expectExceptionMessage('Deletion not possible, entries with account exists.');
$accountManager->delete($account);
}

abstract protected function createAccountRepository(Account ...$accounts): AccountRepositoryInterface;

abstract protected function createPostingRepository(Posting ...$postings): PostingRepositoryInterface;

/**
* @param string[] $newAccountIds
*/
private function createAccountManager(
array $newAccountIds = [],
?AccountRepositoryInterface $accountRepository = null,
?PostingRepositoryInterface $postingRepository = null,
): AccountManager {
return new AccountManager(
$accountRepository ?? $this->createAccountRepository(),
TestFactory::createAccountIdFactory($newAccountIds),
$postingRepository ?? $this->createPostingRepository(),
);
}
}
102 changes: 0 additions & 102 deletions tests/Account/AccountManagerTest.php

This file was deleted.

10 changes: 4 additions & 6 deletions tests/Account/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
use InvalidArgumentException;
use PhpFinance\DoubleEntry\Domain\Account\Account;
use PhpFinance\DoubleEntry\Domain\Account\AccountId;
use PhpFinance\DoubleEntry\Tests\Support\TestTrait;
use PhpFinance\DoubleEntry\Tests\Support\TestFactory;
use PHPUnit\Framework\TestCase;

final class AccountTest extends TestCase
{
use TestTrait;

public function testRename(): void
{
$account = self::createAccount('incomes', name: 'My Incomes');
$account = TestFactory::createAccount('incomes', name: 'My Incomes');

$account->rename('All Incomes');

Expand All @@ -25,7 +23,7 @@ public function testRename(): void

public function testRenameToEmptyString(): void
{
$account = self::createAccount();
$account = TestFactory::createAccount();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Account name must be null or non-empty string no greater than 50 symbols.');
Expand All @@ -35,7 +33,7 @@ public function testRenameToEmptyString(): void
public function testCreateAccountWithParentFromAnotherChart(): void
{
$id = new AccountId('incomes');
$parent = self::createAccount(chartId: 'clients');
$parent = TestFactory::createAccount(chartId: 'clients');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Account chart of parent account is not equal to current.');
Expand Down
25 changes: 25 additions & 0 deletions tests/Account/InMemoryAccountManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace PhpFinance\DoubleEntry\Tests\Account;

use PhpFinance\DoubleEntry\Domain\Account\Account;
use PhpFinance\DoubleEntry\Domain\Account\AccountRepositoryInterface;
use PhpFinance\DoubleEntry\Domain\Posting\Posting;
use PhpFinance\DoubleEntry\Domain\Posting\PostingRepositoryInterface;
use PhpFinance\DoubleEntry\Tests\Support\InMemoryAccountRepository;
use PhpFinance\DoubleEntry\Tests\Support\InMemoryPostingRepository;

final class InMemoryAccountManagerTest extends AbstractAccountManagerTestCase
{
protected function createAccountRepository(Account ...$accounts): AccountRepositoryInterface
{
return new InMemoryAccountRepository($accounts);
}

protected function createPostingRepository(Posting ...$postings): PostingRepositoryInterface
{
return new InMemoryPostingRepository($postings);
}
}

0 comments on commit 5682ef8

Please sign in to comment.