From 70f2c9fca5f47a446179097e7bee9bc017af3fd8 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Wed, 11 Dec 2024 01:07:10 +0300 Subject: [PATCH 1/4] =?UTF-8?q?-=20=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20=D0=B8?= =?UTF-8?q?=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20url=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=BC=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/tests-functional.yml | 3 --- Makefile | 2 +- src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php | 9 ++------- src/Services/Flusher.php | 7 +++---- .../UseCase/ChangeDomainUrl/HandlerTest.php | 5 ++--- .../UseCase/RenewAuthToken/HandlerTest.php | 2 +- 6 files changed, 9 insertions(+), 19 deletions(-) diff --git a/.github/workflows/tests-functional.yml b/.github/workflows/tests-functional.yml index 2227491..9db731a 100644 --- a/.github/workflows/tests-functional.yml +++ b/.github/workflows/tests-functional.yml @@ -39,9 +39,6 @@ jobs: POSTGRES_PASSWORD: b24phpLibTest POSTGRES_DB: b24phpLibTest DATABASE_HOST: localhost - DATABASE_USER: b24phpLibTest - DATABASE_PASSWORD: b24phpLibTest - DATABASE_NAME: b24phpLibTest steps: - name: "Checkout code" diff --git a/Makefile b/Makefile index 28e6bab..ad502d7 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 'testChangeDomainUrlWithHappyPath' tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php + docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testChangeDomainUrlWithHappyPathForManyAccounts' 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 a02af34..e370b63 100644 --- a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php +++ b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php @@ -28,14 +28,9 @@ public function handle(Command $command): void foreach ($accounts as $account) { $account->changeDomainUrl($command->newDomainUrlHost); $this->bitrix24AccountRepository->save($account); - // $this->flusher->flush(); - // todo выяснить почему он не видит объединение типов - // @phpstan-ignore-next-line - /* foreach ($account->emitEvents() as $event) { - $this->eventDispatcher->dispatch($event); - }*/ - } + + //используется как оператор распаковки (splat operator) для передачи массива как отдельных аргументов: $this->flusher->flush(...$accounts); $this->logger->debug('Bitrix24Accounts.ChangeDomainUrl.Finish'); diff --git a/src/Services/Flusher.php b/src/Services/Flusher.php index ec9af9e..c16a8e2 100644 --- a/src/Services/Flusher.php +++ b/src/Services/Flusher.php @@ -7,12 +7,12 @@ use Bitrix24\Lib\AggregateRoot; use Doctrine\ORM\EntityManagerInterface; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; class Flusher { private $em; private $eventDispatcher; - public function __construct(EntityManagerInterface $em,EventDispatcher $eventDispatcher) { + public function __construct(EntityManagerInterface $em,EventDispatcherInterface $eventDispatcher) { $this->em = $em; $this->eventDispatcher = $eventDispatcher; } @@ -20,11 +20,10 @@ public function __construct(EntityManagerInterface $em,EventDispatcher $eventDis public function flush(AggregateRoot ...$roots): void { $this->em->flush(); - foreach ($roots as $root) { $events = $root->emitEvents(); + var_dump($events); foreach ($events as $event) { - var_dump($event); $this->eventDispatcher->dispatch($event); } } diff --git a/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php index b724574..784aad2 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php @@ -26,10 +26,9 @@ use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; -use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Uid\Uuid; - +use Symfony\Component\EventDispatcher\EventDispatcher; /** * @internal */ @@ -50,8 +49,8 @@ protected function setUp(): void $entityManager = EntityManagerFactory::get(); $eventDispatcher = new EventDispatcher(); $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager,$eventDispatcher); $this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch()); + $this->flusher = new Flusher($entityManager,$this->eventDispatcher); $this->handler = new Bitrix24Accounts\UseCase\ChangeDomainUrl\Handler( $this->repository, $this->flusher, diff --git a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php index fb775d0..e8369b1 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php @@ -28,7 +28,7 @@ use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Stopwatch\Stopwatch; /** From 9f1ee0c30e6bbe1efab1d78e8089f5c834a6d6d6 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sat, 14 Dec 2024 00:13:22 +0300 Subject: [PATCH 2/4] =?UTF-8?q?-=20=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php | 7 ++----- src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php | 4 ---- src/Services/Flusher.php | 3 +-- .../Doctrine/Bitrix24AccountRepositoryTest.php | 5 ++++- .../Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php | 6 +++--- .../Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php | 4 ++-- .../UseCase/RenewAuthToken/HandlerTest.php | 6 +++--- .../Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php | 4 ++-- 8 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php b/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php index 35e9916..ab099c0 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php @@ -18,7 +18,6 @@ readonly class Handler { public function __construct( - private EventDispatcherInterface $eventDispatcher, private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository, private Flusher $flusher, private LoggerInterface $logger @@ -46,10 +45,8 @@ public function handle(Command $command): void $bitrix24Account->applicationInstalled($command->applicationToken); $this->bitrix24AccountRepository->save($bitrix24Account); - $this->flusher->flush(); - foreach ($bitrix24Account->emitEvents() as $event) { - $this->eventDispatcher->dispatch($event); - } + $this->flusher->flush($bitrix24Account); + $this->logger->debug('Bitrix24Accounts.InstallFinish.Finish'); } diff --git a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php index 13d408c..6551bc1 100644 --- a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php +++ b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php @@ -11,7 +11,6 @@ use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface; use Psr\Log\LoggerInterface; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; readonly class Handler { @@ -45,9 +44,6 @@ public function handle(Command $command): void $this->bitrix24AccountRepository->save($bitrix24Account); $this->flusher->flush($bitrix24Account); - /* foreach ($bitrix24Account->emitEvents() as $event) { - $this->eventDispatcher->dispatch($event); - }*/ $this->logger->debug('Bitrix24Accounts.RenewAuthToken.finish'); } diff --git a/src/Services/Flusher.php b/src/Services/Flusher.php index c16a8e2..585360b 100644 --- a/src/Services/Flusher.php +++ b/src/Services/Flusher.php @@ -12,7 +12,7 @@ class Flusher { private $em; private $eventDispatcher; - public function __construct(EntityManagerInterface $em,EventDispatcherInterface $eventDispatcher) { + public function __construct(EntityManagerInterface $em, EventDispatcherInterface $eventDispatcher) { $this->em = $em; $this->eventDispatcher = $eventDispatcher; } @@ -22,7 +22,6 @@ public function flush(AggregateRoot ...$roots): void $this->em->flush(); foreach ($roots as $root) { $events = $root->emitEvents(); - var_dump($events); foreach ($events as $event) { $this->eventDispatcher->dispatch($event); } diff --git a/tests/Functional/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepositoryTest.php b/tests/Functional/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepositoryTest.php index cf6e12b..177ce15 100644 --- a/tests/Functional/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepositoryTest.php +++ b/tests/Functional/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepositoryTest.php @@ -28,6 +28,7 @@ use Carbon\CarbonImmutable; use Override; use PHPUnit\Framework\Attributes\CoversClass; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Uid\Uuid; #[CoversClass(Bitrix24AccountRepository::class)] @@ -73,6 +74,8 @@ protected function createBitrix24AccountRepositoryImplementation(): Bitrix24Acco #[Override] protected function createRepositoryFlusherImplementation(): TestRepositoryFlusherInterface { - return new FlusherDecorator(new Flusher(EntityManagerFactory::get())); + $entityManager = EntityManagerFactory::get(); + $eventDispatcher = new EventDispatcher(); + return new FlusherDecorator(new Flusher($entityManager, $eventDispatcher)); } } \ No newline at end of file diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php index 2ea70ec..287db48 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php @@ -85,11 +85,11 @@ protected function setUp(): void { $entityManager = EntityManagerFactory::get(); $eventDispatcher = new EventDispatcher(); - $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager,$eventDispatcher); $this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch()); + $this->repository = new Bitrix24AccountRepository($entityManager); + $this->flusher = new Flusher($entityManager,$this->eventDispatcher); + $this->handler = new Bitrix24Accounts\UseCase\InstallFinish\Handler( - $this->eventDispatcher, $this->repository, $this->flusher, new NullLogger() diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php index 057ce05..add9878 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php @@ -53,9 +53,9 @@ protected function setUp(): void { $entityManager = EntityManagerFactory::get(); $eventDispatcher = new EventDispatcher(); - $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager,$eventDispatcher); $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); + $this->repository = new Bitrix24AccountRepository($entityManager); + $this->flusher = new Flusher($entityManager,$this->eventDispatcher); $this->handler = new Bitrix24Accounts\UseCase\InstallStart\Handler( $this->repository, $this->flusher, diff --git a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php index e8369b1..39f806c 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php @@ -28,8 +28,8 @@ use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Stopwatch\Stopwatch; +use Symfony\Component\EventDispatcher\EventDispatcher; /** * @internal @@ -50,9 +50,9 @@ protected function setUp(): void { $entityManager = EntityManagerFactory::get(); $eventDispatcher = new EventDispatcher(); - $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager,$eventDispatcher); $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); + $this->repository = new Bitrix24AccountRepository($entityManager); + $this->flusher = new Flusher($entityManager,$this->eventDispatcher); $this->handler = new Handler( $this->repository, $this->flusher, diff --git a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php index c83dc18..da6e1f0 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php @@ -103,9 +103,9 @@ protected function setUp(): void $entityManager = EntityManagerFactory::get(); $eventDispatcher = new EventDispatcher(); + $this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch()); $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager, $eventDispatcher); - $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); + $this->flusher = new Flusher($entityManager, $this->eventDispatcher); $this->handler = new Bitrix24Accounts\UseCase\Uninstall\Handler( $this->repository, From 50733f5956d89f8a2d2c8c3673d25e6983ae27a7 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sun, 15 Dec 2024 14:33:43 +0300 Subject: [PATCH 3/4] =?UTF-8?q?-=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BD=D0=B5=D0=B9=D0=BC=D0=B8=D0=BD=D0=B3=20=D1=84?= =?UTF-8?q?=D0=B5=D1=82=D1=87=D0=B5=D1=80=D0=B0=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=B1=D0=B8=D1=82=D1=80=D0=B8=D0=BA=D1=8124=D0=B0=D0=BA=D0=BA?= =?UTF-8?q?=D0=B0=D1=83=D0=BD=D1=82=D0=B0.=20-=20=D0=94=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20?= =?UTF-8?q?Command=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5?= =?UTF-8?q?=D1=81=D1=81=D0=B0=20=D0=B7=D0=B0=D0=B2=D0=B5=D1=80=D1=88=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=83=D1=81=D1=82=D0=B0=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B8.=20-=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=82=D0=B5=D0=BA=D1=81=D1=82=20=D1=81=20deb?= =?UTF-8?q?ug=20=D0=BD=D0=B0=20info=20=D0=B2=20=D1=8E=D0=B7=D0=BA=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=D0=B0=D1=85=20=D1=85=D0=B5=D0=BD=D0=B4=D0=BB=D0=B5?= =?UTF-8?q?=D1=80=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- ...Fetcher.php => Bitrix24AccountFetcher.php} | 2 +- .../UseCase/ChangeDomainUrl/Handler.php | 8 +- .../UseCase/InstallFinish/Command.php | 20 ++- .../UseCase/InstallFinish/Handler.php | 13 +- .../UseCase/InstallStart/Handler.php | 14 +- .../UseCase/RenewAuthToken/Handler.php | 9 +- .../UseCase/Uninstall/Handler.php | 15 +-- ...st.php => Bitrix24AccountsFetcherTest.php} | 40 +++++- .../UseCase/InstallFinish/CommandTest.php | 124 ++++++++++++++++++ 10 files changed, 216 insertions(+), 31 deletions(-) rename src/Bitrix24Accounts/ReadModel/{Fetcher.php => Bitrix24AccountFetcher.php} (97%) rename tests/Functional/Bitrix24Accounts/{FetcherTest.php => Bitrix24AccountsFetcherTest.php} (64%) create mode 100644 tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php diff --git a/Makefile b/Makefile index ad502d7..a21c7d6 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 'testChangeDomainUrlWithHappyPathForManyAccounts' tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php + docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testEmptyDomainUrl' tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php schema-drop: docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force diff --git a/src/Bitrix24Accounts/ReadModel/Fetcher.php b/src/Bitrix24Accounts/ReadModel/Bitrix24AccountFetcher.php similarity index 97% rename from src/Bitrix24Accounts/ReadModel/Fetcher.php rename to src/Bitrix24Accounts/ReadModel/Bitrix24AccountFetcher.php index ac7676c..b2a51c5 100644 --- a/src/Bitrix24Accounts/ReadModel/Fetcher.php +++ b/src/Bitrix24Accounts/ReadModel/Bitrix24AccountFetcher.php @@ -7,7 +7,7 @@ use Doctrine\ORM\EntityManagerInterface; use Knp\Component\Pager\Pagination\PaginationInterface; use Knp\Component\Pager\PaginatorInterface; -class Fetcher +class Bitrix24AccountFetcher { public function __construct( diff --git a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php index e370b63..53246d3 100644 --- a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php +++ b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php @@ -19,7 +19,7 @@ public function __construct( public function handle(Command $command): void { - $this->logger->debug('Bitrix24Accounts.ChangeDomainUrl.start', [ + $this->logger->info('Bitrix24Accounts.ChangeDomainUrl.start', [ 'b24_domain_url_old' => $command->oldDomainUrlHost, 'b24_domain_url_new' => $command->newDomainUrlHost, ]); @@ -33,6 +33,10 @@ public function handle(Command $command): void //используется как оператор распаковки (splat operator) для передачи массива как отдельных аргументов: $this->flusher->flush(...$accounts); - $this->logger->debug('Bitrix24Accounts.ChangeDomainUrl.Finish'); + $this->logger->info('Bitrix24Accounts.ChangeDomainUrl.Finish', + [ + 'b24_domain_url_old' => $command->oldDomainUrlHost, + 'b24_domain_url_new' => $command->newDomainUrlHost, + ]); } } diff --git a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php index b866b25..74ce9c4 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php @@ -4,6 +4,7 @@ namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallFinish; +use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; readonly class Command { public function __construct( @@ -11,5 +12,22 @@ public function __construct( public string $memberId, public string $domainUrl, public ?int $bitrix24UserId, - ) {} + ) { + $this->validate(); + } + + private function validate(): void + { + if (empty($this->applicationToken)) { + throw new InvalidArgumentException('Application token cannot be empty.'); + } + + if (empty($this->memberId)) { + throw new InvalidArgumentException('Member ID cannot be empty.'); + } + + if (empty($this->domainUrl)) { + throw new InvalidArgumentException('Domain URL cannot be empty.'); + } + } } diff --git a/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php b/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php index ab099c0..bd8d043 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php @@ -30,16 +30,13 @@ public function __construct( */ public function handle(Command $command): void { - $this->logger->debug('Bitrix24Accounts.InstallFinish.start', [ + $this->logger->info('Bitrix24Accounts.InstallFinish.start', [ 'b24_domain_url' => $command->domainUrl, 'b24_member_id' => $command->memberId, 'b24_application_id' => $command->applicationToken, 'b24_user_id' => $command->bitrix24UserId, ]); - /** - * @var AggregateRootEventsEmitterInterface|Bitrix24AccountInterface $bitrix24Account - */ $bitrix24Account = $this->getSingleAccountByMemberId($command->domainUrl, $command->memberId, Bitrix24AccountStatus::new, $command->bitrix24UserId); $bitrix24Account->applicationInstalled($command->applicationToken); @@ -48,7 +45,13 @@ public function handle(Command $command): void $this->flusher->flush($bitrix24Account); - $this->logger->debug('Bitrix24Accounts.InstallFinish.Finish'); + $this->logger->info('Bitrix24Accounts.InstallFinish.Finish', + [ + 'b24_domain_url' => $command->domainUrl, + 'b24_member_id' => $command->memberId, + 'b24_application_id' => $command->applicationToken, + 'b24_user_id' => $command->bitrix24UserId, + ]); } public function getSingleAccountByMemberId(string $domainUrl, string $memberId, Bitrix24AccountStatus $bitrix24AccountStatus, ?int $bitrix24UserId): Bitrix24AccountInterface diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php index a401b03..b9d1f17 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php @@ -22,7 +22,7 @@ public function __construct( public function handle(Command $command): void { - $this->logger->debug('Bitrix24Accounts.InstallStart.start', [ + $this->logger->info('Bitrix24Accounts.InstallStart.start', [ 'id' => $command->uuid->toRfc4122(), 'domain_url' => $command->domainUrl, 'member_id' => $command->memberId, @@ -44,12 +44,12 @@ public function handle(Command $command): void ); $this->bitrix24AccountRepository->save($bitrix24Account); $this->flusher->flush($bitrix24Account); - // $this->flusher->flush(); - /*foreach ($bitrix24Account->emitEvents() as $event) { - $this->eventDispatcher->dispatch($event); - }*/ - - $this->logger->debug('Bitrix24Accounts.InstallStart.Finish'); + $this->logger->info('Bitrix24Accounts.InstallStart.Finish', + [ + 'id' => $command->uuid->toRfc4122(), + 'domain_url' => $command->domainUrl, + 'member_id' => $command->memberId, + ]); } } diff --git a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php index 6551bc1..ef3b465 100644 --- a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php +++ b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php @@ -25,7 +25,7 @@ public function __construct( */ public function handle(Command $command): void { - $this->logger->debug('Bitrix24Accounts.RenewAuthToken.start', [ + $this->logger->info('Bitrix24Accounts.RenewAuthToken.start', [ 'domain_url' => $command->renewedAuthToken->domain, 'member_id' => $command->renewedAuthToken->memberId, 'bitrix24_user_id' => $command->bitrix24UserId, @@ -45,7 +45,12 @@ public function handle(Command $command): void $this->bitrix24AccountRepository->save($bitrix24Account); $this->flusher->flush($bitrix24Account); - $this->logger->debug('Bitrix24Accounts.RenewAuthToken.finish'); + $this->logger->info('Bitrix24Accounts.RenewAuthToken.finish', + [ + 'domain_url' => $command->renewedAuthToken->domain, + 'member_id' => $command->renewedAuthToken->memberId, + 'bitrix24_user_id' => $command->bitrix24UserId, + ]); } /** diff --git a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php index f30e983..8c5ea1f 100644 --- a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php +++ b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php @@ -22,27 +22,26 @@ public function __construct( ) {} /** - * @throws Bitrix24AccountNotFoundException * @throws InvalidArgumentException */ public function handle(Command $command): void { - $this->logger->debug('Bitrix24Accounts.Uninstall.start', [ + $this->logger->info('Bitrix24Accounts.Uninstall.start', [ 'b24_application_token' => $command->applicationToken, ]); - /** - * @var AggregateRootEventsEmitterInterface[]|Bitrix24AccountInterface[] $accounts - */ $accounts = $this->bitrix24AccountRepository->findByApplicationToken($command->applicationToken); - + $accountsCount = count($accounts); foreach ($accounts as $account) { $account->applicationUninstalled($command->applicationToken); $this->bitrix24AccountRepository->save($account); } $this->flusher->flush(...$accounts); - - $this->logger->debug('Bitrix24Accounts.Uninstall.Finish'); + $this->logger->info('Bitrix24Accounts.Uninstall.Finish', + [ + 'accountsCount' => $accountsCount, + 'b24_application_token' => $command->applicationToken, + ]); } } diff --git a/tests/Functional/Bitrix24Accounts/FetcherTest.php b/tests/Functional/Bitrix24Accounts/Bitrix24AccountsFetcherTest.php similarity index 64% rename from tests/Functional/Bitrix24Accounts/FetcherTest.php rename to tests/Functional/Bitrix24Accounts/Bitrix24AccountsFetcherTest.php index 46698c9..2b4ad05 100644 --- a/tests/Functional/Bitrix24Accounts/FetcherTest.php +++ b/tests/Functional/Bitrix24Accounts/Bitrix24AccountsFetcherTest.php @@ -5,7 +5,7 @@ namespace Bitrix24\Lib\Tests\Functional\Bitrix24Accounts; use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository; -use Bitrix24\Lib\Bitrix24Accounts\ReadModel\Fetcher; +use Bitrix24\Lib\Bitrix24Accounts\ReadModel\Bitrix24AccountFetcher; use Bitrix24\Lib\Services\Flusher; use Bitrix24\Lib\Tests\EntityManagerFactory; use Knp\Component\Pager\Event\Subscriber\Paginate\PaginationSubscriber; @@ -21,7 +21,7 @@ use Symfony\Component\HttpFoundation\RequestStack; use Knp\Component\Pager\ArgumentAccess\RequestArgumentAccess; -class FetcherTest extends TestCase +class Bitrix24AccountsFetcherTest extends TestCase { private EntityManagerInterface $entityManager; @@ -29,7 +29,7 @@ class FetcherTest extends TestCase private Bitrix24AccountRepositoryInterface $repository; - private Fetcher $fetcher; + private Bitrix24AccountFetcher $fetcher; private Flusher $flusher; @@ -42,7 +42,7 @@ protected function setUp(): void $eventDispatcher->addSubscriber(new SortableSubscriber()); $requestArgumentAccess = new RequestArgumentAccess(new RequestStack()); $this->paginator = new Paginator($eventDispatcher, $requestArgumentAccess); - $this->fetcher = new Fetcher($this->entityManager, $this->paginator); + $this->fetcher = new Bitrix24AccountFetcher($this->entityManager, $this->paginator); $this->flusher = new Flusher($this->entityManager,$eventDispatcher); $this->repository = new Bitrix24AccountRepository($this->entityManager); } @@ -67,4 +67,36 @@ public function testListReturnsPaginatedResults(): void $this->assertGreaterThan(0, $pagination->count()); // Проверяем, что есть хотя бы одна запись } + public function testColumnNamesInBitrix24Accounts(): void + { + // Ожидаемые названия столбцов + $expectedColumns = [ + 'id', + 'status', + 'b24_user_id', + 'is_b24_user_admin', + 'member_id', + 'domain_url', + 'application_token', + 'created_at_utc', + 'updated_at_utc', + 'application_version', + 'authtoken_access_token', + 'authtoken_refresh_token', + 'authtoken_expires', + 'authtoken_expires_in', + 'applicationscope_current_scope' + ]; + + // Получение фактических названий столбцов из базы данных + $connection = $this->entityManager->getConnection(); + $schemaManager = $connection->createSchemaManager(); + $columns = $schemaManager->listTableColumns('bitrix24account'); + $actualColumns = array_keys($columns); + + foreach ($expectedColumns as $column) { + $this->assertContains($column, $actualColumns, "Column '$column' is missing in table 'bitrix24account'."); + } + } + } \ No newline at end of file diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php new file mode 100644 index 0000000..1badc33 --- /dev/null +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php @@ -0,0 +1,124 @@ +withStatus(Bitrix24AccountStatus::new) + ->build(); + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + $applicationToken = Uuid::v7()->toRfc4122(); + $command = new Command( + $applicationToken, + $bitrix24Account->getMemberId(), + $bitrix24Account->getDomainUrl(), + $bitrix24Account->getBitrix24UserId() + ); + $this->assertInstanceOf(Command::class, $command); + } + + public function testEmptyApplicationToken(): void + { + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withStatus(Bitrix24AccountStatus::new) + ->build(); + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Application token cannot be empty.'); + + new Command('', + $bitrix24Account->getMemberId(), + $bitrix24Account->getDomainUrl(), + $bitrix24Account->getBitrix24UserId() + ); + } + + + public function testEmptyMemberId(): void + { + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withStatus(Bitrix24AccountStatus::new) + ->build(); + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Member ID cannot be empty.'); + + $applicationToken = Uuid::v7()->toRfc4122(); + new Command($applicationToken, + '', + $bitrix24Account->getDomainUrl(), + $bitrix24Account->getBitrix24UserId() + ); + } + + public function testEmptyDomainUrl(): void + { + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withStatus(Bitrix24AccountStatus::new) + ->build(); + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Domain URL cannot be empty.'); + + $applicationToken = Uuid::v7()->toRfc4122(); + new Command($applicationToken, + $bitrix24Account->getMemberId(), + '', + $bitrix24Account->getBitrix24UserId() + ); + } + + #[Override] + protected function setUp(): void + { + $entityManager = EntityManagerFactory::get(); + $eventDispatcher = new EventDispatcher(); + $this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch()); + $this->repository = new Bitrix24AccountRepository($entityManager); + $this->flusher = new Flusher($entityManager,$this->eventDispatcher); + } +} \ No newline at end of file From 9a6e0e8f3430b82fd3bf5cf274ef90abf6502af9 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sun, 15 Dec 2024 14:59:14 +0300 Subject: [PATCH 4/4] .. --- Makefile | 2 +- src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php | 4 ++-- src/Services/Flusher.php | 3 ++- .../Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php | 1 - .../Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index a21c7d6..cddd366 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 'testEmptyDomainUrl' tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php + docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testRenewAuthTokenWithoutBitrix24UserId' tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php schema-drop: docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force diff --git a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php index ef3b465..fdfb12d 100644 --- a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php +++ b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php @@ -38,11 +38,11 @@ public function handle(Command $command): void $command->bitrix24UserId ); - // Bitrix24Account extends AggregateRoot and implement AggregateRootEventsEmitterInterface - /** @var AggregateRootEventsEmitterInterface|Bitrix24AccountInterface $bitrix24Account */ + $bitrix24Account->renewAuthToken($command->renewedAuthToken); $this->bitrix24AccountRepository->save($bitrix24Account); + $this->flusher->flush($bitrix24Account); $this->logger->info('Bitrix24Accounts.RenewAuthToken.finish', diff --git a/src/Services/Flusher.php b/src/Services/Flusher.php index 585360b..9b8b8ab 100644 --- a/src/Services/Flusher.php +++ b/src/Services/Flusher.php @@ -6,6 +6,7 @@ use Bitrix24\Lib\AggregateRoot; +use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface; use Doctrine\ORM\EntityManagerInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; class Flusher @@ -17,7 +18,7 @@ public function __construct(EntityManagerInterface $em, EventDispatcherInterface $this->eventDispatcher = $eventDispatcher; } - public function flush(AggregateRoot ...$roots): void + public function flush(AggregateRootEventsEmitterInterface ...$roots): void { $this->em->flush(); foreach ($roots as $root) { diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php index 1badc33..9eb2f62 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php @@ -112,7 +112,6 @@ public function testEmptyDomainUrl(): void ); } - #[Override] protected function setUp(): void { $entityManager = EntityManagerFactory::get(); diff --git a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php index 39f806c..0f63150 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php @@ -50,7 +50,7 @@ protected function setUp(): void { $entityManager = EntityManagerFactory::get(); $eventDispatcher = new EventDispatcher(); - $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); + $this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch()); $this->repository = new Bitrix24AccountRepository($entityManager); $this->flusher = new Flusher($entityManager,$this->eventDispatcher); $this->handler = new Handler(