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

restructurized models list, ExpressionLanguage for property exclusion, tests for excluded properties and inline JSON

This commit is contained in:
Pavel 2020-10-01 12:43:07 +03:00
parent 95b7c27e6c
commit c378ad5385
21 changed files with 382 additions and 43 deletions

View File

@ -31,7 +31,8 @@
"php-http/httplug": "^2.2",
"php-http/message-factory": "^1.0",
"php-http/discovery": "^1.12",
"php-http/multipart-stream-builder": "^1.1"
"php-http/multipart-stream-builder": "^1.1",
"symfony/expression-language": "^5.1"
},
"require-dev": {
"phpunit/phpunit": "^9.3",

View File

@ -8,6 +8,7 @@
<rule ref="rulesets/naming.xml">
<exclude name="ShortVariable" />
<exclude name="LongVariable" />
<exclude name="LongClassName" />
</rule>
<rule ref="rulesets/cleancode.xml">
<exclude name="StaticAccess" />
@ -42,6 +43,11 @@
<property name="maximum" value="30" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/LongClassName">
<properties>
<property name="maximum" value="80" />
</properties>
</rule>
<exclude-pattern>tests/*</exclude-pattern>
</ruleset>

View File

@ -13,7 +13,7 @@
namespace RetailCrm\Component\Exception;
use Exception;
use RetailCrm\Model\Response\Body\ErrorResponseBody;
use RetailCrm\Model\Response\ErrorResponseBody;
use Throwable;
/**
@ -41,7 +41,7 @@ class TopApiException extends Exception
/**
* TopApiException constructor.
*
* @param \RetailCrm\Model\Response\Body\ErrorResponseBody $responseBody
* @param \RetailCrm\Model\Response\ErrorResponseBody $responseBody
* @param string|null $requestId
* @param \Throwable|null $previous
*/

View File

@ -195,8 +195,9 @@ class JsonDeserializationVisitor extends AbstractVisitor implements Deserializat
if (in_array('InlineJsonBody', $metadata->groups ?? [])) {
if (!array_key_exists($metadata->serializedName, $data)) {
throw new RuntimeException(sprintf(
'Cannot find expected key in the data: %s',
$metadata->serializedName
'Cannot find expected key "%s" in the collection: %s',
$metadata->serializedName,
implode(', ', array_keys($data))
));
}

View File

@ -22,6 +22,8 @@ use Psr\Container\ContainerInterface;
use RetailCrm\Component\Constants;
use RetailCrm\Component\JMS\Factory\JsonDeserializationVisitorFactory;
use RetailCrm\Interfaces\FactoryInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use JMS\Serializer\Expression\ExpressionEvaluator;
/**
* Class SerializerFactory
@ -138,6 +140,7 @@ class SerializerFactory implements FactoryInterface
->setSerializationVisitor('json', new JsonSerializationVisitorFactory())
->setDeserializationVisitor('json', new JsonDeserializationVisitorFactory())
->setSerializationContextFactory(new SerializationContextFactory())
->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
->build();
}
}

View File

@ -0,0 +1,61 @@
<?php
/**
* PHP version 7.4
*
* @category CategoryInfo
* @package RetailCrm\Model\Entity
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Entity;
use JMS\Serializer\Annotation as JMS;
/**
* Class CategoryInfo
*
* @category CategoryInfo
* @package RetailCrm\Model\Entity
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license https://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class CategoryInfo
{
/**
* @var int $childrenCategoryId
*
* @JMS\Type("int")
* @JMS\SerializedName("children_category_id")
*/
public $childrenCategoryId;
/**
* @var bool $isLeafCategory
*
* @JMS\Type("bool")
* @JMS\SerializedName("is_leaf_category")
*/
public $isLeafCategory;
/**
* @var int $level
*
* @JMS\Type("int")
* @JMS\SerializedName("level")
*/
public $level;
/**
* @var array $multiLanguageNames
*
* @JMS\Type("array")
* @JMS\SerializedName("multi_language_names")
* @JMS\Groups(groups={"InlineJsonBody"})
*/
public $multiLanguageNames;
}

View File

@ -0,0 +1,66 @@
<?php
/**
* PHP version 7.4
*
* @category SellerCategoryTreeQuery
* @package RetailCrm\Model\Request\AliExpressSolution
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Request\AliExpressSolution;
use RetailCrm\Model\Request\BaseRequest;
use JMS\Serializer\Annotation as JMS;
use RetailCrm\Model\Response\AliExpressSolution\SellerCategoryTreeQueryResponse;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class SellerCategoryTreeQuery
*
* @category SellerCategoryTreeQuery
* @package RetailCrm\Model\Request\AliExpressSolution
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license https://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class SellerCategoryTreeQuery extends BaseRequest
{
/**
* @var int $categoryId
*
* @JMS\Type("int")
* @JMS\SerializedName("category_id")
* @Assert\NotBlank()
* @Assert\PositiveOrZero()
*/
public $categoryId;
/**
* @var bool $filterNoPermission
*
* @JMS\Type("bool")
* @JMS\SerializedName("filter_no_permission")
* @Assert\NotNull()
*/
public $filterNoPermission = false;
/**
* @inheritDoc
*/
public function getMethod(): string
{
return 'aliexpress.solution.seller.category.tree.query';
}
/**
* @inheritDoc
*/
public function getExpectedResponse(): string
{
return SellerCategoryTreeQueryResponse::class;
}
}

