diff --git a/Makefile b/Makefile index 0bbd5b1..d61aebf 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,10 @@ include $(ENV) -include $(ENV_LOCAL) + +coding-standards: vendor + vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --diff --verbose + default: @echo "make needs target:" @egrep -e '^\S+' ./Makefile | grep -v default | sed -r 's/://' | sed -r 's/^/ - /' @@ -106,7 +110,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 'testUninstallWithHappyPath' tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php + docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testCreateExistingAccount' tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php schema-drop: docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force diff --git a/src/Bitrix24Accounts/Entity/Bitrix24Account.php b/src/Bitrix24Accounts/Entity/Bitrix24Account.php index 8c84f92..597c68d 100644 --- a/src/Bitrix24Accounts/Entity/Bitrix24Account.php +++ b/src/Bitrix24Accounts/Entity/Bitrix24Account.php @@ -36,21 +36,20 @@ class Bitrix24Account extends AggregateRoot implements Bitrix24AccountInterface private ?string $comment = null; public function __construct( - private readonly Uuid $id, - private readonly int $bitrix24UserId, - private readonly bool $isBitrix24UserAdmin, + private readonly Uuid $id, + private readonly int $bitrix24UserId, + private readonly bool $isBitrix24UserAdmin, /** bitrix24 portal unique id */ - private readonly string $memberId, - private string $domainUrl, - private Bitrix24AccountStatus $status, - private AuthToken $authToken, + private readonly string $memberId, + private string $domainUrl, + private Bitrix24AccountStatus $status, + private AuthToken $authToken, private readonly CarbonImmutable $createdAt, - private CarbonImmutable $updatedAt, - private int $applicationVersion, - private Scope $applicationScope, - bool $isEmitBitrix24AccountCreatedEvent = false, - ) - { + private CarbonImmutable $updatedAt, + private int $applicationVersion, + private Scope $applicationScope, + bool $isEmitBitrix24AccountCreatedEvent = false, + ) { $this->addAccountCreatedEventIfNeeded($isEmitBitrix24AccountCreatedEvent); } @@ -195,50 +194,15 @@ public function applicationInstalled(string $applicationToken): void ); } - private function guardEmpty($applicationToken) - { - if ('' === $applicationToken) { - throw new InvalidArgumentException('application token cannot be empty'); - } - } - - private function guardTokenMismatch($applicationToken) - { - if ($this->applicationToken !== $applicationToken) { - throw new InvalidArgumentException( - sprintf( - 'application token «%s» mismatch with application token «%s» for bitrix24 account %s for domain %s', - $applicationToken, - $this->applicationToken, - $this->id->toRfc4122(), - $this->domainUrl - ) - ); - } - } - - private function guardStatusIsActive() - { - if (Bitrix24AccountStatus::active !== $this->status) { - throw new InvalidArgumentException( - sprintf( - 'for uninstall account must be in status «active», current status - «%s»', - $this->status->name - ) - ); - } - } - /** * @throws InvalidArgumentException */ #[\Override] public function applicationUninstalled(string $applicationToken): void { - - $this->guardEmpty($applicationToken); + $this->guardEmptyToken($applicationToken); $this->guardTokenMismatch($applicationToken); - $this->guardStatusIsActive(); + $this->guardApplicationIsActive(); $this->status = Bitrix24AccountStatus::deleted; $this->updatedAt = new CarbonImmutable(); $this->events[] = new Bitrix24AccountApplicationUninstalledEvent( @@ -345,6 +309,40 @@ public function getComment(): ?string return $this->comment; } + private function guardEmptyToken($applicationToken) + { + if ('' === $applicationToken) { + throw new InvalidArgumentException('application token cannot be empty'); + } + } + + private function guardTokenMismatch($applicationToken): void + { + if ($this->applicationToken !== $applicationToken) { + throw new InvalidArgumentException( + sprintf( + 'application token «%s» mismatch with application token «%s» for bitrix24 account %s for domain %s', + $applicationToken, + $this->applicationToken, + $this->id->toRfc4122(), + $this->domainUrl + ) + ); + } + } + + private function guardApplicationIsActive(): void + { + if (Bitrix24AccountStatus::active !== $this->status) { + throw new InvalidArgumentException( + sprintf( + 'for uninstall account must be in status «active», current status - «%s»', + $this->status->name + ) + ); + } + } + 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 2621722..804b299 100644 --- a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php +++ b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php @@ -11,7 +11,6 @@ use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface; use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; -use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Symfony\Component\Uid\Uuid; @@ -20,8 +19,7 @@ class Bitrix24AccountRepository extends EntityRepository implements Bitrix24Acco { public function __construct( EntityManagerInterface $entityManager - ) - { + ) { parent::__construct($entityManager, $entityManager->getClassMetadata(Bitrix24Account::class)); } @@ -33,7 +31,6 @@ public function __construct( #[\Override] public function getById(Uuid $uuid): Bitrix24AccountInterface { - $account = $this->getEntityManager()->getRepository(Bitrix24Account::class) ->createQueryBuilder('b24') ->where('b24.id = :id') @@ -41,8 +38,8 @@ public function getById(Uuid $uuid): Bitrix24AccountInterface ->setParameter('id', $uuid) ->setParameter('status', Bitrix24AccountStatus::deleted) ->getQuery() - ->getOneOrNullResult(); - // $account = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid); + ->getOneOrNullResult(); + if (null === $account) { throw new Bitrix24AccountNotFoundException( sprintf('bitrix24 account not found by id %s', $uuid->toRfc4122()) @@ -83,10 +80,10 @@ public function save(Bitrix24AccountInterface $bitrix24Account): void */ #[\Override] public function findByMemberId( - string $memberId, + string $memberId, ?Bitrix24AccountStatus $bitrix24AccountStatus = null, - ?int $bitrix24UserId = null, - ?bool $isAdmin = null + ?int $bitrix24UserId = null, + ?bool $isAdmin = null ): array { if ('' === trim($memberId)) { @@ -198,9 +195,9 @@ public function findOneAdminByMemberId(string $memberId): ?Bitrix24AccountInterf */ #[\Override] public function findByDomain( - string $domainUrl, + string $domainUrl, ?Bitrix24AccountStatus $bitrix24AccountStatus = null, - ?bool $isAdmin = null + ?bool $isAdmin = null ): array { if ('' === trim($domainUrl)) { diff --git a/src/Bitrix24Accounts/ReadModel/Bitrix24AccountFetcher.php b/src/Bitrix24Accounts/ReadModel/Bitrix24AccountFetcher.php index b2a51c5..73b4900 100644 --- a/src/Bitrix24Accounts/ReadModel/Bitrix24AccountFetcher.php +++ b/src/Bitrix24Accounts/ReadModel/Bitrix24AccountFetcher.php @@ -7,9 +7,9 @@ use Doctrine\ORM\EntityManagerInterface; use Knp\Component\Pager\Pagination\PaginationInterface; use Knp\Component\Pager\PaginatorInterface; + class Bitrix24AccountFetcher { - public function __construct( private readonly EntityManagerInterface $em, private readonly PaginatorInterface $paginator @@ -19,19 +19,19 @@ public function list( int $page, int $size ): PaginationInterface { - $qb = $this->em->createQueryBuilder() - ->select( - 'b24account.id as id', - 'b24account.status as status', - '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('Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account', 'b24account') - ->orderBy('b24account.createdAt', 'DESC'); + $qb = $this->em->createQueryBuilder() + ->select( + 'b24account.id as id', + 'b24account.status as status', + '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('Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account', 'b24account') + ->orderBy('b24account.createdAt', 'DESC'); - return $this->paginator->paginate($qb, $page, $size); + return $this->paginator->paginate($qb, $page, $size); } } diff --git a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php index 6d9308a..e488548 100644 --- a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php +++ b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php @@ -4,7 +4,6 @@ namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\ChangeDomainUrl; -use InvalidArgumentException; readonly class Command { public function __construct( @@ -16,9 +15,7 @@ public function __construct( * @var non-empty-string $newDomainUrlHost */ public string $newDomainUrlHost - ) - { - + ) { $this->validateDomain($oldDomainUrlHost, 'oldDomainUrlHost'); $this->validateDomain($newDomainUrlHost, 'newDomainUrlHost'); } @@ -26,7 +23,7 @@ public function __construct( private function validateDomain(string $domain, string $parameterName): void { if (empty($domain) || !filter_var($domain, FILTER_VALIDATE_URL)) { - throw new InvalidArgumentException(sprintf('Invalid value for %s: %s', $parameterName, $domain)); + throw new \InvalidArgumentException(sprintf('Invalid value for %s: %s', $parameterName, $domain)); } } } diff --git a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php index c1a0b12..a5a8137 100644 --- a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php +++ b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php @@ -9,7 +9,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 { @@ -26,20 +25,22 @@ public function handle(Command $command): void 'b24_domain_url_new' => $command->newDomainUrlHost, ]); - /** @var Bitrix24AccountInterface[]|AggregateRootEventsEmitterInterface[] $accounts */ + /** @var AggregateRootEventsEmitterInterface[]|Bitrix24AccountInterface[] $accounts */ $accounts = $this->bitrix24AccountRepository->findByDomain($command->oldDomainUrlHost); foreach ($accounts as $account) { $account->changeDomainUrl($command->newDomainUrlHost); $this->bitrix24AccountRepository->save($account); } - //используется как оператор распаковки (splat operator) для передачи массива как отдельных аргументов: + // используется как оператор распаковки (splat operator) для передачи массива как отдельных аргументов: $this->flusher->flush(...$accounts); - $this->logger->info('Bitrix24Accounts.ChangeDomainUrl.Finish', - [ - 'b24_domain_url_old' => $command->oldDomainUrlHost, - 'b24_domain_url_new' => $command->newDomainUrlHost, - ]); + $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 f40ce9e..946532e 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php @@ -3,7 +3,6 @@ declare(strict_types=1); namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallFinish; -use InvalidArgumentException; readonly class Command { @@ -19,14 +18,13 @@ public function __construct( private function validate(): void { if (empty($this->applicationToken)) { - throw new InvalidArgumentException('Application token cannot be empty.'); + throw new \InvalidArgumentException('Application token cannot be empty.'); } - if (empty($this->memberId)) { - throw new InvalidArgumentException('Member ID cannot be empty.'); + throw new \InvalidArgumentException('Member ID cannot be empty.'); } if (!filter_var($this->domainUrl, FILTER_VALIDATE_URL)) { - throw new InvalidArgumentException('Domain URL is not valid.'); + throw new \InvalidArgumentException('Domain URL is not valid.'); } } } diff --git a/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php b/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php index 037188d..f78eab9 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php @@ -13,7 +13,6 @@ use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface; use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; use Psr\Log\LoggerInterface; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; readonly class Handler { @@ -37,7 +36,7 @@ public function handle(Command $command): void 'b24_user_id' => $command->bitrix24UserId, ]); - /** @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $bitrix24Account */ + /** @var AggregateRootEventsEmitterInterface|Bitrix24AccountInterface $bitrix24Account */ $bitrix24Account = $this->getSingleAccountByMemberId($command->domainUrl, $command->memberId, $command->bitrix24UserId); $bitrix24Account->applicationInstalled($command->applicationToken); @@ -45,14 +44,15 @@ public function handle(Command $command): void $this->bitrix24AccountRepository->save($bitrix24Account); $this->flusher->flush($bitrix24Account); - - $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, - ]); + $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, + ] + ); } private function getSingleAccountByMemberId(string $domainUrl, string $memberId, ?int $bitrix24UserId): Bitrix24AccountInterface diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php index ff97cfd..89ac909 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php @@ -7,44 +7,42 @@ use Bitrix24\SDK\Core\Credentials\AuthToken; use Bitrix24\SDK\Core\Credentials\Scope; use Symfony\Component\Uid\Uuid; -use InvalidArgumentException; readonly class Command { public function __construct( - public Uuid $uuid, - public int $bitrix24UserId, - public bool $isBitrix24UserAdmin, - public string $memberId, - public string $domainUrl, + public Uuid $uuid, + public int $bitrix24UserId, + public bool $isBitrix24UserAdmin, + public string $memberId, + public string $domainUrl, public AuthToken $authToken, - public int $applicationVersion, - public Scope $applicationScope - ) - { + public int $applicationVersion, + public Scope $applicationScope + ) { $this->validate(); } private function validate(): void { - if (empty($this->uuid) || !Uuid::isValid($this->uuid->toString())) { - throw new InvalidArgumentException('Empty uuid or invalid UUID provided.'); + if (empty($this->uuid)) { + throw new \InvalidArgumentException('Empty UUID provided.'); } if ($this->bitrix24UserId <= 0) { - throw new InvalidArgumentException('Bitrix24 User ID must be a positive integer.'); + throw new \InvalidArgumentException('Bitrix24 User ID must be a positive integer.'); } if (!is_string($this->memberId) || empty($this->memberId)) { - throw new InvalidArgumentException('Member ID must be a non-empty string.'); + throw new \InvalidArgumentException('Member ID must be a non-empty string.'); } if (!filter_var($this->domainUrl, FILTER_VALIDATE_URL)) { - throw new InvalidArgumentException('Domain URL is not valid.'); + throw new \InvalidArgumentException('Domain URL is not valid.'); } if ($this->applicationVersion <= 0) { - throw new InvalidArgumentException('Application version must be a positive integer.'); + throw new \InvalidArgumentException('Application version must be a positive integer.'); } } } diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php index 981149b..3f8f1d6 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php @@ -11,17 +11,14 @@ use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; use Carbon\CarbonImmutable; use Psr\Log\LoggerInterface; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; readonly class Handler { public function __construct( private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository, - private Flusher $flusher, - private LoggerInterface $logger - ) - { - } + private Flusher $flusher, + private LoggerInterface $logger + ) {} public function handle(Command $command): void { @@ -52,14 +49,17 @@ public function handle(Command $command): void $this->bitrix24AccountRepository->save($bitrix24Account); $this->flusher->flush($bitrix24Account); - $this->logger->info('Bitrix24Accounts.InstallStart.Finish', + $this->logger->info( + 'Bitrix24Accounts.InstallStart.Finish', [ 'id' => $command->uuid->toRfc4122(), 'domain_url' => $command->domainUrl, 'member_id' => $command->memberId, - ]); - }else{ - $this->logger->info('Bitrix24Accounts.InstallStart.AlreadyExists', + ] + ); + } else { + $this->logger->info( + 'Bitrix24Accounts.InstallStart.AlreadyExists', [ 'id' => $command->uuid->toRfc4122(), 'domain_url' => $command->domainUrl, diff --git a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php index 63d2b6a..e33957d 100644 --- a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php +++ b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php @@ -5,11 +5,11 @@ namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\RenewAuthToken; use Bitrix24\SDK\Core\Response\DTO\RenewedAuthToken; + readonly class Command { public function __construct( public RenewedAuthToken $renewedAuthToken, - public ?int $bitrix24UserId = null, - ) { - } + public int $bitrix24UserId, + ) {} } diff --git a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php index 1afff9f..5670338 100644 --- a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php +++ b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php @@ -16,11 +16,9 @@ { public function __construct( private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository, - private Flusher $flusher, - private LoggerInterface $logger - ) - { - } + private Flusher $flusher, + private LoggerInterface $logger + ) {} /** * @throws MultipleBitrix24AccountsFoundException @@ -33,26 +31,27 @@ public function handle(Command $command): void 'bitrix24_user_id' => $command->bitrix24UserId, ]); - /** @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $bitrix24Account */ + /** @var AggregateRootEventsEmitterInterface|Bitrix24AccountInterface $bitrix24Account */ $bitrix24Account = $this->getSingleAccountByMemberId( $command->renewedAuthToken->domain, $command->renewedAuthToken->memberId, $command->bitrix24UserId ); - $bitrix24Account->renewAuthToken($command->renewedAuthToken); $this->bitrix24AccountRepository->save($bitrix24Account); $this->flusher->flush($bitrix24Account); - $this->logger->info('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/Command.php b/src/Bitrix24Accounts/UseCase/Uninstall/Command.php index 7134c12..71138f5 100644 --- a/src/Bitrix24Accounts/UseCase/Uninstall/Command.php +++ b/src/Bitrix24Accounts/UseCase/Uninstall/Command.php @@ -4,9 +4,6 @@ namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\Uninstall; -use InvalidArgumentException; -use Symfony\Component\Uid\Uuid; - readonly class Command { public function __construct( @@ -20,8 +17,8 @@ public function __construct( private function validate(): void { - if (empty($this->applicationToken) || !Uuid::isValid($this->applicationToken)) { - throw new InvalidArgumentException('Empty application token or invalid application token.'); + if (empty($this->applicationToken)) { + throw new \InvalidArgumentException('Empty application token application token.'); } } } diff --git a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php index 4a64d89..f72a088 100644 --- a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php +++ b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php @@ -6,12 +6,10 @@ use Bitrix24\Lib\Services\Flusher; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface; -use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface; use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; use Psr\Log\LoggerInterface; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; readonly class Handler { @@ -30,7 +28,7 @@ public function handle(Command $command): void 'b24_application_token' => $command->applicationToken, ]); - /** @var Bitrix24AccountInterface[]|AggregateRootEventsEmitterInterface[] $accounts */ + /** @var AggregateRootEventsEmitterInterface[]|Bitrix24AccountInterface[] $accounts */ $accounts = $this->bitrix24AccountRepository->findByApplicationToken($command->applicationToken); $accountsCount = count($accounts); foreach ($accounts as $account) { @@ -39,10 +37,12 @@ public function handle(Command $command): void } $this->flusher->flush(...$accounts); - $this->logger->info('Bitrix24Accounts.Uninstall.Finish', - [ - 'accountsCount' => $accountsCount, - 'b24_application_token' => $command->applicationToken, - ]); + $this->logger->info( + 'Bitrix24Accounts.Uninstall.Finish', + [ + 'accountsCount' => $accountsCount, + 'b24_application_token' => $command->applicationToken, + ] + ); } } diff --git a/src/Services/Flusher.php b/src/Services/Flusher.php index 9b8b8ab..bd33cdd 100644 --- a/src/Services/Flusher.php +++ b/src/Services/Flusher.php @@ -4,16 +4,17 @@ namespace Bitrix24\Lib\Services; - -use Bitrix24\Lib\AggregateRoot; use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface; use Doctrine\ORM\EntityManagerInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; + 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; } diff --git a/tests/EntityManagerFactory.php b/tests/EntityManagerFactory.php index 3cf197c..e3935cf 100644 --- a/tests/EntityManagerFactory.php +++ b/tests/EntityManagerFactory.php @@ -6,19 +6,16 @@ use Bitrix24\SDK\Core\Exceptions\WrongConfigurationException; use Carbon\Doctrine\CarbonImmutableType; - +use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Types\Type; +use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Exception\ORMException; use Doctrine\ORM\OptimisticLockException; -use Monolog\Logger; -use Symfony\Bridge\Doctrine\Types\UuidType; -use Doctrine\DBAL\DriverManager; -use Doctrine\ORM\EntityManager; use Doctrine\ORM\ORMSetup; -use Doctrine\DBAL\Types\Type; -use Monolog\Handler\StreamHandler; -use Monolog\Level; +use Symfony\Bridge\Doctrine\Types\UuidType; + class EntityManagerFactory { /** @@ -27,14 +24,13 @@ class EntityManagerFactory * @throws ORMException * @throws Exception */ - private static ?EntityManager $entityManager = null; public static function get(): EntityManagerInterface { - if (!self::$entityManager instanceof \Doctrine\ORM\EntityManager) { + if (!self::$entityManager instanceof EntityManager) { $paths = [ - dirname(__DIR__) . '/config/xml' + dirname(__DIR__).'/config/xml', ]; $isDevMode = true; @@ -78,4 +74,4 @@ public static function get(): EntityManagerInterface return self::$entityManager; } -} \ No newline at end of file +} diff --git a/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php b/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php index 813c644..89423e8 100644 --- a/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php +++ b/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php @@ -55,7 +55,7 @@ public function __construct() $this->bitrix24UserId = random_int(1, 1_000_000); $this->isBitrix24UserAdmin = true; $this->memberId = Uuid::v4()->toRfc4122(); - $this->domainUrl = 'https://'.Uuid::v7()->toRfc4122() . '-test.bitrix24.com'; + $this->domainUrl = 'https://'.Uuid::v7()->toRfc4122().'-test.bitrix24.com'; $this->authToken = new AuthToken('old_1', 'old_2', 3600); $this->createdAt = CarbonImmutable::now(); $this->updatedAt = CarbonImmutable::now(); @@ -66,30 +66,35 @@ public function __construct() public function withMemberId(string $memberId): self { $this->memberId = $memberId; + return $this; } public function withDomainUrl(string $domainUrl): self { $this->domainUrl = $domainUrl; + return $this; } public function withApplicationScope(Scope $applicationScope): self { $this->applicationScope = $applicationScope; + return $this; } public function withApplicationToken(string $applicationToken): self { $this->applicationToken = $applicationToken; + return $this; } public function withStatus(Bitrix24AccountStatus $bitrix24AccountStatus): self { $this->status = $bitrix24AccountStatus; + return $this; } @@ -109,7 +114,7 @@ public function build(): AggregateRootEventsEmitterInterface&Bitrix24AccountInte $this->applicationScope ); - if (isset($this->applicationToken) && $this->status == Bitrix24AccountStatus::new) { + if (isset($this->applicationToken) && Bitrix24AccountStatus::new == $this->status) { $account->applicationInstalled($this->applicationToken); } diff --git a/tests/Functional/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepositoryTest.php b/tests/Functional/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepositoryTest.php index 177ce15..4736e55 100644 --- a/tests/Functional/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepositoryTest.php +++ b/tests/Functional/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepositoryTest.php @@ -26,29 +26,30 @@ use Bitrix24\SDK\Tests\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterfaceTest; use Bitrix24\SDK\Tests\Application\Contracts\TestRepositoryFlusherInterface; use Carbon\CarbonImmutable; -use Override; use PHPUnit\Framework\Attributes\CoversClass; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Uid\Uuid; +/** + * @internal + */ #[CoversClass(Bitrix24AccountRepository::class)] class Bitrix24AccountRepositoryTest extends Bitrix24AccountRepositoryInterfaceTest { - #[Override] + #[\Override] protected function createBitrix24AccountImplementation( - Uuid $uuid, - int $bitrix24UserId, - bool $isBitrix24UserAdmin, - string $memberId, - string $domainUrl, + Uuid $uuid, + int $bitrix24UserId, + bool $isBitrix24UserAdmin, + string $memberId, + string $domainUrl, Bitrix24AccountStatus $bitrix24AccountStatus, - AuthToken $authToken, - CarbonImmutable $createdAt, - CarbonImmutable $updatedAt, - int $applicationVersion, - Scope $applicationScope - ): Bitrix24AccountInterface - { + AuthToken $authToken, + CarbonImmutable $createdAt, + CarbonImmutable $updatedAt, + int $applicationVersion, + Scope $applicationScope + ): Bitrix24AccountInterface { return new Bitrix24Account( $uuid, $bitrix24UserId, @@ -64,18 +65,20 @@ protected function createBitrix24AccountImplementation( ); } - #[Override] + #[\Override] protected function createBitrix24AccountRepositoryImplementation(): Bitrix24AccountRepositoryInterface { $entityManager = EntityManagerFactory::get(); + return new Bitrix24AccountRepository($entityManager); } - #[Override] + #[\Override] protected function createRepositoryFlusherImplementation(): TestRepositoryFlusherInterface { $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/ChangeDomainUrl/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php index 784aad2..c21d48f 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php @@ -26,9 +26,10 @@ 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,7 +51,7 @@ protected function setUp(): void $eventDispatcher = new EventDispatcher(); $this->repository = new Bitrix24AccountRepository($entityManager); $this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch()); - $this->flusher = new Flusher($entityManager,$this->eventDispatcher); + $this->flusher = new Flusher($entityManager, $this->eventDispatcher); $this->handler = new Bitrix24Accounts\UseCase\ChangeDomainUrl\Handler( $this->repository, $this->flusher, @@ -62,8 +63,8 @@ protected function setUp(): void #[TestDox('Test change domain url with happy path - one account')] public function testChangeDomainUrlWithHappyPath(): void { - $oldDomainUrl = Uuid::v7()->toRfc4122() . '-test.bitrix24.com'; - $newDomainUrl = 'new-' . $oldDomainUrl; + $oldDomainUrl = Uuid::v7()->toRfc4122().'-test.bitrix24.com'; + $newDomainUrl = 'new-'.$oldDomainUrl; $bitrix24Account = (new Bitrix24AccountBuilder()) ->withDomainUrl($oldDomainUrl) @@ -89,10 +90,11 @@ public function testChangeDomainUrlWithHappyPath(): void ) ); - $this->assertTrue(in_array( - Bitrix24AccountDomainUrlChangedEvent::class, - $this->eventDispatcher->getOrphanedEvents(), - ), + $this->assertTrue( + in_array( + Bitrix24AccountDomainUrlChangedEvent::class, + $this->eventDispatcher->getOrphanedEvents(), + ), sprintf( 'Event %s was expected to be in the list of orphan events, but it is missing', Bitrix24AccountDomainUrlChangedEvent::class @@ -104,8 +106,8 @@ public function testChangeDomainUrlWithHappyPath(): void #[TestDox('Test change domain url with happy path - many accounts')] public function testChangeDomainUrlWithHappyPathForManyAccounts(): void { - $oldDomainUrl = Uuid::v7()->toRfc4122() . '-test.bitrix24.com'; - $newDomainUrl = 'new-' . $oldDomainUrl; + $oldDomainUrl = Uuid::v7()->toRfc4122().'-test.bitrix24.com'; + $newDomainUrl = 'new-'.$oldDomainUrl; $b24MemberId = Uuid::v7()->toRfc4122(); $bitrix24AccountA = (new Bitrix24AccountBuilder()) @@ -143,10 +145,11 @@ public function testChangeDomainUrlWithHappyPathForManyAccounts(): void ); } - $this->assertTrue(in_array( - Bitrix24AccountDomainUrlChangedEvent::class, - $this->eventDispatcher->getOrphanedEvents() - ), + $this->assertTrue( + in_array( + Bitrix24AccountDomainUrlChangedEvent::class, + $this->eventDispatcher->getOrphanedEvents() + ), sprintf( 'Event %s was expected to be in the list of orphan events, but it is missing', Bitrix24AccountDomainUrlChangedEvent::class diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php index eccc111..0b1d67b 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php @@ -13,15 +13,14 @@ namespace Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\UseCase\InstallFinish; +use Bitrix24\Lib\Bitrix24Accounts; use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository; use Bitrix24\Lib\Services\Flusher; -use Bitrix24\Lib\Bitrix24Accounts; use Bitrix24\Lib\Tests\EntityManagerFactory; use Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\Builders\Bitrix24AccountBuilder; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Events\Bitrix24AccountApplicationInstalledEvent; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; -use Override; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\TestDox; @@ -32,6 +31,9 @@ use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Uid\Uuid; +/** + * @internal + */ #[CoversClass(Bitrix24Accounts\UseCase\InstallFinish\Handler::class)] class HandlerTest extends TestCase { @@ -43,6 +45,22 @@ class HandlerTest extends TestCase private TraceableEventDispatcher $eventDispatcher; + #[\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); + + $this->handler = new Bitrix24Accounts\UseCase\InstallFinish\Handler( + $this->repository, + $this->flusher, + new NullLogger() + ); + } + #[Test] #[TestDox('test finish installation with happy path')] public function testFinishInstallationWithHappyPath(): void @@ -65,35 +83,22 @@ public function testFinishInstallationWithHappyPath(): void ); $updated = $this->repository->getById($bitrix24Account->getId()); - $this->assertEquals(Bitrix24AccountStatus::active, $updated->getStatus(),'expected status is active'); + $this->assertEquals(Bitrix24AccountStatus::active, $updated->getStatus(), 'expected status is active'); $this->assertTrue( $updated->isApplicationTokenValid($applicationToken), - sprintf('failed application token «%s» validation for bitrix24 account with id «%s»', + sprintf( + 'failed application token «%s» validation for bitrix24 account with id «%s»', $applicationToken, $bitrix24Account->getId()->toString() - )); + ) + ); $this->assertTrue( in_array(Bitrix24AccountApplicationInstalledEvent::class, $this->eventDispatcher->getOrphanedEvents(), true), - sprintf('emited event «%s» for bitrix24 account wiht id «%s» not found', + sprintf( + 'emited event «%s» for bitrix24 account wiht id «%s» not found', Bitrix24AccountApplicationInstalledEvent::class, $bitrix24Account->getId()->toString() ) ); } - - #[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); - - $this->handler = new Bitrix24Accounts\UseCase\InstallFinish\Handler( - $this->repository, - $this->flusher, - new NullLogger() - ); - } -} \ No newline at end of file +} diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php index 877cf5e..fc8572e 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php @@ -22,7 +22,6 @@ use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Events\Bitrix24AccountCreatedEvent; 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; use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; use Bitrix24\SDK\Core\Exceptions\UnknownScopeCodeException; @@ -34,8 +33,7 @@ use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Stopwatch\Stopwatch; -use Symfony\Component\Uid\Uuid; -use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface; + /** * @internal */ @@ -56,7 +54,7 @@ protected function setUp(): void $entityManager = EntityManagerFactory::get(); $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager,$this->eventDispatcher); + $this->flusher = new Flusher($entityManager, $this->eventDispatcher); $this->handler = new Bitrix24Accounts\UseCase\InstallStart\Handler( $this->repository, $this->flusher, @@ -153,7 +151,7 @@ public function testInstallStartHappyPath(): void 'Object not equals' ); - $this->assertEquals(Bitrix24AccountStatus::new,$bitrix24Account->getStatus()); + $this->assertEquals(Bitrix24AccountStatus::new, $bitrix24Account->getStatus()); $this->assertContains( Bitrix24AccountCreatedEvent::class, @@ -177,7 +175,6 @@ public function testCreateExistingAccount(): void ->withApplicationScope(new Scope(['crm'])) ->build(); - $this->handler->handle( new Bitrix24Accounts\UseCase\InstallStart\Command( $bitrix24Account->getId(), @@ -191,7 +188,6 @@ public function testCreateExistingAccount(): void ) ); - $this->expectException(Bitrix24AccountNotFoundException::class); $this->expectExceptionMessage( sprintf('bitrix24account with uuid "%s" already exists', $bitrix24Account->getId()) @@ -210,4 +206,7 @@ public function testCreateExistingAccount(): void ) ); } + + #[Test] + public function testUpdateAppVersion(): void {} } diff --git a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php index 621d3fa..257efde 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\Stopwatch\Stopwatch; use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\Stopwatch\Stopwatch; /** * @internal @@ -52,7 +52,7 @@ protected function setUp(): void $eventDispatcher = new EventDispatcher(); $this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch()); $this->repository = new Bitrix24AccountRepository($entityManager); - $this->flusher = new Flusher($entityManager,$this->eventDispatcher); + $this->flusher = new Flusher($entityManager, $this->eventDispatcher); $this->handler = new Handler( $this->repository, $this->flusher, @@ -78,7 +78,8 @@ public function testRenewAuthTokenWithoutBitrix24UserId(): void 'https://server-endpoint.com', ApplicationStatus::subscription(), $bitrix24Account->getDomainUrl() - ) + ), + $bitrix24Account->getBitrix24UserId() ) ); $updated = $this->repository->getById($bitrix24Account->getId()); diff --git a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php index 25a5118..556af50 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php @@ -13,9 +13,9 @@ namespace Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\UseCase\Uninstall; +use Bitrix24\Lib\Bitrix24Accounts; use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository; use Bitrix24\Lib\Services\Flusher; -use Bitrix24\Lib\Bitrix24Accounts; use Bitrix24\Lib\Tests\EntityManagerFactory; use Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\Builders\Bitrix24AccountBuilder; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus; @@ -23,7 +23,6 @@ use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; -use Override; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; @@ -33,6 +32,9 @@ use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Uid\Uuid; +/** + * @internal + */ #[CoversClass(Bitrix24Accounts\UseCase\Uninstall\Handler::class)] class HandlerTest extends TestCase { @@ -44,6 +46,22 @@ class HandlerTest extends TestCase private TraceableEventDispatcher $eventDispatcher; + #[\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); + + $this->handler = new Bitrix24Accounts\UseCase\Uninstall\Handler( + $this->repository, + $this->flusher, + new NullLogger(), + ); + } + /** * @throws InvalidArgumentException * @throws Bitrix24AccountNotFoundException @@ -51,7 +69,6 @@ class HandlerTest extends TestCase #[Test] public function testUninstallWithHappyPath(): void { - $applicationToken = Uuid::v7()->toRfc4122(); $bitrix24Account = (new Bitrix24AccountBuilder()) @@ -67,31 +84,15 @@ public function testUninstallWithHappyPath(): void $this->expectException(Bitrix24AccountNotFoundException::class); $updated = $this->repository->getById($bitrix24Account->getId()); - $this->assertTrue(in_array( - Bitrix24AccountApplicationUninstalledEvent::class, - $this->eventDispatcher->getOrphanedEvents() - ), + $this->assertTrue( + in_array( + Bitrix24AccountApplicationUninstalledEvent::class, + $this->eventDispatcher->getOrphanedEvents() + ), sprintf( 'Event %s was expected to be in the list of orphan events, but it is missing', Bitrix24AccountApplicationUninstalledEvent::class ) ); } - - #[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); - - $this->handler = new Bitrix24Accounts\UseCase\Uninstall\Handler( - $this->repository, - $this->flusher, - new NullLogger(), - ); - } -} \ No newline at end of file +} diff --git a/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php index f8543f5..6cd78db 100644 --- a/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php +++ b/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php @@ -5,27 +5,41 @@ namespace Bitrix24\Lib\Tests\Unit\Bitrix24Accounts\UseCase\ChangeDomainUrl; use Bitrix24\Lib\Bitrix24Accounts\UseCase\ChangeDomainUrl\Command; -use InvalidArgumentException; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; - use Symfony\Component\Uid\Uuid; +/** + * @internal + */ #[CoversClass(Command::class)] class CommandTest extends TestCase { + #[Test] + #[DataProvider('dataForCommand')] + public function testValidCommand( + string $oldDomainUrl, + string $newDomainUrl, + ?string $expectedException + ) { + if (null !== $expectedException) { + $this->expectException($expectedException); + } - public function testValidDomainUrl(): void - { - $oldDomainUrl = 'https://'.Uuid::v7()->toRfc4122() . '-test.bitrix24.com'; - $newDomainUrl = Uuid::v7()->toRfc4122() . '-test.bitrix24.com'; - - $this->expectException(InvalidArgumentException::class); new Command($oldDomainUrl, $newDomainUrl); } - protected function setUp(): void + public static function dataForCommand(): \Generator { - + $oldDomainUrl = 'https://'.Uuid::v7()->toRfc4122().'-test.bitrix24.com'; + $newDomainUrl = Uuid::v7()->toRfc4122().'-test.bitrix24.com'; + + yield 'validDomainUrl' => [ + $oldDomainUrl, + $newDomainUrl, + \InvalidArgumentException::class + ]; } -} \ No newline at end of file +} diff --git a/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php index 1b1bc2e..df1225d 100644 --- a/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php +++ b/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php @@ -7,89 +7,76 @@ use Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallFinish\Command; use Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\Builders\Bitrix24AccountBuilder; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus; -use InvalidArgumentException; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; -use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; use Symfony\Component\Uid\Uuid; +/** + * @internal + */ #[CoversClass(Command::class)] class CommandTest extends TestCase { #[Test] - #[TestDox('test finish installation for Command')] - public function testValidCommand(): void - { - $bitrix24Account = (new Bitrix24AccountBuilder()) - ->withStatus(Bitrix24AccountStatus::new) - ->build(); - - $applicationToken = Uuid::v7()->toRfc4122(); + #[DataProvider('dataForCommand')] + public function testValidCommand( + string $applicationToken, + string $memberId, + string $domainUrl, + int $bitrix24UserId, + ?string $expectedException, + ?string $expectedExceptionMessage, + ) { + if (null !== $expectedException) { + $this->expectException($expectedException); + } + + if (null !== $expectedExceptionMessage) { + $this->expectExceptionMessage($expectedExceptionMessage); + } - $command = new Command( + new Command( $applicationToken, - $bitrix24Account->getMemberId(), - $bitrix24Account->getDomainUrl(), - $bitrix24Account->getBitrix24UserId() + $memberId, + $domainUrl, + $bitrix24UserId ); - $this->assertInstanceOf(Command::class, $command); } - public function testEmptyApplicationToken(): void + public static function dataForCommand(): \Generator { + $applicationToken = Uuid::v7()->toRfc4122(); $bitrix24Account = (new Bitrix24AccountBuilder()) ->withStatus(Bitrix24AccountStatus::new) ->build(); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Application token cannot be empty.'); - - new Command( + yield 'emptyApplicationToken' => [ '', $bitrix24Account->getMemberId(), $bitrix24Account->getDomainUrl(), - $bitrix24Account->getBitrix24UserId() - ); - } - + $bitrix24Account->getBitrix24UserId(), + \InvalidArgumentException::class, + 'Application token cannot be empty.' + ]; - public function testEmptyMemberId(): void - { - $bitrix24Account = (new Bitrix24AccountBuilder()) - ->withStatus(Bitrix24AccountStatus::new) - ->build(); - - $applicationToken = Uuid::v7()->toRfc4122(); - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Member ID cannot be empty.'); - - new Command( + yield 'emptyMemberId' => [ $applicationToken, '', $bitrix24Account->getDomainUrl(), - $bitrix24Account->getBitrix24UserId() - ); - } - - public function testValidDomainUrl(): void - { - $bitrix24Account = (new Bitrix24AccountBuilder()) - ->withStatus(Bitrix24AccountStatus::new) - ->build(); - - $applicationToken = Uuid::v7()->toRfc4122(); + $bitrix24Account->getBitrix24UserId(), + \InvalidArgumentException::class, + 'Member ID cannot be empty.' + ]; - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Domain URL is not valid.'); - - new Command( + yield 'validDomainUrl' => [ $applicationToken, $bitrix24Account->getMemberId(), '', - $bitrix24Account->getBitrix24UserId() - ); + $bitrix24Account->getBitrix24UserId(), + \InvalidArgumentException::class, + 'Domain URL is not valid.' + ]; } -} \ No newline at end of file +} diff --git a/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php index 21079f3..a66d531 100644 --- a/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php +++ b/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php @@ -7,44 +7,62 @@ use Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallStart\Command; use Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\Builders\Bitrix24AccountBuilder; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus; -use InvalidArgumentException; +use Bitrix24\SDK\Core\Credentials\AuthToken; +use Bitrix24\SDK\Core\Credentials\Scope; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use Symfony\Component\Uid\Uuid; +/** + * @internal + */ #[CoversClass(Command::class)] class CommandTest extends TestCase { - public function testValidBitrix24UserId(): void - { - $bitrix24Account = (new Bitrix24AccountBuilder()) - ->withStatus(Bitrix24AccountStatus::new) - ->build(); + #[Test] + #[DataProvider('dataForCommand')] + public function testValidCommand( + Uuid $uuid, + int $bitrix24UserId, + bool $isBitrix24UserAdmin, + string $memberId, + string $domainUrl, + AuthToken $authToken, + int $applicationVersion, + Scope $applicationScope, + ?string $expectedException, + ?string $expectedExceptionMessage, + ) { + if (null !== $expectedException) { + $this->expectException($expectedException); + } - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Bitrix24 User ID must be a positive integer.'); + if (null !== $expectedExceptionMessage) { + $this->expectExceptionMessage($expectedExceptionMessage); + } new Command( - $bitrix24Account->getId(), - 0, - $bitrix24Account->isBitrix24UserAdmin(), - $bitrix24Account->getMemberId(), - $bitrix24Account->getDomainUrl(), - $bitrix24Account->getAuthToken(), - $bitrix24Account->getApplicationVersion(), - $bitrix24Account->getApplicationScope() + $uuid, + $bitrix24UserId, + $isBitrix24UserAdmin, + $memberId, + $domainUrl, + $authToken, + $applicationVersion, + $applicationScope ); } - public function testValidMemberId(): void + public static function dataForCommand(): \Generator { + $applicationToken = Uuid::v7()->toRfc4122(); $bitrix24Account = (new Bitrix24AccountBuilder()) ->withStatus(Bitrix24AccountStatus::new) ->build(); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Member ID must be a non-empty string.'); - - new Command( + yield 'emptyMemberId' => [ $bitrix24Account->getId(), $bitrix24Account->getBitrix24UserId(), $bitrix24Account->isBitrix24UserAdmin(), @@ -52,20 +70,12 @@ public function testValidMemberId(): void $bitrix24Account->getDomainUrl(), $bitrix24Account->getAuthToken(), $bitrix24Account->getApplicationVersion(), - $bitrix24Account->getApplicationScope() - ); - } - - public function testValidDomainUrl(): void - { - $bitrix24Account = (new Bitrix24AccountBuilder()) - ->withStatus(Bitrix24AccountStatus::new) - ->build(); - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Domain URL is not valid.'); + $bitrix24Account->getApplicationScope(), + \InvalidArgumentException::class, + 'Member ID must be a non-empty string.' + ]; - new Command( + yield 'validDomainUrl' => [ $bitrix24Account->getId(), $bitrix24Account->getBitrix24UserId(), $bitrix24Account->isBitrix24UserAdmin(), @@ -73,20 +83,25 @@ public function testValidDomainUrl(): void '', $bitrix24Account->getAuthToken(), $bitrix24Account->getApplicationVersion(), - $bitrix24Account->getApplicationScope() - ); - } - - public function testValidApplicationVersion(): void - { - $bitrix24Account = (new Bitrix24AccountBuilder()) - ->withStatus(Bitrix24AccountStatus::new) - ->build(); + $bitrix24Account->getApplicationScope(), + \InvalidArgumentException::class, + 'Domain URL is not valid.' + ]; - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Application version must be a positive integer.'); + yield 'validBitrix24UserId' => [ + $bitrix24Account->getId(), + 0, + $bitrix24Account->isBitrix24UserAdmin(), + $bitrix24Account->getMemberId(), + $bitrix24Account->getDomainUrl(), + $bitrix24Account->getAuthToken(), + $bitrix24Account->getApplicationVersion(), + $bitrix24Account->getApplicationScope(), + \InvalidArgumentException::class, + 'Bitrix24 User ID must be a positive integer.' + ]; - new Command( + yield 'validApplicationToken' => [ $bitrix24Account->getId(), $bitrix24Account->getBitrix24UserId(), $bitrix24Account->isBitrix24UserAdmin(), @@ -94,7 +109,9 @@ public function testValidApplicationVersion(): void $bitrix24Account->getDomainUrl(), $bitrix24Account->getAuthToken(), 0, - $bitrix24Account->getApplicationScope() - ); + $bitrix24Account->getApplicationScope(), + \InvalidArgumentException::class, + 'Application version must be a positive integer.' + ]; } -} \ No newline at end of file +} diff --git a/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php index 48a4613..36fac0f 100644 --- a/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php +++ b/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php @@ -5,20 +5,41 @@ namespace Bitrix24\Lib\Tests\Unit\Bitrix24Accounts\UseCase\Uninstall; use Bitrix24\Lib\Bitrix24Accounts\UseCase\Uninstall\Command; -use InvalidArgumentException; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; -use Symfony\Component\Uid\Uuid; +/** + * @internal + */ #[CoversClass(Command::class)] class CommandTest extends TestCase { - public function testValidApplicationToken(): void - { + #[Test] + #[DataProvider('dataForCommand')] + public function testValidCommand( + string $applicationToken, + ?string $expectedException, + ?string $expectedExceptionMessage, + ) { + if (null !== $expectedException) { + $this->expectException($expectedException); + } + + if (null !== $expectedExceptionMessage) { + $this->expectExceptionMessage($expectedExceptionMessage); + } - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Empty application token or invalid application token.'); + new Command($applicationToken); + } - new Command('123_test_string'); + public static function dataForCommand(): \Generator + { + yield 'validApplicationToken' => [ + '', + \InvalidArgumentException::class, + 'Empty application token application token.', + ]; } -} \ No newline at end of file +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9003ecd..2f9c358 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -14,8 +14,9 @@ use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Dotenv\Dotenv; -require_once dirname(__DIR__) . '/vendor/autoload.php'; -require_once dirname(__DIR__) . '/tests/EntityManagerFactory.php'; +require_once dirname(__DIR__).'/vendor/autoload.php'; + +require_once dirname(__DIR__).'/tests/EntityManagerFactory.php'; if (!class_exists(Dotenv::class)) { throw new LogicException('You need to add "symfony/dotenv" as Composer dependencies.'); @@ -23,11 +24,11 @@ $input = new ArgvInput(); if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) { - putenv('APP_ENV=' . $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env); + putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env); } if ($input->hasParameterOption('--no-debug', true)) { - putenv('APP_DEBUG=' . $_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0'); + putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0'); } -(new Dotenv())->loadEnv(dirname(__DIR__) . '/.env'); \ No newline at end of file +(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');