From 129e83b66f90eb4650a018212d3a4b2d3212e5b5 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Thu, 26 Dec 2024 00:13:13 +0300 Subject: [PATCH 1/6] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Entity/Bitrix24Account.php | 68 +++++++++++-------- .../Doctrine/Bitrix24AccountRepository.php | 43 +++++++++--- .../UseCase/InstallStart/Command.php | 4 +- 3 files changed, 75 insertions(+), 40 deletions(-) diff --git a/src/Bitrix24Accounts/Entity/Bitrix24Account.php b/src/Bitrix24Accounts/Entity/Bitrix24Account.php index 01da0b4..8c84f92 100644 --- a/src/Bitrix24Accounts/Entity/Bitrix24Account.php +++ b/src/Bitrix24Accounts/Entity/Bitrix24Account.php @@ -36,20 +36,21 @@ 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); } @@ -194,37 +195,50 @@ public function applicationInstalled(string $applicationToken): void ); } - /** - * @throws InvalidArgumentException - */ - #[\Override] - public function applicationUninstalled(string $applicationToken): void + private function guardEmpty($applicationToken) { if ('' === $applicationToken) { throw new InvalidArgumentException('application token cannot be empty'); } + } - if (Bitrix24AccountStatus::active !== $this->status) { + private function guardTokenMismatch($applicationToken) + { + if ($this->applicationToken !== $applicationToken) { throw new InvalidArgumentException( sprintf( - 'for uninstall account must be in status «active», current status - «%s»', - $this->status->name + 'application token «%s» mismatch with application token «%s» for bitrix24 account %s for domain %s', + $applicationToken, + $this->applicationToken, + $this->id->toRfc4122(), + $this->domainUrl ) ); } + } - /*if ($this->applicationToken !== $applicationToken) { + private function guardStatusIsActive() + { + if (Bitrix24AccountStatus::active !== $this->status) { 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 + '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->guardTokenMismatch($applicationToken); + $this->guardStatusIsActive(); $this->status = Bitrix24AccountStatus::deleted; $this->updatedAt = new CarbonImmutable(); $this->events[] = new Bitrix24AccountApplicationUninstalledEvent( diff --git a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php index be5e124..5d86017 100644 --- a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php +++ b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php @@ -11,6 +11,7 @@ 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; @@ -19,7 +20,8 @@ class Bitrix24AccountRepository extends EntityRepository implements Bitrix24Acco { public function __construct( EntityManagerInterface $entityManager - ) { + ) + { parent::__construct($entityManager, $entityManager->getClassMetadata(Bitrix24Account::class)); } @@ -31,7 +33,16 @@ public function __construct( #[\Override] public function getById(Uuid $uuid): Bitrix24AccountInterface { - $account = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid); + + $account = $this->getEntityManager()->getRepository(Bitrix24Account::class) + ->createQueryBuilder('b24') + ->where('b24.id = :id') + ->andWhere('b24.status != :status') + ->setParameter('id', $uuid) + ->setParameter('status', Bitrix24AccountStatus::deleted) + ->getQuery() + ->getOneOrNullResult(); + // $account = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid); if (null === $account) { throw new Bitrix24AccountNotFoundException( sprintf('bitrix24 account not found by id %s', $uuid->toRfc4122()) @@ -43,8 +54,16 @@ public function getById(Uuid $uuid): Bitrix24AccountInterface public function existsById(Uuid $uuid): bool { - if ($this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid)) - { + $account = $this->getEntityManager()->getRepository(Bitrix24Account::class) + ->createQueryBuilder('b24') + ->where('b24.id = :id') + ->andWhere('b24.status != :status') + ->setParameter('id', $uuid) + ->setParameter('status', Bitrix24AccountStatus::deleted) + ->getQuery() + ->getOneOrNullResult(); + + if ($account) { return true; } @@ -64,11 +83,12 @@ public function save(Bitrix24AccountInterface $bitrix24Account): void */ #[\Override] public function findByMemberId( - string $memberId, + string $memberId, ?Bitrix24AccountStatus $bitrix24AccountStatus = null, - ?int $bitrix24UserId = null, - ?bool $isAdmin = null - ): array { + ?int $bitrix24UserId = null, + ?bool $isAdmin = null + ): array + { if ('' === trim($memberId)) { throw new InvalidArgumentException('memberId cannot be empty'); } @@ -178,10 +198,11 @@ public function findOneAdminByMemberId(string $memberId): ?Bitrix24AccountInterf */ #[\Override] public function findByDomain( - string $domainUrl, + string $domainUrl, ?Bitrix24AccountStatus $bitrix24AccountStatus = null, - ?bool $isAdmin = null - ): array { + ?bool $isAdmin = null + ): array + { if ('' === trim($domainUrl)) { throw new InvalidArgumentException('domainUrl cannot be an empty string'); } diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php index 22db6ef..be22835 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php @@ -39,9 +39,9 @@ private function validate(): void throw new InvalidArgumentException('Member ID must be a non-empty string.'); } - if (!filter_var($this->domainUrl, FILTER_VALIDATE_URL)) { + /* 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.'); From ae0d1a5059e15a41f219d4f92221ba51ca35bd42 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sat, 28 Dec 2024 00:07:57 +0300 Subject: [PATCH 2/6] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UseCase/InstallStart/Command.php | 8 +--- .../Builders/Bitrix24AccountBuilder.php | 16 ++++++- .../UseCase/InstallStart/HandlerTest.php | 4 +- .../UseCase/RenewAuthToken/HandlerTest.php | 3 +- .../UseCase/Uninstall/HandlerTest.php | 48 +++---------------- .../UseCase/InstallFinish/CommandTest.php | 48 ++++--------------- .../UseCase/InstallStart/CommandTest.php | 36 -------------- 7 files changed, 37 insertions(+), 126 deletions(-) diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php index be22835..ff97cfd 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php @@ -39,16 +39,12 @@ private function validate(): void throw new InvalidArgumentException('Member ID must be a non-empty string.'); } - /* if (!filter_var($this->domainUrl, FILTER_VALIDATE_URL)) { + 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/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php b/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php index 8e54084..4e6fc7a 100644 --- a/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php +++ b/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php @@ -47,6 +47,8 @@ class Bitrix24AccountBuilder private readonly Scope $applicationScope; + private ?string $applicationToken = null; + public function __construct() { $this->id = Uuid::v7(); @@ -73,6 +75,12 @@ public function withDomainUrl(string $domainUrl): self return $this; } + public function withApplicationToken(string $applicationToken): self + { + $this->applicationToken = $applicationToken; + return $this; + } + public function withStatus(Bitrix24AccountStatus $bitrix24AccountStatus): self { $this->status = $bitrix24AccountStatus; @@ -81,7 +89,7 @@ public function withStatus(Bitrix24AccountStatus $bitrix24AccountStatus): self public function build(): AggregateRootEventsEmitterInterface&Bitrix24AccountInterface { - return new Bitrix24Account( + $account = new Bitrix24Account( $this->id, $this->bitrix24UserId, $this->isBitrix24UserAdmin, @@ -94,5 +102,11 @@ public function build(): AggregateRootEventsEmitterInterface&Bitrix24AccountInte $this->applicationVersion, $this->applicationScope ); + + if (isset($this->applicationToken) && $this->status == Bitrix24AccountStatus::new) { + $account->applicationInstalled($this->applicationToken); + } + + return $account; } } diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php index 3eba91c..a26e919 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php @@ -17,6 +17,7 @@ use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository; use Bitrix24\Lib\Services\Flusher; use Bitrix24\Lib\Tests\EntityManagerFactory; +use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Events\Bitrix24AccountCreatedEvent; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; @@ -52,7 +53,6 @@ class HandlerTest extends TestCase protected function setUp(): void { $entityManager = EntityManagerFactory::get(); - $eventDispatcher = new EventDispatcher(); $this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $this->repository = new Bitrix24AccountRepository($entityManager); $this->flusher = new Flusher($entityManager,$this->eventDispatcher); @@ -156,7 +156,7 @@ public function testInstallStartHappyPath(): void 'Object not equals' ); - $this->assertEquals('new',$bitrix24Account->getStatus()->value); + $this->assertEquals(Bitrix24AccountStatus::new,$bitrix24Account->getStatus()); $this->assertContains( Bitrix24AccountCreatedEvent::class, diff --git a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php index 0f63150..621d3fa 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php @@ -64,8 +64,7 @@ protected function setUp(): void public function testRenewAuthTokenWithoutBitrix24UserId(): void { $bitrix24Account = (new Bitrix24AccountBuilder()) - ->build() - ; + ->build(); $this->repository->save($bitrix24Account); $this->flusher->flush(); diff --git a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php index 9330227..5d6c113 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php @@ -56,56 +56,22 @@ class HandlerTest extends TestCase #[Test] 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( - $id, - 1, - true, - $memberId, - $oldDomainUrl, - Bitrix24AccountStatus::active, - new AuthToken('old_1', 'old_2', 3600), - new CarbonImmutable(), - new CarbonImmutable(), - 1, - new Scope(), - false - ); + + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withStatus(Bitrix24AccountStatus::new) + ->withApplicationToken($applicationToken) + ->build(); $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( - Bitrix24AccountStatus::deleted, - $updated->getStatus(), - 'Expected status deleted' - ); - $this->assertTrue(in_array( Bitrix24AccountApplicationUninstalledEvent::class, $this->eventDispatcher->getOrphanedEvents() diff --git a/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php index 2ab3302..1b1bc2e 100644 --- a/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php +++ b/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php @@ -4,33 +4,19 @@ 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 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; #[CoversClass(Command::class)] class CommandTest extends TestCase { - - private Flusher $flusher; - - private Bitrix24AccountRepositoryInterface $repository; - - private TraceableEventDispatcher $eventDispatcher; - #[Test] #[TestDox('test finish installation for Command')] public function testValidCommand(): void @@ -39,10 +25,8 @@ public function testValidCommand(): void ->withStatus(Bitrix24AccountStatus::new) ->build(); - $this->repository->save($bitrix24Account); - $this->flusher->flush(); - $applicationToken = Uuid::v7()->toRfc4122(); + $command = new Command( $applicationToken, $bitrix24Account->getMemberId(), @@ -58,13 +42,11 @@ public function testEmptyApplicationToken(): void ->withStatus(Bitrix24AccountStatus::new) ->build(); - $this->repository->save($bitrix24Account); - $this->flusher->flush(); - $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Application token cannot be empty.'); - new Command('', + new Command( + '', $bitrix24Account->getMemberId(), $bitrix24Account->getDomainUrl(), $bitrix24Account->getBitrix24UserId() @@ -78,14 +60,13 @@ public function testEmptyMemberId(): void ->withStatus(Bitrix24AccountStatus::new) ->build(); - $this->repository->save($bitrix24Account); - $this->flusher->flush(); + $applicationToken = Uuid::v7()->toRfc4122(); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Member ID cannot be empty.'); - $applicationToken = Uuid::v7()->toRfc4122(); - new Command($applicationToken, + new Command( + $applicationToken, '', $bitrix24Account->getDomainUrl(), $bitrix24Account->getBitrix24UserId() @@ -98,26 +79,17 @@ public function testValidDomainUrl(): void ->withStatus(Bitrix24AccountStatus::new) ->build(); - $this->repository->save($bitrix24Account); - $this->flusher->flush(); + $applicationToken = Uuid::v7()->toRfc4122(); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Domain URL is not valid.'); - $applicationToken = Uuid::v7()->toRfc4122(); - new Command($applicationToken, + new Command( + $applicationToken, $bitrix24Account->getMemberId(), '', $bitrix24Account->getBitrix24UserId() ); } - - 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 diff --git a/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php index 6448793..21079f3 100644 --- a/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php +++ b/tests/Unit/Bitrix24Accounts/UseCase/InstallStart/CommandTest.php @@ -4,40 +4,22 @@ namespace Bitrix24\Lib\Tests\Unit\Bitrix24Accounts\UseCase\InstallStart; -use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository; use Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallStart\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 PHPUnit\Framework\TestCase; -use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; -use Symfony\Component\EventDispatcher\EventDispatcher; -use Symfony\Component\Stopwatch\Stopwatch; #[CoversClass(Command::class)] class CommandTest extends TestCase { - - private Flusher $flusher; - - private Bitrix24AccountRepositoryInterface $repository; - - private TraceableEventDispatcher $eventDispatcher; - - public function testValidBitrix24UserId(): void { $bitrix24Account = (new Bitrix24AccountBuilder()) ->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.'); @@ -59,9 +41,6 @@ public function testValidMemberId(): void ->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.'); @@ -83,9 +62,6 @@ public function testValidDomainUrl(): void ->withStatus(Bitrix24AccountStatus::new) ->build(); - $this->repository->save($bitrix24Account); - $this->flusher->flush(); - $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Domain URL is not valid.'); @@ -107,9 +83,6 @@ public function testValidApplicationVersion(): void ->withStatus(Bitrix24AccountStatus::new) ->build(); - $this->repository->save($bitrix24Account); - $this->flusher->flush(); - $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Application version must be a positive integer.'); @@ -124,13 +97,4 @@ public function testValidApplicationVersion(): void $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 From 2535f7b663720f58e72c4b4b3cc56e26a2b885b2 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sun, 29 Dec 2024 01:33:43 +0300 Subject: [PATCH 3/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=202=20=D1=8E=D0=BD=D0=B8=D1=82=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UseCase/RenewAuthToken/Command.php | 14 ++++++++++++-- src/Bitrix24Accounts/UseCase/Uninstall/Command.php | 14 +++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php index a71666c..acd8276 100644 --- a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php +++ b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php @@ -5,11 +5,21 @@ namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\RenewAuthToken; use Bitrix24\SDK\Core\Response\DTO\RenewedAuthToken; - +use Symfony\Component\Uid\Uuid; +use InvalidArgumentException; readonly class Command { public function __construct( public RenewedAuthToken $renewedAuthToken, public ?int $bitrix24UserId = null, - ) {} + ) { + $this->validate(); + } + + private function validate(): void + { + if ($this->bitrix24UserId <= 0) { + throw new InvalidArgumentException('Bitrix24 User ID must be a positive integer.'); + } + } } diff --git a/src/Bitrix24Accounts/UseCase/Uninstall/Command.php b/src/Bitrix24Accounts/UseCase/Uninstall/Command.php index 16038c8..7134c12 100644 --- a/src/Bitrix24Accounts/UseCase/Uninstall/Command.php +++ b/src/Bitrix24Accounts/UseCase/Uninstall/Command.php @@ -4,6 +4,9 @@ namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\Uninstall; +use InvalidArgumentException; +use Symfony\Component\Uid\Uuid; + readonly class Command { public function __construct( @@ -11,5 +14,14 @@ public function __construct( * @var non-empty-string $applicationToken */ public string $applicationToken - ) {} + ) { + $this->validate(); + } + + private function validate(): void + { + if (empty($this->applicationToken) || !Uuid::isValid($this->applicationToken)) { + throw new InvalidArgumentException('Empty application token or invalid application token.'); + } + } } From 9a83c79d4c9e56da5f1002ce85d22b68a246952a Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sun, 29 Dec 2024 01:33:56 +0300 Subject: [PATCH 4/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=202=20=D1=8E=D0=BD=D0=B8=D1=82=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UseCase/RenewAuthToken/CommandTest.php | 42 +++++++++++++++++++ .../UseCase/Uninstall/CommandTest.php | 24 +++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/Unit/Bitrix24Accounts/UseCase/RenewAuthToken/CommandTest.php create mode 100644 tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php diff --git a/tests/Unit/Bitrix24Accounts/UseCase/RenewAuthToken/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/RenewAuthToken/CommandTest.php new file mode 100644 index 0000000..4a12cf4 --- /dev/null +++ b/tests/Unit/Bitrix24Accounts/UseCase/RenewAuthToken/CommandTest.php @@ -0,0 +1,42 @@ +withStatus(Bitrix24AccountStatus::new) + ->build(); + + $newAuthToken = new AuthToken('new_1', 'new_2', 3600); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Bitrix24 User ID must be a positive integer.'); + + new Command( + new RenewedAuthToken( + $newAuthToken, + $bitrix24Account->getMemberId(), + 'https://client-endpoint.com', + 'https://server-endpoint.com', + ApplicationStatus::subscription(), + $bitrix24Account->getDomainUrl() + ), + ); + } +} \ No newline at end of file diff --git a/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php new file mode 100644 index 0000000..48a4613 --- /dev/null +++ b/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php @@ -0,0 +1,24 @@ +expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Empty application token or invalid application token.'); + + new Command('123_test_string'); + } +} \ No newline at end of file From 98eb191574b7dbfcbd073c77c2d846fa9d8c1a9c Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sun, 5 Jan 2025 19:01:55 +0300 Subject: [PATCH 5/6] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B2=20=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=80=D0=BD=D1=83=D1=8E=20=D1=83=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2=D0=BA=D1=83=20=D0=B0=D0=BA=D0=BA=D0=B0?= =?UTF-8?q?=D1=83=D0=BD=D1=82=D0=B0=20=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20=D0=BE=D0=B1=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=82=D0=BE=D0=BA?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=82=D0=B0=D0=BA=20=D0=BA=D0=B0=D0=BA=20?= =?UTF-8?q?=D1=82=D0=B0=D0=BC=20=D0=BD=D0=B5=D1=87=D0=B5=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D1=8F=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Doctrine/Bitrix24AccountRepository.php | 2 +- .../UseCase/InstallFinish/Command.php | 4 -- .../UseCase/InstallStart/Handler.php | 24 +++++++---- .../UseCase/RenewAuthToken/Command.php | 10 ----- .../UseCase/InstallFinish/HandlerTest.php | 2 +- .../UseCase/InstallStart/HandlerTest.php | 17 ++++++-- .../UseCase/RenewAuthToken/CommandTest.php | 42 ------------------- 7 files changed, 31 insertions(+), 70 deletions(-) delete mode 100644 tests/Unit/Bitrix24Accounts/UseCase/RenewAuthToken/CommandTest.php diff --git a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php index 5d86017..2621722 100644 --- a/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php +++ b/src/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepository.php @@ -41,7 +41,7 @@ public function getById(Uuid $uuid): Bitrix24AccountInterface ->setParameter('id', $uuid) ->setParameter('status', Bitrix24AccountStatus::deleted) ->getQuery() - ->getOneOrNullResult(); + ->getOneOrNullResult(); // $account = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid); if (null === $account) { throw new Bitrix24AccountNotFoundException( diff --git a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php index 9149646..f40ce9e 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php @@ -25,10 +25,6 @@ private function validate(): void if (empty($this->memberId)) { throw new InvalidArgumentException('Member ID 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/InstallStart/Handler.php b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php index f54133f..32fe1c6 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php @@ -44,15 +44,21 @@ public function handle(Command $command): void $command->applicationScope, true ); - $this->bitrix24AccountRepository->save($bitrix24Account); - $this->flusher->flush($bitrix24Account); - - $this->logger->info('Bitrix24Accounts.InstallStart.Finish', - [ - 'id' => $command->uuid->toRfc4122(), - 'domain_url' => $command->domainUrl, - 'member_id' => $command->memberId, - ]); + + $isAccountExists = $this->bitrix24AccountRepository->existsById($bitrix24Account->getId()); + + if (!$isAccountExists) { + $this->bitrix24AccountRepository->save($bitrix24Account); + $this->flusher->flush($bitrix24Account); + + $this->logger->info('Bitrix24Accounts.InstallStart.Finish', + [ + 'id' => $command->uuid->toRfc4122(), + 'domain_url' => $command->domainUrl, + 'member_id' => $command->memberId, + ]); + } + } } diff --git a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php index acd8276..63d2b6a 100644 --- a/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php +++ b/src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php @@ -5,21 +5,11 @@ namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\RenewAuthToken; use Bitrix24\SDK\Core\Response\DTO\RenewedAuthToken; -use Symfony\Component\Uid\Uuid; -use InvalidArgumentException; readonly class Command { public function __construct( public RenewedAuthToken $renewedAuthToken, public ?int $bitrix24UserId = null, ) { - $this->validate(); - } - - private function validate(): void - { - if ($this->bitrix24UserId <= 0) { - throw new InvalidArgumentException('Bitrix24 User ID must be a positive integer.'); - } } } diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php index 29cc2f1..eccc111 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallFinish/HandlerTest.php @@ -65,7 +65,7 @@ public function testFinishInstallationWithHappyPath(): void ); $updated = $this->repository->getById($bitrix24Account->getId()); - $this->assertEquals('active', $updated->getStatus()->value,'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»', diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php index a26e919..5c449fc 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php @@ -34,7 +34,7 @@ use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Uid\Uuid; -use Doctrine\ORM\Exception\EntityIdentityCollisionException; +use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface; /** * @internal */ @@ -169,7 +169,7 @@ public function testInstallStartHappyPath(): void } #[Test] - public function testReinstallApplication(): void + public function testCreateExistingAccount(): void { $uuidV7 = Uuid::v7(); $b24UserId = random_int(1, 100_000); @@ -192,7 +192,6 @@ public function testReinstallApplication(): void ) ); - $this->expectException(EntityIdentityCollisionException::class); $this->handler->handle( new Bitrix24Accounts\UseCase\InstallStart\Command( $uuidV7, @@ -206,5 +205,17 @@ public function testReinstallApplication(): void ) ); + $accounts = $this->repository->find(['id' => $uuidV7]); + + if ($accounts instanceof Bitrix24AccountInterface) { + // Если это один объект, количество будет 1 + $count = 1; + } else { + // Если ничего не найдено, количество будет 0 + $count = 0; + } + + $this->assertEquals(1, $count); + } } diff --git a/tests/Unit/Bitrix24Accounts/UseCase/RenewAuthToken/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/RenewAuthToken/CommandTest.php deleted file mode 100644 index 4a12cf4..0000000 --- a/tests/Unit/Bitrix24Accounts/UseCase/RenewAuthToken/CommandTest.php +++ /dev/null @@ -1,42 +0,0 @@ -withStatus(Bitrix24AccountStatus::new) - ->build(); - - $newAuthToken = new AuthToken('new_1', 'new_2', 3600); - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Bitrix24 User ID must be a positive integer.'); - - new Command( - new RenewedAuthToken( - $newAuthToken, - $bitrix24Account->getMemberId(), - 'https://client-endpoint.com', - 'https://server-endpoint.com', - ApplicationStatus::subscription(), - $bitrix24Account->getDomainUrl() - ), - ); - } -} \ No newline at end of file From 92347d8abb5b3ad7069955c5c64e2a43d8b75039 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sun, 5 Jan 2025 22:08:33 +0300 Subject: [PATCH 6/6] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B2=20=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=80=D0=BD=D1=83=D1=8E=20=D1=83=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2=D0=BA=D1=83=20=D0=B0=D0=BA=D0=BA=D0=B0?= =?UTF-8?q?=D1=83=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UseCase/InstallStart/Handler.php | 15 ++- .../Builders/Bitrix24AccountBuilder.php | 8 +- .../UseCase/InstallStart/HandlerTest.php | 124 ++++++++---------- .../UseCase/Uninstall/HandlerTest.php | 5 - 4 files changed, 78 insertions(+), 74 deletions(-) diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php index 32fe1c6..981149b 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php @@ -7,6 +7,7 @@ use Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account; use Bitrix24\Lib\Services\Flusher; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus; +use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; use Carbon\CarbonImmutable; use Psr\Log\LoggerInterface; @@ -57,8 +58,18 @@ public function handle(Command $command): void 'domain_url' => $command->domainUrl, 'member_id' => $command->memberId, ]); - } - + }else{ + $this->logger->info('Bitrix24Accounts.InstallStart.AlreadyExists', + [ + 'id' => $command->uuid->toRfc4122(), + 'domain_url' => $command->domainUrl, + 'member_id' => $command->memberId, + ] + ); + throw new Bitrix24AccountNotFoundException( + sprintf('bitrix24account with uuid "%s" already exists', $command->uuid->toRfc4122()) + ); + } } } diff --git a/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php b/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php index 4e6fc7a..813c644 100644 --- a/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php +++ b/tests/Functional/Bitrix24Accounts/Builders/Bitrix24AccountBuilder.php @@ -45,7 +45,7 @@ class Bitrix24AccountBuilder private readonly int $applicationVersion; - private readonly Scope $applicationScope; + private Scope $applicationScope; private ?string $applicationToken = null; @@ -75,6 +75,12 @@ public function withDomainUrl(string $domainUrl): self return $this; } + public function withApplicationScope(Scope $applicationScope): self + { + $this->applicationScope = $applicationScope; + return $this; + } + public function withApplicationToken(string $applicationToken): self { $this->applicationToken = $applicationToken; diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php index 5c449fc..877cf5e 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php @@ -17,6 +17,7 @@ use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository; 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\Events\Bitrix24AccountCreatedEvent; use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException; @@ -72,86 +73,82 @@ protected function setUp(): void #[Test] public function testInstallStartHappyPath(): void { - $uuidV7 = Uuid::v7(); - $b24UserId = random_int(1, 100_000); - $isB24UserAdmin = true; - $b24MemberId = Uuid::v7()->toRfc4122(); - $b24DomainUrl = Uuid::v7()->toRfc4122().'-test.bitrix24.com'; - $authToken = new AuthToken('old_1', 'old_2', 3600); - $appVersion = 1; - $scope = new Scope(['crm']); + $bitrix24AccountBuilder = (new Bitrix24AccountBuilder()) + ->withApplicationScope(new Scope(['crm'])) + ->build(); + $this->handler->handle( new Bitrix24Accounts\UseCase\InstallStart\Command( - $uuidV7, - $b24UserId, - $isB24UserAdmin, - $b24MemberId, - $b24DomainUrl, - $authToken, - $appVersion, - $scope + $bitrix24AccountBuilder->getId(), + $bitrix24AccountBuilder->getBitrix24UserId(), + $bitrix24AccountBuilder->isBitrix24UserAdmin(), + $bitrix24AccountBuilder->getMemberId(), + $bitrix24AccountBuilder->getDomainUrl(), + $bitrix24AccountBuilder->getAuthToken(), + $bitrix24AccountBuilder->getApplicationVersion(), + $bitrix24AccountBuilder->getApplicationScope() ) ); - $bitrix24Account = $this->repository->getById($uuidV7); + $bitrix24Account = $this->repository->getById($bitrix24AccountBuilder->getId()); $this->assertEquals( - $b24UserId, + $bitrix24AccountBuilder->getBitrix24UserId(), $bitrix24Account->getBitrix24UserId(), sprintf( 'Expected the property value to be "%s", but got "%s"', - $b24UserId, + $bitrix24AccountBuilder->getBitrix24UserId(), $bitrix24Account->getBitrix24UserId() ) ); $this->assertEquals( - $isB24UserAdmin, + $bitrix24AccountBuilder->isBitrix24UserAdmin(), $bitrix24Account->isBitrix24UserAdmin(), sprintf( 'Expected the property value to be "%s", but got "%s"', - $isB24UserAdmin, + $bitrix24AccountBuilder->isBitrix24UserAdmin(), $bitrix24Account->isBitrix24UserAdmin() ) ); $this->assertEquals( - $b24MemberId, + $bitrix24AccountBuilder->getMemberId(), $bitrix24Account->getMemberId(), sprintf( 'Expected the property value to be "%s", but got "%s"', - $b24MemberId, + $bitrix24AccountBuilder->getMemberId(), $bitrix24Account->getMemberId() ) ); $this->assertEquals( - $b24DomainUrl, + $bitrix24AccountBuilder->getDomainUrl(), $bitrix24Account->getDomainUrl(), sprintf( 'Expected the property value to be "%s", but got "%s"', - $b24DomainUrl, + $bitrix24AccountBuilder->getDomainUrl(), $bitrix24Account->getDomainUrl() ) ); $this->assertEquals( - $authToken, + $bitrix24AccountBuilder->getAuthToken(), $bitrix24Account->getAuthToken(), 'Object not equals' ); $this->assertEquals( - $appVersion, + $bitrix24AccountBuilder->getApplicationVersion(), $bitrix24Account->getApplicationVersion(), sprintf( 'Expected the property value to be "%s", but got "%s"', - $appVersion, + $bitrix24AccountBuilder->getApplicationVersion(), $bitrix24Account->getApplicationVersion() ) ); $this->assertEquals( - $scope, + $bitrix24AccountBuilder->getApplicationScope(), $bitrix24Account->getApplicationScope(), 'Object not equals' ); @@ -168,54 +165,49 @@ public function testInstallStartHappyPath(): void ); } + /** + * @throws Bitrix24AccountNotFoundException + * @throws InvalidArgumentException + * @throws UnknownScopeCodeException + */ #[Test] public function testCreateExistingAccount(): 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']); + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withApplicationScope(new Scope(['crm'])) + ->build(); + + $this->handler->handle( new Bitrix24Accounts\UseCase\InstallStart\Command( - $uuidV7, - $b24UserId, - $isB24UserAdmin, - $b24MemberId, - $b24DomainUrl, - $authToken, - $appVersion, - $scope + $bitrix24Account->getId(), + $bitrix24Account->getBitrix24UserId(), + $bitrix24Account->isBitrix24UserAdmin(), + $bitrix24Account->getMemberId(), + $bitrix24Account->getDomainUrl(), + $bitrix24Account->getAuthToken(), + $bitrix24Account->getApplicationVersion(), + $bitrix24Account->getApplicationScope() ) ); + + $this->expectException(Bitrix24AccountNotFoundException::class); + $this->expectExceptionMessage( + sprintf('bitrix24account with uuid "%s" already exists', $bitrix24Account->getId()) + ); + $this->handler->handle( new Bitrix24Accounts\UseCase\InstallStart\Command( - $uuidV7, - $b24UserId, - $isB24UserAdmin, - $b24MemberId, - $b24DomainUrl, - $authToken, - $appVersion, - $scope + $bitrix24Account->getId(), + $bitrix24Account->getBitrix24UserId(), + $bitrix24Account->isBitrix24UserAdmin(), + $bitrix24Account->getMemberId(), + $bitrix24Account->getDomainUrl(), + $bitrix24Account->getAuthToken(), + $bitrix24Account->getApplicationVersion(), + $bitrix24Account->getApplicationScope() ) ); - - $accounts = $this->repository->find(['id' => $uuidV7]); - - if ($accounts instanceof Bitrix24AccountInterface) { - // Если это один объект, количество будет 1 - $count = 1; - } else { - // Если ничего не найдено, количество будет 0 - $count = 0; - } - - $this->assertEquals(1, $count); - } } diff --git a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php index 5d6c113..25a5118 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php @@ -13,8 +13,6 @@ 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; use Bitrix24\Lib\Bitrix24Accounts; @@ -24,10 +22,7 @@ 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; use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; -use Carbon\CarbonImmutable; use Override; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Test;