1
0
mirror of synced 2024-11-21 21:06:07 +03:00

Add notifications section (#159)

This commit is contained in:
curse89 2023-03-09 09:58:39 +03:00 committed by GitHub
parent e973d605f4
commit 5b91648590
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 154 additions and 0 deletions

View File

@ -44,6 +44,7 @@ class ApiVersion5 extends AbstractLoader
use V5\Delivery;
use V5\Files;
use V5\Module;
use V5\Notifications;
use V5\Orders;
use V5\Packs;
use V5\References;

View File

@ -0,0 +1,47 @@
<?php
namespace RetailCrm\Methods\V5;
use RetailCrm\Response\ApiResponse;
trait Notifications
{
protected static $allowedTypes = ['api.info', 'api.error'];
/**
* @return ApiResponse
*/
public function sendNotification(array $notification)
{
if (empty($notification['type']) || !in_array($notification['type'], self::$allowedTypes, true)) {
throw new \InvalidArgumentException(
'Parameter `type` must be not empty & must be equal one of these values: api.info|api.error'
);
}
if (empty($notification['message'])) {
throw new \InvalidArgumentException(
'Parameter `message` should not be blank'
);
}
if (empty($notification['userGroups']) && empty($notification['userIds'])) {
throw new \InvalidArgumentException(
'One of the parameters must be filled: `userGroups` or `userIds`'
);
}
if (!empty($notification['userGroups']) && !empty($notification['userIds'])) {
throw new \InvalidArgumentException(
'Only one of the two fields must be set: `userIds`, `userGroups`'
);
}
/** @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
"/notifications/send",
"POST",
['notification' => \json_encode($notification, JSON_UNESCAPED_SLASHES)]
);
}
}

View File

@ -0,0 +1,106 @@
<?php
namespace RetailCrm\Tests\Methods\Version5;
use RetailCrm\Test\TestCase;
class ApiClientNotificationsTest extends TestCase
{
/**
* @dataProvider notificationsProvider
*
* @group notifications_v5
*
* @param callable $getNotification
* @param string|null $exceptionClass
*
* @return void
*/
public function testSendNotification(callable $getNotification, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->sendNotification($getNotification());
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function notificationsProvider()
{
return [
'error_type' => [
static function () {
$notification = self::getErrorNotification();
$notification['type'] = 'another';
return $notification;
},
'InvalidArgumentException',
],
'error_message' => [
static function () {
$notification = self::getErrorNotification();
$notification['message'] = [];
return $notification;
},
'InvalidArgumentException',
],
'error_users_empty' => [
static function () {
$notification = self::getErrorNotification();
$notification['userGroups'] = [];
$notification['userIds'] = [];
return $notification;
},
'InvalidArgumentException',
],
'error_users_filled' => [
static function () {
return self::getErrorNotification();;
},
'InvalidArgumentException',
],
'success_group' => [
static function () {
$notification = self::getErrorNotification();
unset($notification['userIds']);
return $notification;
},
null,
],
'success_ids' => [
static function () {
$notification = self::getErrorNotification();
unset($notification['userGroups']);
return $notification;
},
null,
],
];
}
/**
* @return array
*/
protected static function getErrorNotification()
{
return [
'type' => 'api.error',
'userIds' => [1],
'userGroups' => ['superadmins'],
'message' => '<a href="/admin/integration/moysklad2/edit">[Модуль МойСклад]</a>
Возникли проблемы при проверке доступов входа. Введен неверный логин/пароль, проверьте корректность введенных данных.',
];
}
}