Skip to content

Commit

Permalink
Новые атрибуты входящих веб-хуков #3
Browse files Browse the repository at this point in the history
  • Loading branch information
TTATPuOT committed Mar 7, 2024
1 parent f108f38 commit 52fafad
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 17 deletions.
12 changes: 12 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ class Constants
*/
public const HOOK_PRINT_STATUS = 'PRINT_FORM';

/**
* Хук: DOWNLOAD_PHOTO
* @var string
*/
public const HOOK_DOWNLOAD_PHOTO = 'DOWNLOAD_PHOTO';

/**
* Хук: PREALERT_CLOSED
* @var string
*/
public const HOOK_PREALERT_CLOSED = 'PREALERT_CLOSED';

/**
* Хук: задел на будущее
* @var string
Expand Down
10 changes: 10 additions & 0 deletions src/Constraints/PrintFormTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace CdekSDK2\Constraints;

class PrintFormTypes
{
public const WAYBILL = 'WAYBILL';

public const BARCODE = 'BARCODE';
}
65 changes: 62 additions & 3 deletions src/Dto/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
class Attributes
{
/**
* Признак возвратного заказа
* Признак возвратного заказа:
* true - является возвратной
* false - является прямой
* @Type("bool")
* @var bool
*/
Expand Down Expand Up @@ -51,11 +53,39 @@ class Attributes
public $status_date_time;

/**
* Наименование города(места), где произошло изменение статуса
* Наименование города возникновения статуса
* @Type("string")
* @var string
*/
public $city;
public $city_name;

/**
* Код города возникновения статуса (не возвращается для статуса "Создан")
* @Type("string")
* @var int
*/
public $city_code;

/**
* Код статуса (подробнее см. приложение 1)
* @Type("string")
* @var string
*/
public $code;

/**
* Признак возвратного заказа
* @Type("boolean")
* @var boolean
*/
public $is_reverse;

/**
* Признак клиентского возврата
* @Type("boolean")
* @var boolean
*/
public $is_client_return;

/**
* Тип печатной формы
Expand All @@ -70,4 +100,33 @@ class Attributes
* @var string
*/
public $url;

/**
* Ссылка на скачивание файла:
* Формат: https://photo-docs.production.cdek.ru/archives/qWErtY
* @Type("string")
* @var string
*/
public $link;

/**
* Номер СДЭК закрытого преалерта
* @Type("string")
* @var string
*/
public $prealert_number;

/**
* Дата закрытия
* @Type("string")
* @var string
*/
public $closed_date;

/**
* Фактический ПВЗ, в который были переданы заказы
* @Type("string")
* @var string
*/
public $fact_shipment_point;
}
98 changes: 84 additions & 14 deletions tests/Actions/WebhooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace CdekSDK2\Tests\Actions;

use CdekSDK2\Actions\Webhooks;
use CdekSDK2\Constraints\PrintFormTypes;
use CdekSDK2\Dto\InputHook;
use CdekSDK2\BaseTypes\WebHook;
use CdekSDK2\Client;
Expand Down Expand Up @@ -48,40 +49,109 @@ protected function tearDown(): void
$this->webhooks = null;
}

public function testParseHookStatus()
public function testParseHookOrderStatus()
{
$input_hook = '{
"type": "ORDER_STATUS",
"date_time": "2019-07-11T13:07:34+0700",
"uuid": "72753031-8347-40c0-ab0f-1a49c7a262c1",
"date_time": "2023-11-28T07:44:45+0000",
"uuid": "72753031-1820-4f99-9240-aab139f05ca5",
"attributes": {
"cdek_number": "1106153417",
"number": "1106152821",
"status_code": "4",
"status_date_time": "2019-07-11T12:41:43+0700",
"city": "Новосибирск"
"is_return": false,
"is_reverse": false,
"is_client_return": false,
"cdek_number": "1100285492",
"number": "17011574744791",
"related_entities": [],
"code": "RECEIVED_AT_SHIPMENT_WAREHOUSE",
"status_code": "3",
"status_date_time": "2023-11-28T07:44:45+0000",
"city_name": "Новосибирск",
"city_code": "270"
}
}';
$hook = $this->webhooks->parse($input_hook);
$this->assertInstanceOf(InputHook::class, $hook);
$this->assertStringContainsString(Constants::HOOK_TYPE_STATUS, $hook->type);
$this->assertEquals('2023-11-28T07:44:45+0000', $hook->date_time);
$this->assertEquals('72753031-1820-4f99-9240-aab139f05ca5', $hook->uuid);
$this->assertEquals(false, $hook->attributes->is_return);
$this->assertEquals(false, $hook->attributes->is_reverse);
$this->assertEquals(false, $hook->attributes->is_client_return);
$this->assertEquals(1100285492, $hook->attributes->cdek_number);
$this->assertEquals(17011574744791, $hook->attributes->number);
$this->assertEquals('RECEIVED_AT_SHIPMENT_WAREHOUSE', $hook->attributes->code); //TODO: Упаковать в константы
$this->assertEquals(3, $hook->attributes->status_code);
$this->assertEquals('2023-11-28T07:44:45+0000', $hook->attributes->status_date_time);
$this->assertEquals('Новосибирск', $hook->attributes->city_name);
$this->assertEquals(270, $hook->attributes->city_code);
}

