ServiceVersionV2Test.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Routing;
  7. class ServiceVersionV2Test extends \Magento\Webapi\Routing\BaseService
  8. {
  9. protected function setUp()
  10. {
  11. $this->_version = 'V2';
  12. $this->_soapService = 'testModule1AllSoapAndRestV2';
  13. $this->_restResourcePath = "/{$this->_version}/testmodule1/";
  14. }
  15. /**
  16. * Test to assert overriding of the existing 'Item' api in V2 version of the same service
  17. */
  18. public function testItem()
  19. {
  20. $itemId = 1;
  21. $serviceInfo = [
  22. 'rest' => [
  23. 'resourcePath' => $this->_restResourcePath . $itemId,
  24. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  25. ],
  26. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Item'],
  27. ];
  28. $requestData = ['id' => $itemId];
  29. $item = $this->_webApiCall($serviceInfo, $requestData);
  30. // Asserting for additional attribute returned by the V2 api
  31. $this->assertEquals(1, $item['price'], 'Item was retrieved unsuccessfully from V2');
  32. }
  33. /**
  34. * Test fetching all items
  35. */
  36. public function testItems()
  37. {
  38. $itemArr = [
  39. ['id' => 1, 'name' => 'testProduct1', 'price' => '1'],
  40. ['id' => 2, 'name' => 'testProduct2', 'price' => '2'],
  41. ];
  42. $serviceInfo = [
  43. 'rest' => [
  44. 'resourcePath' => $this->_restResourcePath,
  45. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  46. ],
  47. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Items'],
  48. ];
  49. $item = $this->_webApiCall($serviceInfo);
  50. $this->assertEquals($itemArr, $item, 'Items were not retrieved');
  51. }
  52. /**
  53. * Test fetching items when filters are applied
  54. *
  55. * @param string[] $filters
  56. * @param array $expectedResult
  57. * @dataProvider itemsWithFiltersDataProvider
  58. */
  59. public function testItemsWithFilters($filters, $expectedResult)
  60. {
  61. $restFilter = '';
  62. foreach ($filters as $filterItemKey => $filterMetadata) {
  63. foreach ($filterMetadata as $filterMetaKey => $filterMetaValue) {
  64. $paramsDelimiter = empty($restFilter) ? '?' : '&';
  65. $restFilter .= "{$paramsDelimiter}filters[{$filterItemKey}][{$filterMetaKey}]={$filterMetaValue}";
  66. }
  67. }
  68. $serviceInfo = [
  69. 'rest' => [
  70. 'resourcePath' => $this->_restResourcePath . $restFilter,
  71. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  72. ],
  73. 'soap' => [
  74. 'service' => $this->_soapService,
  75. 'operation' => $this->_soapService . 'Items',
  76. ],
  77. ];
  78. $requestData = [];
  79. if (!empty($filters)) {
  80. $requestData['filters'] = $filters;
  81. }
  82. $item = $this->_webApiCall($serviceInfo, $requestData);
  83. $this->assertEquals($expectedResult, $item, 'Filtration does not seem to work correctly.');
  84. }
  85. public function itemsWithFiltersDataProvider()
  86. {
  87. $firstItem = ['id' => 1, 'name' => 'testProduct1', 'price' => 1];
  88. $secondItem = ['id' => 2, 'name' => 'testProduct2', 'price' => 2];
  89. return [
  90. 'Both items filter' => [
  91. [
  92. ['field' => 'id', 'conditionType' => 'eq','value' => 1],
  93. ['field' => 'id', 'conditionType' => 'eq','value' => 2],
  94. ],
  95. [$firstItem, $secondItem],
  96. ],
  97. 'First item filter' => [[['field' => 'id', 'conditionType' => 'eq','value' => 1]], [$firstItem]],
  98. 'Second item filter' => [[['field' => 'id', 'conditionType' => 'eq','value' => 2]], [$secondItem]],
  99. 'Empty filter' => [[], [$firstItem, $secondItem]],
  100. ];
  101. }
  102. /**
  103. * Test update item
  104. */
  105. public function testUpdate()
  106. {
  107. $itemId = 1;
  108. $serviceInfo = [
  109. 'rest' => [
  110. 'resourcePath' => $this->_restResourcePath . $itemId,
  111. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  112. ],
  113. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Update'],
  114. ];
  115. $requestData = ['entityItem' => ['id' => $itemId, 'name' => 'testName', 'price' => '4']];
  116. $item = $this->_webApiCall($serviceInfo, $requestData);
  117. $this->assertEquals('Updated' . $requestData['entityItem']['name'], $item['name'], 'Item update failed');
  118. }
  119. /**
  120. * Test to assert presence of new 'delete' api added in V2 version of the same service
  121. */
  122. public function testDelete()
  123. {
  124. $itemId = 1;
  125. $serviceInfo = [
  126. 'rest' => [
  127. 'resourcePath' => $this->_restResourcePath . $itemId,
  128. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  129. ],
  130. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Delete'],
  131. ];
  132. $requestData = ['id' => $itemId, 'name' => 'testName'];
  133. $item = $this->_webApiCall($serviceInfo, $requestData);
  134. $this->assertEquals($itemId, $item['id'], "Item delete failed");
  135. }
  136. }