This repository has been archived by the owner on Feb 7, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Bitrix24AccountInterface test entity implementation
A new file containing a test implementation of the Bitrix24AccountInterface has been added, which includes methods for checking user status, managing authentication tokens, updating domain URL, and handling application installation and upgrade cases. The created tests help ensure the functionality and reliability of the system with Bitrix24Accounts. Signed-off-by: mesilov <[email protected]>
- Loading branch information
Showing
1 changed file
with
254 additions
and
0 deletions.
There are no files selected for viewing
254 changes: 254 additions & 0 deletions
254
...on/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountInterfaceTestEntityImplementation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bitrix24\SDK\Tests\Unit\Application\Contracts\Bitrix24Accounts\Entity; | ||
|
||
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface; | ||
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus; | ||
use Bitrix24\SDK\Core\Credentials\AuthToken; | ||
use Bitrix24\SDK\Core\Credentials\Scope; | ||
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; | ||
use Bitrix24\SDK\Core\Response\DTO\RenewedAuthToken; | ||
use Carbon\CarbonImmutable; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
/** | ||
* Class Bitrix24AccountInterfaceTestEntityImplementation | ||
* | ||
* This class use for test interface and use cases for work with Bitrix24AccountInterface methods | ||
* | ||
* @implements Bitrix24AccountInterface | ||
*/ | ||
final class Bitrix24AccountInterfaceTestEntityImplementation implements Bitrix24AccountInterface | ||
{ | ||
private string $accessToken; | ||
private string $refreshToken; | ||
private int $expires; | ||
private array $applicationScope; | ||
private ?string $applicationToken; | ||
private ?string $comment; | ||
|
||
public function __construct( | ||
private Uuid $id, | ||
private int $bitrix24UserId, | ||
private bool $isBitrix24UserAdmin, | ||
private string $memberId, | ||
private string $domainUrl, | ||
private Bitrix24AccountStatus $accountStatus, | ||
AuthToken $authToken, | ||
private CarbonImmutable $createdAt, | ||
private CarbonImmutable $updatedAt, | ||
private int $applicationVersion, | ||
Scope $applicationScope, | ||
) | ||
{ | ||
$this->accessToken = $authToken->getAccessToken(); | ||
$this->refreshToken = $authToken->getRefreshToken(); | ||
$this->expires = $authToken->getExpires(); | ||
$this->applicationScope = $applicationScope->getScopeCodes(); | ||
} | ||
|
||
public function getId(): Uuid | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getBitrix24UserId(): int | ||
{ | ||
return $this->bitrix24UserId; | ||
} | ||
|
||
public function isBitrix24UserAdmin(): bool | ||
{ | ||
return $this->isBitrix24UserAdmin; | ||
} | ||
|
||
public function getMemberId(): string | ||
{ | ||
return $this->memberId; | ||
} | ||
|
||
public function getDomainUrl(): string | ||
{ | ||
return $this->domainUrl; | ||
} | ||
|
||
public function getStatus(): Bitrix24AccountStatus | ||
{ | ||
return $this->accountStatus; | ||
} | ||
|
||
public function getAuthToken(): AuthToken | ||
{ | ||
return new AuthToken($this->accessToken, $this->refreshToken, $this->expires); | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function renewAuthToken(RenewedAuthToken $renewedAuthToken): void | ||
{ | ||
if ($this->getMemberId() !== $renewedAuthToken->memberId) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'member id %s for bitrix24 account %s for domain %s mismatch with member id %s for renewed access token', | ||
$this->getMemberId(), | ||
$this->getId()->toRfc4122(), | ||
$this->getDomainUrl(), | ||
$renewedAuthToken->memberId, | ||
) | ||
); | ||
} | ||
|
||
$this->accessToken = $renewedAuthToken->authToken->getAccessToken(); | ||
$this->refreshToken = $renewedAuthToken->authToken->getRefreshToken(); | ||
$this->expires = $renewedAuthToken->authToken->getExpires(); | ||
$this->updatedAt = new CarbonImmutable(); | ||
} | ||
|
||
public function getApplicationVersion(): int | ||
{ | ||
return $this->applicationVersion; | ||
} | ||
|
||
public function getApplicationScope(): Scope | ||
{ | ||
return new Scope($this->applicationScope); | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function changeDomainUrl(string $newDomainUrl): void | ||
{ | ||
if ($newDomainUrl === '') { | ||
throw new InvalidArgumentException('new domain url cannot be empty'); | ||
} | ||
if (Bitrix24AccountStatus::blocked === $this->accountStatus || Bitrix24AccountStatus::deleted === $this->accountStatus) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'bitrix24 account %s for domain %s must be in active or new state, now account in %s state. domain url cannot be changed', | ||
$this->id->toRfc4122(), | ||
$this->domainUrl, | ||
$this->accountStatus->name | ||
) | ||
); | ||
} | ||
|
||
$this->domainUrl = $newDomainUrl; | ||
$this->updatedAt = new CarbonImmutable(); | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function applicationInstalled(string $applicationToken): void | ||
{ | ||
if (Bitrix24AccountStatus::new !== $this->accountStatus) { | ||
throw new InvalidArgumentException('new account must be in status new'); | ||
} | ||
if ($applicationToken === '') { | ||
throw new InvalidArgumentException('application token cannot be empty'); | ||
} | ||
|
||
$this->accountStatus = Bitrix24AccountStatus::active; | ||
$this->applicationToken = $applicationToken; | ||
$this->updatedAt = new CarbonImmutable(); | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function applicationUninstalled(string $applicationToken): void | ||
{ | ||
if ($applicationToken === '') { | ||
throw new InvalidArgumentException('application token cannot be empty'); | ||
} | ||
if ($this->applicationToken !== $applicationToken) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'application token %s mismatch with application token %s for bitrix24 account %s for domain %s', | ||
$applicationToken, | ||
$this->applicationToken, | ||
$this->id->toRfc4122(), | ||
$this->domainUrl | ||
) | ||
); | ||
} | ||
|
||
$this->accountStatus = Bitrix24AccountStatus::deleted; | ||
$this->updatedAt = new CarbonImmutable(); | ||
} | ||
|
||
public function isApplicationTokenValid(string $applicationToken): bool | ||
{ | ||
return $this->applicationToken === $applicationToken; | ||
} | ||
|
||
public function getCreatedAt(): CarbonImmutable | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
public function getUpdatedAt(): CarbonImmutable | ||
{ | ||
return $this->updatedAt; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function updateApplicationVersion(int $version, ?Scope $newScope): void | ||
{ | ||
if (Bitrix24AccountStatus::active !== $this->accountStatus) { | ||
throw new InvalidArgumentException(sprintf('account must be in status «active», but now account in status «%s»', $this->accountStatus->name)); | ||
} | ||
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»', | ||
$this->applicationVersion, | ||
$version)); | ||
} | ||
$this->applicationVersion = $version; | ||
if ($newScope !== null) { | ||
$this->applicationScope = $newScope->getScopeCodes(); | ||
} | ||
$this->updatedAt = new CarbonImmutable(); | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function markAsActive(?string $comment): void | ||
{ | ||
if (Bitrix24AccountStatus::blocked !== $this->accountStatus) { | ||
throw new InvalidArgumentException( | ||
sprintf('you can activate account only in status blocked, now account in status %s', | ||
$this->accountStatus->name)); | ||
} | ||
|
||
$this->accountStatus = Bitrix24AccountStatus::active; | ||
$this->comment = $comment; | ||
$this->updatedAt = new CarbonImmutable(); | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function markAsBlocked(?string $comment): void | ||
{ | ||
if (Bitrix24AccountStatus::deleted === $this->accountStatus) { | ||
throw new InvalidArgumentException('you cannot block account in status «deleted»'); | ||
} | ||
|
||
$this->accountStatus = Bitrix24AccountStatus::active; | ||
$this->comment = $comment; | ||
$this->updatedAt = new CarbonImmutable(); | ||
} | ||
|
||
public function getComment(): ?string | ||
{ | ||
return $this->comment; | ||
} | ||
} |