RequestIdOverrideTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Routing;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. /**
  9. * Class to test overriding request body identifier property with id passed in url path parameter
  10. *
  11. * Refer to \Magento\Framework\Webapi\Rest\Request::overrideRequestBodyIdWithPathParam
  12. */
  13. class RequestIdOverrideTest extends \Magento\Webapi\Routing\BaseService
  14. {
  15. /**
  16. * @var string
  17. */
  18. protected $_version;
  19. /**
  20. * @var string
  21. */
  22. protected $_restResourcePath;
  23. /**
  24. * @var \Magento\TestModule5\Service\V1\Entity\AllSoapAndRestFactory
  25. */
  26. protected $itemFactory;
  27. /**
  28. * @var string
  29. */
  30. protected $_soapService = 'testModule5AllSoapAndRest';
  31. protected function setUp()
  32. {
  33. $this->_markTestAsRestOnly('Request Id overriding is a REST based feature.');
  34. $this->_version = 'V1';
  35. $this->_restResourcePath = "/{$this->_version}/TestModule5/";
  36. $this->itemFactory = Bootstrap::getObjectManager()
  37. ->create(\Magento\TestModule5\Service\V1\Entity\AllSoapAndRestFactory::class);
  38. }
  39. public function testOverride()
  40. {
  41. $itemId = 1;
  42. $incorrectItemId = 2;
  43. $serviceInfo = [
  44. 'rest' => [
  45. 'resourcePath' => $this->_restResourcePath . $itemId,
  46. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  47. ],
  48. ];
  49. $item = $this->itemFactory->create()
  50. ->setEntityId($incorrectItemId)
  51. ->setName('test');
  52. $requestData = ['entityItem' => $item->__toArray()];
  53. $item = $this->_webApiCall($serviceInfo, $requestData);
  54. $this->assertEquals(
  55. $itemId,
  56. $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
  57. 'Identifier overriding failed.'
  58. );
  59. }
  60. public function testOverrideNested()
  61. {
  62. $firstItemId = 1;
  63. $secondItemId = 11;
  64. $incorrectItemId = 2;
  65. $serviceInfo = [
  66. 'rest' => [
  67. 'resourcePath' => $this->_restResourcePath . $firstItemId . '/nestedResource/' . $secondItemId,
  68. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  69. ],
  70. ];
  71. $item = $this->itemFactory->create()
  72. ->setEntityId($incorrectItemId)
  73. ->setName('test');
  74. $requestData = ['entityItem' => $item->__toArray()];
  75. $item = $this->_webApiCall($serviceInfo, $requestData);
  76. $this->assertEquals(
  77. $secondItemId,
  78. $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
  79. 'Identifier overriding failed for nested resource request.'
  80. );
  81. }
  82. public function testOverrideAdd()
  83. {
  84. $itemId = 1;
  85. $serviceInfo = [
  86. 'rest' => [
  87. 'resourcePath' => $this->_restResourcePath . $itemId,
  88. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  89. ],
  90. ];
  91. $item = $this->itemFactory->create()
  92. ->setName('test');
  93. $requestData = ['entityItem' => $item->__toArray()];
  94. $item = $this->_webApiCall($serviceInfo, $requestData);
  95. $this->assertEquals(
  96. $itemId,
  97. $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
  98. 'Identifier replacing failed.'
  99. );
  100. }
  101. /**
  102. * Test if the framework works if camelCase path parameters are provided instead of valid snake case ones.
  103. * Webapi Framework currently accepts both cases due to shortcoming in Serialization (MAGETWO-29833).
  104. * Unless it is fixed this use case is valid.
  105. */
  106. public function testAddCaseMismatch()
  107. {
  108. $itemId = 1;
  109. $incorrectItemId = 2;
  110. $serviceInfo = [
  111. 'rest' => [
  112. 'resourcePath' => $this->_restResourcePath . $itemId,
  113. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  114. ],
  115. ];
  116. $requestData = ['entityItem' => ['entityId' => $incorrectItemId, 'name' => 'test']];
  117. $item = $this->_webApiCall($serviceInfo, $requestData);
  118. $this->assertEquals(
  119. $itemId,
  120. $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
  121. 'Identifier overriding failed.'
  122. );
  123. }
  124. public function testOverrideWithScalarValues()
  125. {
  126. $firstItemId = 1;
  127. $secondItemId = 11;
  128. $incorrectItemId = 2;
  129. $serviceInfo = [
  130. 'rest' => [
  131. 'resourcePath' => "/{$this->_version}/TestModule5/OverrideService/" . $firstItemId
  132. . '/nestedResource/' . $secondItemId,
  133. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  134. ],
  135. ];
  136. $requestData = ['entity_id' => $incorrectItemId, 'name' => 'test', 'orders' => true];
  137. $item = $this->_webApiCall($serviceInfo, $requestData);
  138. $this->assertEquals(
  139. $secondItemId,
  140. $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
  141. 'Identifier overriding failed.'
  142. );
  143. }
  144. }