public function testParseHookPrint()
public function testParseHookPrintForm()
{
$input_hook = '{
"type": "PRINT_FORM",
"date_time": "2019-07-11T13:07:34+0700",
"uuid": "72753031-8347-40c0-ab0f-1a49c7a262c1",
"date_time": "2023-11-28T09:03:31+0000",
"uuid": "72753031-e1f1-4fc8-97ee-d58a010b6a67",
"attributes": {
"type": "barcode",
"url": "https://api.cdek.ru/v2/print/barcodes/{uuid}.pdf"
"type": "BARCODE",
"url": "https://api.cdek.ru/v2/print/barcodes/72753031-e1f1-4fc8-97ee-d58a010b6a67.pdf"
}
}';
$hook = $this->webhooks->parse($input_hook);
$this->assertInstanceOf(InputHook::class, $hook);
$this->assertStringContainsString(Constants::HOOK_PRINT_STATUS, $hook->type);
$this->assertStringContainsString(Constants::PRINT_TYPE_BARCODE, $hook->attributes->type);
$this->assertEquals('2023-11-28T09:03:31+0000', $hook->date_time);
$this->assertEquals('72753031-e1f1-4fc8-97ee-d58a010b6a67', $hook->uuid);
$this->assertEquals(PrintFormTypes::BARCODE, $hook->attributes->type);
$this->assertEquals(
'https://api.cdek.ru/v2/print/barcodes/72753031-e1f1-4fc8-97ee-d58a010b6a67.pdf',
$hook->attributes->url
);
}

public function testParseHookDownloadPhoto()
{
$input_hook = '{
"type": "DOWNLOAD_PHOTO",
"date_time": "2023-11-29T05:00:01+0000",
"uuid": "72753031-7288-4d57-a893-29451197aa01",
"attributes": {
"cdek_number": 1100239959,
"link": "https://photo-docs.production.cdek.ru/archives/qWErtY"
}
}';
$hook = $this->webhooks->parse($input_hook);
$this->assertInstanceOf(InputHook::class, $hook);
$this->assertStringContainsString(Constants::HOOK_DOWNLOAD_PHOTO, $hook->type);
$this->assertEquals('2023-11-29T05:00:01+0000', $hook->date_time);
$this->assertEquals('72753031-7288-4d57-a893-29451197aa01', $hook->uuid);
$this->assertEquals(1100239959, $hook->attributes->cdek_number);
$this->assertEquals(
'https://photo-docs.production.cdek.ru/archives/qWErtY',
$hook->attributes->link
);
}

public function testParseHookPrealertClosed()
{
$input_hook = '{
"type": "PREALERT_CLOSED",
"date_time": "2023-01-23T10:20:02+0000",
"uuid": "72753031-a1d3-4266-bc9f-8052f0fc3b2c",
"attributes": {
"prealert_number": "PA/7/583",
"closed_date": "2023-01-17T07:59:18+0000",
"fact_shipment_point": "NSK1"
}
}';
$hook = $this->webhooks->parse($input_hook);
$this->assertInstanceOf(InputHook::class, $hook);
$this->assertStringContainsString(Constants::HOOK_PREALERT_CLOSED, $hook->type);
$this->assertEquals('2023-01-23T10:20:02+0000', $hook->date_time);
$this->assertEquals('72753031-a1d3-4266-bc9f-8052f0fc3b2c', $hook->uuid);
$this->assertEquals('PA/7/583', $hook->attributes->prealert_number);
$this->assertEquals('2023-01-17T07:59:18+0000', $hook->attributes->closed_date);
$this->assertEquals('NSK1', $hook->attributes->fact_shipment_point);
}

public function testParseHookFailData()
Expand Down

0 comments on commit 52fafad

Please sign in to comment.