Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

237 serializer test #308

Merged
merged 23 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"symfony/http-foundation": "5.4.* || 6.*",
"symfony/event-dispatcher": "5.4.* || 6.*",
"ramsey/uuid": "^4.2.3",
"moneyphp/money": "3.* || 4.*"
"moneyphp/money": "3.* || 4.*",
"ext-intl": "*"
},
"require-dev": {
"monolog/monolog": "2.1.*",
Expand Down
30 changes: 28 additions & 2 deletions src/Services/CRM/Common/Result/AbstractCrmItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,33 @@
use Bitrix24\SDK\Core\Result\AbstractItem;
use Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException;
use DateTimeImmutable;
use Money\Currency;
use Money\Money;

class AbstractCrmItem extends AbstractItem
{
private const CRM_USERFIELD_PREFIX = 'UF_CRM_';

/**
* @var \Money\Currency
*/
private Currency $currency;

public function __construct(array $data, Currency $currency = null)
{
parent::__construct($data);
if ($currency !== null) {
$this->currency = $currency;
}

}

/**
* @param int|string $offset
*
* @return bool|\DateTimeImmutable|int|mixed|null
*/

public function __get($offset)
{
// todo унести в отдельный класс и покрыть тестами
Expand All @@ -30,6 +47,8 @@ public function __get($offset)
case 'LEAD_ID':
case 'CONTACT_ID':
case 'QUOTE_ID':
// productRow
case 'OWNER_ID':
if ($this->data[$offset] !== '' && $this->data[$offset] !== null) {
return (int)$this->data[$offset];
}
Expand All @@ -39,16 +58,23 @@ public function __get($offset)
if ($this->data[$offset] !== '' && $this->data[$offset] !== null && $this->data[$offset] !== '0') {
return (int)$this->data[$offset];
}

return null;

// contact
case 'EXPORT':
case 'HAS_PHONE':
case 'HAS_EMAIL':
case 'HAS_IMOL':
case 'OPENED':
// deal
case 'PRICE_EXCLUSIVE':
case 'PRICE_NETTO':
case 'PRICE_BRUTTO':
case 'PRICE':
if ($this->data[$offset] !== '' && $this->data[$offset] !== null) {
$var = $this->data[$offset] * 100;
return new Money((string)$var, new Currency($this->currency->getCode()));
}
return null;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавь остальные поля

 * @property-read Money $PRICE_EXCLUSIVE
 * @property-read Money $PRICE_NETTO
 * @property-read Money $PRICE_BRUTTO

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

готово

case 'IS_MANUAL_OPPORTUNITY':
case 'CLOSED':
case 'IS_NEW':
Expand Down
29 changes: 17 additions & 12 deletions src/Services/CRM/Deal/Result/DealProductRowItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,36 @@

namespace Bitrix24\SDK\Services\CRM\Deal\Result;

use Bitrix24\SDK\Core\Result\AbstractItem;
use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem;
use Money\Currency;
use Money\Money;

/**
* Class DealProductRowItemResult
*
* @property-read int $ID
* @property-read int $OWNER_ID
* @property-read int $ID
* @property-read int $OWNER_ID
* @property-read string $OWNER_TYPE
* @property-read int $PRODUCT_ID
* @property-read int $PRODUCT_ID
* @property-read string $PRODUCT_NAME
* @property-read string $PRICE
* @property-read string $PRICE_EXCLUSIVE
* @property-read string $PRICE_NETTO
* @property-read string $PRICE_BRUTTO
* @property-read Money $PRICE
* @property-read Money $PRICE_EXCLUSIVE
* @property-read Money $PRICE_NETTO
* @property-read Money $PRICE_BRUTTO
* @property-read string $QUANTITY
* @property-read int $DISCOUNT_TYPE_ID
* @property-read int $DISCOUNT_TYPE_ID
* @property-read string $DISCOUNT_RATE
* @property-read string $DISCOUNT_SUM
* @property-read string $TAX_RATE
* @property-read string $TAX_INCLUDED
* @property-read string $CUSTOMIZED
* @property-read int $MEASURE_CODE
* @property-read int $MEASURE_CODE
* @property-read string $MEASURE_NAME
* @property-read int $SORT
* @property-read int $RESERVE_ID
* @property-read int $RESERVE_QUANTITY
* @property-read int $SORT
*/
class DealProductRowItemResult extends AbstractItem
class DealProductRowItemResult extends AbstractCrmItem
{

}
20 changes: 18 additions & 2 deletions src/Services/CRM/Deal/Result/DealProductRowItemsResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
namespace Bitrix24\SDK\Services\CRM\Deal\Result;

use Bitrix24\SDK\Core\Exceptions\BaseException;
use Bitrix24\SDK\Core\Response\Response;
use Bitrix24\SDK\Core\Result\AbstractResult;
use Money\Currency;

/**
* Class DealProductRowItemsResult
Expand All @@ -15,15 +17,29 @@
*/
class DealProductRowItemsResult extends AbstractResult
{
private Currency $currency;

public function __construct(Response $coreResponse,Currency $currency)
{
parent::__construct($coreResponse);
$this->currency = $currency;
}

/**
* @return DealProductRowItemResult[]
* @throws BaseException
*/
public function getProductRows(): array
{
$res = [];
foreach ($this->getCoreResponse()->getResponseData()->getResult() as $productRow) {
$res[] = new DealProductRowItemResult($productRow);
if(!empty($this->getCoreResponse()->getResponseData()->getResult()['result']['rows'])) {
foreach ($this->getCoreResponse()->getResponseData()->getResult()['result']['rows'] as $productRow) {
$res[] = new DealProductRowItemResult($productRow, $this->currency);
}
} else {
foreach ($this->getCoreResponse()->getResponseData()->getResult() as $productRow) {
$res[] = new DealProductRowItemResult($productRow, $this->currency);
}
}

return $res;
Expand Down
30 changes: 23 additions & 7 deletions src/Services/CRM/Deal/Service/DealProductRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Bitrix24\SDK\Core\Result\UpdatedItemResult;
use Bitrix24\SDK\Services\AbstractService;
use Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult;
use Bitrix24\SDK\Services\CRM\Deal\Result\DealResult;
use Money\Currency;

/**
* Class DealProductRows
Expand All @@ -23,23 +25,37 @@ class DealProductRows extends AbstractService
* @link https://training.bitrix24.com/rest_help/crm/deals/crm_deal_productrows_get.php
*
* @param int $dealId
*
* @return DealProductRowItemsResult
* @throws BaseException
* @throws TransportException
* @param \Money\Currency|null $currency
* @return \Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
* @throws \Bitrix24\SDK\Core\Exceptions\TransportException
*/
public function get(int $dealId): DealProductRowItemsResult
public function get(int $dealId, Currency $currency = null): DealProductRowItemsResult
{
if ($currency === null) {
$res = $this->core->call('batch', [
'halt' => 0,
'cmd' => [
'deal' => sprintf('crm.deal.get?ID=%s', $dealId),
'rows' => sprintf('crm.deal.productrows.get?ID=%s', $dealId)
],
]);
$data = $res->getResponseData()->getResult();
$currency = new Currency($data['result']['deal']['CURRENCY_ID']);
return new DealProductRowItemsResult($res,$currency);
}
return new DealProductRowItemsResult(
$this->core->call(
'crm.deal.productrows.get',
[
'id' => $dealId,
]
)
),
$currency
);
}


/**
* Creates or updates product entries inside the specified deal.
*
Expand Down Expand Up @@ -78,7 +94,7 @@ public function set(int $dealId, array $productRows): UpdatedItemResult
$this->core->call(
'crm.deal.productrows.set',
[
'id' => $dealId,
'id' => $dealId,
'rows' => $productRows,
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Bitrix24\SDK\Services\CRM\Deal\Service\Deal;
use Bitrix24\SDK\Services\CRM\Deal\Service\DealProductRows;
use Bitrix24\SDK\Tests\Integration\Fabric;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\DecimalMoneyFormatter;
use Money\Money;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Причеши тесты, оставь тест на get( с двумя вариантами:

  • мы не знаем валюту
  • мы знаем валюту

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KarlsonComplete а сейчас?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ну тесты я делаю не очень , надеюсь готово)

use PHPUnit\Framework\TestCase;

/**
Expand All @@ -28,46 +32,67 @@ class DealProductRowsTest extends TestCase
*/
public function testSet(): void
{

$callCosts = new Money(1050, new Currency('USD'));
$currencies = new ISOCurrencies();

$moneyFormatter = new DecimalMoneyFormatter($currencies);
$newDealId = $this->dealService->add(['TITLE' => 'test deal'])->getId();
$this::assertCount(0, $this->dealProductRowsService->get($newDealId)->getProductRows());
$this::assertCount(5, $this->dealProductRowsService->get($newDealId)->getProductRows());
$this::assertTrue(
$this->dealProductRowsService->set(
$newDealId,
[
[
'PRODUCT_NAME' => 'qqqq',
'PRODUCT_NAME' => 'wine',
'PRICE' => $moneyFormatter->format($callCosts),
],
]
)->isSuccess()
);
$this::assertCount(1, $this->dealProductRowsService->get($newDealId)->getProductRows());


}

/**
* @throws BaseException
* @throws TransportException
* @covers \Bitrix24\SDK\Services\CRM\Deal\Service\DealProductRows::get
*/
public function testGet(): void
{
$newDealId = $this->dealService->add(['TITLE' => 'test deal'])->getId();
$this::assertCount(0, $this->dealProductRowsService->get($newDealId)->getProductRows());
$callCosts = new Money(1050, new Currency('USD'));
$currencies = new ISOCurrencies();

$moneyFormatter = new DecimalMoneyFormatter($currencies);
$newDealId = $this->dealService->add(['TITLE' => 'test deal', 'CURRENCY_ID' => $callCosts->getCurrency()->getCode()])->getId();
$this::assertTrue(
$this->dealProductRowsService->set(
$newDealId,
[
[
'PRODUCT_NAME' => 'qqqq',
'PRODUCT_NAME' => 'wine',
'PRICE' => $moneyFormatter->format($callCosts),
],
]
)->isSuccess()
);
$this::assertCount(1, $this->dealProductRowsService->get($newDealId)->getProductRows());
$currency = $callCosts->getCurrency();

$resultWithoutAvailableCurrency = $this->dealProductRowsService->get($newDealId);
$resultWithAvailableCurrency = $this->dealProductRowsService->get($newDealId, $currency);
foreach ($resultWithoutAvailableCurrency->getProductRows() as $productRow) {
$this::assertEquals($callCosts, $productRow->PRICE);
}
foreach ($resultWithAvailableCurrency->getProductRows() as $productRow) {
$this::assertEquals($callCosts, $productRow->PRICE);
}
}

public function setUp(): void
{
$this->dealService = Fabric::getServiceBuilder()->getCRMScope()->deal();
$this->dealProductRowsService = Fabric::getServiceBuilder()->getCRMScope()->dealProductRows();
$this->core = Fabric::getCore();
}
}
2 changes: 1 addition & 1 deletion tests/Integration/Services/Telephony/Service/CallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function testAttachTranscription(): void
'ADD_TO_CHAT' => 1
])->getExternalCallFinish();

self::assertGreaterThan(1,
self::assertGreaterThanOrEqual(1,
$this->callService->attachTranscription(
$registerCallResult->CALL_ID,
$callCosts,
Expand Down