View File

@ -91,7 +91,7 @@ abstract class BaseRequest
*
* @JMS\Type("bool")
* @JMS\SerializedName("simplify")
* @JMS\Accessor(getter="isSimplify")
* @JMS\Exclude(if="!object.simplify")
*/
public $simplify = false;
@ -132,14 +132,6 @@ abstract class BaseRequest
$this->method = $this->getMethod();
}
/**
* @return bool|null
*/
public function isSimplify(): ?bool
{
return $this->simplify ? true : null;
}
/**
* @param string $method
*

View File

@ -4,21 +4,22 @@
* PHP version 7.3
*
* @category HttpDnsGetRequest
* @package RetailCrm\Model\Request
* @package RetailCrm\Model\Request\Taobao
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT https://mit-license.org
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Request;
namespace RetailCrm\Model\Request\Taobao;
use RetailCrm\Model\Response\HttpDnsGetResponse;
use RetailCrm\Model\Request\BaseRequest;
use RetailCrm\Model\Response\Taobao\HttpDnsGetResponse;
/**
* Class HttpDnsGetRequest
*
* @category HttpDnsGetRequest
* @package RetailCrm\Model\Request
* @package RetailCrm\Model\Request\Taobao
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT https://mit-license.org
* @link http://retailcrm.ru

View File

@ -0,0 +1,44 @@
<?php
/**
* PHP version 7.4
*
* @category SellerCategoryTreeQueryResponseData
* @package RetailCrm\Model\Response\AliExpressSolution\Data
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Response\AliExpressSolution\Data;
use JMS\Serializer\Annotation as JMS;
/**
* Class SellerCategoryTreeQueryResponseData
*
* @category SellerCategoryTreeQueryResponseData
* @package RetailCrm\Model\Response\AliExpressSolution\Data
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license https://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class SellerCategoryTreeQueryResponseData
{
/**
* @var SellerCategoryTreeQueryResponseDataChildrenCategoryList
*
* @JMS\Type("RetailCrm\Model\Response\AliExpressSolution\Data\SellerCategoryTreeQueryResponseDataChildrenCategoryList")
* @JMS\SerializedName("children_category_list")
*/
public $childrenCategoryList;
/**
* @var bool $isSuccess
*
* @JMS\Type("bool")
* @JMS\SerializedName("is_success")
*/
public $isSuccess;
}

