Skip to content

Commit

Permalink
Add custom Bitrix24 assertion and integration test.
Browse files Browse the repository at this point in the history
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 <mesilov.maxim@gmail.com>
  • Loading branch information
mesilov committed Aug 17, 2024
1 parent 7dae75d commit 9adf3a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
36 changes: 36 additions & 0 deletions tests/CustomAssertions/CustomBitrix24Assertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

namespace Bitrix24\SDK\Tests\CustomAssertions;

use Bitrix24\SDK\Services\CRM\Contact\Result\ContactItemResult;
use Typhoon\Reflection\TyphoonReflector;

trait CustomBitrix24Assertions
{
/**
* @param array<int, non-empty-string> $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)))
));
}
}
16 changes: 13 additions & 3 deletions tests/Integration/Services/CRM/Contact/Service/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9adf3a7

Please sign in to comment.