QuoteManagementTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Quote\Model;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\Catalog\Model\Product\Type;
  10. use Magento\Framework\Api\SearchCriteriaBuilder;
  11. use Magento\Quote\Api\CartManagementInterface;
  12. use Magento\Quote\Api\CartRepositoryInterface;
  13. use Magento\Sales\Api\OrderRepositoryInterface;
  14. use Magento\TestFramework\Helper\Bootstrap;
  15. use Magento\TestFramework\ObjectManager;
  16. /**
  17. * Class for testing QuoteManagement model
  18. */
  19. class QuoteManagementTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var ObjectManager
  23. */
  24. private $objectManager;
  25. /**
  26. * @var CartManagementInterface
  27. */
  28. private $cartManagement;
  29. /**
  30. * @inheritdoc
  31. */
  32. protected function setUp()
  33. {
  34. $this->objectManager = Bootstrap::getObjectManager();
  35. $this->cartManagement = $this->objectManager->create(CartManagementInterface::class);
  36. }
  37. /**
  38. * Creates order with product that has child items.
  39. *
  40. * @magentoAppIsolation enabled
  41. * @magentoDataFixture Magento/Sales/_files/quote_with_bundle.php
  42. */
  43. public function testSubmit()
  44. {
  45. $quote = $this->getQuote('test01');
  46. $orderId = $this->cartManagement->placeOrder($quote->getId());
  47. /** @var OrderRepositoryInterface $orderRepository */
  48. $orderRepository = $this->objectManager->create(OrderRepositoryInterface::class);
  49. $order = $orderRepository->get($orderId);
  50. $orderItems = $order->getItems();
  51. self::assertCount(3, $orderItems);
  52. foreach ($orderItems as $orderItem) {
  53. if ($orderItem->getProductType() == Type::TYPE_SIMPLE) {
  54. self::assertNotEmpty($orderItem->getParentItem(), 'Parent is not set for child product');
  55. self::assertNotEmpty($orderItem->getParentItemId(), 'Parent is not set for child product');
  56. }
  57. }
  58. }
  59. /**
  60. * Tries to create order with product that has child items and one of them was deleted.
  61. *
  62. * @magentoAppArea adminhtml
  63. * @magentoAppIsolation enabled
  64. * @magentoDataFixture Magento/Sales/_files/quote_with_bundle.php
  65. * @expectedException \Magento\Framework\Exception\LocalizedException
  66. * @expectedExceptionMessage Some of the products below do not have all the required options.
  67. */
  68. public function testSubmitWithDeletedItem()
  69. {
  70. /** @var ProductRepositoryInterface $productRepository */
  71. $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
  72. $product = $productRepository->get('simple-2');
  73. $productRepository->delete($product);
  74. $quote = $this->getQuote('test01');
  75. $this->cartManagement->placeOrder($quote->getId());
  76. }
  77. /**
  78. * Tries to create order with item of stock during checkout.
  79. *
  80. * @magentoDataFixture Magento/Sales/_files/quote.php
  81. * @expectedException \Magento\Framework\Exception\LocalizedException
  82. * @expectedExceptionMessage Some of the products are out of stock.
  83. * @magentoDbIsolation enabled
  84. */
  85. public function testSubmitWithItemOutOfStock()
  86. {
  87. $this->makeProductOutOfStock('simple');
  88. $quote = $this->getQuote('test01');
  89. $this->cartManagement->placeOrder($quote->getId());
  90. }
  91. /**
  92. * Gets quote by reserved order ID.
  93. *
  94. * @param string $reservedOrderId
  95. * @return Quote
  96. */
  97. private function getQuote(string $reservedOrderId): Quote
  98. {
  99. /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
  100. $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
  101. $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
  102. ->create();
  103. /** @var CartRepositoryInterface $quoteRepository */
  104. $quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
  105. $items = $quoteRepository->getList($searchCriteria)
  106. ->getItems();
  107. return array_pop($items);
  108. }
  109. /**
  110. * Makes provided product as out of stock.
  111. *
  112. * @param string $sku
  113. * @return void
  114. */
  115. private function makeProductOutOfStock(string $sku)
  116. {
  117. /** @var ProductRepositoryInterface $productRepository */
  118. $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
  119. $product = $productRepository->get($sku);
  120. $extensionAttributes = $product->getExtensionAttributes();
  121. $stockItem = $extensionAttributes->getStockItem();
  122. $stockItem->setIsInStock(false);
  123. $productRepository->save($product);
  124. }
  125. }