OrderGetTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Sales\Service\V1;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Magento\Framework\Webapi\Rest\Request;
  10. use Magento\Sales\Api\Data\OrderInterface;
  11. use Magento\Sales\Model\Order;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\TestFramework\TestCase\WebapiAbstract;
  14. class OrderGetTest extends WebapiAbstract
  15. {
  16. const RESOURCE_PATH = '/V1/orders';
  17. const SERVICE_READ_NAME = 'salesOrderRepositoryV1';
  18. const SERVICE_VERSION = 'V1';
  19. const ORDER_INCREMENT_ID = '100000001';
  20. /**
  21. * @var ObjectManagerInterface
  22. */
  23. private $objectManager;
  24. /**
  25. * @inheritdoc
  26. */
  27. protected function setUp(): void
  28. {
  29. $this->objectManager = Bootstrap::getObjectManager();
  30. }
  31. /**
  32. * Checks order attributes.
  33. *
  34. * @magentoApiDataFixture Magento/Sales/_files/order.php
  35. */
  36. public function testOrderGet(): void
  37. {
  38. $expectedOrderData = [
  39. 'base_subtotal' => '100.0000',
  40. 'subtotal' => '100.0000',
  41. 'customer_is_guest' => '1',
  42. 'increment_id' => self::ORDER_INCREMENT_ID,
  43. ];
  44. $expectedPayments = [
  45. 'method' => 'checkmo',
  46. 'additional_information' => [
  47. 0 => '11122', // last transaction id
  48. // metadata
  49. 1 => json_encode([
  50. 'type' => 'free',
  51. 'fraudulent' => false
  52. ])
  53. ]
  54. ];
  55. $expectedBillingAddressNotEmpty = [
  56. 'city',
  57. 'postcode',
  58. 'lastname',
  59. 'street',
  60. 'region',
  61. 'telephone',
  62. 'country_id',
  63. 'firstname',
  64. ];
  65. $expectedShippingAddress = [
  66. 'address_type' => 'shipping',
  67. 'city' => 'Los Angeles',
  68. 'email' => 'customer@null.com',
  69. 'postcode' => '11111',
  70. 'region' => 'CA'
  71. ];
  72. $result = $this->makeServiceCall(self::ORDER_INCREMENT_ID);
  73. foreach ($expectedOrderData as $field => $value) {
  74. self::assertArrayHasKey($field, $result);
  75. self::assertEquals($value, $result[$field]);
  76. }
  77. self::assertArrayHasKey('payment', $result);
  78. foreach ($expectedPayments as $field => $value) {
  79. self::assertEquals($value, $result['payment'][$field]);
  80. }
  81. self::assertArrayHasKey('billing_address', $result);
  82. foreach ($expectedBillingAddressNotEmpty as $field) {
  83. self::assertArrayHasKey($field, $result['billing_address']);
  84. }
  85. self::assertArrayHasKey('extension_attributes', $result);
  86. self::assertArrayHasKey('shipping_assignments', $result['extension_attributes']);
  87. $shippingAssignments = $result['extension_attributes']['shipping_assignments'];
  88. self::assertCount(1, $shippingAssignments);
  89. $shippingAddress = $shippingAssignments[0]['shipping']['address'];
  90. foreach ($expectedShippingAddress as $key => $value) {
  91. self::assertArrayHasKey($key, $shippingAddress);
  92. self::assertEquals($value, $shippingAddress[$key]);
  93. }
  94. //check that nullable fields were marked as optional and were not sent
  95. foreach ($result as $value) {
  96. self::assertNotNull($value);
  97. }
  98. }
  99. /**
  100. * Checks order extension attributes.
  101. *
  102. * @magentoApiDataFixture Magento/Sales/_files/order_with_tax.php
  103. */
  104. public function testOrderGetExtensionAttributes(): void
  105. {
  106. $expectedTax = [
  107. 'code' => 'US-NY-*-Rate 1',
  108. 'type' => 'shipping'
  109. ];
  110. $result = $this->makeServiceCall(self::ORDER_INCREMENT_ID);
  111. $appliedTaxes = $result['extension_attributes']['applied_taxes'];
  112. self::assertEquals($expectedTax['code'], $appliedTaxes[0]['code']);
  113. $appliedTaxes = $result['extension_attributes']['item_applied_taxes'];
  114. self::assertEquals($expectedTax['type'], $appliedTaxes[0]['type']);
  115. self::assertNotEmpty($appliedTaxes[0]['applied_taxes']);
  116. self::assertEquals(true, $result['extension_attributes']['converting_from_quote']);
  117. self::assertArrayHasKey('payment_additional_info', $result['extension_attributes']);
  118. self::assertNotEmpty($result['extension_attributes']['payment_additional_info']);
  119. }
  120. /**
  121. * Checks if the order contains product option attributes.
  122. *
  123. * @magentoApiDataFixture Magento/Sales/_files/order_with_bundle.php
  124. */
  125. public function testGetOrderWithProductOption(): void
  126. {
  127. $expected = [
  128. 'extension_attributes' => [
  129. 'bundle_options' => [
  130. [
  131. 'option_id' => 1,
  132. 'option_selections' => [1],
  133. 'option_qty' => 1
  134. ]
  135. ]
  136. ]
  137. ];
  138. $result = $this->makeServiceCall(self::ORDER_INCREMENT_ID);
  139. $bundleProduct = $this->getBundleProduct($result['items']);
  140. self::assertNotEmpty($bundleProduct, '"Bundle Product" should not be empty.');
  141. self::assertNotEmpty($bundleProduct['product_option'], '"Product Option" should not be empty.');
  142. self::assertEquals($expected, $bundleProduct['product_option']);
  143. }
  144. /**
  145. * Gets order by increment ID.
  146. *
  147. * @param string $incrementId
  148. * @return OrderInterface
  149. */
  150. private function getOrder(string $incrementId): OrderInterface
  151. {
  152. /** @var Order $order */
  153. $order = $this->objectManager->create(Order::class);
  154. $order->loadByIncrementId($incrementId);
  155. return $order;
  156. }
  157. /**
  158. * Makes service call.
  159. *
  160. * @param string $incrementId
  161. * @return array
  162. */
  163. private function makeServiceCall(string $incrementId): array
  164. {
  165. $order = $this->getOrder($incrementId);
  166. $serviceInfo = [
  167. 'rest' => [
  168. 'resourcePath' => self::RESOURCE_PATH . '/' . $order->getId(),
  169. 'httpMethod' => Request::HTTP_METHOD_GET,
  170. ],
  171. 'soap' => [
  172. 'service' => self::SERVICE_READ_NAME,
  173. 'serviceVersion' => self::SERVICE_VERSION,
  174. 'operation' => self::SERVICE_READ_NAME . 'get',
  175. ],
  176. ];
  177. return $this->_webApiCall($serviceInfo, ['id' => $order->getId()]);
  178. }
  179. /**
  180. * Gets a bundle product from the result.
  181. *
  182. * @param array $items
  183. * @return array
  184. */
  185. private function getBundleProduct(array $items): array
  186. {
  187. foreach ($items as $item) {
  188. if ($item['product_type'] == 'bundle') {
  189. return $item;
  190. }
  191. }
  192. return [];
  193. }
  194. }