OrderItemRepositoryTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Api;
  7. use Magento\TestFramework\TestCase\WebapiAbstract;
  8. class OrderItemRepositoryTest extends WebapiAbstract
  9. {
  10. const RESOURCE_PATH = '/V1/orders/items';
  11. const SERVICE_VERSION = 'V1';
  12. const SERVICE_NAME = 'salesOrderItemRepositoryV1';
  13. const ORDER_INCREMENT_ID = '100000001';
  14. /**
  15. * @var \Magento\TestFramework\ObjectManager
  16. */
  17. protected $objectManager;
  18. protected function setUp()
  19. {
  20. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  21. }
  22. /**
  23. * @magentoApiDataFixture Magento/Bundle/_files/order_item_with_bundle_and_options.php
  24. */
  25. public function testGet()
  26. {
  27. /** @var \Magento\Sales\Model\Order $order */
  28. $order = $this->objectManager->create(\Magento\Sales\Model\Order::class);
  29. $order->loadByIncrementId(self::ORDER_INCREMENT_ID);
  30. $orderItem = current($order->getItems());
  31. $serviceInfo = [
  32. 'rest' => [
  33. 'resourcePath' => self::RESOURCE_PATH . '/' . $orderItem->getId(),
  34. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  35. ],
  36. 'soap' => [
  37. 'service' => self::SERVICE_NAME,
  38. 'serviceVersion' => self::SERVICE_VERSION,
  39. 'operation' => self::SERVICE_NAME . 'get',
  40. ],
  41. ];
  42. $response = $this->_webApiCall($serviceInfo, ['id' => $orderItem->getId()]);
  43. $this->assertTrue(is_array($response));
  44. $this->assertOrderItem($orderItem, $response);
  45. }
  46. /**
  47. * @magentoApiDataFixture Magento/Bundle/_files/order_item_with_bundle_and_options.php
  48. */
  49. public function testGetList()
  50. {
  51. /** @var \Magento\Sales\Model\Order $order */
  52. $order = $this->objectManager->create(\Magento\Sales\Model\Order::class);
  53. $order->loadByIncrementId(self::ORDER_INCREMENT_ID);
  54. /** @var $searchCriteriaBuilder \Magento\Framework\Api\SearchCriteriaBuilder */
  55. $searchCriteriaBuilder = $this->objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
  56. /** @var $filterBuilder \Magento\Framework\Api\FilterBuilder */
  57. $filterBuilder = $this->objectManager->create(\Magento\Framework\Api\FilterBuilder::class);
  58. $searchCriteriaBuilder->addFilters(
  59. [
  60. $filterBuilder->setField('order_id')
  61. ->setValue($order->getId())
  62. ->create(),
  63. ]
  64. );
  65. $requestData = ['searchCriteria' => $searchCriteriaBuilder->create()->__toArray()];
  66. $serviceInfo = [
  67. 'rest' => [
  68. 'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($requestData),
  69. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  70. ],
  71. 'soap' => [
  72. 'service' => self::SERVICE_NAME,
  73. 'serviceVersion' => self::SERVICE_VERSION,
  74. 'operation' => self::SERVICE_NAME . 'getList',
  75. ],
  76. ];
  77. $response = $this->_webApiCall($serviceInfo, $requestData);
  78. $this->assertTrue(is_array($response));
  79. $this->assertArrayHasKey('items', $response);
  80. $this->assertCount(1, $response['items']);
  81. $this->assertTrue(is_array($response['items'][0]));
  82. $this->assertOrderItem(current($order->getItems()), $response['items'][0]);
  83. }
  84. /**
  85. * @param \Magento\Sales\Model\Order\Item $orderItem
  86. * @param array $response
  87. * @return void
  88. */
  89. protected function assertOrderItem(\Magento\Sales\Model\Order\Item $orderItem, array $response)
  90. {
  91. $bundleOption = $orderItem->getBuyRequest()->getBundleOption();
  92. $bundleOptionQty = $orderItem->getBuyRequest()->getBundleOptionQty();
  93. $this->assertArrayHasKey('product_option', $response);
  94. $this->assertArrayHasKey('extension_attributes', $response['product_option']);
  95. $this->assertArrayHasKey('bundle_options', $response['product_option']['extension_attributes']);
  96. $actualOptions = $response['product_option']['extension_attributes']['bundle_options'];
  97. $this->assertEquals(array_keys($bundleOption), array_column($actualOptions, 'option_id'));
  98. $this->assertEquals($bundleOptionQty, array_column($actualOptions, 'option_qty', 'option_id'));
  99. foreach ($actualOptions as $option) {
  100. $expectedSelections = is_array($bundleOption[$option['option_id']])
  101. ? $bundleOption[$option['option_id']]
  102. : [$bundleOption[$option['option_id']]];
  103. $this->assertEquals($expectedSelections, $option['option_selections']);
  104. }
  105. }
  106. }