1
0
mirror of synced 2024-11-22 03:46:02 +03:00

Several important fixes, new tests

This commit is contained in:
Pavel 2019-06-21 11:36:15 +03:00
parent 02a45d8272
commit 348d500843
13 changed files with 150 additions and 32 deletions

View File

@ -13,6 +13,7 @@
namespace RetailCrm\Mg\Bot\Model\Entity\Chat; namespace RetailCrm\Mg\Bot\Model\Entity\Chat;
use JMS\Serializer\Annotation as Serializer;
use RetailCrm\Mg\Bot\Model\Entity\Channel\Channel; use RetailCrm\Mg\Bot\Model\Entity\Channel\Channel;
use RetailCrm\Mg\Bot\Model\Entity\CommonFields; use RetailCrm\Mg\Bot\Model\Entity\CommonFields;
use JMS\Serializer\Annotation\Accessor; use JMS\Serializer\Annotation\Accessor;
@ -55,6 +56,7 @@ class Chat
* *
* @Type("RetailCrm\Mg\Bot\Model\Entity\Channel\Channel") * @Type("RetailCrm\Mg\Bot\Model\Entity\Channel\Channel")
* @Accessor(getter="getChannel",setter="setChannel") * @Accessor(getter="getChannel",setter="setChannel")
* @SkipWhenEmpty()
*/ */
private $channel; private $channel;

View File

@ -49,10 +49,10 @@ trait CommonFields
private $createdAt; private $createdAt;
/** /**
* @var \DateTime $createdAt * @var \DateTime $updatedAt
* *
* @Type("DateTime<'Y-m-d\TH:i:s\.u\Z'>") * @Type("DateTime<'Y-m-d\TH:i:s\.u\Z'>")
* @Accessor(getter="getCreatedAt",setter="setCreatedAt") * @Accessor(getter="getUpdatedAt",setter="setUpdatedAt")
* @SkipWhenEmpty() * @SkipWhenEmpty()
*/ */
private $updatedAt; private $updatedAt;

View File

@ -13,7 +13,6 @@
namespace RetailCrm\Mg\Bot\Model\Entity; namespace RetailCrm\Mg\Bot\Model\Entity;
use RetailCrm\Mg\Bot\Model\Entity\CommonFields;
use JMS\Serializer\Annotation\Accessor; use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\SkipWhenEmpty; use JMS\Serializer\Annotation\SkipWhenEmpty;
use JMS\Serializer\Annotation\Type; use JMS\Serializer\Annotation\Type;

View File

