Skip to content

Commit 5ab4f7b

Browse files
committed
Add integration tests for Type service and implement crm.type.update API method with corresponding result class.
Signed-off-by: mesilov <[email protected]>
1 parent 043e623 commit 5ab4f7b

File tree

3 files changed

+163
-9
lines changed

3 files changed

+163
-9
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the bitrix24-php-sdk package.
5+
*
6+
* © Maksim Mesilov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the MIT-LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Bitrix24\SDK\Services\CRM\Type\Result;
15+
16+
use Bitrix24\SDK\Core\Result\AbstractResult;
17+
18+
class UpdatedTypeItemResult extends AbstractResult
19+
{
20+
public function type(): TypeItemResult
21+
{
22+
return new TypeItemResult($this->getCoreResponse()->getResponseData()->getResult()['type']);
23+
}
24+
25+
public function isSuccess(): bool
26+
{
27+
return true;
28+
}
29+
}

src/Services/CRM/Type/Service/Type.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@
1414
namespace Bitrix24\SDK\Services\CRM\Type\Service;
1515

1616
use Bitrix24\SDK\Attributes\ApiServiceMetadata;
17-
use Bitrix24\SDK\Core\Contracts\CoreInterface;
1817
use Bitrix24\SDK\Core\Credentials\Scope;
1918
use Bitrix24\SDK\Core\Exceptions\BaseException;
2019
use Bitrix24\SDK\Core\Exceptions\TransportException;
21-
use Bitrix24\SDK\Core\Result\AddedItemResult;
2220
use Bitrix24\SDK\Core\Result\FieldsResult;
23-
use Bitrix24\SDK\Core\Result\UpdatedItemResult;
2421
use Bitrix24\SDK\Services\AbstractService;
2522
use Bitrix24\SDK\Attributes\ApiEndpointMetadata;
2623
use Bitrix24\SDK\Services\CRM\Type\Result\AddedTypeItemResult;
2724
use Bitrix24\SDK\Services\CRM\Type\Result\DeletedItemResult;
2825
use Bitrix24\SDK\Services\CRM\Type\Result\TypeItemResult;
2926
use Bitrix24\SDK\Services\CRM\Type\Result\TypeResult;
3027
use Bitrix24\SDK\Services\CRM\Type\Result\TypesResult;
28+
use Bitrix24\SDK\Services\CRM\Type\Result\UpdatedTypeItemResult;
3129

3230
#[ApiServiceMetadata(new Scope(['crm']))]
3331
class Type extends AbstractService
@@ -72,9 +70,6 @@ public function add(string $title, ?int $entityTypeId = null, array $parameters
7270
{
7371
$fields = array_merge(['title' => $title], $parameters);
7472
if ($entityTypeId !== null) {
75-
// Значение entityTypeId обязано быть в одном из двух диапазонов:
76-
// четным целым числом, которое больше или равно 1030
77-
// в диапазоне от 128 до 192
7873
$fields['entityTypeId'] = $entityTypeId;
7974
}
8075

@@ -83,12 +78,33 @@ public function add(string $title, ?int $entityTypeId = null, array $parameters
8378
]));
8479
}
8580

