ServiceSerializationTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\DataObjectSerialization;
  7. class ServiceSerializationTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  8. {
  9. /**
  10. * @var string
  11. */
  12. protected $_version;
  13. /**
  14. * @var string
  15. */
  16. protected $_restResourcePath;
  17. protected function setUp()
  18. {
  19. $this->_markTestAsRestOnly();
  20. $this->_version = 'V1';
  21. $this->_restResourcePath = "/{$this->_version}/testmodule4/";
  22. }
  23. /**
  24. * Test simple request data
  25. */
  26. public function testGetServiceCall()
  27. {
  28. $itemId = 1;
  29. $name = 'Test';
  30. $serviceInfo = [
  31. 'rest' => [
  32. 'resourcePath' => $this->_restResourcePath . $itemId,
  33. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  34. ],
  35. ];
  36. $item = $this->_webApiCall($serviceInfo, []);
  37. $this->assertEquals($itemId, $item['entity_id'], 'id field returned incorrectly');
  38. $this->assertEquals($name, $item['name'], 'name field returned incorrectly');
  39. }
  40. /**
  41. * Test multiple params with Data Object
  42. */
  43. public function testUpdateServiceCall()
  44. {
  45. $itemId = 1;
  46. $name = 'Test';
  47. $serviceInfo = [
  48. 'rest' => [
  49. 'resourcePath' => $this->_restResourcePath . $itemId,
  50. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  51. ],
  52. ];
  53. $item = $this->_webApiCall($serviceInfo, ['request' => ['name' => $name]]);
  54. $this->assertEquals($itemId, $item['entity_id'], 'id field returned incorrectly');
  55. $this->assertEquals($name, $item['name'], 'name field returned incorrectly');
  56. }
  57. /**
  58. * Test nested Data Object
  59. */
  60. public function testNestedDataObjectCall()
  61. {
  62. $itemId = 1;
  63. $name = 'Test';
  64. $serviceInfo = [
  65. 'rest' => [
  66. 'resourcePath' => $this->_restResourcePath . $itemId . '/nested',
  67. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  68. ],
  69. ];
  70. $item = $this->_webApiCall($serviceInfo, ['request' => ['details' => ['name' => $name]]]);
  71. $this->assertEquals($itemId, $item['entity_id'], 'id field returned incorrectly');
  72. $this->assertEquals($name, $item['name'], 'name field returned incorrectly');
  73. }
  74. public function testScalarResponse()
  75. {
  76. $id = 2;
  77. $serviceInfo = [
  78. 'rest' => [
  79. 'resourcePath' => "{$this->_restResourcePath}scalar/{$id}",
  80. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  81. ],
  82. ];
  83. $this->assertEquals($id, $this->_webApiCall($serviceInfo), 'Scalar service output is serialized incorrectly.');
  84. }
  85. public function testExtensibleCall()
  86. {
  87. $id = 2;
  88. $serviceInfo = [
  89. 'rest' => [
  90. 'resourcePath' => "{$this->_restResourcePath}extensibleDataObject/{$id}",
  91. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  92. ],
  93. ];
  94. $name = 'Magento';
  95. $requestData = [
  96. 'name' => $name,
  97. ];
  98. $item = $this->_webApiCall($serviceInfo, ['request' => $requestData]);
  99. $this->assertEquals($id, $item['entity_id'], 'id field returned incorrectly');
  100. $this->assertEquals($name, $item['name'], 'name field returned incorrectly');
  101. }
  102. }