1
0
mirror of synced 2024-11-21 19:36:02 +03:00

File operations, code cleanup, tests

This commit is contained in:
Pavel 2019-06-21 16:42:04 +03:00
parent 3a3bf0eda7
commit 004b27f62c
28 changed files with 844 additions and 455 deletions

View File

@ -12,4 +12,4 @@ before_script:
- flags="-o"
- composer install $flags
script: make test
script: make travis

View File

@ -21,4 +21,7 @@ endif
stan:
@echo "==> Running analysis"
@php $(BIN_DIR)/phpstan analyse -l 4 -c $(ROOT_DIR)/phpstan.neon $(SRC_DIR)
@echo "==> Analysis complete"
@echo "==> Analysis complete"
travis: test stan
@echo "==> Completed"

View File

@ -22,7 +22,7 @@
"guzzlehttp/guzzle": "6.3.*"
},
"require-dev": {
"phpunit/phpunit": "6.5.*",
"phpunit/phpunit": "7.4.*",
"phpmd/phpmd": "2.6.*",
"phpstan/phpstan": "0.9.*",
"squizlabs/php_codesniffer": "3.4.*",

874
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -16,10 +16,10 @@ namespace RetailCrm\Mg\Bot;
use RetailCrm\Common\Exception\InvalidJsonException;
use RetailCrm\Common\Url;
use RetailCrm\Common\Serializer;
use RetailCrm\Mg\Bot\Model\Response\AssignResponse;
use RetailCrm\Mg\Bot\Model\Response\ErrorOnlyResponse;
use RetailCrm\Mg\Bot\Model\Request\GetFileRequest;
use RetailCrm\Mg\Bot\Model\Request\UploadFileByUrlRequest;
use RetailCrm\Mg\Bot\Model\Response\FullFileResponse;
use RetailCrm\Mg\Bot\Model\Response\ListResponse;
use RetailCrm\Mg\Bot\Model\Response\MessageSendResponse;
/**
* PHP version 7.0
@ -67,13 +67,13 @@ class Client
* @param string $method
* @param object $request Request parameters
* @param string $responseType
* @param int $serializeTo
* @param string $serializeTo
* @param bool $arrayOfObjects
*
* @return object
* @return object|null
* @throws \Exception
*/
private function getData(
private function submitRequest(
$path,
$method,
$request,
@ -121,7 +121,7 @@ class Client
{
$path = $fromRoot ? '\\' : '';
foreach($classes as $class) {
foreach ($classes as $class) {
$path .= '\\' . $class;
}
@ -158,7 +158,7 @@ class Client
*/
public function bots(Model\Request\BotsRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/bots',
HttpClient::METHOD_GET,
$request,
@ -178,7 +178,7 @@ class Client
*/
public function info(Model\Request\InfoRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/my/info',
HttpClient::METHOD_PATCH,
$request,
@ -196,7 +196,7 @@ class Client
*/
public function channels(Model\Request\ChannelsRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/channels',
HttpClient::METHOD_GET,
$request,
@ -216,7 +216,7 @@ class Client
*/
public function chats(Model\Request\ChatsRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/chats',
HttpClient::METHOD_GET,
$request,
@ -236,7 +236,7 @@ class Client
*/
public function commands(Model\Request\CommandsRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/my/commands',
HttpClient::METHOD_GET,
$request,
@ -256,7 +256,7 @@ class Client
*/
public function commandEdit(Model\Request\CommandEditRequest $request)
{
return $this->getData(
return $this->submitRequest(
sprintf("/my/commands/%s", $request->getName()),
HttpClient::METHOD_PUT,
$request,
@ -275,7 +275,7 @@ class Client
*/
public function commandDelete(string $request)
{
return $this->getData(
return $this->submitRequest(
sprintf("/my/commands/%s", $request),
HttpClient::METHOD_DELETE,
null,
@ -294,7 +294,7 @@ class Client
*/
public function customers(Model\Request\CustomersRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/customers',
HttpClient::METHOD_GET,
$request,
@ -314,7 +314,7 @@ class Client
*/
public function dialogs(Model\Request\DialogsRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/dialogs',
HttpClient::METHOD_GET,
$request,
@ -334,7 +334,7 @@ class Client
*/
public function dialogAssign(Model\Request\DialogAssignRequest $request)
{
return $this->getData(
return $this->submitRequest(
sprintf("/dialogs/%d/assign", $request->getDialogId()),
HttpClient::METHOD_PATCH,
$request,
@ -353,7 +353,7 @@ class Client
*/
public function dialogClose(string $request)
{
return $this->getData(
return $this->submitRequest(
sprintf("/dialogs/%d/close", $request),
HttpClient::METHOD_DELETE,
null,
@ -371,7 +371,7 @@ class Client
*/
public function members(Model\Request\MembersRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/members',
HttpClient::METHOD_GET,
$request,
@ -391,7 +391,7 @@ class Client
*/
public function messages(Model\Request\MessagesRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/messages',
HttpClient::METHOD_GET,
$request,
@ -411,7 +411,7 @@ class Client
*/
public function messageSend(Model\Request\MessageSendRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/messages',
HttpClient::METHOD_POST,
$request,
@ -429,7 +429,7 @@ class Client
*/
public function messageEdit(Model\Request\MessageEditRequest $request)
{
return $this->getData(
return $this->submitRequest(
sprintf("/messages/%d", $request->getId()),
HttpClient::METHOD_PATCH,
$request,
@ -447,7 +447,7 @@ class Client
*/
public function messageDelete(string $request)
{
return $this->getData(
return $this->submitRequest(
sprintf("/messages/%d", $request),
HttpClient::METHOD_DELETE,
null,
@ -465,7 +465,7 @@ class Client
*/
public function users(Model\Request\UsersRequest $request)
{
return $this->getData(
return $this->submitRequest(
'/users',
HttpClient::METHOD_GET,
$request,
@ -474,4 +474,54 @@ class Client
true
);
}
/**
* Returns filtered users list
*
* @param string $url File URL
*
* @return \RetailCrm\Mg\Bot\Model\Response\UploadFileResponse|object
* @throws \Exception
*/
public function uploadFileByUrl(string $url)
{
$request = new UploadFileByUrlRequest();
$request->setUrl($url);
return $this->submitRequest(
'/files/upload_by_url',
HttpClient::METHOD_POST,
$request,
self::getResponseClass('UploadFileResponse')
);
}
/**
* @param string $filename
* @return Model\Response\UploadFileResponse
*
* @throws \Exception
*/
public function uploadFile(string $filename)
{
return $this->client->uploadFileViaForm($filename);
}
/**
* @param string $fileId
* @return Model\Response\FullFileResponse|null
*
* @throws \Exception
*/
public function getFileById(string $fileId)
{
$obj = $this->submitRequest(
\sprintf('/files/%s', $fileId),
HttpClient::METHOD_GET,
null,
self::getResponseClass('FullFileResponse')
);
return ($obj instanceof FullFileResponse) ? $obj : null;
}
}

View File

@ -18,9 +18,9 @@ use RetailCrm\Common\Exception\LimitException;
use InvalidArgumentException;
use RetailCrm\Common\Serializer;
use RetailCrm\Common\Url;
use RetailCrm\Mg\Bot\Model\Response\UploadFileResponse;
use Symfony\Component\Validator\Validation;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
@ -107,7 +107,7 @@ class HttpClient
* @param string $path Request URL
* @param string $method Request method (default: 'GET')
* @param mixed $request Request model (default: null)
* @param int $serializeTo Serializer::S_JSON or Serializer::S_ARRAY
* @param string $serializeTo Serializer::S_JSON or Serializer::S_ARRAY
*
* @return ResponseInterface
* @throws \Exception
@ -122,7 +122,7 @@ class HttpClient
$this->validateRequest($request);
}
if ($method == self::METHOD_GET) {
if ($method == self::METHOD_GET && !is_null($request)) {
$getParameters = Url::buildGetParameters(Serializer::serialize($request, Serializer::S_ARRAY));
}
@ -176,6 +176,47 @@ class HttpClient
return $responseObject;
}
/**
* @param string $filename
* @return UploadFileResponse
*
* @throws \Exception
*/
public function uploadFileViaForm(string $filename): UploadFileResponse
{
if (!file_exists($filename)) {
throw new \InvalidArgumentException("File doesn't exist");
}
try {
$responseData = $this->client->request(
self::METHOD_POST,
\sprintf("%s/files/upload", $this->basePath),
[
'headers' => [
'X-Bot-Token' => $this->token
],
'multipart' => [
[
'name' => basename($filename),
'filename' => basename($filename),
'contents' => fopen($filename, 'r')
]
]
]
);
} catch (GuzzleException $exception) {
throw new \Exception($exception->getMessage(), $exception->getCode(), $exception);
}
$obj = Serializer::deserialize(
(string) $responseData->getBody(),
'RetailCrm\Mg\Bot\Model\Response\UploadFileResponse'
);
return $obj instanceof UploadFileResponse ? $obj : null;
}
/**
* Validate HTTP method
*

View File

@ -255,4 +255,4 @@ class Bot
{
$this->isSystem = $isSystem;
}
}
}

View File

@ -174,4 +174,4 @@ class Channel
{
$this->name = $name;
}
}
}

View File

@ -201,4 +201,4 @@ class ChannelSettings
{
$this->file = $file;
}
}
}

View File

@ -104,4 +104,4 @@ trait CommonFields
{
$this->updatedAt = $updatedAt;
}
}
}

View File

@ -103,4 +103,4 @@ class Item
{
$this->caption = $caption;
}
}
}

View File

@ -458,4 +458,4 @@ class Message
{
$this->product = $product;
}
}
}

View File

@ -79,4 +79,4 @@ class MessageCost
{
$this->currency = $currency;
}
}
}

View File

@ -131,4 +131,4 @@ class MessageDelivery
{
$this->comment = $comment;
}
}
}

View File

@ -16,9 +16,6 @@ namespace RetailCrm\Mg\Bot\Model\Entity\Message;
use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\SkipWhenEmpty;
use JMS\Serializer\Annotation\Type;
use RetailCrm\Mg\Bot\Model\Entity\Cost;
use RetailCrm\Mg\Bot\Model\Entity\Delivery;
use RetailCrm\Mg\Bot\Model\Entity\Status;
/**
* PHP version 7.0
@ -32,16 +29,6 @@ use RetailCrm\Mg\Bot\Model\Entity\Status;
*/
class MessageOrder
{
// const JSON_PROPERTY_MAP = [
// 'cost' => 'MessageCost',
// 'date' => 'string',
// 'delivery' => 'MessageDelivery',
// 'items' => 'MessageOrderItem[]',
// 'number' => 'string',
// 'payments' => 'MessagePayment[]',
// 'status' => 'MessageStatus'
// ];
/**
* @var string $number
*
@ -241,4 +228,4 @@ class MessageOrder
{
$this->payments = $payments;
}
}
}

View File

@ -1,14 +1,14 @@
<?php
/**
* PHP version 7.0
* PHP version 7.0.
*
* MessageOrderItem entity
*
* @package RetailCrm\Mg\Bot\Model\Entity\Message
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*
* @see http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Mg\Bot\Model\Entity\Message;
@ -18,19 +18,19 @@ use JMS\Serializer\Annotation\SkipWhenEmpty;
use JMS\Serializer\Annotation\Type;
/**
* PHP version 7.0
* PHP version 7.0.
*
* MessageOrderItem class
*
* @package RetailCrm\Mg\Bot\Model\Entity\Message
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*
* @see http://help.retailcrm.pro/docs/Developers
*/
class MessageOrderItem
{
/**
* @var string $name
* @var string
*
* @Type("string")
* @Accessor(getter="getName",setter="setName")
@ -39,7 +39,7 @@ class MessageOrderItem
private $name;
/**
* @var string $url
* @var string
*
* @Type("string")
* @Accessor(getter="getUrl",setter="setUrl")
@ -48,7 +48,7 @@ class MessageOrderItem
private $url;
/**
* @var string $img
* @var string
*
* @Type("string")
* @Accessor(getter="getImg",setter="setImg")
@ -57,7 +57,7 @@ class MessageOrderItem
private $img;
/**
* @var MessageCost $price
* @var MessageCost
*
* @Type("MessageCost")
* @Accessor(getter="getPrice",setter="setPrice")
@ -66,7 +66,7 @@ class MessageOrderItem
private $price;
/**
* @var MessageQuantity $quantity
* @var MessageQuantity
*
* @Type("MessageQuantity")
* @Accessor(getter="getQuantity",setter="setQuantity)
@ -153,4 +153,4 @@ class MessageOrderItem
{
$this->quantity = $quantity;
}
}
}

View File

@ -81,4 +81,4 @@ class MessageOrderPaymentStatus
{
$this->payed = $payed;
}
}
}

View File

@ -103,4 +103,4 @@ class MessagePayment
{
$this->amount = $amount;
}
}
}

View File

@ -203,4 +203,4 @@ class MessageProduct
{
$this->quantity = $quantity;
}
}
}

View File

@ -82,4 +82,4 @@ class MessageQuantity
{
$this->unit = $unit;
}
}
}

View File

@ -78,4 +78,4 @@ class MessageStatus
{
$this->name = $name;
}
}
}

View File

@ -30,6 +30,8 @@ use JMS\Serializer\Annotation\Type;
trait CommonFields
{
/**
* @var int
*
* @Type("int")
* @Accessor(getter="getId",setter="setId")
* @SkipWhenEmpty
@ -37,14 +39,18 @@ trait CommonFields
private $id;
/**
* @Type("string")
* @var \DateTime
*
* @Type("DateTime<'Y-m-d\TH:i:s\.u\Z'>")
* @Accessor(getter="getSince",setter="setSince")
* @SkipWhenEmpty
*/
private $since;
/**
* @Type("string")
* @var \DateTime
*
* @Type("DateTime<'Y-m-d\TH:i:s\.u\Z'>")
* @Accessor(getter="getUntil",setter="setUntil")
* @SkipWhenEmpty
*/
@ -67,7 +73,7 @@ trait CommonFields
}
/**
* @return string
* @return \DateTime
*/
public function getSince()
{
@ -75,15 +81,15 @@ trait CommonFields
}
/**
* @param string $since
* @param \DateTime $since
*/
public function setSince($since)
public function setSince(\DateTime $since): void
{
$this->since = $since;
}
/**
* @return string
* @return \DateTime
*/
public function getUntil()
{
@ -91,9 +97,9 @@ trait CommonFields
}
/**
* @param string $until
* @param \DateTime $until
*/
public function setUntil($until)
public function setUntil(\DateTime $until): void
{
$this->until = $until;
}

View File

@ -0,0 +1,57 @@
<?php
/**
* PHP version 7.0
*
* Upload file by url request
*
* @package RetailCrm\Mg\Bot\Model\Request
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Mg\Bot\Model\Request;
use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\Type;
use Symfony\Component\Validator\Constraints as Assert;
/**
* PHP version 7.0
*
* UploadFileByUrlRequest class
*
* @package RetailCrm\Mg\Bot\Model\Request
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class UploadFileByUrlRequest
{
/**
* @var string $url
*
* @Type("string")
* @Accessor(getter="getUrl",setter="setUrl")
* @Assert\NotBlank()
* @Assert\Url()
*/
private $url;
/**
* @return string
*/
public function getUrl(): string
{
return $this->url;
}
/**
* @param string $url
*/
public function setUrl(string $url): void
{
$this->url = $url;
}
}

View File

@ -23,6 +23,7 @@ namespace RetailCrm\Mg\Bot\Model\Response;
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class ErrorOnlyResponse {
class ErrorOnlyResponse
{
use CommonFields;
}

View File

@ -36,7 +36,7 @@ class Serializer
* Serialize given object to JSON or Array
*
* @param object $request
* @param int $serialize
* @param string $serialize
*
* @return array|string
*/
@ -63,10 +63,10 @@ class Serializer
* Deserialize given array or JSON to object
*
* @param mixed $data
* @param string self::normalizeNamespace($entityType)
* @param string $entityType
* @param string $from
*
* @return array|object|null
* @return object|null
*/
public static function deserialize($data, $entityType, $from = self::S_JSON)
{
@ -85,7 +85,7 @@ class Serializer
break;
}
return $deserialized;
return is_object($deserialized) ? $deserialized : null;
}
/**

View File

@ -22,7 +22,6 @@ use RetailCrm\Mg\Bot\Model\Entity\Dialog;
use RetailCrm\Mg\Bot\Model\Entity\Message\Message;
use RetailCrm\Mg\Bot\Model\Entity\User;
use RetailCrm\Mg\Bot\Model\Request;
use RetailCrm\Mg\Bot\Model\Response;
use RetailCrm\Mg\Bot\Test\TestCase;
/**
@ -37,7 +36,6 @@ use RetailCrm\Mg\Bot\Test\TestCase;
*/
class ClientListTest extends TestCase
{
/**
* @group("list")
* @throws \Exception

View File

@ -0,0 +1,113 @@
<?php
/**
* PHP version 7.0
*
* Upload file 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 RetailCrm\Mg\Bot\Model\Constants;
use RetailCrm\Mg\Bot\Model\Request\MessageSendRequest;
use RetailCrm\Mg\Bot\Model\Response\FullFileResponse;
use RetailCrm\Mg\Bot\Model\Response\UploadFileResponse;
use RetailCrm\Mg\Bot\Test\TestCase;
/**
* PHP version 7.0
*
* Class UploadFileTest
*
* @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 FileTest extends TestCase
{
/**
* @group("upload")
* @throws \Exception
*/
public function testUploadFileByUrlException()
{
$client = self::getApiClient(
null,
null,
false,
$this->getEmptyResponse(400)
);
self::expectException(\InvalidArgumentException::class);
$client->uploadFileByUrl('');
$client->uploadFileByUrl('rar');
}
/**
* @group("upload")
* @throws \Exception
*/
public function testUploadFileByUrl()
{
$client = self::getApiClient(
null,
null,
false,
$this->getResponse('{"id":"881712bb-4062-4973-9e23-3373135836e2","type":"image","size":3773}')
);
$response = $client->uploadFileByUrl('https://2ip.ru/images/logo.gif');
self::assertTrue($response->isSuccessful());
self::assertEquals('881712bb-4062-4973-9e23-3373135836e2', $response->getId());
self::assertEquals('image', $response->getType());
self::assertEquals('3773', $response->getSize());
}
/**
* @group("upload")
* @throws \Exception
*/
public function testUploadFileViaForm()
{
$client = self::getApiClient(
null,
null,
false,
$this->getResponse('{"id":"b2bdba90-166c-4e0a-829d-69f26a09fd2a","type":"file","size":214}')
);
$response = $client->uploadFile(__FILE__);
self::assertTrue($response instanceof UploadFileResponse);
self::assertEquals('b2bdba90-166c-4e0a-829d-69f26a09fd2a', $response->getId());
self::assertEquals('file', $response->getType());
self::assertEquals(214, $response->getSize());
}
/**
* @group("upload")
* @throws \Exception
*/
public function testGetFileById()
{
$fileId = 'b2bdba90-166c-4e0a-829d-69f26a09fd2a';
$client = self::getApiClient(
null,
null,
false,
$this->getJsonResponse('getFile')
);
$response = $client->getFileById($fileId);
self::assertTrue($response instanceof FullFileResponse);
self::assertEquals($fileId, $response->getId());
}
}

View File

@ -0,0 +1 @@
{"id":"b2bdba90-166c-4e0a-829d-69f26a09fd2a","type":"file","size":214,"Url":"https://s3.eu-central-1.amazonaws.com/mg-node-files/files/21/b2bdba90-166c-4e0a-829d-69f26a09fd2a"}