|
11 | 11 |
|
12 | 12 | use Magento\Catalog\Api\ProductRepositoryInterface; |
13 | 13 | use Magento\Checkout\Model\Session; |
| 14 | +use Magento\Checkout\Model\Session as CheckoutSession; |
14 | 15 | use Magento\Customer\Model\ResourceModel\CustomerRepository; |
15 | 16 | use Magento\Framework\Data\Form\FormKey; |
| 17 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 18 | +use Magento\Quote\Model\Quote; |
| 19 | +use Magento\Quote\Api\CartRepositoryInterface; |
16 | 20 | use Magento\TestFramework\Helper\Bootstrap; |
17 | 21 | use Magento\TestFramework\Request; |
18 | 22 | use Magento\Customer\Model\Session as CustomerSession; |
|
25 | 29 | */ |
26 | 30 | class CartTest extends \Magento\TestFramework\TestCase\AbstractController |
27 | 31 | { |
| 32 | + /** @var CheckoutSession */ |
| 33 | + private $checkoutSession; |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritdoc |
| 37 | + */ |
| 38 | + protected function setUp() |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + $this->checkoutSession = $this->_objectManager->get(CheckoutSession::class); |
| 42 | + $this->_objectManager->addSharedInstance($this->checkoutSession, CheckoutSession::class); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritdoc |
| 47 | + */ |
| 48 | + protected function tearDown() |
| 49 | + { |
| 50 | + $this->_objectManager->removeSharedInstance(CheckoutSession::class); |
| 51 | + parent::tearDown(); |
| 52 | + } |
| 53 | + |
28 | 54 | /** |
29 | 55 | * Test for \Magento\Checkout\Controller\Cart::configureAction() with simple product |
30 | 56 | * |
31 | 57 | * @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php |
32 | 58 | */ |
33 | 59 | public function testConfigureActionWithSimpleProduct() |
34 | 60 | { |
35 | | - /** @var $session \Magento\Checkout\Model\Session */ |
36 | | - $session = $this->_objectManager->create(\Magento\Checkout\Model\Session::class); |
| 61 | + /** @var $session CheckoutSession */ |
| 62 | + $session = $this->_objectManager->create(CheckoutSession::class); |
37 | 63 |
|
38 | 64 | /** @var ProductRepositoryInterface $productRepository */ |
39 | 65 | $productRepository = $this->_objectManager->create(ProductRepositoryInterface::class); |
@@ -63,19 +89,20 @@ public function testConfigureActionWithSimpleProduct() |
63 | 89 | /** |
64 | 90 | * Test for \Magento\Checkout\Controller\Cart::configureAction() with simple product and custom option |
65 | 91 | * |
66 | | - * @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product_and_custom_option.php |
| 92 | + * @magentoDataFixture Magento/Checkout/_files/cart_with_simple_product_and_custom_options.php |
67 | 93 | */ |
68 | 94 | public function testConfigureActionWithSimpleProductAndCustomOption() |
69 | 95 | { |
70 | | - /** @var $session \Magento\Checkout\Model\Session */ |
71 | | - $session = $this->_objectManager->create(\Magento\Checkout\Model\Session::class); |
| 96 | + /** @var Quote $quote */ |
| 97 | + $quote = $this->getQuote('test_order_item_with_custom_options'); |
| 98 | + $this->checkoutSession->setQuoteId($quote->getId()); |
72 | 99 |
|
73 | 100 | /** @var ProductRepositoryInterface $productRepository */ |
74 | 101 | $productRepository = $this->_objectManager->create(ProductRepositoryInterface::class); |
75 | 102 | /** @var $product \Magento\Catalog\Model\Product */ |
76 | | - $product = $productRepository->get('simple'); |
| 103 | + $product = $productRepository->get('simple_with_custom_options'); |
77 | 104 |
|
78 | | - $quoteItem = $this->_getQuoteItemIdByProductId($session->getQuote(), $product->getId()); |
| 105 | + $quoteItem = $this->_getQuoteItemIdByProductId($quote, $product->getId()); |
79 | 106 | $this->assertNotNull($quoteItem, 'Cannot get quote item for simple product with custom option'); |
80 | 107 |
|
81 | 108 | $this->dispatch( |
@@ -112,8 +139,8 @@ public function testConfigureActionWithSimpleProductAndCustomOption() |
112 | 139 | */ |
113 | 140 | public function testConfigureActionWithBundleProduct() |
114 | 141 | { |
115 | | - /** @var $session \Magento\Checkout\Model\Session */ |
116 | | - $session = $this->_objectManager->create(\Magento\Checkout\Model\Session::class); |
| 142 | + /** @var $session CheckoutSession */ |
| 143 | + $session = $this->_objectManager->create(CheckoutSession::class); |
117 | 144 |
|
118 | 145 | /** @var ProductRepositoryInterface $productRepository */ |
119 | 146 | $productRepository = $this->_objectManager->create(ProductRepositoryInterface::class); |
@@ -147,8 +174,8 @@ public function testConfigureActionWithBundleProduct() |
147 | 174 | */ |
148 | 175 | public function testConfigureActionWithDownloadableProduct() |
149 | 176 | { |
150 | | - /** @var $session \Magento\Checkout\Model\Session */ |
151 | | - $session = $this->_objectManager->create(\Magento\Checkout\Model\Session::class); |
| 177 | + /** @var $session CheckoutSession */ |
| 178 | + $session = $this->_objectManager->create(CheckoutSession::class); |
152 | 179 |
|
153 | 180 | /** @var ProductRepositoryInterface $productRepository */ |
154 | 181 | $productRepository = $this->_objectManager->create(ProductRepositoryInterface::class); |
@@ -201,8 +228,8 @@ public function testUpdatePostAction() |
201 | 228 | $productId = $product->getId(); |
202 | 229 | $originalQuantity = 1; |
203 | 230 | $updatedQuantity = 2; |
204 | | - /** @var $checkoutSession \Magento\Checkout\Model\Session */ |
205 | | - $checkoutSession = $this->_objectManager->create(\Magento\Checkout\Model\Session::class); |
| 231 | + /** @var $checkoutSession CheckoutSession */ |
| 232 | + $checkoutSession = $this->_objectManager->create(CheckoutSession::class); |
206 | 233 | $quoteItem = $this->_getQuoteItemIdByProductId($checkoutSession->getQuote(), $productId); |
207 | 234 |
|
208 | 235 | /** @var FormKey $formKey */ |
@@ -235,6 +262,26 @@ public function testUpdatePostAction() |
235 | 262 | $this->assertEquals($updatedQuantity, $quoteItem->getQty(), "Invalid quote item quantity"); |
236 | 263 | } |
237 | 264 |
|
| 265 | + /** |
| 266 | + * Gets quote by reserved order id. |
| 267 | + * |
| 268 | + * @param string $reservedOrderId |
| 269 | + * @return Quote |
| 270 | + */ |
| 271 | + private function getQuote($reservedOrderId) |
| 272 | + { |
| 273 | + /** @var SearchCriteriaBuilder $searchCriteriaBuilder */ |
| 274 | + $searchCriteriaBuilder = $this->_objectManager->get(SearchCriteriaBuilder::class); |
| 275 | + $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId) |
| 276 | + ->create(); |
| 277 | + |
| 278 | + /** @var CartRepositoryInterface $quoteRepository */ |
| 279 | + $quoteRepository = $this->_objectManager->get(CartRepositoryInterface::class); |
| 280 | + $items = $quoteRepository->getList($searchCriteria)->getItems(); |
| 281 | + |
| 282 | + return array_pop($items); |
| 283 | + } |
| 284 | + |
238 | 285 | /** |
239 | 286 | * Gets \Magento\Quote\Model\Quote\Item from \Magento\Quote\Model\Quote by product id |
240 | 287 | * |
|
0 commit comments