-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'mainline/2.4-develop' into 2.4.1-develop
- Loading branch information
Showing
24 changed files
with
321 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
app/code/Magento/Catalog/Model/Product/Webapi/Rest/RequestTypeBasedDeserializer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\Model\Product\Webapi\Rest; | ||
|
||
use Magento\Framework\Webapi\Rest\Request\DeserializerInterface; | ||
use Magento\Framework\Webapi\Rest\Request\DeserializerFactory; | ||
use Magento\Framework\Webapi\Rest\Request; | ||
|
||
/** | ||
* Class RequestTypeBasedDeserializer | ||
* | ||
* Used for deserialization rest request body. | ||
* Runs appropriate deserialization class object based on request body content type. | ||
*/ | ||
class RequestTypeBasedDeserializer implements DeserializerInterface | ||
{ | ||
/** | ||
* @var Request | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @var DeserializerFactory | ||
*/ | ||
private $deserializeFactory; | ||
|
||
/** | ||
* RequestTypeBasedDeserializer constructor. | ||
* | ||
* @param DeserializerFactory $deserializeFactory | ||
* @param Request $request | ||
*/ | ||
public function __construct( | ||
DeserializerFactory $deserializeFactory, | ||
Request $request | ||
) { | ||
$this->deserializeFactory = $deserializeFactory; | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* | ||
* Parse request body into array of params with identifying request body content type | ||
* to use appropriate instance of deserializer class | ||
* | ||
* @param string $body Posted content from request | ||
* @return array|null Return NULL if content is invalid | ||
* @throws \Magento\Framework\Exception\InputException | ||
* @throws \Magento\Framework\Webapi\Exception | ||
*/ | ||
public function deserialize($body) | ||
{ | ||
$deserializer = $this->deserializeFactory->get($this->request->getContentType()); | ||
return $deserializer->deserialize($body); | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
.../Magento/Catalog/Test/Unit/Model/Product/Webapi/Rest/RequestTypeBasedDeserializerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Catalog\Test\Unit\Model\Product\Webapi\Rest; | ||
|
||
use Magento\Catalog\Model\Product\Webapi\Rest\RequestTypeBasedDeserializer; | ||
use Magento\Framework\Webapi\Rest\Request\DeserializerFactory; | ||
use Magento\Framework\Webapi\Rest\Request; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Magento\Framework\Webapi\Rest\Request\DeserializerInterface; | ||
use Magento\Framework\Webapi\Rest\Request\Deserializer\Json as DeserializerJson; | ||
use Magento\Framework\Webapi\Rest\Request\Deserializer\Xml as DeserializerXml; | ||
use Magento\Framework\App\State; | ||
use Magento\Framework\Json\Decoder; | ||
use Magento\Framework\Serialize\Serializer\Json as SerializerJson; | ||
use Magento\Framework\Xml\Parser as ParserXml; | ||
|
||
class RequestTypeBasedDeserializerTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** @var RequestTypeBasedDeserializer */ | ||
private $requestTypeBasedDeserializer; | ||
/** | ||
* @var DeserializerFactory|MockObject | ||
*/ | ||
private $deserializeFactoryMock; | ||
|
||
/** | ||
* @var Request|MockObject | ||
*/ | ||
private $requestMock; | ||
|
||
public function setUp(): void | ||
{ | ||
/** @var DeserializerFactory|MockObject $deserializeFactoryMock */ | ||
$this->deserializeFactoryMock = $this->createMock(DeserializerFactory::class); | ||
/** @var Request|MockObject $requestMock */ | ||
$this->requestMock = $this->createMock(Request::class); | ||
/** @var requestTypeBasedDeserializer */ | ||
$this->requestTypeBasedDeserializer = new RequestTypeBasedDeserializer( | ||
$this->deserializeFactoryMock, | ||
$this->requestMock | ||
); | ||
} | ||
|
||
/** | ||
* Test RequestTypeBasedDeserializer::deserializeMethod() | ||
* | ||
* @dataProvider getDeserializerDataProvider | ||
* @param string $body | ||
* @param string $contentType | ||
* @param DeserializerInterface $deserializer | ||
* @param array $expectedResult | ||
* @throws \Magento\Framework\Exception\InputException | ||
* @throws \Magento\Framework\Webapi\Exception | ||
*/ | ||
public function testDeserialize( | ||
string $body, | ||
string $contentType, | ||
DeserializerInterface $deserializer, | ||
array $expectedResult | ||
): void { | ||
$this->requestMock->method('getContentType') | ||
->willReturn($contentType); | ||
$this->deserializeFactoryMock->expects($this->any()) | ||
->method('get') | ||
->with($contentType) | ||
->willReturn($deserializer); | ||
$this->assertEquals($expectedResult, $this->requestTypeBasedDeserializer->deserialize($body)); | ||
} | ||
|
||
public function getDeserializerDataProvider(): array | ||
{ | ||
return [ | ||
'request body with xml data' => [ | ||
'body' => '<products> | ||
<product> | ||
<sku>testSku1</sku> | ||
<name>testName1</name> | ||
<weight>10</weight> | ||
<attribute_set_id>4</attribute_set_id> | ||
<status>1</status> | ||
</product> | ||
</products>', | ||
'content-type' => 'application/xml', | ||
'deserializer' => $this->prepareXmlDeserializer(), | ||
'expectedResult' => [ | ||
'product' => [ | ||
'sku' => 'testSku1', | ||
'name' => 'testName1', | ||
'weight' => '10', | ||
'attribute_set_id' => '4', | ||
'status' => '1' | ||
] | ||
] | ||
], | ||
'request body with json data' => [ | ||
'body' => '{ | ||
"product": { | ||
"sku": "testSku2", | ||
"name": "testName2", | ||
"weight": 5, | ||
"attribute_set_id": 4, | ||
"status": 0 | ||
} | ||
}', | ||
'content-type' => 'application/json', | ||
'deserializer' => $this->prepareJsonDeserializer(), | ||
'expectedResult' => [ | ||
'product' => [ | ||
'sku' => 'testSku2', | ||
'name' => 'testName2', | ||
'weight' => 5, | ||
'attribute_set_id' => 4, | ||
'status' => 0 | ||
] | ||
] | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* Creates Json Deserializer instance with some mocked parameters | ||
* | ||
* @return DeserializerJson | ||
*/ | ||
private function prepareJsonDeserializer(): DeserializerJson | ||
{ | ||
/** @var Decoder|MockObject $decoder */ | ||
$decoder = $this->createMock(Decoder::class); | ||
/** @var State|MockObject $appStateMock */ | ||
$appStateMock = $this->createMock(State::class); | ||
$serializer = new SerializerJson(); | ||
return new DeserializerJson($decoder, $appStateMock, $serializer); | ||
} | ||
|
||
/** | ||
* Creates XML Deserializer instance with some mocked parameters | ||
* | ||
* @return DeserializerXml | ||
*/ | ||
private function prepareXmlDeserializer(): DeserializerXml | ||
{ | ||
$parserXml = new ParserXml(); | ||
/** @var State|MockObject $appStateMock */ | ||
$appStateMock = $this->createMock(State::class); | ||
return new DeserializerXml($parserXml, $appStateMock); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.