From 556720d328d885037ed6568d419e7aeb18739077 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Mon, 10 Feb 2025 23:15:59 +0300 Subject: [PATCH 1/3] =?UTF-8?q?-=20=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=B4=D0=BE=D0=BC=D0=B5=D0=BD=20-=20=D0=9F?= =?UTF-8?q?=D0=BE=D1=84=D0=B8=D0=BA=D1=81=D0=B8=D0=BB=20=D1=8E=D0=BD=D0=B8?= =?UTF-8?q?=D1=82=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD=D0=B0=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=B8=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B4=D0=BE=D0=BC=D0=B5=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UseCase/ChangeDomainUrl/Command.php | 8 +- .../UseCase/InstallFinish/Command.php | 7 +- .../UseCase/InstallStart/Command.php | 4 +- src/Bitrix24Accounts/ValueObjects/Domain.php | 7 +- .../UseCase/ChangeDomainUrl/CommandTest.php | 142 +++++++++++------- .../UseCase/InstallFinish/CommandTest.php | 7 +- .../UseCase/Uninstall/CommandTest.php | 2 +- 7 files changed, 99 insertions(+), 78 deletions(-) diff --git a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php index e93ce70..2dcc847 100644 --- a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php +++ b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php @@ -8,15 +8,15 @@ readonly class Command { - public string $oldDomain; + public Domain $oldDomain; - public string $newDomain; + public Domain $newDomain; public function __construct( Domain $oldDomain, Domain $newDomain ) { - $this->oldDomain = $oldDomain->getValue(); - $this->newDomain = $newDomain->getValue(); + $this->oldDomain = $oldDomain; + $this->newDomain = $newDomain; } } diff --git a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php index 2ab9f21..1e253b3 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php @@ -8,7 +8,7 @@ readonly class Command { - public string $domain; + public Domain $domain; public function __construct( public string $applicationToken, @@ -16,8 +16,9 @@ public function __construct( Domain $domain, public int $bitrix24UserId, ) { + $this->domain = $domain; $this->validate(); - $this->domain = $domain->getValue(); + } private function validate(): void @@ -26,7 +27,7 @@ private function validate(): void throw new \InvalidArgumentException('Application token cannot be empty.'); } - if ('' === $this->memberId || '0' === $this->memberId) { + if ('' === $this->memberId) { throw new \InvalidArgumentException('Member ID cannot be empty.'); } diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php index 3b95869..936169e 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php @@ -11,7 +11,7 @@ readonly class Command { - public string $domain; + public Domain $domain; public function __construct( public Uuid $uuid, @@ -24,7 +24,7 @@ public function __construct( public Scope $applicationScope ) { $this->validate(); - $this->domain = $domain->getValue(); + $this->domain = $domain; } private function validate(): void diff --git a/src/Bitrix24Accounts/ValueObjects/Domain.php b/src/Bitrix24Accounts/ValueObjects/Domain.php index f958a5a..a399ee7 100644 --- a/src/Bitrix24Accounts/ValueObjects/Domain.php +++ b/src/Bitrix24Accounts/ValueObjects/Domain.php @@ -6,7 +6,7 @@ readonly class Domain { - private string $value; + public string $value; public function __construct(string $domain) { @@ -14,11 +14,6 @@ public function __construct(string $domain) $this->value = $domain; } - public function getValue(): string - { - return $this->value; - } - private function validate(string $domain): void { // Регулярное выражение для проверки допустимых символов (латиница и кириллица) diff --git a/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php index 23b1435..bb3646f 100644 --- a/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php +++ b/tests/Unit/Bitrix24Accounts/UseCase/ChangeDomainUrl/CommandTest.php @@ -19,98 +19,124 @@ class CommandTest extends TestCase { #[Test] - #[DataProvider('dataForValidateInvalidDomain')] + #[DataProvider('dataForValidateValidDomain')] public function testValidateDomain( - array $arrDomains, - ?string $expectedException + string $oldDomain, + string $newDomain, ): void { - - $exceptionCount = 0; - foreach ($arrDomains as $arrDomain) { - try { - $oldDomain = new Domain($arrDomain['oldDomain']); - $newDomain = new Domain($arrDomain['newDomain']); - new Command($oldDomain, $newDomain); - } catch (\InvalidArgumentException) { - // Увеличиваем счетчик при каждом выбросе исключения - $exceptionCount++; - } - } - - // Проверяем, сколько исключений было выброшено - if ($expectedException !== null) { - $this->assertEquals(6, $exceptionCount, 'Expected 6 invalid exception and received ' . $exceptionCount); - } else { - // Если ожидается отсутствие исключений, проверяем что их не было - $this->assertEquals(0, $exceptionCount, sprintf('No exceptions were expected but %d were thrown.', $exceptionCount)); - } - + new Domain($oldDomain); + new Domain($newDomain); + $this->assertTrue(true); } #[Test] - #[DataProvider('dataForValidateValidDomain')] + #[DataProvider('dataForValidateInvalidDomain')] public function testValidateInvalidDomain( - array $arrDomains, - ?string $expectedException + string $oldDomain, + string $newDomain, + ?string $expectedException, + ?string $expectedExceptionMessage ): void { - $exceptionCount = 0; - foreach ($arrDomains as $arrDomain) { - try { - $oldDomain = new Domain($arrDomain['oldDomain']); - $newDomain = new Domain($arrDomain['newDomain']); - new Command($oldDomain, $newDomain); - } catch (\InvalidArgumentException) { - // Увеличиваем счетчик при каждом выбросе исключения - $exceptionCount++; - } + if ($expectedException !== null) { + $this->expectException($expectedException); } - // Проверяем, сколько исключений было выброшено - if ($expectedException !== null) { - $this->assertEquals(6, $exceptionCount, 'Expected 6 invalid exception and received ' . $exceptionCount); - } else { - // Если ожидается отсутствие исключений, проверяем что их не было - $this->assertEquals(0, $exceptionCount, sprintf('No exceptions were expected but %d were thrown.', $exceptionCount)); + if ($expectedExceptionMessage !== null) { + $this->expectExceptionMessage($expectedExceptionMessage); } + new Domain($oldDomain); + new Domain($newDomain); + } public static function dataForValidateValidDomain(): \Generator { // Примеры допустимых доменов - $arrValidDomains = [ + /*$arrValidDomains = [ ['oldDomain' => 'example.com', 'newDomain' => 'example.org'], ['oldDomain' => 'пример.рф', 'newDomain' => 'пример.рус'], ['oldDomain' => 'test-site.org', 'newDomain' => 'test-site.ru'], ['oldDomain' => 'valid-domain.co.uk', 'newDomain' => 'valid-domain.net'], ['oldDomain' => 'subdomain.example.com', 'newDomain' => 'subdomain2.example.com'], ['oldDomain' => 'тест.рус', 'newDomain' => 'тест2.рус'], // Пример с кириллицей + ];*/ + + yield 'validDomain1' => [ + 'example.com', + 'example.org', ]; - yield 'validDomain' => [ - $arrValidDomains, // Оборачиваем в массив для передачи в testValidCommand - null // Здесь исключение не ожидается + yield 'validDomain2' => [ + 'пример.рф', + 'пример.рус', + ]; + + yield 'validDomain3' => [ + 'test-site.org', + 'test-site.ru', + ]; + + yield 'validDomain4' => [ + 'valid-domain.co.uk', + 'valid-domain.net', + ]; + + yield 'validDomain5' => [ + 'subdomain.example.com', + 'subdomain2.example.com', + ]; + + yield 'validDomain6' => [ + 'тест.рус', + 'тест2.рус', ]; } public static function dataForValidateInvalidDomain(): \Generator { + yield 'invalidDomain1' => [ + 'invalid_domain.com', // Неправильный формат (подчеркивание) + 'valid.com', + \InvalidArgumentException::class, + sprintf('Invalid domain: %s', 'invalid_domain.com') + ]; + + yield 'invalidDomain2' => [ + '-invalid.com', // Домен не может начинаться с дефиса + 'valid.com', + \InvalidArgumentException::class, + sprintf('Invalid domain: %s', '-invalid.com') + ]; + + yield 'invalidDomain3' => [ + 'invalid-.com', // Домен не может заканчиваться на дефис + 'valid.com', + \InvalidArgumentException::class, + sprintf('Invalid domain: %s', 'invalid-.com') + ]; + + yield 'invalidDomain4' => [ + '123.456.789.0', // Неправильный формат (IP-адрес) + 'valid.com', + \InvalidArgumentException::class, + sprintf('Invalid domain: %s', '123.456.789.0') + ]; - // Примеры недопустимых доменов - $arrInvalidDomains = [ - ['oldDomain' => 'invalid_domain.com', 'newDomain' => 'valid.com'], // Неправильный формат (подчеркивание) - ['oldDomain' => '-invalid.com', 'newDomain' => 'valid.com'], // Домен не может начинаться с дефиса - ['oldDomain' => 'invalid-.com', 'newDomain' => 'valid.com'], // Домен не может заканчиваться на дефис - ['oldDomain' => '123.456.789.0', 'newDomain' => 'valid.com'], // Неправильный формат (IP-адрес) - ['oldDomain' => 'example..com', 'newDomain' => 'valid.com'], // Два подряд идущих точки - ['oldDomain' => 'example.c', 'newDomain' => 'valid.com'] // Слишком короткая доменная зона + yield 'invalidDomain5' => [ + 'example..com', // Два подряд идущих точки + 'valid.com', + \InvalidArgumentException::class, + sprintf('Invalid domain: %s', 'example..com') ]; - yield 'invalidDomain' => [ - $arrInvalidDomains, // Оборачиваем в массив для передачи в testValidCommand - \InvalidArgumentException::class + yield 'invalidDomain6' => [ + 'example.c', // Слишком короткая доменная зона + 'valid.com', + \InvalidArgumentException::class, + sprintf('Invalid domain: %s', 'example.c') ]; } } diff --git a/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php index 71d250f..44559ef 100644 --- a/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php +++ b/tests/Unit/Bitrix24Accounts/UseCase/InstallFinish/CommandTest.php @@ -32,6 +32,7 @@ public function testValidCommand( ?string $expectedExceptionMessage, ): void { + if (null !== $expectedException) { $this->expectException($expectedException); } @@ -41,14 +42,12 @@ public function testValidCommand( } $domain = new Domain($domainUrl); - - new Command( + new Command( $applicationToken, $memberId, $domain, $bitrix24UserId ); - } public static function dataForCommand(): \Generator @@ -76,7 +75,7 @@ public static function dataForCommand(): \Generator 'Member ID cannot be empty.' ]; - yield 'validDomain' => [ + yield 'invalidDomain' => [ $applicationToken, $bitrix24Account->getMemberId(), '', diff --git a/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php b/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php index 817dbb0..257a99d 100644 --- a/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php +++ b/tests/Unit/Bitrix24Accounts/UseCase/Uninstall/CommandTest.php @@ -23,7 +23,7 @@ public function testValidTokenForCommand( string $applicationToken, ): void { $command = new Command($applicationToken); - $this->assertInstanceOf(Command::class, $command); + $this->assertEquals($applicationToken,$command->applicationToken); } #[Test] From c80df26044d3583fac8380bda2ae721b806806b6 Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sat, 15 Feb 2025 23:49:58 +0300 Subject: [PATCH 2/3] =?UTF-8?q?-=20=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=B4=D0=BE=D0=BC=D0=B5=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UseCase/ChangeDomainUrl/Command.php | 12 +----------- .../UseCase/ChangeDomainUrl/Handler.php | 8 ++++---- .../UseCase/InstallFinish/Command.php | 6 +----- .../UseCase/InstallFinish/Handler.php | 2 +- .../UseCase/InstallStart/Command.php | 5 +---- .../UseCase/InstallStart/Handler.php | 2 +- 6 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php index 2dcc847..0d640a8 100644 --- a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php +++ b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php @@ -8,15 +8,5 @@ readonly class Command { - public Domain $oldDomain; - - public Domain $newDomain; - - public function __construct( - Domain $oldDomain, - Domain $newDomain - ) { - $this->oldDomain = $oldDomain; - $this->newDomain = $newDomain; - } + public function __construct(public Domain $oldDomain, public Domain $newDomain) {} } diff --git a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php index d6322ed..5896f9e 100644 --- a/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php +++ b/src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php @@ -21,14 +21,14 @@ public function __construct( public function handle(Command $command): void { $this->logger->info('Bitrix24Accounts.ChangeDomainUrl.start', [ - 'b24_domain_url_old' => $command->oldDomain, - 'b24_domain_url_new' => $command->newDomain, + 'b24_domain_url_old' => $command->oldDomain->value, + 'b24_domain_url_new' => $command->newDomain->value, ]); /** @var AggregateRootEventsEmitterInterface[]|Bitrix24AccountInterface[] $accounts */ - $accounts = $this->bitrix24AccountRepository->findByDomain($command->oldDomain); + $accounts = $this->bitrix24AccountRepository->findByDomain($command->oldDomain->value); foreach ($accounts as $account) { - $account->changeDomainUrl($command->newDomain); + $account->changeDomainUrl($command->newDomain->value); $this->bitrix24AccountRepository->save($account); } diff --git a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php index 1e253b3..979b0e1 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Command.php @@ -8,17 +8,13 @@ readonly class Command { - public Domain $domain; - public function __construct( public string $applicationToken, public string $memberId, - Domain $domain, + public Domain $domain, public int $bitrix24UserId, ) { - $this->domain = $domain; $this->validate(); - } private function validate(): void diff --git a/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php b/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php index 1a763b3..e5b805d 100644 --- a/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php @@ -37,7 +37,7 @@ public function handle(Command $command): void ]); /** @var AggregateRootEventsEmitterInterface|Bitrix24AccountInterface $bitrix24Account */ - $bitrix24Account = $this->getSingleAccountByMemberId($command->domain, $command->memberId, $command->bitrix24UserId); + $bitrix24Account = $this->getSingleAccountByMemberId($command->domain->value, $command->memberId, $command->bitrix24UserId); $bitrix24Account->applicationInstalled($command->applicationToken); diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php index 936169e..a63c792 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Command.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Command.php @@ -11,20 +11,17 @@ readonly class Command { - public Domain $domain; - public function __construct( public Uuid $uuid, public int $bitrix24UserId, public bool $isBitrix24UserAdmin, public string $memberId, - Domain $domain, + public Domain $domain, public AuthToken $authToken, public int $applicationVersion, public Scope $applicationScope ) { $this->validate(); - $this->domain = $domain; } private function validate(): void diff --git a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php index 078d78b..01e7ee0 100644 --- a/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php +++ b/src/Bitrix24Accounts/UseCase/InstallStart/Handler.php @@ -33,7 +33,7 @@ public function handle(Command $command): void $command->bitrix24UserId, $command->isBitrix24UserAdmin, $command->memberId, - $command->domain, + $command->domain->value, Bitrix24AccountStatus::new, $command->authToken, new CarbonImmutable(), From 024e6912d2c14c127a80791c9e9d5ee31125c78d Mon Sep 17 00:00:00 2001 From: KarlsonComplete Date: Sun, 16 Feb 2025 13:52:55 +0300 Subject: [PATCH 3/3] =?UTF-8?q?-=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20useCase=20=D0=BD=D0=B0=20=D0=BE=D0=B1=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=D0=B5=D1=80=D1=81?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=B8=20=D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UseCase/UpdateVersion/Command.php | 39 +++++ .../UseCase/UpdateVersion/Handler.php | 92 ++++++++++ .../UseCase/InstallStart/HandlerTest.php | 5 - .../UseCase/UpdateVersion/HandlerTest.php | 157 ++++++++++++++++++ 4 files changed, 288 insertions(+), 5 deletions(-) create mode 100644 src/Bitrix24Accounts/UseCase/UpdateVersion/Command.php create mode 100644 src/Bitrix24Accounts/UseCase/UpdateVersion/Handler.php create mode 100644 tests/Functional/Bitrix24Accounts/UseCase/UpdateVersion/HandlerTest.php diff --git a/src/Bitrix24Accounts/UseCase/UpdateVersion/Command.php b/src/Bitrix24Accounts/UseCase/UpdateVersion/Command.php new file mode 100644 index 0000000..ed6c4f0 --- /dev/null +++ b/src/Bitrix24Accounts/UseCase/UpdateVersion/Command.php @@ -0,0 +1,39 @@ +validate(); + } + + private function validate(): void + { + if ($this->bitrix24UserId <= 0) { + throw new \InvalidArgumentException('Bitrix24 User ID must be a positive integer.'); + } + + if ('' === $this->memberId) { + throw new \InvalidArgumentException('Member ID must be a non-empty string.'); + } + + if ($this->newApplicationVersion <= 0) { + throw new \InvalidArgumentException('Application version must be a positive integer.'); + } + } +} diff --git a/src/Bitrix24Accounts/UseCase/UpdateVersion/Handler.php b/src/Bitrix24Accounts/UseCase/UpdateVersion/Handler.php new file mode 100644 index 0000000..997e500 --- /dev/null +++ b/src/Bitrix24Accounts/UseCase/UpdateVersion/Handler.php @@ -0,0 +1,92 @@ +logger->info('Bitrix24Accounts.UpdateVersion.start', [ + 'uuid' => $command->uuid, + 'bitrix24_user_id' => $command->bitrix24UserId, + 'is_bitrix24UserAdmin' => $command->isBitrix24UserAdmin, + 'member_id' => $command->memberId, + 'auth_token' => $command->authToken, + 'new_application_version' => $command->newApplicationVersion, + 'new_application_scope' => $command->newApplicationScope, + ]); + + $accounts = $this->bitrix24AccountRepository->findByMemberId( + $command->memberId, + Bitrix24AccountStatus::active, + $command->bitrix24UserId, + ); + + if ([] !== $accounts) { + /** @var AggregateRootEventsEmitterInterface|Bitrix24AccountInterface $bitrix24Account */ + $bitrix24Account = $accounts[0]; + $bitrix24Account->updateApplicationVersion( + $command->authToken, + $command->bitrix24UserId, + $command->newApplicationVersion, + $command->newApplicationScope, + ); + + $this->bitrix24AccountRepository->save($bitrix24Account); + $this->flusher->flush($bitrix24Account); + + $this->logger->info('Bitrix24Accounts.UpdateVersion.finish', [ + 'uuid' => $command->uuid, + 'bitrix24_user_id' => $command->bitrix24UserId, + 'is_bitrix24UserAdmin' => $command->isBitrix24UserAdmin, + 'member_id' => $command->memberId, + 'auth_token' => $command->authToken, + 'new_application_version' => $command->newApplicationVersion, + 'new_application_scope' => $command->newApplicationScope, + ]); + } else { + $this->logger->info( + 'Bitrix24Accounts.UpdateVersion.NotFoundAccount', + [ + 'uuid' => $command->uuid, + 'bitrix24_user_id' => $command->bitrix24UserId, + 'is_bitrix24UserAdmin' => $command->isBitrix24UserAdmin, + 'member_id' => $command->memberId, + 'auth_token' => $command->authToken, + 'new_application_version' => $command->newApplicationVersion, + 'new_application_scope' => $command->newApplicationScope, + ] + ); + + throw new MultipleBitrix24AccountsFoundException( + sprintf( + 'bitrix24account not found by memberId %s, status %s and bitrix24UserId %s ', + $command->memberId, + 'active', + $command->bitrix24UserId + ) + ); + } + } +} diff --git a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php index 8fe33ba..0090188 100644 --- a/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php +++ b/tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php @@ -206,9 +206,4 @@ public function testInstallExistingAccount(): void ) ); } - - - - #[Test] - public function testUpdateAppVersion(): void {} } diff --git a/tests/Functional/Bitrix24Accounts/UseCase/UpdateVersion/HandlerTest.php b/tests/Functional/Bitrix24Accounts/UseCase/UpdateVersion/HandlerTest.php new file mode 100644 index 0000000..8147815 --- /dev/null +++ b/tests/Functional/Bitrix24Accounts/UseCase/UpdateVersion/HandlerTest.php @@ -0,0 +1,157 @@ + + * + * For the full copyright and license information, please view the MIT-LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\UseCase\UpdateVersion; + +use Bitrix24\Lib\Bitrix24Accounts; +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\Exceptions\MultipleBitrix24AccountsFoundException; +use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface; +use Bitrix24\SDK\Core\Credentials\Scope; +use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; +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; + +/** + * @internal + */ +#[CoversClass(Bitrix24Accounts\UseCase\UpdateVersion\Handler::class)] +class HandlerTest extends TestCase +{ + private Bitrix24Accounts\UseCase\UpdateVersion\Handler $handler; + + private Flusher $flusher; + + private Bitrix24AccountRepositoryInterface $repository; + + 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\UpdateVersion\Handler( + $this->repository, + $this->flusher, + new NullLogger(),); + + } + + #[Test] + public function testSuccessUpdateVersion(): void + { + $applicationToken = Uuid::v7()->toRfc4122(); + + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withStatus(Bitrix24AccountStatus::active) + ->withApplicationToken($applicationToken) + ->build(); + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + $this->handler->handle(new Bitrix24Accounts\UseCase\UpdateVersion\Command( + $bitrix24Account->getId(), + $bitrix24Account->getBitrix24UserId(), + $bitrix24Account->isBitrix24UserAdmin(), + $bitrix24Account->getMemberId(), + $bitrix24Account->getAuthToken(), + 2, + new Scope(['crm','log']) + )); + + $updated = $this->repository->getById($bitrix24Account->getId()); + + $this->assertEquals(2,$updated->getApplicationVersion(), 'expected application version is 2'); + $this->assertEquals(new Scope(['crm','log']),$updated->getApplicationScope(), 'application Scope is not equal'); + } + + #[Test] + public function testNotFoundBitrix24AccountForUpdateVersion(): void + { + $applicationToken = Uuid::v7()->toRfc4122(); + + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withStatus(Bitrix24AccountStatus::active) + ->withApplicationToken($applicationToken) + ->build(); + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + $this->expectException(MultipleBitrix24AccountsFoundException::class); + $this->expectExceptionMessage(sprintf('bitrix24account not found by memberId %s, status %s and bitrix24UserId %s ', + $bitrix24Account->getMemberId(), + 'active', + 3558) + ); + + $this->handler->handle(new Bitrix24Accounts\UseCase\UpdateVersion\Command( + $bitrix24Account->getId(), + 3558, + $bitrix24Account->isBitrix24UserAdmin(), + $bitrix24Account->getMemberId(), + $bitrix24Account->getAuthToken(), + 2, + new Scope(['crm','log']) + )); + } + + #[Test] + public function testNotValidVersionForUpdateVersion(): void + { + $bitrix24Account = (new Bitrix24AccountBuilder()) + ->withStatus(Bitrix24AccountStatus::active) + ->build(); + + + $this->repository->save($bitrix24Account); + $this->flusher->flush(); + + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + sprintf( + 'you cannot downgrade application version or set some version, current version «%s», but you try to upgrade to «%s»', + $bitrix24Account->getApplicationVersion(), + '1' + ) + ); + + $this->handler->handle(new Bitrix24Accounts\UseCase\UpdateVersion\Command( + $bitrix24Account->getId(), + $bitrix24Account->getBitrix24UserId(), + $bitrix24Account->isBitrix24UserAdmin(), + $bitrix24Account->getMemberId(), + $bitrix24Account->getAuthToken(), + 1, + new Scope(['crm','log']) + )); + } +}