@ -30,9 +30,9 @@ use JMS\Serializer\Annotation\Type;
class Responsible class Responsible
{ {
/** /**
* @var \DateTime $assignedAt * @var string $assignedAt
* *
* @Type("DateTime<'Y-m-d\TH:i:s\.u\Z'>") * @Type("string")
* @Accessor(getter="getAssignedAt",setter="setAssignedAt") * @Accessor(getter="getAssignedAt",setter="setAssignedAt")
* @SkipWhenEmpty() * @SkipWhenEmpty()
*/ */
@ -57,17 +57,17 @@ class Responsible
private $type; private $type;
/** /**
* @return \DateTime * @return string
*/ */
public function getAssignedAt(): \DateTime public function getAssignedAt(): string
{ {
return $this->assignedAt; return $this->assignedAt;
} }
/** /**
* @param \DateTime $assignedAt * @param string $assignedAt
*/ */
public function setAssignedAt(\DateTime $assignedAt) public function setAssignedAt(string $assignedAt)
{ {
$this->assignedAt = $assignedAt; $this->assignedAt = $assignedAt;
} }

View File

@ -33,8 +33,7 @@ class DialogAssignRequest
* @var int $dialogId * @var int $dialogId
* *
* @Type("int") * @Type("int")
* @Accessor(getter="getDialogId,setter="setDialogId") * @Accessor(getter="getDialogId", setter="setDialogId")
* @SkipWhenEmpty
*/ */
private $dialogId; private $dialogId;
@ -43,7 +42,7 @@ class DialogAssignRequest
* *
* @Type("int") * @Type("int")
* @Accessor(getter="getUserId", setter="setUserId") * @Accessor(getter="getUserId", setter="setUserId")
* @SkipWhenEmpty * @SkipWhenEmpty()
*/ */
private $userId; private $userId;
@ -67,7 +66,7 @@ class DialogAssignRequest
/** /**
* @param int $dialogId * @param int $dialogId
*/ */
public function setDialogId(int $dialogId) public function setDialogId($dialogId)
{ {
$this->dialogId = $dialogId; $this->dialogId = $dialogId;
} }

View File

@ -53,7 +53,7 @@ class AssignResponse
/** /**
* @var Responsible $previousResponsible * @var Responsible $previousResponsible
* *
* @Type("Responsible") * @Type("RetailCrm\Mg\Bot\Model\Entity\Responsible")
* @Accessor(getter="getPreviousResponsible",setter="setPreviousResponsible") * @Accessor(getter="getPreviousResponsible",setter="setPreviousResponsible")
* @SkipWhenEmpty() * @SkipWhenEmpty()
*/ */
@ -62,7 +62,7 @@ class AssignResponse
/** /**
* @var Responsible $responsible * @var Responsible $responsible
* *
* @Type("Responsible") * @Type("RetailCrm\Mg\Bot\Model\Entity\Responsible")
* @Accessor(getter="getResponsible",setter="setResponsible") * @Accessor(getter="getResponsible",setter="setResponsible")
* @SkipWhenEmpty() * @SkipWhenEmpty()
*/ */
@ -71,7 +71,7 @@ class AssignResponse
/** /**
* @return bool * @return bool
*/ */
public function isReassign(): bool public function getIsReassign(): bool
{ {
return $this->isReassign; return $this->isReassign;
} }

View File

@ -54,11 +54,8 @@ class ListResponse implements \Iterator, \ArrayAccess, \Countable {
$this->errors = $data['errors']; $this->errors = $data['errors'];
} else { } else {
foreach($data as $item) { foreach($data as $item) {
if (substr($responseType, 0, 1) == '\\') { $this->items[] =
$responseType = substr($responseType, 1); Serializer::deserialize($item, $responseType, Serializer::S_ARRAY);
}
$this->items[] = Serializer::deserialize($item, $responseType, Serializer::S_ARRAY);
} }
} }
} }

View File

@ -76,10 +76,12 @@ class Serializer
switch ($from) { switch ($from) {
case self::S_ARRAY: case self::S_ARRAY:
$deserialized = $serializer->fromArray(array_filter($data), self::normalizeNamespace($entityType), $context); $deserialized =
$serializer->fromArray(array_filter($data), self::normalizeNamespace($entityType), $context);
break; break;
case self::S_JSON: case self::S_JSON:
$deserialized = $serializer->deserialize($data, self::normalizeNamespace($entityType), $from, $context); $deserialized =
$serializer->deserialize($data, self::normalizeNamespace($entityType), $from, $context);
break; break;
} }
@ -112,9 +114,9 @@ class Serializer
private static function normalizeNamespace(string $namespace) private static function normalizeNamespace(string $namespace)
{ {
if (substr($namespace, 0, 1) == '\\') { if (substr($namespace, 0, 1) == '\\') {
return substr($namespace, 1); $namespace = substr($namespace, 1);
} else { }
return $namespace; return $namespace;
} }
} }
}

View File

