Skip to content

Commit

Permalink
Fix formatting and simplify tests
Browse files Browse the repository at this point in the history
Adjusted the whitespace formatting in Handler.php and Bitrix24Account.php for improved readability. Refactored HandlerTest.php to use Bitrix24AccountBuilder for cleaner test setup and added a new test case for handling domain URL changes across multiple accounts.

Signed-off-by: mesilov <[email protected]>
  • Loading branch information
mesilov committed Nov 10, 2024
1 parent 33f6356 commit e648cba
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 96 deletions.
55 changes: 31 additions & 24 deletions src/Bitrix24Accounts/Entity/Bitrix24Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,20 @@ class Bitrix24Account implements Bitrix24AccountInterface, AggregateRootEventsEm
*/
private array $events = [];


public function __construct(
private Uuid $id,
private readonly int $bitrix24UserId,
private readonly bool $isBitrix24UserAdmin,
private Uuid $id,
private readonly int $bitrix24UserId,
private readonly bool $isBitrix24UserAdmin,
/** bitrix24 portal unique id */
private readonly string $memberId,
private string $domainUrl,
private Bitrix24AccountStatus $status,
AuthToken $authToken,
private readonly string $memberId,
private string $domainUrl,
private Bitrix24AccountStatus $status,
AuthToken $authToken,
private readonly CarbonImmutable $createdAt,
private CarbonImmutable $updatedAt,
private int $applicationVersion,
Scope $applicationScope,
)
{
private CarbonImmutable $updatedAt,
private int $applicationVersion,
Scope $applicationScope,
) {
$this->authToken = $authToken;
$this->accessToken = $authToken->accessToken;
$this->refreshToken = $authToken->refreshToken;
Expand Down Expand Up @@ -161,7 +159,7 @@ public function getApplicationScope(): Scope
#[Override]
public function changeDomainUrl(string $newDomainUrl): void
{
if ($newDomainUrl === '') {
if ('' === $newDomainUrl) {
throw new InvalidArgumentException('new domain url cannot be empty');
}

Expand Down Expand Up @@ -193,10 +191,11 @@ public function applicationInstalled(string $applicationToken): void
if (Bitrix24AccountStatus::new !== $this->status) {
throw new InvalidArgumentException(sprintf(
'for finish installation bitrix24 account must be in status «new», current status - «%s»',
$this->status->name));
$this->status->name
));
}

if ($applicationToken === '') {
if ('' === $applicationToken) {
throw new InvalidArgumentException('application token cannot be empty');
}

Expand All @@ -215,14 +214,15 @@ public function applicationInstalled(string $applicationToken): void
#[Override]
public function applicationUninstalled(string $applicationToken): void
{
if ($applicationToken === '') {
if ('' === $applicationToken) {
throw new InvalidArgumentException('application token cannot be empty');
}

if (Bitrix24AccountStatus::active !== $this->status) {
throw new InvalidArgumentException(sprintf(
'for uninstall account must be in status «active», current status - «%s»',
$this->status->name));
$this->status->name
));
}

if ($this->applicationToken !== $applicationToken) {
Expand Down Expand Up @@ -275,13 +275,16 @@ public function updateApplicationVersion(int $version, ?Scope $newScope): void

if ($this->applicationVersion >= $version) {
throw new InvalidArgumentException(
sprintf('you cannot downgrade application version or set some version, current version «%s», but you try to upgrade to «%s»',
sprintf(
'you cannot downgrade application version or set some version, current version «%s», but you try to upgrade to «%s»',
$this->applicationVersion,
$version));
$version
)
);
}

$this->applicationVersion = $version;
if ($newScope instanceof \Bitrix24\SDK\Core\Credentials\Scope) {
if ($newScope instanceof Scope) {
$this->applicationScope = $newScope->getScopeCodes();
}

Expand All @@ -300,8 +303,11 @@ public function markAsActive(?string $comment): void
{
if (Bitrix24AccountStatus::blocked !== $this->status) {
throw new InvalidArgumentException(
sprintf('you can activate account only in status blocked, now account in status %s',
$this->status->name));
sprintf(
'you can activate account only in status «blocked», now account in status «%s»',
$this->status->name
)
);
}

$this->status = Bitrix24AccountStatus::active;
Expand Down Expand Up @@ -342,6 +348,7 @@ public function emitEvents(): array
{
$events = $this->events;
$this->events = [];

return $events;
}
}
}
20 changes: 8 additions & 12 deletions src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
readonly class Handler
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private EventDispatcherInterface $eventDispatcher,
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger
)
{
}
private Flusher $flusher,
private LoggerInterface $logger
) {}