View File

@ -0,0 +1,37 @@
<?php
/**
* PHP version 7.4
*
* @category SellerCategoryTreeQueryResponseDataChildrenCategoryList
* @package RetailCrm\Model\Response\AliExpressSolution\Data
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Response\AliExpressSolution\Data;
use RetailCrm\Model\Entity\CategoryInfo;
use JMS\Serializer\Annotation as JMS;
/**
* Class SellerCategoryTreeQueryResponseDataChildrenCategoryList
*
* @category SellerCategoryTreeQueryResponseDataChildrenCategoryList
* @package RetailCrm\Model\Response\AliExpressSolution\Data
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license https://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class SellerCategoryTreeQueryResponseDataChildrenCategoryList
{
/**
* @var CategoryInfo[] $categoryInfo
*
* @JMS\Type("array<RetailCrm\Model\Entity\CategoryInfo>")
* @JMS\SerializedName("category_info")
*/
public $categoryInfo;
}

View File

@ -0,0 +1,38 @@
<?php
/**
* PHP version 7.4
*
* @category SellerCategoryTreeQueryResponse
* @package RetailCrm\Model\Response\AliExpressSolution
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Response\AliExpressSolution;
use RetailCrm\Model\Response\BaseResponse;
use JMS\Serializer\Annotation as JMS;
/**
* Class SellerCategoryTreeQueryResponse
*
* @category SellerCategoryTreeQueryResponse
* @package RetailCrm\Model\Response\AliExpressSolution
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license https://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class SellerCategoryTreeQueryResponse extends BaseResponse
{
/**
* @var \RetailCrm\Model\Response\AliExpressSolution\Data\SellerCategoryTreeQueryResponseData $responseData
*
* @JMS\Type("RetailCrm\Model\Response\AliExpressSolution\Data\SellerCategoryTreeQueryResponseData")
* @JMS\SerializedName("aliexpress_solution_seller_category_tree_query_response")
*/
public $responseData;
}

View File

