Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

237 serializer test #308

Merged
merged 23 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 22 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
27 changes: 25 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,20 @@ 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':
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
{

}
14 changes: 12 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,23 @@
*/
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);
foreach ($this->getCoreResponse()->getResponseData()->getResult()['result']['rows'] as $productRow) {
Copy link
Owner

Choose a reason for hiding this comment

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

А почему ты тут добавил?
['result']['rows']

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.

Вообщем у меня с этим сейчас возникли проблемы , но я вроде бы пофиксил это добавив дополнительное условие. Проблема была в том что наш метод очень умный getSuperSuperSmart(get). И если мы принимаем только айдишник сделки, то у нас выполняется batch запрос в котором мы возвращаем саму сделку и строку товара, это все у нас записывается в переменную $res. Потом мы ее добавляем в наш объект DealProductRowItemsResult. После чего нам приходится дополнительно указывать [result][rows] , так как есть еще выбор выбрать [result][deal]. А вот если мы указываем в нашем методе и айдишник и валюту , то тогда в DealProductRowItemsResult у нас приходит лишь один запрос с одним массивом и нам не нужно указывать [result][rows].

$res[] = new DealProductRowItemResult($productRow,$this->currency);
}

return $res;
Expand Down
68 changes: 62 additions & 6 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,20 +25,74 @@ class DealProductRows extends AbstractService
* @link https://training.bitrix24.com/rest_help/crm/deals/crm_deal_productrows_get.php
*
* @param int $dealId
*
* @param \Money\Currency $currency
* @return DealProductRowItemsResult
* @throws BaseException
* @throws TransportException
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
* @throws \Bitrix24\SDK\Core\Exceptions\TransportException
*/
public function get(int $dealId): DealProductRowItemsResult
public function getStupid(int $dealId, Currency $currency): DealProductRowItemsResult
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.

готово

{
return new DealProductRowItemsResult(
$this->core->call(
'crm.deal.productrows.get',
[
'id' => $dealId,
]
)
),
$currency
);
}

/**
* @param int $dealId
* @return \Bitrix24\SDK\Services\CRM\Deal\Result\DealProductRowItemsResult
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
* @throws \Bitrix24\SDK\Core\Exceptions\TransportException
*/
public function getSuperSmart(int $dealId): DealProductRowItemsResult
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.

готово

{
$res = $this->core->call('batch',[
mesilov marked this conversation as resolved.
Show resolved Hide resolved
'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);
}

/**
* @param int $dealId
* @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 getSuperSuperSmart(int $dealId, Currency $currency = null): DealProductRowItemsResult
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
Contributor Author

Choose a reason for hiding this comment

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

готово

{
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
);
}

Expand Down Expand Up @@ -78,7 +134,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,64 @@ class DealProductRowsTest extends TestCase
*/
public function testSet(): void
{

$callCosts = new Money(1050, new Currency('RUB'));
$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(0, $this->dealProductRowsService->getSuperSmart($newDealId)->getProductRows());
$this::assertTrue(
$this->dealProductRowsService->set(
$newDealId,
[
[
'PRODUCT_NAME' => 'qqqq',
'PRICE'=> $moneyFormatter->format($callCosts),
],
]
)->isSuccess()
);
$this::assertCount(1, $this->dealProductRowsService->get($newDealId)->getProductRows());
$this::assertCount(1, $this->dealProductRowsService->getSuperSmart($newDealId)->getProductRows());
$mas = $this->dealProductRowsService->getSuperSmart($newDealId)->getProductRows()[0];
$money = $moneyFormatter->format($mas->PRICE);


}

/**
* @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('EUR'));
$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',
'PRICE'=> $moneyFormatter->format($callCosts),
],
]
)->isSuccess()
);
$this::assertCount(1, $this->dealProductRowsService->get($newDealId)->getProductRows());
$currency = $callCosts->getCurrency();
$res = $this->dealProductRowsService->getSuperSuperSmart($newDealId);
foreach ($res->getProductRows() as $productRow){
var_dump($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