123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Webapi\Routing;
- use Magento\TestFramework\Helper\Bootstrap;
- /**
- * Class to test overriding request body identifier property with id passed in url path parameter
- *
- * Refer to \Magento\Framework\Webapi\Rest\Request::overrideRequestBodyIdWithPathParam
- */
- class RequestIdOverrideTest extends \Magento\Webapi\Routing\BaseService
- {
- /**
- * @var string
- */
- protected $_version;
- /**
- * @var string
- */
- protected $_restResourcePath;
- /**
- * @var \Magento\TestModule5\Service\V1\Entity\AllSoapAndRestFactory
- */
- protected $itemFactory;
- /**
- * @var string
- */
- protected $_soapService = 'testModule5AllSoapAndRest';
- protected function setUp()
- {
- $this->_markTestAsRestOnly('Request Id overriding is a REST based feature.');
- $this->_version = 'V1';
- $this->_restResourcePath = "/{$this->_version}/TestModule5/";
- $this->itemFactory = Bootstrap::getObjectManager()
- ->create(\Magento\TestModule5\Service\V1\Entity\AllSoapAndRestFactory::class);
- }
- public function testOverride()
- {
- $itemId = 1;
- $incorrectItemId = 2;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => $this->_restResourcePath . $itemId,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- ];
- $item = $this->itemFactory->create()
- ->setEntityId($incorrectItemId)
- ->setName('test');
- $requestData = ['entityItem' => $item->__toArray()];
- $item = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals(
- $itemId,
- $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
- 'Identifier overriding failed.'
- );
- }
- public function testOverrideNested()
- {
- $firstItemId = 1;
- $secondItemId = 11;
- $incorrectItemId = 2;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => $this->_restResourcePath . $firstItemId . '/nestedResource/' . $secondItemId,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- ];
- $item = $this->itemFactory->create()
- ->setEntityId($incorrectItemId)
- ->setName('test');
- $requestData = ['entityItem' => $item->__toArray()];
- $item = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals(
- $secondItemId,
- $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
- 'Identifier overriding failed for nested resource request.'
- );
- }
- public function testOverrideAdd()
- {
- $itemId = 1;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => $this->_restResourcePath . $itemId,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- ];
- $item = $this->itemFactory->create()
- ->setName('test');
- $requestData = ['entityItem' => $item->__toArray()];
- $item = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals(
- $itemId,
- $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
- 'Identifier replacing failed.'
- );
- }
- /**
- * Test if the framework works if camelCase path parameters are provided instead of valid snake case ones.
- * Webapi Framework currently accepts both cases due to shortcoming in Serialization (MAGETWO-29833).
- * Unless it is fixed this use case is valid.
- */
- public function testAddCaseMismatch()
- {
- $itemId = 1;
- $incorrectItemId = 2;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => $this->_restResourcePath . $itemId,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- ];
- $requestData = ['entityItem' => ['entityId' => $incorrectItemId, 'name' => 'test']];
- $item = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals(
- $itemId,
- $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
- 'Identifier overriding failed.'
- );
- }
- public function testOverrideWithScalarValues()
- {
- $firstItemId = 1;
- $secondItemId = 11;
- $incorrectItemId = 2;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => "/{$this->_version}/TestModule5/OverrideService/" . $firstItemId
- . '/nestedResource/' . $secondItemId,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- ];
- $requestData = ['entity_id' => $incorrectItemId, 'name' => 'test', 'orders' => true];
- $item = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals(
- $secondItemId,
- $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
- 'Identifier overriding failed.'
- );
- }
- }
|