@ -27,9 +27,9 @@ use JMS\Serializer\Annotation as JMS;
class BaseResponse implements TopResponseInterface
{
/**
* @var \RetailCrm\Model\Response\Body\ErrorResponseBody
* @var \RetailCrm\Model\Response\ErrorResponseBody
*
* @JMS\Type("RetailCrm\Model\Response\Body\ErrorResponseBody")
* @JMS\Type("RetailCrm\Model\Response\ErrorResponseBody")
* @JMS\SerializedName("error_response")
*/
public $errorResponse;

View File

@ -4,13 +4,13 @@
* PHP version 7.3
*
* @category ErrorResponseBody
* @package RetailCrm\Model\Response\Body
* @package RetailCrm\Model\Response
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Response\Body;
namespace RetailCrm\Model\Response;
use JMS\Serializer\Annotation as JMS;
@ -18,7 +18,7 @@ use JMS\Serializer\Annotation as JMS;
* Class ErrorResponseBody
*
* @category ErrorResponseBody
* @package RetailCrm\Model\Response\Body
* @package RetailCrm\Model\Response
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru

View File

@ -4,22 +4,22 @@
* PHP version 7.3
*
* @category HttpDnsGetResponseData
* @package RetailCrm\Model\Response\Data
* @package RetailCrm\Model\Response\Taobao\Data
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Response\Data;
namespace RetailCrm\Model\Response\Taobao\Data;
use JMS\Serializer\Annotation as JMS;
use RetailCrm\Model\Response\Body\InlineJsonBody\HttpDnsGetResponseResult;
use RetailCrm\Model\Response\Taobao\Result\HttpDnsGetResponseResult;
/**
* Class HttpDnsGetResponseData
*
* @category HttpDnsGetResponseData
* @package RetailCrm\Model\Response\Data
* @package RetailCrm\Model\Response\Taobao\Data
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
@ -30,7 +30,7 @@ class HttpDnsGetResponseData
/**
* @var HttpDnsGetResponseResult $result
*
* @JMS\Type("RetailCrm\Model\Response\Body\InlineJsonBody\HttpDnsGetResponseResult")
* @JMS\Type("RetailCrm\Model\Response\Taobao\Result\HttpDnsGetResponseResult")
* @JMS\SerializedName("result")
* @JMS\Groups(groups={"InlineJsonBody"})
*/

View File

@ -4,21 +4,22 @@
* PHP version 7.3
*
* @category HttpDnsGetResponse
* @package RetailCrm\Model\Response
* @package RetailCrm\Model\Response\Taobao
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Response;
namespace RetailCrm\Model\Response\Taobao;
use JMS\Serializer\Annotation as JMS;
use RetailCrm\Model\Response\BaseResponse;
/**
* Class HttpDnsGetResponse
*
* @category HttpDnsGetResponse
* @package RetailCrm\Model\Response
* @package RetailCrm\Model\Response\Taobao
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
@ -27,9 +28,9 @@ use JMS\Serializer\Annotation as JMS;
class HttpDnsGetResponse extends BaseResponse
{
/**
* @var \RetailCrm\Model\Response\Data\HttpDnsGetResponseData $responseData
* @var \RetailCrm\Model\Response\Taobao\Data\HttpDnsGetResponseData $responseData
*
* @JMS\Type("RetailCrm\Model\Response\Data\HttpDnsGetResponseData")
* @JMS\Type("RetailCrm\Model\Response\Taobao\Data\HttpDnsGetResponseData")
* @JMS\SerializedName("httpdns_get_response")
*/
public $responseData;

View File

@ -4,13 +4,13 @@
* PHP version 7.3
*
* @category HttpDnsGetResponseResult
* @package RetailCrm\Model\Response\Body\InlineJsonBody
* @package RetailCrm\Model\Response\Taobao\Result
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Response\Body\InlineJsonBody;
namespace RetailCrm\Model\Response\Taobao\Result;
use JMS\Serializer\Annotation as JMS;
@ -18,7 +18,7 @@ use JMS\Serializer\Annotation as JMS;
* Class HttpDnsGetResponseResult
*
* @category HttpDnsGetResponseResult
* @package RetailCrm\Model\Response\Body\InlineJsonBody
* @package RetailCrm\Model\Response\Taobao\Result
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru

View File

@ -13,13 +13,11 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use RetailCrm\Builder\ContainerBuilder;
use RetailCrm\Component\AppData;
use RetailCrm\Component\Authenticator\TokenAuthenticator;
use RetailCrm\Component\Constants;
use RetailCrm\Component\Environment;
use RetailCrm\Component\Logger\StdoutLogger;
use RetailCrm\Factory\FileItemFactory;
use RetailCrm\Interfaces\AppDataInterface;
use RetailCrm\Interfaces\AuthenticatorInterface;
use RetailCrm\Interfaces\FileItemFactoryInterface;
/**

View File

@ -0,0 +1,92 @@
<?php
/**
* PHP version 7.4
*
* @category JsonDeserializationVisitorTest
* @package RetailCrm\Tests\Component\JMS\Visitor\Deserialization
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Tests\Component\JMS\Visitor\Deserialization;
use RetailCrm\Component\Constants;
use RetailCrm\Model\Entity\CategoryInfo;
use RetailCrm\Model\Response\AliExpressSolution\Data\SellerCategoryTreeQueryResponseData;
use RetailCrm\Model\Response\AliExpressSolution\Data\SellerCategoryTreeQueryResponseDataChildrenCategoryList;
use RetailCrm\Model\Response\AliExpressSolution\SellerCategoryTreeQueryResponse;
use RetailCrm\Test\TestCase;
/**
* Class JsonDeserializationVisitorTest
*
* @category JsonDeserializationVisitorTest
* @package RetailCrm\Tests\Component\JMS\Visitor\Deserialization
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license https://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class JsonDeserializationVisitorTest extends TestCase
{
public function testDeserializeInlineJson()
{
$json = <<<'EOF'
{
"aliexpress_solution_seller_category_tree_query_response":{
"children_category_list":{
"category_info":[
{
"children_category_id":5090301,
"is_leaf_category":true,
"level":2,
"multi_language_names":"{ \"de\": \"Mobiltelefon\", \"ru\": \"Мобильные телефоны\", \"pt\": \"Telefonia\", \"in\": \"Ponsel\", \"en\": \"Mobile Phones\", \"it\": \"Telefoni cellulari\", \"fr\": \"Smartphones\", \"es\": \"Smartphones\", \"tr\": \"Cep Telefonu\", \"nl\": \"Mobiele telefoons\" }"
}
]
},
"is_success":true
}
}
EOF;
$expectedLangs = [
'de' => 'Mobiltelefon',
'ru' => 'Мобильные телефоны',
'pt' => 'Telefonia',
'in' => 'Ponsel',
'en' => 'Mobile Phones',
'it' => 'Telefoni cellulari',
'fr' => 'Smartphones',
'es' => 'Smartphones',
'tr' => 'Cep Telefonu',
'nl' => 'Mobiele telefoons'
];
/** @var \JMS\Serializer\SerializerInterface $serializer */
$serializer = $this->getContainer()->get(Constants::SERIALIZER);
/** @var SellerCategoryTreeQueryResponse $result */
$result = $serializer->deserialize($json, SellerCategoryTreeQueryResponse::class, 'json');
self::assertInstanceOf(SellerCategoryTreeQueryResponseData::class, $result->responseData);
self::assertInstanceOf(
SellerCategoryTreeQueryResponseDataChildrenCategoryList::class,
$result->responseData->childrenCategoryList
);
self::assertIsArray($result->responseData->childrenCategoryList->categoryInfo);
self::assertCount(1, $result->responseData->childrenCategoryList->categoryInfo);
$info = $result->responseData->childrenCategoryList->categoryInfo[0];
self::assertInstanceOf(CategoryInfo::class, $info);
self::assertEquals(5090301, $info->childrenCategoryId);
self::assertTrue($info->isLeafCategory);
self::assertEquals(2, $info->level);
self::assertIsArray($info->multiLanguageNames);
foreach ($expectedLangs as $lang => $value) {
self::assertArrayHasKey($lang, $info->multiLanguageNames);
self::assertEquals($value, $info->multiLanguageNames[$lang]);
}
}
}

View File

@ -42,6 +42,7 @@ class TopRequestFactoryTest extends TestCase
self::assertEmpty($contents);
self::assertNotEmpty($uri->getQuery());
self::assertFalse(stripos($uri->getQuery(), 'simplify'), $uri->getQuery());
self::assertNotFalse(stripos($uri->getQuery(), 'SPAIN_LOCAL_CORREOS'));
}

View File

@ -15,12 +15,9 @@ namespace RetailCrm\Tests\TopClient;
use Psr\Http\Message\RequestInterface;
use RetailCrm\Builder\ClientBuilder;
use RetailCrm\Component\AppData;
use RetailCrm\Component\Authenticator\TokenAuthenticator;
use RetailCrm\Component\Constants;
use RetailCrm\Component\Exception\TopApiException;
use RetailCrm\Model\Request\HttpDnsGetRequest;
use RetailCrm\Model\Request\Taobao\HttpDnsGetRequest;
use RetailCrm\Model\Response\BaseResponse;
use RetailCrm\Model\Response\Body\ErrorResponseBody;
use RetailCrm\Model\Response\ErrorResponseBody;
use RetailCrm\Test\ClosureRequestMatcher;
use RetailCrm\Test\TestCase;