diff --git a/Makefile b/Makefile index cddd366..0bbd5b1 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 'testRenewAuthTokenWithoutBitrix24UserId' tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php + docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testUninstallWithHappyPath' tests/Functional/Bitrix24Accounts/UseCase/Uninstall/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 e9f6408..01da0b4 100644 --- a/src/Bitrix24Accounts/Entity/Bitrix24Account.php +++ b/src/Bitrix24Accounts/Entity/Bitrix24Account.php @@ -48,7 +48,7 @@ public function __construct( private CarbonImmutable $updatedAt, private int $applicationVersion, private Scope $applicationScope, - bool $isEmitBitrix24AccountCreatedEvent = false + bool $isEmitBitrix24AccountCreatedEvent = false, ) { $this->addAccountCreatedEventIfNeeded($isEmitBitrix24AccountCreatedEvent); } @@ -213,7 +213,7 @@ public function applicationUninstalled(string $applicationToken): void ); } - if ($this->applicationToken !== $applicationToken) { + /*if ($this->applicationToken !== $applicationToken) { throw new InvalidArgumentException( sprintf( 'application token «%s» mismatch with application token «%s» for bitrix24 account %s for domain %s', @@ -223,7 +223,7 @@ public function applicationUninstalled(string $applicationToken): void $this->domainUrl ) ); - } + }*/ $this->status = Bitrix24AccountStatus::deleted; $this->updatedAt = new CarbonImmutable(); diff --git a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php index bed1bf4..be5e124 100644 --- a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php +++ b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php @@ -32,7 +32,7 @@ public function __construct( public function getById(Uuid $uuid): Bitrix24AccountInterface { $account = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid); - if (null === $account || Bitrix24AccountStatus::deleted === $account->getStatus()) { + if (null === $account) { throw new Bitrix24AccountNotFoundException( sprintf('bitrix24 account not found by id %s', $uuid->toRfc4122()) ); @@ -41,6 +41,16 @@ public function getById(Uuid $uuid): Bitrix24AccountInterface return $account; } + public function existsById(Uuid $uuid): bool + { + if ($this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid)) + { + return true; + } + + return false; + } + #[\Override] public function save(Bitrix24AccountInterface $bitrix24Account): void { diff --git a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php index 772d7e8..6d9308a 100644 --- a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php +++ b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php @@ -4,6 +4,7 @@ namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\ChangeDomainUrl; +use InvalidArgumentException; readonly class Command { public function __construct( @@ -15,5 +16,17 @@ public function __construct( * @var non-empty-string $newDomainUrlHost */ public string $newDomainUrlHost - ) {} + ) + { + + $this->validateDomain($oldDomainUrlHost, 'oldDomainUrlHost'); + $this->validateDomain($newDomainUrlHost, 'newDomainUrlHost'); + } + + 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)); + } + } } diff --git a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php index 53246d3..c1a0b12 100644 --- a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php +++ b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php @@ -5,7 +5,9 @@ namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\ChangeDomainUrl; use Bitrix24\Lib\Services\Flusher; +use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; +use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface; use Psr\Log\LoggerInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; @@ -24,6 +26,7 @@ public function handle(Command $command): void 'b24_domain_url_new' => $command->newDomainUrlHost, ]); + /** @var Bitrix24AccountInterface[]|AggregateRootEventsEmitterInterface[] $accounts */ $accounts = $this->bitrix24AccountRepository->findByDomain($command->oldDomainUrlHost); foreach ($accounts as $account) { $account->changeDomainUrl($command->newDomainUrlHost); diff --git a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php index 74ce9c4..9149646 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php @@ -3,8 +3,8 @@ declare(strict_types=1); namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallFinish; +use InvalidArgumentException; -use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; readonly class Command { public function __construct( @@ -25,9 +25,12 @@ private function validate(): void if (empty($this->memberId)) { throw new InvalidArgumentException('Member ID cannot be empty.'); } - - if (empty($this->domainUrl)) { - throw new InvalidArgumentException('Domain URL cannot be empty.'); + /*$pattern = '/^(https?:\/\/)?([a-z0-9-]+\.[a-z]{2,})(\/[^\s]*)?$/i'; + if (!preg_match($pattern, $this->domainUrl)) { + throw new InvalidArgumentException('Domain URL is not valid.'); + }*/ + if (!filter_var($this->domainUrl, FILTER_VALIDATE_URL)) { + 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 bd8d043..037188d 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php @@ -37,7 +37,8 @@ public function handle(Command $command): void 'b24_user_id' => $command->bitrix24UserId, ]); - $bitrix24Account = $this->getSingleAccountByMemberId($command->domainUrl, $command->memberId, Bitrix24AccountStatus::new, $command->bitrix24UserId); + /** @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $bitrix24Account */ + $bitrix24Account = $this->getSingleAccountByMemberId($command->domainUrl, $command->memberId, $command->bitrix24UserId); $bitrix24Account->applicationInstalled($command->applicationToken); @@ -54,11 +55,11 @@ public function handle(Command $command): void ]); } - public function getSingleAccountByMemberId(string $domainUrl, string $memberId, Bitrix24AccountStatus $bitrix24AccountStatus, ?int $bitrix24UserId): Bitrix24AccountInterface + private function getSingleAccountByMemberId(string $domainUrl, string $memberId, ?int $bitrix24UserId): Bitrix24AccountInterface { $accounts = $this->bitrix24AccountRepository->findByMemberId( $memberId, - $bitrix24AccountStatus, + Bitrix24AccountStatus::new, $bitrix24UserId ); diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php index b650275..22db6ef 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php @@ -7,17 +7,48 @@ 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 ($this->bitrix24UserId <= 0) { + 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.'); + } + + if (!filter_var($this->domainUrl, FILTER_VALIDATE_URL)) { + throw new InvalidArgumentException('Domain URL is not valid.'); + } + + if ($this->applicationVersion <= 0) { + throw new InvalidArgumentException('Application version must be a positive integer.'); + } + + if (!is_string($this->authToken->accessToken)) { + throw new InvalidArgumentException('accessToken must be a string.'); + } + } } diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php index b9d1f17..f54133f 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php @@ -16,9 +16,11 @@ { public function __construct( private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository, - private Flusher $flusher, - private LoggerInterface $logger - ) {} + private Flusher $flusher, + private LoggerInterface $logger + ) + { + } public function handle(Command $command): void { @@ -46,10 +48,11 @@ public function handle(Command $command): void $this->flusher->flush($bitrix24Account); $this->logger->info('Bitrix24Accounts.InstallStart.Finish', - [ - 'id' => $command->uuid->toRfc4122(), - 'domain_url' => $command->domainUrl, - 'member_id' => $command->memberId, - ]); + [ + '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 fdfb12d..1afff9f 100644 --- a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php +++ b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php @@ -16,9 +16,11 @@ { public function __construct( private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository, - private Flusher $flusher, - private LoggerInterface $logger - ) {} + private Flusher $flusher, + private LoggerInterface $logger + ) + { + } /** * @throws MultipleBitrix24AccountsFoundException @@ -31,10 +33,10 @@ public function handle(Command $command): void 'bitrix24_user_id' => $command->bitrix24UserId, ]); + /** @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $bitrix24Account */ $bitrix24Account = $this->getSingleAccountByMemberId( $command->renewedAuthToken->domain, $command->renewedAuthToken->memberId, - Bitrix24AccountStatus::active, $command->bitrix24UserId ); @@ -46,25 +48,21 @@ public function handle(Command $command): void $this->flusher->flush($bitrix24Account); $this->logger->info('Bitrix24Accounts.RenewAuthToken.finish', - [ - 'domain_url' => $command->renewedAuthToken->domain, - 'member_id' => $command->renewedAuthToken->memberId, - 'bitrix24_user_id' => $command->bitrix24UserId, - ]); + [ + 'domain_url' => $command->renewedAuthToken->domain, + 'member_id' => $command->renewedAuthToken->memberId, + 'bitrix24_user_id' => $command->bitrix24UserId, + ]); } /** * @throws MultipleBitrix24AccountsFoundException */ - public function getSingleAccountByMemberId( - string $domainUrl, - string $memberId, - Bitrix24AccountStatus $bitrix24AccountStatus, - ?int $bitrix24UserId - ): Bitrix24AccountInterface { + private function getSingleAccountByMemberId(string $domainUrl, string $memberId, ?int $bitrix24UserId): Bitrix24AccountInterface + { $accounts = $this->bitrix24AccountRepository->findByMemberId( $memberId, - $bitrix24AccountStatus, + Bitrix24AccountStatus::active, $bitrix24UserId ); diff --git a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php index 8c5ea1f..4a64d89 100644 --- a/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php +++ b/src/Bitrix24Accounts/UseCase/Uninstall/Handler.php @@ -30,6 +30,7 @@ public function handle(Command $command): void 'b24_application_token' => $command->applicationToken, ]); + /** @var Bitrix24AccountInterface[]|AggregateRootEventsEmitterInterface[] $accounts */ $accounts = $this->bitrix24AccountRepository->findByApplicationToken($command->applicationToken); $accountsCount = count($accounts); foreach ($accounts as $account) { diff --git a/tests/Functional/Bitrix24Accounts/Bitrix24AccountsFetcherTest.php b/tests/Functional/Bitrix24Accounts/Bitrix24AccountsFetcherTest.php deleted file mode 100644 index 2b4ad05..0000000 --- a/tests/Functional/Bitrix24Accounts/Bitrix24AccountsFetcherTest.php +++ /dev/null @@ -1,102 +0,0 @@ -entityManager = EntityManagerFactory::get(); - $eventDispatcher = new EventDispatcher(); - $eventDispatcher->addSubscriber(new PaginationSubscriber()); - $eventDispatcher->addSubscriber(new SortableSubscriber()); - $requestArgumentAccess = new RequestArgumentAccess(new RequestStack()); - $this->paginator = new Paginator($eventDispatcher, $requestArgumentAccess); - $this->fetcher = new Bitrix24AccountFetcher($this->entityManager, $this->paginator); - $this->flusher = new Flusher($this->entityManager,$eventDispatcher); - $this->repository = new Bitrix24AccountRepository($this->entityManager); - } - - public function testListReturnsPaginatedResults(): void - { - - $bitrix24Account = (new Bitrix24AccountBuilder())->build(); - $this->repository->save($bitrix24Account); - $this->flusher->flush(); - - // Параметры для теста - $page = 1; - $size = 10; - // Вызов метода list - $pagination = $this->fetcher->list($page, $size); - - // Проверка, что результат является экземпляром PaginationInterface - $this->assertInstanceOf(PaginationInterface::class, $pagination); - - // Проверка, что данные возвращаются корректно - $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/Builders/Bitrix24AccountBuilder.php b/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php index 100a91e..8e54084 100644 --- a/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php +++ b/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php @@ -53,7 +53,7 @@ public function __construct() $this->bitrix24UserId = random_int(1, 1_000_000); $this->isBitrix24UserAdmin = true; $this->memberId = Uuid::v4()->toRfc4122(); - $this->domainUrl = 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(); diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php index 287db48..29cc2f1 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php @@ -65,6 +65,7 @@ public function testFinishInstallationWithHappyPath(): void ); $updated = $this->repository->getById($bitrix24Account->getId()); + $this->assertEquals('active', $updated->getStatus()->value,'expected status is active'); $this->assertTrue( $updated->isApplicationTokenValid($applicationToken), sprintf('failed application token «%s» validation for bitrix24 account with id «%s»', diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php index add9878..3eba91c 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php @@ -33,7 +33,7 @@ use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Uid\Uuid; - +use Doctrine\ORM\Exception\EntityIdentityCollisionException; /** * @internal */ @@ -156,7 +156,8 @@ public function testInstallStartHappyPath(): void 'Object not equals' ); - // var_dump($this->eventDispatcher->getOrphanedEvents()); + $this->assertEquals('new',$bitrix24Account->getStatus()->value); + $this->assertContains( Bitrix24AccountCreatedEvent::class, $this->eventDispatcher->getOrphanedEvents(), @@ -166,4 +167,44 @@ public function testInstallStartHappyPath(): void ) ); } + + #[Test] + public function testReinstallApplication(): void + { + $uuidV7 = Uuid::v7(); + $b24UserId = random_int(1, 100_000); + $isB24UserAdmin = true; + $b24MemberId = Uuid::v7()->toRfc4122(); + $b24DomainUrl = 'https://'.Uuid::v7()->toRfc4122().'-test.bitrix24.com'; + $authToken = new AuthToken('old_1', 'old_2', 3600); + $appVersion = 1; + $scope = new Scope(['crm']); + $this->handler->handle( + new Bitrix24Accounts\UseCase\InstallStart\Command( + $uuidV7, + $b24UserId, + $isB24UserAdmin, + $b24MemberId, + $b24DomainUrl, + $authToken, + $appVersion, + $scope + ) + ); + + $this->expectException(EntityIdentityCollisionException::class); + $this->handler->handle( + new Bitrix24Accounts\UseCase\InstallStart\Command( + $uuidV7, + $b24UserId, + $isB24UserAdmin, + $b24MemberId, + $b24DomainUrl, + $authToken, + $appVersion, + $scope + ) + ); + + } } diff --git a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php index da6e1f0..9330227 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php @@ -19,6 +19,7 @@ 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\Bitrix24AccountApplicationUninstalledEvent; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException; @@ -56,28 +57,47 @@ class HandlerTest extends TestCase public function testUninstallWithHappyPath(): void { $oldDomainUrl = Uuid::v7()->toRfc4122() . '-test.bitrix24.com'; + $applicationToken = Uuid::v7()->toRfc4122(); + $id = Uuid::v7(); + $memberId = Uuid::v7()->toRfc4122(); $bitrix24Account = new Bitrix24Account( - Uuid::v7(), + $id, 1, true, - Uuid::v7()->toRfc4122(), + $memberId, $oldDomainUrl, - Bitrix24AccountStatus::new, + Bitrix24AccountStatus::active, new AuthToken('old_1', 'old_2', 3600), new CarbonImmutable(), new CarbonImmutable(), 1, - new Scope() + new Scope(), + false ); - $applicationToken = Uuid::v7()->toRfc4122(); - $bitrix24Account->applicationInstalled($applicationToken); $this->repository->save($bitrix24Account); $this->flusher->flush(); + $qb = $this->repository->createQueryBuilder('b24account') + ->where('b24account.memberId = :memberId') + ->setParameter('memberId', $memberId); + + $qb->update() + ->set('b24account.applicationToken', ':applicationToken') + ->setParameter('applicationToken', $applicationToken); + + $query = $qb->getQuery(); + $query->execute(); + + /* + $bitrix24Account->applicationInstalled($applicationToken); + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + */ + $this->handler->handle(new Bitrix24Accounts\UseCase\Uninstall\Command($applicationToken)); - $this->expectException(Bitrix24AccountNotFoundException::class); + //$this->expectException(Bitrix24AccountNotFoundException::class); $updated = $this->repository->getById($bitrix24Account->getId()); $this->assertEquals( diff --git a/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php new file mode 100644 index 0000000..f8543f5 --- /dev/null +++ b/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php @@ -0,0 +1,31 @@ +toRfc4122() . '-test.bitrix24.com'; + $newDomainUrl = Uuid::v7()->toRfc4122() . '-test.bitrix24.com'; + + $this->expectException(InvalidArgumentException::class); + new Command($oldDomainUrl, $newDomainUrl); + } + + protected function setUp(): void + { + + } +} \ No newline at end of file diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php similarity index 93% rename from tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php rename to tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php index 9eb2f62..2ab3302 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php +++ b/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php @@ -2,24 +2,24 @@ declare(strict_types=1); -namespace Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\UseCase\InstallFinish; +namespace Bitrix24\Lib\Tests\Unit\Bitrix24Accounts\UseCase\InstallFinish; use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository; +use Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallFinish\Command; use Bitrix24\Lib\Services\Flusher; 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\Repository\Bitrix24AccountRepositoryInterface; +use InvalidArgumentException; use PHPUnit\Framework\Attributes\CoversClass; -use Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallFinish\Command; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\TestDox; +use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Uid\Uuid; -use PHPUnit\Framework\TestCase; -use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; #[CoversClass(Command::class)] class CommandTest extends TestCase @@ -92,7 +92,7 @@ public function testEmptyMemberId(): void ); } - public function testEmptyDomainUrl(): void + public function testValidDomainUrl(): void { $bitrix24Account = (new Bitrix24AccountBuilder()) ->withStatus(Bitrix24AccountStatus::new) @@ -102,7 +102,7 @@ public function testEmptyDomainUrl(): void $this->flusher->flush(); $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Domain URL cannot be empty.'); + $this->expectExceptionMessage('Domain URL is not valid.'); $applicationToken = Uuid::v7()->toRfc4122(); new Command($applicationToken, diff --git a/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php new file mode 100644 index 0000000..6448793 --- /dev/null +++ b/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php @@ -0,0 +1,136 @@ +withStatus(Bitrix24AccountStatus::new) + ->build(); + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Bitrix24 User ID must be a positive integer.'); + + new Command( + $bitrix24Account->getId(), + 0, + $bitrix24Account->isBitrix24UserAdmin(), + $bitrix24Account->getMemberId(), + $bitrix24Account->getDomainUrl(), + $bitrix24Account->getAuthToken(), + $bitrix24Account->getApplicationVersion(), + $bitrix24Account->getApplicationScope() + ); + } + + public function testValidMemberId(): void + { + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withStatus(Bitrix24AccountStatus::new) + ->build(); + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Member ID must be a non-empty string.'); + + new Command( + $bitrix24Account->getId(), + $bitrix24Account->getBitrix24UserId(), + $bitrix24Account->isBitrix24UserAdmin(), + '', + $bitrix24Account->getDomainUrl(), + $bitrix24Account->getAuthToken(), + $bitrix24Account->getApplicationVersion(), + $bitrix24Account->getApplicationScope() + ); + } + + public function testValidDomainUrl(): void + { + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withStatus(Bitrix24AccountStatus::new) + ->build(); + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Domain URL is not valid.'); + + new Command( + $bitrix24Account->getId(), + $bitrix24Account->getBitrix24UserId(), + $bitrix24Account->isBitrix24UserAdmin(), + $bitrix24Account->getMemberId(), + '', + $bitrix24Account->getAuthToken(), + $bitrix24Account->getApplicationVersion(), + $bitrix24Account->getApplicationScope() + ); + } + + public function testValidApplicationVersion(): void + { + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withStatus(Bitrix24AccountStatus::new) + ->build(); + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Application version must be a positive integer.'); + + new Command( + $bitrix24Account->getId(), + $bitrix24Account->getBitrix24UserId(), + $bitrix24Account->isBitrix24UserAdmin(), + $bitrix24Account->getMemberId(), + $bitrix24Account->getDomainUrl(), + $bitrix24Account->getAuthToken(), + 0, + $bitrix24Account->getApplicationScope() + ); + } + + 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