/**
* @throws Bitrix24AccountNotFoundException
Expand All @@ -34,15 +32,15 @@ public function handle(Command $command): void
'b24_domain_url' => $command->domainUrl,
'b24_member_id' => $command->memberId,
'b24_application_id' => $command->applicationToken,
'b24_user_id' => $command->bitrix24UserId
'b24_user_id' => $command->bitrix24UserId,
]);

$accounts = $this->bitrix24AccountRepository->findByMemberId(
$command->memberId,
Bitrix24AccountStatus::new,
$command->bitrix24UserId
);
if ($accounts === []) {
if ([] === $accounts) {
throw new Bitrix24AccountNotFoundException(sprintf(
'bitrix24 account for domain %s with member id %s in status «new» not found',
$command->domainUrl,
Expand All @@ -59,9 +57,7 @@ public function handle(Command $command): void
}

$targetAccount = $accounts[0];
/**
* @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $targetAccount
*/
// @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $targetAccount
$targetAccount->applicationInstalled($command->applicationToken);

$this->bitrix24AccountRepository->save($targetAccount);
Expand All @@ -72,4 +68,4 @@ public function handle(Command $command): void

$this->logger->debug('Bitrix24Accounts.InstallFinish.Finish');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/**
* This file is part of the bitrix24-php-lib package.
*
* © Maksim Mesilov <[email protected]>
*
* 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\Builders;

use Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus;
use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface;
use Bitrix24\SDK\Core\Credentials\AuthToken;
use Bitrix24\SDK\Core\Credentials\Scope;
use Carbon\CarbonImmutable;
use Symfony\Component\Uid\Uuid;

class Bitrix24AccountBuilder
{
private Uuid $id;
private int $bitrix24UserId;
private bool $isBitrix24UserAdmin;
/** bitrix24 portal unique id */
private string $memberId;
private string $domainUrl;
private Bitrix24AccountStatus $status;
private AuthToken $authToken;
private CarbonImmutable $createdAt;
private CarbonImmutable $updatedAt;
private int $applicationVersion;
private Scope $applicationScope;

public function __construct()
{
$this->id = Uuid::v7();
$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->status = Bitrix24AccountStatus::active;
$this->authToken = new AuthToken('old_1', 'old_2', 3600);
$this->createdAt = CarbonImmutable::now();
$this->updatedAt = CarbonImmutable::now();
$this->applicationVersion = 1;
$this->applicationScope = new Scope();
}

public function withMemberId(string $memberId): self
{
$this->memberId = $memberId;
return $this;
}

public function withDomainUrl(string $domainUrl): self
{
$this->domainUrl = $domainUrl;
return $this;
}

public function withStatus(Bitrix24AccountStatus $status): self
{
$this->status = $status;
return $this;
}

public function build(): AggregateRootEventsEmitterInterface&Bitrix24AccountInterface
{
return new Bitrix24Account(
$this->id,
$this->bitrix24UserId,
$this->isBitrix24UserAdmin,
$this->memberId,
$this->domainUrl,
$this->status,
$this->authToken,
$this->createdAt,
$this->updatedAt,
$this->applicationVersion,
$this->applicationScope
);
}
}
Loading

0 comments on commit e648cba

Please sign in to comment.