Skip to content

Commit 4ef7548

Browse files
committed
Add integration tests for Item service and implement getSmartProcessItem method in AbstractCrmItem.
Signed-off-by: mesilov <[email protected]>
1 parent 423f988 commit 4ef7548

File tree

2 files changed

+212
-1
lines changed

2 files changed

+212
-1
lines changed

src/Services/CRM/Common/Result/AbstractCrmItem.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Bitrix24\SDK\Services\CRM\Common\Result;
1515

16+
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
1617
use Bitrix24\SDK\Core\Result\AbstractItem;
1718
use Bitrix24\SDK\Services\CRM\Activity\ActivityContentType;
1819
use Bitrix24\SDK\Services\CRM\Activity\ActivityDirectionType;
@@ -266,11 +267,33 @@ protected function getKeyWithUserfieldByFieldName(string $fieldName): mixed
266267
return $this->$fieldName;
267268
}
268269

270+
/**
271+
* Get smart process item by entity type id
272+
*
273+
* @param positive-int $entityTypeId
274+
* @throws InvalidArgumentException
275+
*/
276+
public function getSmartProcessItem(int $entityTypeId): ?int
277+
{
278+
if ($entityTypeId <= 0) {
279+
throw new InvalidArgumentException('entityTypeId must be positive integer');
280+
}
281+
$fieldKey = sprintf('PARENT_ID_%d', $entityTypeId);
282+
if (!array_key_exists($fieldKey, $this->data)) {
283+
throw new InvalidArgumentException(sprintf('field «%s» for smart process with entityTypeId «%d» not found', $fieldKey, $entityTypeId));
284+
}
285+
if ($this->data[$fieldKey] === '' || $this->data[$fieldKey] === null) {
286+
return null;
287+
}
288+
289+
return (int)$this->data[$fieldKey];
290+
}
291+
269292
public function __construct(array $data, Currency $currency = null)
270293
{
271294
parent::__construct($data);
272295
if ($currency !== null) {
273296
$this->currency = $currency;
274297
}
275298
}
276-
}
299+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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\Item\Service;
15+
16+
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
17+
use Bitrix24\SDK\Core\Exceptions\ItemNotFoundException;
18+
use Bitrix24\SDK\Services\CRM\Contact\Service\Contact;
19+
use Bitrix24\SDK\Services\CRM\Item\Service\Item;
20+
use Bitrix24\SDK\Services\CRM\Type\Service\Type;
21+
use Bitrix24\SDK\Tests\Integration\Fabric;
22+
use PHPUnit\Framework\Attributes\CoversClass;
23+
use PHPUnit\Framework\TestCase;
24+
use Bitrix24\SDK\Tests\CustomAssertions\CustomBitrix24Assertions;
25+
26+
#[CoversClass(Item::class)]
27+
class ItemTest extends TestCase
28+
{
29+
use CustomBitrix24Assertions;
30+
31+
protected Type $typeService;
32+
33+
protected Item $itemService;
34+
35+
protected Contact $contactService;
36+
37+
public function testAdd(): void
38+
{
39+
$title = sprintf('%s test SPA type', time());
40+
$addedTypeItemResult = $this->typeService->add($title);
41+
$this->assertEquals($title, $addedTypeItemResult->type()->title);
42+
43+
// add item to SP
44+
$this->itemService->add($addedTypeItemResult->type()->entityTypeId, [
45+
'title' => sprintf('test sp item %s', time()),
46+
'xmlId' => sprintf('b24-php-sdk-test-item-%s', time())
47+
])->item();
48+
49+
$this->assertTrue($this->typeService->delete($addedTypeItemResult->getId())->isSuccess());
50+
}
51+
52+
public function testUpdate(): void
53+
{
54+
$title = sprintf('%s test SPA type', time());
55+
$addedTypeItemResult = $this->typeService->add($title);
56+
$this->assertEquals($title, $addedTypeItemResult->type()->title);
57+
58+
// add item to SP
59+
$itemItemResult = $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [
60+
'title' => sprintf('test sp item %s', time()),
61+
'xmlId' => sprintf('b24-php-sdk-test-item-%s', time())
62+
])->item();
63+
64+
// update item
65+
$newTitle = sprintf('test sp item %s updated', time());
66+
$updatedItemResult = $this->itemService->update($addedTypeItemResult->type()->entityTypeId, $itemItemResult->id, ['title' => $newTitle]);
67+
$this->assertEquals($newTitle, $updatedItemResult->item()->title);
68+
$this->assertTrue($updatedItemResult->isSuccess());
69+
70+
$updatedItem = $this->itemService->get($addedTypeItemResult->type()->entityTypeId, $itemItemResult->id)->item();
71+
$this->assertEquals($newTitle, $updatedItem->title);
72+
73+
$this->assertTrue($this->typeService->delete($addedTypeItemResult->getId())->isSuccess());
74+
}
75+
76+
public function testGet(): void
77+
{
78+
$title = sprintf('%s test SPA type', time());
79+
$addedTypeItemResult = $this->typeService->add($title);
80+
$this->assertEquals($title, $addedTypeItemResult->type()->title);
81+
82+
// add item to SP
83+
$itemItemResult = $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [
84+
'title' => sprintf('test sp item %s', time()),
85+
'xmlId' => sprintf('b24-php-sdk-test-item-%s', time())
86+
])->item();
87+
88+
$item = $this->itemService->get($addedTypeItemResult->type()->entityTypeId, $itemItemResult->id)->item();
89+
$this->assertEquals($itemItemResult->title, $item->title);
90+
$this->assertEquals($itemItemResult->xmlId, $item->xmlId);
91+
92+
$this->assertTrue($this->typeService->delete($addedTypeItemResult->type()->entityTypeId)->isSuccess());
93+
}
94+
95+
public function testList(): void
96+
{
97+
$title = sprintf('%s test SPA type', time());
98+
$addedTypeItemResult = $this->typeService->add($title);
99+
$this->assertEquals($title, $addedTypeItemResult->type()->title);
100+
101+
// add item to SP
102+
$this->itemService->add($addedTypeItemResult->type()->entityTypeId, [
103+
'title' => sprintf('test sp item %s', time()),
104+
'xmlId' => sprintf('b24-php-sdk-test-item-%s', time())
105+
])->item();
106+
$this->itemService->add($addedTypeItemResult->type()->entityTypeId, [
107+
'title' => sprintf('test sp item %s', time()),
108+
'xmlId' => sprintf('b24-php-sdk-test-item-%s', time())
109+
])->item();
110+
111+
$items = $this->itemService->list($addedTypeItemResult->type()->entityTypeId, [], [], [])->getItems();
112+
$this->assertCount(2, $items);
113+
114+
$this->assertTrue($this->typeService->delete($addedTypeItemResult->type()->entityTypeId)->isSuccess());
115+
}
116+
117+
public function testGetSmartProcessItem(): void
118+
{
119+
$title = sprintf('%s test SPA type', time());
120+
$addedTypeItemResult = $this->typeService->add($title, null, [
121+
'relations' => [
122+
'child' => [
123+
[
124+
// allow bind to contact
125+
'entityTypeId' => 3,
126+
'isChildrenListEnabled' => 'N',
127+
'isPredefined' => 'N'
128+
]
129+
]
130+
]
131+
]);
132+
$this->assertEquals($title, $addedTypeItemResult->type()->title);
133+
134+
// add item to SP
135+
$itemItemResult = $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [
136+
'title' => sprintf('test sp item %s', time()),
137+
'xmlId' => sprintf('b24-php-sdk-test-item-%s', time())
138+
])->item();
139+
140+
// add contact with sp item
141+
// @phpstan-ignore-next-line argument.type
142+
$b24ContactId = $this->contactService->add([
143+
'NAME' => sprintf('Test contact %s', time()),
144+
'PARENT_ID_' . $addedTypeItemResult->type()->entityTypeId => $itemItemResult->id,
145+
])->getId();
146+
$contact = $this->contactService->get($b24ContactId)->contact();
147+
$this->assertEquals(
148+
$itemItemResult->id,
149+
$contact->getSmartProcessItem($addedTypeItemResult->type()->entityTypeId)
150+
);
151+
$this->expectException(InvalidArgumentException::class);
152+
$contact->getSmartProcessItem(1);
153+
154+
$b24ContactId = $this->contactService->add([
155+
'NAME' => sprintf('Test contact %s', time())
156+
])->getId();
157+
$this->assertNull($this->contactService->get($b24ContactId)->contact()->getSmartProcessItem($addedTypeItemResult->type()->entityTypeId));
158+
159+
$this->assertTrue($this->typeService->delete($addedTypeItemResult->type()->entityTypeId)->isSuccess());
160+
}
161+
162+
public function testDelete(): void
163+
{
164+
$title = sprintf('%s test SPA type', time());
165+
$addedTypeItemResult = $this->typeService->add($title);
166+
$this->assertEquals($title, $addedTypeItemResult->type()->title);
167+
168+
// add item to SP
169+
$itemItemResult = $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [
170+
'title' => sprintf('test sp item %s', time()),
171+
'xmlId' => sprintf('b24-php-sdk-test-item-%s', time())
172+
])->item();
173+
174+
$this->assertTrue($this->itemService->delete($addedTypeItemResult->type()->entityTypeId, $itemItemResult->id)->isSuccess());
175+
176+
$this->expectException(ItemNotFoundException::class);
177+
$this->itemService->get($addedTypeItemResult->type()->entityTypeId, $itemItemResult->id)->item();
178+
179+
$this->assertTrue($this->typeService->delete($addedTypeItemResult->type()->entityTypeId)->isSuccess());
180+
}
181+
182+
protected function setUp(): void
183+
{
184+
$this->typeService = Fabric::getServiceBuilder()->getCRMScope()->type();
185+
$this->itemService = Fabric::getServiceBuilder()->getCRMScope()->item();
186+
$this->contactService = Fabric::getServiceBuilder()->getCRMScope()->contact();
187+
}
188+
}

0 commit comments

Comments
 (0)