From d04c08aeeadeb9a6b0094e783439b1c19d6a3f7d Mon Sep 17 00:00:00 2001 From: mesilov Date: Sun, 7 Jul 2024 01:47:08 +0600 Subject: [PATCH] Add additional tests and improve account status change This update adds more unit tests to validate application behavior. Particularly, tests for methods like 'isApplicationTokenValid', 'getCreatedAt', and 'getUpdatedAt' have been included. Also, the account status change from 'active' to 'blocked' has been corrected. Signed-off-by: mesilov --- phpunit.xml.dist | 57 ++-- ...ntInterfaceReferenceImplementationTest.php | 3 +- .../Entity/Bitrix24AccountInterfaceTest.php | 293 +++++++++++++++++- ...24AccountReferenceEntityImplementation.php | 9 +- 4 files changed, 316 insertions(+), 46 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e878d19c..3cdf6e26 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,31 +1,32 @@ - - - - - - ./tests/Unit - - - ./tests/Integration - - - ./tests/Integration/Core/ - - - ./tests/Integration/Services/Telephony/ - - - ./tests/Integration/Services/User/ - - - ./tests/Integration/Services/Workflows/ - - - - - ./src - - + + + + + + ./tests/Unit + ./tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountInterfaceTest.php + + + ./tests/Integration + + + ./tests/Integration/Core/ + + + ./tests/Integration/Services/Telephony/ + + + ./tests/Integration/Services/User/ + + + ./tests/Integration/Services/Workflows/ + + + + + ./src + + diff --git a/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountInterfaceReferenceImplementationTest.php b/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountInterfaceReferenceImplementationTest.php index 0d4ad60c..a1da3dc7 100644 --- a/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountInterfaceReferenceImplementationTest.php +++ b/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountInterfaceReferenceImplementationTest.php @@ -12,9 +12,10 @@ use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; use Carbon\CarbonImmutable; use Generator; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Symfony\Component\Uid\Uuid; - +#[CoversClass(Bitrix24AccountInterface::class)] class Bitrix24AccountInterfaceReferenceImplementationTest extends Bitrix24AccountInterfaceTest { protected function createBitrix24AccountImplementation( diff --git a/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountInterfaceTest.php b/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountInterfaceTest.php index 61352ac8..00ad7478 100644 --- a/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountInterfaceTest.php +++ b/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountInterfaceTest.php @@ -22,7 +22,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Uid\Uuid; use Throwable; - #[CoversClass(Bitrix24AccountInterface::class)] abstract class Bitrix24AccountInterfaceTest extends TestCase { @@ -288,6 +287,9 @@ final public function testChangeDomainUrl( $this->assertEquals($newDomainUrl, $ob->getDomainUrl()); } + /** + * @throws InvalidArgumentException + */ #[Test] #[DataProvider('bitrix24AccountForInstallDataProvider')] #[TestDox('test applicationInstalled method')] @@ -319,19 +321,19 @@ final public function testApplicationInstalled( #[DataProvider('bitrix24AccountForUninstallDataProvider')] #[TestDox('test applicationUninstalled method')] final public function testApplicationUninstalled( - Uuid $id, - int $bitrix24UserId, - bool $isBitrix24UserAdmin, - string $memberId, - string $domainUrl, - Bitrix24AccountStatus $accountStatusForInstall, - AuthToken $authToken, - CarbonImmutable $createdAt, - CarbonImmutable $updatedAt, - int $applicationVersion, - Scope $applicationScope, - string $applicationToken, - ?Throwable $exception + Uuid $id, + int $bitrix24UserId, + bool $isBitrix24UserAdmin, + string $memberId, + string $domainUrl, + Bitrix24AccountStatus $accountStatusForInstall, + AuthToken $authToken, + CarbonImmutable $createdAt, + CarbonImmutable $updatedAt, + int $applicationVersion, + Scope $applicationScope, + string $applicationToken, + ?Throwable $exception ): void { if ($exception !== null) { @@ -343,6 +345,266 @@ final public function testApplicationUninstalled( $this->assertEquals(Bitrix24AccountStatus::deleted, $ob->getStatus()); } + /** + * @throws InvalidArgumentException + */ + #[Test] + #[DataProvider('bitrix24AccountWithStatusNewDataProvider')] + #[TestDox('test isApplicationTokenValid method')] + final public function testIsApplicationTokenValid( + Uuid $id, + int $bitrix24UserId, + bool $isBitrix24UserAdmin, + string $memberId, + string $domainUrl, + Bitrix24AccountStatus $accountStatus, + AuthToken $authToken, + CarbonImmutable $createdAt, + CarbonImmutable $updatedAt, + int $applicationVersion, + Scope $applicationScope, + string $applicationToken, + ): void + { + $ob = $this->createBitrix24AccountImplementation($id, $bitrix24UserId, $isBitrix24UserAdmin, $memberId, $domainUrl, $accountStatus, $authToken, $createdAt, $updatedAt, $applicationVersion, $applicationScope); + $this->assertFalse($ob->isApplicationTokenValid($applicationToken)); + $ob->applicationInstalled($applicationToken); + $this->assertTrue($ob->isApplicationTokenValid($applicationToken)); + } + + /** + * @throws InvalidArgumentException + */ + #[Test] + #[DataProvider('bitrix24AccountWithStatusNewDataProvider')] + #[TestDox('test getCreatedAt method')] + final public function testGetCreatedAt( + Uuid $id, + int $bitrix24UserId, + bool $isBitrix24UserAdmin, + string $memberId, + string $domainUrl, + Bitrix24AccountStatus $accountStatus, + AuthToken $authToken, + CarbonImmutable $createdAt, + CarbonImmutable $updatedAt, + int $applicationVersion, + Scope $applicationScope, + string $applicationToken, + ): void + { + $ob = $this->createBitrix24AccountImplementation($id, $bitrix24UserId, $isBitrix24UserAdmin, $memberId, $domainUrl, $accountStatus, $authToken, $createdAt, $updatedAt, $applicationVersion, $applicationScope); + $this->assertTrue($ob->getCreatedAt()->equalTo($createdAt)); + $ob->applicationInstalled($applicationToken); + $this->assertTrue($ob->getCreatedAt()->equalTo($createdAt)); + } + + /** + * @throws InvalidArgumentException + */ + #[Test] + #[DataProvider('bitrix24AccountWithStatusNewDataProvider')] + #[TestDox('test getUpdatedAt method')] + final public function testGetUpdatedAt( + Uuid $id, + int $bitrix24UserId, + bool $isBitrix24UserAdmin, + string $memberId, + string $domainUrl, + Bitrix24AccountStatus $accountStatus, + AuthToken $authToken, + CarbonImmutable $createdAt, + CarbonImmutable $updatedAt, + int $applicationVersion, + Scope $applicationScope, + string $applicationToken, + ): void + { + $ob = $this->createBitrix24AccountImplementation($id, $bitrix24UserId, $isBitrix24UserAdmin, $memberId, $domainUrl, $accountStatus, $authToken, $createdAt, $updatedAt, $applicationVersion, $applicationScope); + $this->assertTrue($ob->getUpdatedAt()->equalTo($updatedAt)); + $ob->applicationInstalled($applicationToken); + $this->assertFalse($ob->getUpdatedAt()->equalTo($createdAt)); + } + + /** + * @throws InvalidArgumentException + */ + #[Test] + #[DataProvider('bitrix24AccountWithStatusNewDataProvider')] + #[TestDox('test updateApplicationVersion method')] + final public function testUpdateApplicationVersion( + Uuid $id, + int $bitrix24UserId, + bool $isBitrix24UserAdmin, + string $memberId, + string $domainUrl, + Bitrix24AccountStatus $accountStatus, + AuthToken $authToken, + CarbonImmutable $createdAt, + CarbonImmutable $updatedAt, + int $applicationVersion, + Scope $applicationScope, + string $applicationToken, + int $newApplicationVersion, + Scope $newApplicationScope, + ?Throwable $exception + ): void + { + if ($exception !== null) { + $this->expectException($exception::class); + } + $ob = $this->createBitrix24AccountImplementation($id, $bitrix24UserId, $isBitrix24UserAdmin, $memberId, $domainUrl, $accountStatus, $authToken, $createdAt, $updatedAt, $applicationVersion, $applicationScope); + $ob->applicationInstalled($applicationToken); + $ob->updateApplicationVersion($newApplicationVersion, $newApplicationScope); + $this->assertEquals($newApplicationVersion, $ob->getApplicationVersion()); + $this->assertTrue($newApplicationScope->equal($ob->getApplicationScope())); + } + + /** + * @throws InvalidArgumentException + */ + #[Test] + #[DataProvider('bitrix24AccountForInstallDataProvider')] + #[TestDox('test markAsBlocked and getComment methods')] + final public function testMarkAsBlocked( + Uuid $id, + int $bitrix24UserId, + bool $isBitrix24UserAdmin, + string $memberId, + string $domainUrl, + Bitrix24AccountStatus $accountStatus, + AuthToken $authToken, + CarbonImmutable $createdAt, + CarbonImmutable $updatedAt, + int $applicationVersion, + Scope $applicationScope, + string $applicationToken, + ?Throwable $exception + ): void + { + if ($exception !== null) { + $this->expectException($exception::class); + } + $ob = $this->createBitrix24AccountImplementation($id, $bitrix24UserId, $isBitrix24UserAdmin, $memberId, $domainUrl, $accountStatus, $authToken, $createdAt, $updatedAt, $applicationVersion, $applicationScope); + $ob->applicationInstalled($applicationToken); + $comment = 'block account just for fun'; + $ob->markAsBlocked($comment); + $this->assertEquals(Bitrix24AccountStatus::blocked, $ob->getStatus()); + $this->assertEquals($comment, $ob->getComment()); + } + + /** + * @throws InvalidArgumentException + */ + #[Test] + #[DataProvider('bitrix24AccountForInstallDataProvider')] + #[TestDox('test markAsActive and getComment methods')] + final public function testMarkAsActive( + Uuid $id, + int $bitrix24UserId, + bool $isBitrix24UserAdmin, + string $memberId, + string $domainUrl, + Bitrix24AccountStatus $accountStatus, + AuthToken $authToken, + CarbonImmutable $createdAt, + CarbonImmutable $updatedAt, + int $applicationVersion, + Scope $applicationScope, + string $applicationToken, + ?Throwable $exception + ): void + { + if ($exception !== null) { + $this->expectException($exception::class); + } + $ob = $this->createBitrix24AccountImplementation($id, $bitrix24UserId, $isBitrix24UserAdmin, $memberId, $domainUrl, $accountStatus, $authToken, $createdAt, $updatedAt, $applicationVersion, $applicationScope); + $ob->applicationInstalled($applicationToken); + $comment = 'block account just for fun'; + $ob->markAsBlocked($comment); + $this->assertEquals(Bitrix24AccountStatus::blocked, $ob->getStatus()); + $this->assertEquals($comment, $ob->getComment()); + $comment = 'activate account'; + $ob->markAsActive($comment); + $this->assertEquals(Bitrix24AccountStatus::active, $ob->getStatus()); + $this->assertEquals($comment, $ob->getComment()); + } + + /** + * @throws UnknownScopeCodeException + */ + public static function bitrix24AccountWithStatusNewDataProvider(): Generator + { + yield 'valid-update' => [ + Uuid::v7(), + 12345, + true, + 'member123', + 'https://example.com', + Bitrix24AccountStatus::new, + new AuthToken('access_token', 'refresh_token', 1609459200), + CarbonImmutable::now(), + CarbonImmutable::now(), + 1, + new Scope(['crm', 'task']), + 'application_token', + 2, + new Scope(['crm', 'task', 'telephony']), + null + ]; + yield 'valid-update-same-scope' => [ + Uuid::v7(), + 12345, + true, + 'member123', + 'https://example.com', + Bitrix24AccountStatus::new, + new AuthToken('access_token', 'refresh_token', 1609459200), + CarbonImmutable::now(), + CarbonImmutable::now(), + 1, + new Scope(['crm', 'task']), + 'application_token', + 2, + new Scope(['task','crm']), + null + ]; + yield 'valid-downgrade-scope' => [ + Uuid::v7(), + 12345, + true, + 'member123', + 'https://example.com', + Bitrix24AccountStatus::new, + new AuthToken('access_token', 'refresh_token', 1609459200), + CarbonImmutable::now(), + CarbonImmutable::now(), + 1, + new Scope([]), + 'application_token', + 2, + new Scope(['task','crm']), + null + ]; + yield 'invalid-version' => [ + Uuid::v7(), + 12345, + true, + 'member123', + 'https://example.com', + Bitrix24AccountStatus::new, + new AuthToken('access_token', 'refresh_token', 1609459200), + CarbonImmutable::now(), + CarbonImmutable::now(), + 1, + new Scope(['crm', 'task']), + 'application_token', + 1, + new Scope(['crm', 'task', 'telephony']), + new InvalidArgumentException() + ]; + } + public static function bitrix24AccountForUninstallDataProvider(): Generator { yield 'empty-application-token' => [ @@ -424,6 +686,9 @@ public static function bitrix24AccountForUninstallDataProvider(): Generator ]; } + /** + * @throws UnknownScopeCodeException + */ public static function bitrix24AccountForInstallDataProvider(): Generator { yield 'empty-application-token' => [ diff --git a/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountReferenceEntityImplementation.php b/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountReferenceEntityImplementation.php index d8b007f0..ebaf355a 100644 --- a/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountReferenceEntityImplementation.php +++ b/tests/Unit/Application/Contracts/Bitrix24Accounts/Entity/Bitrix24AccountReferenceEntityImplementation.php @@ -9,6 +9,7 @@ use Bitrix24\SDK\Core\Credentials\AuthToken; use Bitrix24\SDK\Core\Credentials\Scope; use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; +use Bitrix24\SDK\Core\Exceptions\UnknownScopeCodeException; use Bitrix24\SDK\Core\Response\DTO\RenewedAuthToken; use Carbon\CarbonImmutable; use Symfony\Component\Uid\Uuid; @@ -18,7 +19,6 @@ * * This class uses ONLY for demonstration and tests interface, use cases for work with Bitrix24AccountInterface methods * - * @implements Bitrix24AccountInterface */ final class Bitrix24AccountReferenceEntityImplementation implements Bitrix24AccountInterface { @@ -112,6 +112,9 @@ public function getApplicationVersion(): int return $this->applicationVersion; } + /** + * @throws UnknownScopeCodeException + */ public function getApplicationScope(): Scope { return new Scope($this->applicationScope); @@ -249,7 +252,7 @@ public function markAsBlocked(?string $comment): void throw new InvalidArgumentException('you cannot block account in status «deleted»'); } - $this->accountStatus = Bitrix24AccountStatus::active; + $this->accountStatus = Bitrix24AccountStatus::blocked; $this->comment = $comment; $this->updatedAt = new CarbonImmutable(); } @@ -258,4 +261,4 @@ public function getComment(): ?string { return $this->comment; } -} \ No newline at end of file +}