@ -3,7 +3,7 @@
/** /**
* PHP version 7.0 * PHP version 7.0
* *
* Client Test * Commands Test
* *
* @package RetailCrm\Mg\Bot\Tests * @package RetailCrm\Mg\Bot\Tests
* @author retailCRM <integration@retailcrm.ru> * @author retailCRM <integration@retailcrm.ru>
@ -23,7 +23,7 @@ use RetailCrm\Mg\Bot\Test\TestCase;
/** /**
* PHP version 7.0 * PHP version 7.0
* *
* Class ClientTest * Class CommandsTest
* *
* @package RetailCrm\Mg\Bot\Tests * @package RetailCrm\Mg\Bot\Tests
* @author retailCRM <integration@retailcrm.ru> * @author retailCRM <integration@retailcrm.ru>

View File

@ -0,0 +1,118 @@
<?php
/**
* PHP version 7.0
*
* Dialogs Test
*
* @package RetailCrm\Mg\Bot\Tests
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Mg\Bot\Tests;
use InvalidArgumentException;
use RetailCrm\Mg\Bot\Model\Entity\Responsible;
use RetailCrm\Mg\Bot\Model\Request\DialogAssignRequest;
use RetailCrm\Mg\Bot\Model\Response\ErrorOnlyResponse;
use RetailCrm\Mg\Bot\Test\TestCase;
/**
* PHP version 7.0
*
* Class DialogsTest
*
* @package RetailCrm\Mg\Bot\Tests
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class DialogsTest extends TestCase
{
/**
* @group("dialogs")
* @throws \Exception
*/
public function testDialogAssignError()
{
$client = self::getApiClient(
null,
null,
false,
$this->getResponse('{"errors":["incorrect dialog_id"]}', 400)
);
$request = new DialogAssignRequest();
$request->setDialogId(-1);
$response = $client->dialogAssign($request);
self::assertTrue(!$response->isSuccessful());
self::assertNotEmpty($response->getErrors());
}
/**
* @group("dialogs")
* @throws \Exception
*/
public function testDialogAssign()
{
$client = self::getApiClient(
null,
null,
false,
$this->getJsonResponse('dialogReassigned')
);
$request = new DialogAssignRequest();
$request->setDialogId(60);
$request->setUserId(4);
$response = $client->dialogAssign($request);
self::assertTrue($response->isSuccessful());
self::assertTrue($response->getIsReassign());
self::assertTrue($response->getPreviousResponsible() instanceof Responsible);
self::assertTrue($response->getResponsible() instanceof Responsible);
}
/**
* @group("dialogs")
* @throws \Exception
*/
public function testDialogCloseError()
{
self::expectException(InvalidArgumentException::class);
$client = self::getApiClient(
null,
null,
false,
$this->getResponse('{"errors":["dialog #2131231231 not found"]}', 404)
);
$client->dialogClose('2131231231');
}
/**
* @group("dialogs")
* @throws \Exception
*/
public function testDialogClose()
{
$client = self::getApiClient(
null,
null,
false,
$this->getResponse('{}')
);
$response = $client->dialogClose('62');
self::assertTrue($response instanceof ErrorOnlyResponse);
self::assertTrue($response->isSuccessful());
self::assertEmpty($response->getErrors());
}
}

View File

@ -3,7 +3,7 @@
/** /**
* PHP version 7.0 * PHP version 7.0
* *
* Client Test * Messages Test
* *
* @package RetailCrm\Mg\Bot\Tests * @package RetailCrm\Mg\Bot\Tests
* @author retailCRM <integration@retailcrm.ru> * @author retailCRM <integration@retailcrm.ru>

View File

@ -38,7 +38,7 @@
} }
}, },
"created_at": "2019-06-11T12:46:48.72241Z", "created_at": "2019-06-11T12:46:48.72241Z",
"updated_at": "2019-06-11T12:46:48.72241Z", "updated_at": "2019-06-11T12:56:48.72241Z",
"activated_at": "2019-06-11T12:46:48.722086Z", "activated_at": "2019-06-11T12:46:48.722086Z",
"deactivated_at": null, "deactivated_at": null,
"is_active": true "is_active": true

View File

@ -0,0 +1 @@
{"responsible":{"type":"user","id":4,"assigned_at":"2019-06-21T08:01:09Z"},"is_reassign":true,"previous_responsible":{"type":"user","id":12,"assigned_at":"2019-06-11T12:49:34Z"},"left_user_id":12}