From 9adf3a74287d11ee5e40845b3e38ec930923c2a4 Mon Sep 17 00:00:00 2001 From: mesilov Date: Sat, 17 Aug 2024 21:53:26 +0600 Subject: [PATCH] Add custom Bitrix24 assertion and integration test. Introduced a new trait `CustomBitrix24Assertions` for verifying Bitrix24 API fields against PHPDoc annotations. Incorporated the new assertion in `ContactTest` and adjusted `composer.json` to include necessary dependencies. Signed-off-by: mesilov --- composer.json | 5 +-- .../CustomBitrix24Assertions.php | 36 +++++++++++++++++++ .../CRM/Contact/Service/ContactTest.php | 16 +++++++-- 3 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 tests/CustomAssertions/CustomBitrix24Assertions.php diff --git a/composer.json b/composer.json index 319e7751..36f3e0d1 100644 --- a/composer.json +++ b/composer.json @@ -39,13 +39,13 @@ "symfony/dotenv": "^6 || ^7", "symfony/filesystem": "^6 || ^7", "symfony/mime": "^6 || ^7", + "symfony/finder": "^6 || ^7", "symfony/http-client-contracts": "^2 || ^3", "symfony/http-foundation": "^6 || ^7", "symfony/event-dispatcher": "^6 || ^7", "symfony/uid": "^6 || ^7" }, "require-dev": { - "typhoon/reflection": "^0.4", "fakerphp/faker": "^1", "monolog/monolog": "^3", "nunomaduro/phpinsights": "^2", @@ -55,7 +55,8 @@ "rector/rector": "^1", "roave/security-advisories": "dev-master", "symfony/debug-bundle": "^6 || ^7", - "symfony/stopwatch": "^6 || ^7" + "symfony/stopwatch": "^6 || ^7", + "typhoon/reflection": "^0.4" }, "autoload": { "psr-4": { diff --git a/tests/CustomAssertions/CustomBitrix24Assertions.php b/tests/CustomAssertions/CustomBitrix24Assertions.php new file mode 100644 index 00000000..09e25ab1 --- /dev/null +++ b/tests/CustomAssertions/CustomBitrix24Assertions.php @@ -0,0 +1,36 @@ + $fieldCodesFromApi + * @param class-string $resultItemClassName + * @return void + */ + protected function assertBitrix24AllResultItemFieldsAnnotated(array $fieldCodesFromApi, string $resultItemClassName): void + { + sort($fieldCodesFromApi); + + // parse keys from phpdoc annotation + $props = TyphoonReflector::build()->reflectClass($resultItemClassName)->properties(); + $propsFromAnnotations = []; + foreach ($props as $meta) { + if ($meta->isAnnotated() && !$meta->isNative()) { + $propsFromAnnotations[] = $meta->id->name; + } + } + sort($propsFromAnnotations); + + $this->assertEquals($fieldCodesFromApi, $propsFromAnnotations, + sprintf('in phpdocs annotations for class %s we not found fields from actual api response: %s', + $resultItemClassName, + implode(', ', array_values(array_diff($fieldCodesFromApi, $propsFromAnnotations))) + )); + } +} \ No newline at end of file diff --git a/tests/Integration/Services/CRM/Contact/Service/ContactTest.php b/tests/Integration/Services/CRM/Contact/Service/ContactTest.php index 058555a1..257f63f8 100644 --- a/tests/Integration/Services/CRM/Contact/Service/ContactTest.php +++ b/tests/Integration/Services/CRM/Contact/Service/ContactTest.php @@ -10,7 +10,9 @@ use Bitrix24\SDK\Services\CRM\Common\Result\SystemFields\Types\InstantMessengerValueType; use Bitrix24\SDK\Services\CRM\Common\Result\SystemFields\Types\PhoneValueType; use Bitrix24\SDK\Services\CRM\Common\Result\SystemFields\Types\WebsiteValueType; +use Bitrix24\SDK\Services\CRM\Contact\Result\ContactItemResult; use Bitrix24\SDK\Services\CRM\Contact\Service\Contact; +use Bitrix24\SDK\Tests\CustomAssertions\CustomBitrix24Assertions; use Bitrix24\SDK\Tests\Integration\Fabric; use PHPUnit\Framework\TestCase; use Bitrix24\SDK\Core; @@ -23,8 +25,10 @@ */ class ContactTest extends TestCase { - protected Contact $contactService; - protected Faker\Generator $faker; + use CustomBitrix24Assertions; + + private Contact $contactService; + private Faker\Generator $faker; /** * @throws BaseException @@ -56,6 +60,12 @@ public function testFields(): void self::assertIsArray($this->contactService->fields()->getFieldsDescription()); } + public function testAllSystemFieldsAnnotated(): void + { + $propListFromApi = (new Core\Fields\FieldsFilter())->filterSystemFields(array_keys($this->contactService->fields()->getFieldsDescription())); + $this->assertBitrix24AllResultItemFieldsAnnotated($propListFromApi, ContactItemResult::class); + } + /** * @throws BaseException * @throws TransportException @@ -203,7 +213,7 @@ public function testGetWebsite(): void 'WEB' => [ [ 'VALUE' => $url, - 'VALUE_TYPE' => WebsiteValueType::work, + 'VALUE_TYPE' => WebsiteValueType::work->name, ] ], ])->getId())->contact()->WEB[0]->VALUE);