diff --git a/Makefile b/Makefile index c71ff1d..441329a 100644 --- a/Makefile +++ b/Makefile @@ -101,7 +101,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 'testRenewAuthTokenWithoutBitrix24UserId' tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php + docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testListReturnsPaginatedResults' tests/Functional/Bitrix24Accounts/FetcherTest.php schema-drop: docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force diff --git a/src/AggregateRoot.php b/src/AggregateRoot.php new file mode 100644 index 0000000..4fc766b --- /dev/null +++ b/src/AggregateRoot.php @@ -0,0 +1,19 @@ +events; + $this->events = []; + + return $events; + } + +} \ No newline at end of file diff --git a/src/Bitrix24Accounts/Entity/Bitrix24Account.php b/src/Bitrix24Accounts/Entity/Bitrix24Account.php index 580d719..6e31484 100644 --- a/src/Bitrix24Accounts/Entity/Bitrix24Account.php +++ b/src/Bitrix24Accounts/Entity/Bitrix24Account.php @@ -12,6 +12,7 @@ namespace Bitrix24\Lib\Bitrix24Accounts\Entity; +use Bitrix24\Lib\AggregateRoot; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Events\Bitrix24AccountApplicationInstalledEvent; @@ -30,9 +31,9 @@ use Symfony\Component\Uid\Uuid; use Symfony\Contracts\EventDispatcher\Event; -class Bitrix24Account implements Bitrix24AccountInterface, AggregateRootEventsEmitterInterface +class Bitrix24Account extends AggregateRoot implements Bitrix24AccountInterface { - private array $applicationScope; + private array|Scope $applicationScope; private ?string $applicationToken = null; @@ -40,11 +41,6 @@ class Bitrix24Account implements Bitrix24AccountInterface, AggregateRootEventsEm private AuthToken $authToken; - /** - * @var Event[] - */ - private array $events = []; - public function __construct( private Uuid $id, private readonly int $bitrix24UserId, @@ -61,7 +57,8 @@ public function __construct( bool $isEmitBitrix24AccountCreatedEvent = false ) { $this->authToken = $authToken; - $this->applicationScope = $applicationScope->getScopeCodes(); + $array = $applicationScope->getScopeCodes(); + $this->applicationScope = $array; $this->addAccountCreatedEventIfNeeded($isEmitBitrix24AccountCreatedEvent); } @@ -101,6 +98,11 @@ public function getStatus(): Bitrix24AccountStatus return $this->status; } + public function setStatus(Bitrix24AccountStatus $status): void + { + $this->status = $status; + } + /** * @throws InvalidArgumentException */ @@ -143,13 +145,11 @@ public function getApplicationVersion(): int return $this->applicationVersion; } - /** - * @throws UnknownScopeCodeException - */ #[\Override] public function getApplicationScope(): Scope { return new Scope($this->applicationScope); + // return $this->applicationScope; } /** @@ -346,19 +346,12 @@ public function getComment(): ?string return $this->comment; } - /** - * @return Event[] - */ - #[\Override] - public function emitEvents(): array + public function getEvents(): array { - $events = $this->events; - $this->events = []; - - return $events; + return $this->events; } - public function addAccountCreatedEventIfNeeded(bool $isEmitCreatedEvent): void + private function addAccountCreatedEventIfNeeded(bool $isEmitCreatedEvent): void { if ($isEmitCreatedEvent) { // Создание события и добавление его в массив событий diff --git a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php index 9cb26d9..c3322af 100644 --- a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php +++ b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php @@ -32,14 +32,15 @@ public function __construct( #[Override] public function getById(Uuid $uuid): Bitrix24AccountInterface { - $res = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid); - if (null === $res) { + $account = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid); + if (null === $account || $account->getStatus() === Bitrix24AccountStatus::deleted) { throw new Bitrix24AccountNotFoundException( sprintf('bitrix24 account not found by id %s', $uuid->toRfc4122()) ); } - return $res; + + return $account; } #[Override] @@ -100,8 +101,8 @@ public function delete(Uuid $uuid): void ) ); } - - $this->getEntityManager()->remove($bitrix24Account); + $bitrix24Account->setStatus(Bitrix24AccountStatus::deleted); + $this->save($bitrix24Account); } public function findAllActive(int|null $limit = null, int|null $offset = null): array diff --git a/src/Bitrix24Accounts/ReadModel/Fetcher.php b/src/Bitrix24Accounts/ReadModel/Fetcher.php index 91b1982..0430f40 100644 --- a/src/Bitrix24Accounts/ReadModel/Fetcher.php +++ b/src/Bitrix24Accounts/ReadModel/Fetcher.php @@ -6,9 +6,9 @@ use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository; use Doctrine\ORM\EntityManagerInterface; +use Knp\Component\Pager\Event\Subscriber\Paginate\Callback\CallbackPagination; use Knp\Component\Pager\Pagination\PaginationInterface; use Knp\Component\Pager\PaginatorInterface; - class Fetcher { public function __construct( @@ -23,7 +23,7 @@ public function list( ): PaginationInterface { - $queryBuilder = $this->em->getConnection()->createQueryBuilder() + /* $queryBuilder = $this->em->getConnection()->createQueryBuilder() ->select( 'b24account.id as id', 'b24account.status as status', @@ -35,7 +35,11 @@ public function list( ) ->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"); - return $this->paginator->paginate($queryBuilder, $page, $size); + return $this->paginator->paginate($query, $page, $size); + // return $this->paginator->paginate($queryBuilder->getSQL(), $page, $size); } } \ No newline at end of file diff --git a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php index 407a7d1..5c72473 100644 --- a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php +++ b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php @@ -4,6 +4,7 @@ namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\Uninstall; +use Bitrix24\Lib\AggregateRoot; use Bitrix24\Lib\Services\Flusher; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException; @@ -19,7 +20,7 @@ public function __construct( private EventDispatcherInterface $eventDispatcher, private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository, private Flusher $flusher, - private LoggerInterface $logger + private LoggerInterface $logger, ) {} /** @@ -41,6 +42,7 @@ public function handle(Command $command): void $account->applicationUninstalled($command->applicationToken); $this->bitrix24AccountRepository->save($account); $this->flusher->flush(); + foreach ($account->emitEvents() as $event) { $this->eventDispatcher->dispatch($event); } diff --git a/tests/Functional/Bitrix24Accounts/FetcherTest.php b/tests/Functional/Bitrix24Accounts/FetcherTest.php index c4e5dc4..fa1012c 100644 --- a/tests/Functional/Bitrix24Accounts/FetcherTest.php +++ b/tests/Functional/Bitrix24Accounts/FetcherTest.php @@ -8,6 +8,8 @@ use Bitrix24\Lib\Bitrix24Accounts\ReadModel\Fetcher; use Bitrix24\Lib\Services\Flusher; use Bitrix24\Lib\Tests\EntityManagerFactory; +use Knp\Component\Pager\Event\Subscriber\Paginate\PaginationSubscriber; +use Knp\Component\Pager\Event\Subscriber\Sortable\SortableSubscriber; use Knp\Component\Pager\Paginator; use PHPUnit\Framework\TestCase; use Doctrine\ORM\EntityManagerInterface; @@ -16,8 +18,9 @@ use Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\Builders\Bitrix24AccountBuilder; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; use Symfony\Component\EventDispatcher\EventDispatcher; -use Knp\Component\Pager\ArgumentAccess; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Request; +use Knp\Component\Pager\ArgumentAccess\RequestArgumentAccess; class FetcherTest extends TestCase { @@ -34,9 +37,13 @@ protected function setUp(): void { $this->entityManager = EntityManagerFactory::get(); $eventDispatcher = new EventDispatcher(); + $eventDispatcher->addSubscriber(new PaginationSubscriber()); + $eventDispatcher->addSubscriber(new SortableSubscriber()); + // dd(Request::createFromGlobals()); $requestStack = new RequestStack(); - $argumentAccess = new ArgumentAccess\RequestArgumentAccess($requestStack); - $this->paginator = new Paginator($eventDispatcher, $argumentAccess); + // Request::createFromGlobals() + $accessor = new RequestArgumentAccess(new RequestStack()); + $this->paginator = new Paginator($eventDispatcher, $accessor); $this->fetcher = new Fetcher($this->entityManager, $this->paginator); $this->flusher = new Flusher($this->entityManager); $this->repository = new Bitrix24AccountRepository($this->entityManager); @@ -55,14 +62,14 @@ public function testListReturnsPaginatedResults(): void $page = 1; $size = 10; // Вызов метода list - /** @var PaginationInterface $result */ $result = $this->fetcher->list($page, $size); - + // var_dump($result->getItems()); + // var_dump($result->count()); // Проверка, что результат является экземпляром PaginationInterface $this->assertInstanceOf(PaginationInterface::class, $result); // Проверка, что данные возвращаются корректно - $this->assertGreaterThan(0, count($result)); // Проверяем, что есть хотя бы одна запись + $this->assertGreaterThan(0, $result->count()); // Проверяем, что есть хотя бы одна запись } } \ No newline at end of file diff --git a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php index 8b6a752..1f2c6db 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php @@ -13,6 +13,7 @@ namespace Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\UseCase\Uninstall; +use Bitrix24\Lib\AggregateRoot; use Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account; use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository; use Bitrix24\Lib\Services\Flusher; @@ -20,6 +21,7 @@ use Bitrix24\Lib\Tests\EntityManagerFactory; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Events\Bitrix24AccountApplicationUninstalledEvent; +use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; use Bitrix24\SDK\Core\Credentials\AuthToken; use Bitrix24\SDK\Core\Credentials\Scope; @@ -65,10 +67,11 @@ public function testUninstallWithHappyPath(): void $this->repository->save($bitrix24Account); $this->flusher->flush(); - $this->handler->handle(new Bitrix24Accounts\UseCase\Uninstall\Command($applicationToken)); + $this->expectException(Bitrix24AccountNotFoundException::class); $updated = $this->repository->getById($bitrix24Account->getId()); + $this->assertEquals( Bitrix24AccountStatus::deleted, $updated->getStatus(), @@ -89,15 +92,17 @@ public function testUninstallWithHappyPath(): void #[Override] protected function setUp(): void { + $entityManager = EntityManagerFactory::get(); $this->repository = new Bitrix24AccountRepository($entityManager); $this->flusher = new Flusher($entityManager); $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); + $this->handler = new Bitrix24Accounts\UseCase\Uninstall\Handler( $this->eventDispatcher, $this->repository, $this->flusher, - new NullLogger() + new NullLogger(), ); } } \ No newline at end of file