81+
/**
82+
* This method updates an existing SPA by its identifier id.
83+
*
84+
* @link https://apidocs.bitrix24.com/api-reference/crm/universal/user-defined-object-types/crm-type-update.html
85+
*
86+
* @throws BaseException
87+
* @throws TransportException
88+
*/
89+
#[ApiEndpointMetadata(
90+
'crm.type.update',
91+
'https://apidocs.bitrix24.com/api-reference/crm/universal/user-defined-object-types/crm-type-update.html',
92+
'This method updates an existing SPA by its identifier id.'
93+
)]
94+
public function update(int $id, array $fields): UpdatedTypeItemResult
95+
{
96+
return new UpdatedTypeItemResult($this->core->call('crm.type.update', [
97+
'id' => $id,
98+
'fields' => $fields,
99+
]));
100+
}
101+
86102
/**
87103
* The method retrieves information about the SPA with the identifier id.
88104
*
89105
* @link https://apidocs.bitrix24.com/api-reference/crm/universal/user-defined-object-types/crm-type-get.html
90106
*
91-
* @return DeletedItemResult
107+
* @return TypeResult
92108
* @throws BaseException
93109
* @throws TransportException
94110
*/
@@ -107,7 +123,8 @@ public function get(int $id): TypeResult
107123
*
108124
* @link https://apidocs.bitrix24.com/api-reference/crm/universal/user-defined-object-types/crm-type-get-by-entity-type-id.html
109125
*
110-
* @return DeletedItemResult
126+
* @param int $entityTypeId
127+
* @return TypeResult
111128
* @throws BaseException
112129
* @throws TransportException
113130
*/
@@ -126,7 +143,10 @@ public function getByEntityTypeId(int $entityTypeId): TypeResult
126143
*
127144
* @link https://apidocs.bitrix24.com/api-reference/crm/universal/user-defined-object-types/crm-type-get-by-entity-type-id.html
128145
*
129-
* @return DeletedItemResult
146+
* @param array $order
147+
* @param array $filter
148+
* @param int $start
149+
* @return TypesResult
130150
* @throws BaseException
131151
* @throws TransportException
132152
*/
@@ -149,6 +169,7 @@ public function list(array $order = [], array $filter = [], int $start = 0): Typ
149169
*
150170
* @link https://apidocs.bitrix24.com/api-reference/crm/universal/user-defined-object-types/crm-type-delete.html
151171
*
172+
* @param int $entityTypeId
152173
* @return DeletedItemResult
153174
* @throws BaseException
154175
* @throws TransportException
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the bitrix24-php-sdk package.
5+
*
6+
* © Maksim Mesilov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the MIT-LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Bitrix24\SDK\Tests\Integration\Services\CRM\Type\Service;
15+
16+
use Bitrix24\SDK\Core\Fields\FieldsFilter;
17+
use Bitrix24\SDK\Services\CRM\Type\Result\TypeItemResult;
18+
use Bitrix24\SDK\Services\CRM\Type\Service\Type;
19+
use Bitrix24\SDK\Tests\Integration\Fabric;
20+
use PHPUnit\Framework\Attributes\CoversClass;
21+
use PHPUnit\Framework\TestCase;
22+
use Bitrix24\SDK\Tests\CustomAssertions\CustomBitrix24Assertions;
23+
24+
#[CoversClass(Type::class)]
25+
class TypeTest extends TestCase
26+
{
27+
use CustomBitrix24Assertions;
28+
29+
protected Type $typeService;
30+
31+
// in response, we have all system fields and some additional system fields
32+
// public function testAllSystemFieldsAnnotated(): void
33+
// {
34+
// $propListFromApi = (new FieldsFilter())->filterSystemFields(array_keys($this->typeService->fields()->getFieldsDescription()['fields']));
35+
// $this->assertBitrix24AllResultItemFieldsAnnotated($propListFromApi, TypeItemResult::class);
36+
// }
37+
38+
public function testAdd(): void
39+
{
40+
$title = sprintf('%s test SPA type', time());
41+
$result = $this->typeService->add($title);
42+
$this->assertEquals($title, $result->type()->title);
43+
$this->assertTrue($this->typeService->delete($result->getId())->isSuccess());
44+
}
45+
46+
public function testUpdate(): void
47+
{
48+
$title = sprintf('%s test SPA type', time());
49+
$result = $this->typeService->add($title);
50+
$this->assertEquals($title, $result->type()->title);
51+
52+
$title = sprintf('%s updated SPA type', time());
53+
$updatedResult = $this->typeService->update($result->getId(), ['title' => $title]);
54+
$this->assertEquals($title, $updatedResult->type()->title);
55+
56+
$this->assertTrue($this->typeService->delete($result->getId())->isSuccess());
57+
}
58+
59+
public function testGet(): void
60+
{
61+
$title = sprintf('%s test SPA type', time());
62+
$addResult = $this->typeService->add($title);
63+
64+
$result = $this->typeService->get($addResult->getId());
65+
$this->assertEquals($title, $addResult->type()->title);
66+
$this->assertEquals($result->type()->id, $addResult->type()->id);
67+
$this->assertTrue($this->typeService->delete($addResult->getId())->isSuccess());
68+
}
69+
70+
public function testList(): void
71+
{
72+
$title = sprintf('%s test SPA type', time());
73+
$addResult = $this->typeService->add($title);
74+
$result = $this->typeService->list([], ['id' => $addResult->getId()])->getTypes()[0];
75+
$this->assertEquals($title, $addResult->type()->title);
76+
$this->assertEquals($result->id, $addResult->type()->id);
77+
$this->assertTrue($this->typeService->delete($addResult->getId())->isSuccess());
78+
}
79+
80+
public function testGetByEntityTypeId(): void
81+
{
82+
$title = sprintf('%s test SPA type', time());
83+
$result = $this->typeService->add($title);
84+
$this->assertEquals($title, $result->type()->title);
85+
86+
$resultTypeId = $this->typeService->getByEntityTypeId($result->type()->entityTypeId);
87+
$this->assertEquals($title, $resultTypeId->type()->title);
88+
$this->assertEquals($result->type()->id, $resultTypeId->type()->id);
89+
$this->assertTrue($this->typeService->delete($result->getId())->isSuccess());
90+
}
91+
92+
public function testDelete(): void
93+
{
94+
$title = sprintf('%s test SPA type', time());
95+
$result = $this->typeService->add($title);
96+
$this->assertEquals($title, $result->type()->title);
97+
$this->assertTrue($this->typeService->delete($result->getId())->isSuccess());
98+
}
99+
100+
protected function setUp(): void
101+
{
102+
$this->typeService = Fabric::getServiceBuilder()->getCRMScope()->type();
103+
}
104+
}

0 commit comments

Comments
 (0)