Skip to content

Commit

Permalink
Added the account repository interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick in t Veld committed Sep 5, 2024
1 parent dbf2390 commit 59ab3fd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Action/Account/FetchAccounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
namespace App\Action\Account;

use App\Action\ActionInterface;
use App\Contract\Repository\AccountRepositoryInterface;
use App\Contract\Repository\MyFxBookRepositoryInterface;
use App\Dto\Aggregator\AggregateInterface;
use App\Event\CreateAccountEvent;
use App\Event\UpdateAccountEvent;
use App\Exception\AccountNotFoundException;
use App\Repository\AccountRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final readonly class FetchAccounts implements ActionInterface
{
public function __construct(
private AccountRepository $accountRepository,
private AccountRepositoryInterface $accountRepository,
private EventDispatcherInterface $eventBus,
private MyFxBookRepositoryInterface $myFxBookRepository
) {}
Expand Down
15 changes: 15 additions & 0 deletions src/Contract/Repository/AccountRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Contract\Repository;

use App\Entity\Account;

/**
* @method Account|null find($id, $lockMode = null, $lockVersion = null)
* @method Account|null findOneBy(array $criteria, array $orderBy = null)
* @method Account[] findAll()
* @method Account[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
interface AccountRepositoryInterface {}
3 changes: 2 additions & 1 deletion src/Repository/AccountRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Repository;

use App\Contract\Repository\AccountRepositoryInterface;
use App\Entity\Account;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
Expand All @@ -16,7 +17,7 @@
* @method Account[] findAll()
* @method Account[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class AccountRepository extends ServiceEntityRepository
class AccountRepository extends ServiceEntityRepository implements AccountRepositoryInterface
{
public function __construct(ManagerRegistry $registry)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Subscriber/CreateAccountEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace App\Subscriber;

use App\Contract\Repository\AccountRepositoryInterface;
use App\Entity\Account;
use App\Event\CreateAccountEvent;
use App\Event\UpdateAccountEvent;
use App\Repository\AccountRepository;
use App\Serializer\Serializer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
Expand All @@ -17,7 +17,7 @@
class CreateAccountEventListener
{
public function __construct(
private readonly AccountRepository $accountRepository,
private readonly AccountRepositoryInterface $accountRepository,
private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventBus,
private readonly Serializer $serializer
Expand Down
8 changes: 4 additions & 4 deletions tests/Action/Account/FetchAccountsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace App\Tests\Action\Account;

use App\Action\Account\FetchAccounts;
use App\Contract\Repository\AccountRepositoryInterface;
use App\Contract\Repository\MyFxBookRepositoryInterface;
use App\Dto\Aggregator\AggregateRoot;
use App\Entity\Account;
use App\Event\CreateAccountEvent;
use App\Event\UpdateAccountEvent;
use App\Repository\AccountRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand All @@ -19,10 +19,10 @@ public function testAction(): void
$account = $this->createMock(Account::class);
$account->method('getAccountId')->willReturn(1);

$accountRepository = $this->getMockBuilder(AccountRepository::class)->disableOriginalConstructor()->getMock();
$accountRepository = $this->getMockBuilder(AccountRepositoryInterface::class)->addMethods(['findAll'])->getMock();
$accountRepository->expects($this->once())->method('findAll')->willReturn([$account]);

$eventBus = $this->getMockBuilder(EventDispatcherInterface::class)->disableOriginalConstructor()->getMock();
$eventBus = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
$eventBus->expects($this->exactly(3))->method('dispatch')->with($this->callback(function ($object) {
if ($object instanceof CreateAccountEvent) {
return true;
Expand All @@ -38,7 +38,7 @@ public function testAction(): void
$myFxBookRepository->expects($this->once())->method('accounts')->with('123abc#')->willReturn([['accountId' => 1], ['accountId' => 2]]);

/**
* @var AccountRepository $accountRepository
* @var AccountRepositoryInterface $accountRepository
* @var EventDispatcherInterface $eventBus
* @var MyFxBookRepositoryInterface $myFxBookRepository
*/
Expand Down

0 comments on commit 59ab3fd

Please sign in to comment.