From b1cdf4eb1a4f9bd49e033790bc3f94395f04ca6c Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sun, 8 Dec 2024 02:02:03 +0300 Subject: [PATCH 1/2] =?UTF-8?q?-=20=D0=9F=D0=BE=D1=87=D0=B8=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20Fetcher=20=D1=82=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Entity/Bitrix24Account.php | 5 ---- .../Doctrine/Bitrix24AccountRepository.php | 1 - src/Bitrix24Accounts/ReadModel/Fetcher.php | 24 ++++++++----------- .../UseCase/Uninstall/Handler.php | 4 +++- .../Bitrix24Accounts/FetcherTest.php | 7 +----- 5 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/Bitrix24Accounts/Entity/Bitrix24Account.php b/src/Bitrix24Accounts/Entity/Bitrix24Account.php index 81aff19..e9f6408 100644 --- a/src/Bitrix24Accounts/Entity/Bitrix24Account.php +++ b/src/Bitrix24Accounts/Entity/Bitrix24Account.php @@ -89,11 +89,6 @@ public function getStatus(): Bitrix24AccountStatus return $this->status; } - public function setStatus(Bitrix24AccountStatus $bitrix24AccountStatus): void - { - $this->status = $bitrix24AccountStatus; - } - /** * @throws InvalidArgumentException */ diff --git a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php index 8959864..bed1bf4 100644 --- a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php +++ b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php @@ -102,7 +102,6 @@ public function delete(Uuid $uuid): void ); } - $bitrix24Account->setStatus(Bitrix24AccountStatus::deleted); $this->save($bitrix24Account); } diff --git a/src/Bitrix24Accounts/ReadModel/Fetcher.php b/src/Bitrix24Accounts/ReadModel/Fetcher.php index c3b3fc4..ac7676c 100644 --- a/src/Bitrix24Accounts/ReadModel/Fetcher.php +++ b/src/Bitrix24Accounts/ReadModel/Fetcher.php @@ -7,9 +7,9 @@ use Doctrine\ORM\EntityManagerInterface; use Knp\Component\Pager\Pagination\PaginationInterface; use Knp\Component\Pager\PaginatorInterface; - class Fetcher { + public function __construct( private readonly EntityManagerInterface $em, private readonly PaginatorInterface $paginator @@ -19,23 +19,19 @@ public function list( int $page, int $size ): PaginationInterface { - /* $queryBuilder = $this->em->getConnection()->createQueryBuilder() + $qb = $this->em->createQueryBuilder() ->select( 'b24account.id as id', 'b24account.status as status', - 'b24account.member_id as member_id', - 'b24account.domain_url as domain_url', - 'b24account.application_version as application_version', - 'b24account.created_at_utc as created_at', - 'b24account.updated_at_utc as updated_at', + 'b24account.memberId as member_id', + 'b24account.domainUrl as domain_url', + 'b24account.applicationVersion as application_version', + 'b24account.createdAt as created_at_utc', + 'b24account.updatedAt as updated_at_utc', ) - ->from('bitrix24account', 'b24account') - ->orderBy('b24account.created_at_utc', 'DESC'); - */ - // var_dump($queryBuilder->getMaxResults()); - $query = $this->em->createQuery('SELECT b24account FROM Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account b24account'); + ->from('Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account', 'b24account') + ->orderBy('b24account.createdAt', 'DESC'); - return $this->paginator->paginate($query, $page, $size); - // return $this->paginator->paginate($queryBuilder->getSQL(), $page, $size); + return $this->paginator->paginate($qb, $page, $size); } } diff --git a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php index 76c1eac..1c2a4c2 100644 --- a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php +++ b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php @@ -40,13 +40,15 @@ public function handle(Command $command): void foreach ($accounts as $account) { $account->applicationUninstalled($command->applicationToken); $this->bitrix24AccountRepository->save($account); - $this->flusher->flush(); + foreach ($account->emitEvents() as $event) { $this->eventDispatcher->dispatch($event); } } + $this->flusher->flush(); + $this->logger->debug('Bitrix24Accounts.Uninstall.Finish'); } } diff --git a/tests/Functional/Bitrix24Accounts/FetcherTest.php b/tests/Functional/Bitrix24Accounts/FetcherTest.php index 5a71445..15e651b 100644 --- a/tests/Functional/Bitrix24Accounts/FetcherTest.php +++ b/tests/Functional/Bitrix24Accounts/FetcherTest.php @@ -19,7 +19,6 @@ use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\HttpFoundation\Request; use Knp\Component\Pager\ArgumentAccess\RequestArgumentAccess; class FetcherTest extends TestCase @@ -41,9 +40,6 @@ protected function setUp(): void $eventDispatcher = new EventDispatcher(); $eventDispatcher->addSubscriber(new PaginationSubscriber()); $eventDispatcher->addSubscriber(new SortableSubscriber()); - // dd(Request::createFromGlobals()); - new RequestStack(); - // Request::createFromGlobals() $requestArgumentAccess = new RequestArgumentAccess(new RequestStack()); $this->paginator = new Paginator($eventDispatcher, $requestArgumentAccess); $this->fetcher = new Fetcher($this->entityManager, $this->paginator); @@ -63,8 +59,7 @@ public function testListReturnsPaginatedResults(): void $size = 10; // Вызов метода list $pagination = $this->fetcher->list($page, $size); - // var_dump($result->getItems()); - // var_dump($result->count()); + // Проверка, что результат является экземпляром PaginationInterface $this->assertInstanceOf(PaginationInterface::class, $pagination); From be68b225709cd3e4ab54219169a95643d734cbb0 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sun, 8 Dec 2024 17:44:16 +0300 Subject: [PATCH 2/2] =?UTF-8?q?-=20=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D0=BB=20flusher=20=D0=B8=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20=D0=BD=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D1=82=D0=B5=D1=81=D1=82=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- .../UseCase/ChangeDomainUrl/Handler.php | 9 ++++---- .../UseCase/InstallStart/Handler.php | 8 +++---- .../UseCase/RenewAuthToken/Handler.php | 7 +++--- .../UseCase/Uninstall/Handler.php | 8 +------ src/Services/Flusher.php | 23 +++++++++++++++---- .../Bitrix24Accounts/FetcherTest.php | 2 +- .../UseCase/ChangeDomainUrl/HandlerTest.php | 6 ++--- .../UseCase/InstallFinish/HandlerTest.php | 5 ++-- .../UseCase/InstallStart/HandlerTest.php | 5 ++-- .../UseCase/RenewAuthToken/HandlerTest.php | 4 ++-- .../UseCase/Uninstall/HandlerTest.php | 9 ++++++-- 12 files changed, 52 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 373bd26..28e6bab 100644 --- a/Makefile +++ b/Makefile @@ -106,7 +106,7 @@ test-run-functional: debug-print-env # Запустить один функциональный тест с дебагером run-one-functional-test: debug-print-env - docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testListReturnsPaginatedResults' tests/Functional/Bitrix24Accounts/FetcherTest.php + docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testChangeDomainUrlWithHappyPath' tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php schema-drop: docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force diff --git a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php index 7f31bff..a02af34 100644 --- a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php +++ b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php @@ -12,7 +12,6 @@ readonly class Handler { public function __construct( - private EventDispatcherInterface $eventDispatcher, private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository, private Flusher $flusher, private LoggerInterface $logger @@ -29,13 +28,15 @@ public function handle(Command $command): void foreach ($accounts as $account) { $account->changeDomainUrl($command->newDomainUrlHost); $this->bitrix24AccountRepository->save($account); - $this->flusher->flush(); + // $this->flusher->flush(); // todo выяснить почему он не видит объединение типов // @phpstan-ignore-next-line - foreach ($account->emitEvents() as $event) { + /* foreach ($account->emitEvents() as $event) { $this->eventDispatcher->dispatch($event); - } + }*/ + } + $this->flusher->flush(...$accounts); $this->logger->debug('Bitrix24Accounts.ChangeDomainUrl.Finish'); } diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php index 074e77a..a401b03 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php @@ -15,7 +15,6 @@ readonly class Handler { public function __construct( - private EventDispatcherInterface $eventDispatcher, private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository, private Flusher $flusher, private LoggerInterface $logger @@ -44,11 +43,12 @@ public function handle(Command $command): void true ); $this->bitrix24AccountRepository->save($bitrix24Account); - $this->flusher->flush(); + $this->flusher->flush($bitrix24Account); + // $this->flusher->flush(); + /*foreach ($bitrix24Account->emitEvents() as $event) { - foreach ($bitrix24Account->emitEvents() as $event) { $this->eventDispatcher->dispatch($event); - } + }*/ $this->logger->debug('Bitrix24Accounts.InstallStart.Finish'); } diff --git a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php index b5ebdda..13d408c 100644 --- a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php +++ b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php @@ -16,7 +16,6 @@ readonly class Handler { public function __construct( - private EventDispatcherInterface $eventDispatcher, private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository, private Flusher $flusher, private LoggerInterface $logger @@ -45,10 +44,10 @@ public function handle(Command $command): void $bitrix24Account->renewAuthToken($command->renewedAuthToken); $this->bitrix24AccountRepository->save($bitrix24Account); - $this->flusher->flush(); - foreach ($bitrix24Account->emitEvents() as $event) { + $this->flusher->flush($bitrix24Account); + /* foreach ($bitrix24Account->emitEvents() as $event) { $this->eventDispatcher->dispatch($event); - } + }*/ $this->logger->debug('Bitrix24Accounts.RenewAuthToken.finish'); } diff --git a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php index 1c2a4c2..f30e983 100644 --- a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php +++ b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php @@ -16,7 +16,6 @@ readonly class Handler { public function __construct( - private EventDispatcherInterface $eventDispatcher, private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository, private Flusher $flusher, private LoggerInterface $logger, @@ -40,14 +39,9 @@ public function handle(Command $command): void foreach ($accounts as $account) { $account->applicationUninstalled($command->applicationToken); $this->bitrix24AccountRepository->save($account); - - - foreach ($account->emitEvents() as $event) { - $this->eventDispatcher->dispatch($event); - } } + $this->flusher->flush(...$accounts); - $this->flusher->flush(); $this->logger->debug('Bitrix24Accounts.Uninstall.Finish'); } diff --git a/src/Services/Flusher.php b/src/Services/Flusher.php index 0d47faa..ec9af9e 100644 --- a/src/Services/Flusher.php +++ b/src/Services/Flusher.php @@ -4,14 +4,29 @@ namespace Bitrix24\Lib\Services; -use Doctrine\ORM\EntityManagerInterface; -final readonly class Flusher +use Bitrix24\Lib\AggregateRoot; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\EventDispatcher\EventDispatcher; +class Flusher { - public function __construct(private EntityManagerInterface $em) {} + private $em; + private $eventDispatcher; + public function __construct(EntityManagerInterface $em,EventDispatcher $eventDispatcher) { + $this->em = $em; + $this->eventDispatcher = $eventDispatcher; + } - public function flush(): void + public function flush(AggregateRoot ...$roots): void { $this->em->flush(); + + foreach ($roots as $root) { + $events = $root->emitEvents(); + foreach ($events as $event) { + var_dump($event); + $this->eventDispatcher->dispatch($event); + } + } } } diff --git a/tests/Functional/Bitrix24Accounts/FetcherTest.php b/tests/Functional/Bitrix24Accounts/FetcherTest.php index 15e651b..46698c9 100644 --- a/tests/Functional/Bitrix24Accounts/FetcherTest.php +++ b/tests/Functional/Bitrix24Accounts/FetcherTest.php @@ -43,7 +43,7 @@ protected function setUp(): void $requestArgumentAccess = new RequestArgumentAccess(new RequestStack()); $this->paginator = new Paginator($eventDispatcher, $requestArgumentAccess); $this->fetcher = new Fetcher($this->entityManager, $this->paginator); - $this->flusher = new Flusher($this->entityManager); + $this->flusher = new Flusher($this->entityManager,$eventDispatcher); $this->repository = new Bitrix24AccountRepository($this->entityManager); } diff --git a/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php index a590319..b724574 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php @@ -48,11 +48,11 @@ class HandlerTest extends TestCase protected function setUp(): void { $entityManager = EntityManagerFactory::get(); + $eventDispatcher = new EventDispatcher(); $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager); - $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); + $this->flusher = new Flusher($entityManager,$eventDispatcher); + $this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch()); $this->handler = new Bitrix24Accounts\UseCase\ChangeDomainUrl\Handler( - $this->eventDispatcher, $this->repository, $this->flusher, new NullLogger() diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php index d444316..2ea70ec 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php @@ -84,9 +84,10 @@ public function testFinishInstallationWithHappyPath(): void protected function setUp(): void { $entityManager = EntityManagerFactory::get(); + $eventDispatcher = new EventDispatcher(); $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager); - $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); + $this->flusher = new Flusher($entityManager,$eventDispatcher); + $this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch()); $this->handler = new Bitrix24Accounts\UseCase\InstallFinish\Handler( $this->eventDispatcher, $this->repository, diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php index f9df3f5..057ce05 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php @@ -52,11 +52,11 @@ class HandlerTest extends TestCase protected function setUp(): void { $entityManager = EntityManagerFactory::get(); + $eventDispatcher = new EventDispatcher(); $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager); + $this->flusher = new Flusher($entityManager,$eventDispatcher); $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $this->handler = new Bitrix24Accounts\UseCase\InstallStart\Handler( - $this->eventDispatcher, $this->repository, $this->flusher, new NullLogger() @@ -156,6 +156,7 @@ public function testInstallStartHappyPath(): void 'Object not equals' ); + // var_dump($this->eventDispatcher->getOrphanedEvents()); $this->assertContains( Bitrix24AccountCreatedEvent::class, $this->eventDispatcher->getOrphanedEvents(), diff --git a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php index 7109cdd..fb775d0 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php @@ -49,11 +49,11 @@ class HandlerTest extends TestCase protected function setUp(): void { $entityManager = EntityManagerFactory::get(); + $eventDispatcher = new EventDispatcher(); $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager); + $this->flusher = new Flusher($entityManager,$eventDispatcher); $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $this->handler = new Handler( - $this->eventDispatcher, $this->repository, $this->flusher, new NullLogger() diff --git a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php index d00bbec..c83dc18 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php @@ -25,6 +25,7 @@ use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; use Bitrix24\SDK\Core\Credentials\AuthToken; use Bitrix24\SDK\Core\Credentials\Scope; +use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; use Carbon\CarbonImmutable; use Override; use PHPUnit\Framework\Attributes\CoversClass; @@ -47,6 +48,10 @@ class HandlerTest extends TestCase private TraceableEventDispatcher $eventDispatcher; + /** + * @throws InvalidArgumentException + * @throws Bitrix24AccountNotFoundException + */ #[Test] public function testUninstallWithHappyPath(): void { @@ -97,12 +102,12 @@ protected function setUp(): void { $entityManager = EntityManagerFactory::get(); + $eventDispatcher = new EventDispatcher(); $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager); + $this->flusher = new Flusher($entityManager, $eventDispatcher); $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $this->handler = new Bitrix24Accounts\UseCase\Uninstall\Handler( - $this->eventDispatcher, $this->repository, $this->flusher, new NullLogger(),