Skip to content

Commit 22c2b57

Browse files
ENGCOM-4704: magento/graphql-ce#544: Replace deprecated fixtures in RemoveItemFromCartTest #545
- Merge Pull Request magento/graphql-ce#545 from magento/graphql-ce:544-new-fixture-for-RemoveItemFromCartTest - Merged commits: 1. c2c04e9 2. 5bc5ee5 3. 7e9f507 4. 3352abd
2 parents 5a8ef03 + 3352abd commit 22c2b57

File tree

3 files changed

+218
-198
lines changed

3 files changed

+218
-198
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveItemFromCartTest.php

Lines changed: 90 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
namespace Magento\GraphQl\Quote\Customer;
99

10-
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\GraphQl\Quote\GetQuoteItemIdByReservedQuoteIdAndSku;
1112
use Magento\Integration\Api\CustomerTokenServiceInterface;
12-
use Magento\Quote\Model\QuoteFactory;
13-
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
14-
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
1513
use Magento\TestFramework\Helper\Bootstrap;
1614
use Magento\TestFramework\TestCase\GraphQlAbstract;
1715

@@ -26,46 +24,37 @@ class RemoveItemFromCartTest extends GraphQlAbstract
2624
private $customerTokenService;
2725

2826
/**
29-
* @var QuoteResource
27+
* @var GetMaskedQuoteIdByReservedOrderId
3028
*/
31-
private $quoteResource;
29+
private $getMaskedQuoteIdByReservedOrderId;
3230

3331
/**
34-
* @var QuoteFactory
32+
* @var GetQuoteItemIdByReservedQuoteIdAndSku
3533
*/
36-
private $quoteFactory;
37-
38-
/**
39-
* @var QuoteIdToMaskedQuoteIdInterface
40-
*/
41-
private $quoteIdToMaskedId;
42-
43-
/**
44-
* @var ProductRepositoryInterface
45-
*/
46-
private $productRepository;
34+
private $getQuoteItemIdByReservedQuoteIdAndSku;
4735

4836
protected function setUp()
4937
{
5038
$objectManager = Bootstrap::getObjectManager();
51-
$this->quoteResource = $objectManager->get(QuoteResource::class);
52-
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
53-
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
5439
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
55-
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class);
40+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
41+
$this->getQuoteItemIdByReservedQuoteIdAndSku = $objectManager->get(
42+
GetQuoteItemIdByReservedQuoteIdAndSku::class
43+
);
5644
}
5745

5846
/**
59-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
47+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
48+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
49+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
50+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
6051
*/
6152
public function testRemoveItemFromCart()
6253
{
63-
$quote = $this->quoteFactory->create();
64-
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
65-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
66-
$itemId = (int)$quote->getItemByProduct($this->productRepository->get('simple'))->getId();
54+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
55+
$itemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute('test_quote', 'simple_product');
6756

68-
$query = $this->prepareMutationQuery($maskedQuoteId, $itemId);
57+
$query = $this->getQuery($maskedQuoteId, $itemId);
6958
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
7059

7160
$this->assertArrayHasKey('removeItemFromCart', $response);
@@ -80,105 +69,24 @@ public function testRemoveItemFromCart()
8069
*/
8170
public function testRemoveItemFromNonExistentCart()
8271
{
83-
$query = $this->prepareMutationQuery('non_existent_masked_id', 1);
72+
$query = $this->getQuery('non_existent_masked_id', 1);
8473
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
8574
}
8675

8776
/**
88-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
77+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
78+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
79+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
80+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
8981
*/
9082
public function testRemoveNonExistentItem()
9183
{
92-
$quote = $this->quoteFactory->create();
93-
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
94-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
84+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
9585
$notExistentItemId = 999;
9686

9787
$this->expectExceptionMessage("Cart doesn't contain the {$notExistentItemId} item.");
9888

99-
$query = $this->prepareMutationQuery($maskedQuoteId, $notExistentItemId);
100-
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
101-
}
102-
103-
/**
104-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
105-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
106-
*/
107-
public function testRemoveItemIfItemIsNotBelongToCart()
108-
{
109-
$firstQuote = $this->quoteFactory->create();
110-
$this->quoteResource->load($firstQuote, 'test_order_1', 'reserved_order_id');
111-
$firstQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$firstQuote->getId());
112-
113-
$secondQuote = $this->quoteFactory->create();
114-
$this->quoteResource->load(
115-
$secondQuote,
116-
'test_order_with_virtual_product_without_address',
117-
'reserved_order_id'
118-
);
119-
$secondQuote->setCustomerId(1);
120-
$this->quoteResource->save($secondQuote);
121-
$secondQuoteItemId = (int)$secondQuote
122-
->getItemByProduct($this->productRepository->get('virtual-product'))
123-
->getId();
124-
125-
$this->expectExceptionMessage("Cart doesn't contain the {$secondQuoteItemId} item.");
126-
127-
$query = $this->prepareMutationQuery($firstQuoteMaskedId, $secondQuoteItemId);
128-
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
129-
}
130-
131-
/**
132-
* @magentoApiDataFixture Magento/Customer/_files/customer.php
133-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
134-
*/
135-
public function testRemoveItemFromGuestCart()
136-
{
137-
$guestQuote = $this->quoteFactory->create();
138-
$this->quoteResource->load(
139-
$guestQuote,
140-
'test_order_with_virtual_product_without_address',
141-
'reserved_order_id'
142-
);
143-
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
144-
$guestQuoteItemId = (int)$guestQuote
145-
->getItemByProduct($this->productRepository->get('virtual-product'))
146-
->getId();
147-
148-
$this->expectExceptionMessage(
149-
"The current user cannot perform operations on cart \"$guestQuoteMaskedId\""
150-
);
151-
152-
$query = $this->prepareMutationQuery($guestQuoteMaskedId, $guestQuoteItemId);
153-
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
154-
}
155-
156-
/**
157-
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
158-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
159-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
160-
*/
161-
public function testRemoveItemFromAnotherCustomerCart()
162-
{
163-
$anotherCustomerQuote = $this->quoteFactory->create();
164-
$this->quoteResource->load(
165-
$anotherCustomerQuote,
166-
'test_order_with_virtual_product_without_address',
167-
'reserved_order_id'
168-
);
169-
$anotherCustomerQuote->setCustomerId(2);
170-
$this->quoteResource->save($anotherCustomerQuote);
171-
172-
$anotherCustomerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$anotherCustomerQuote->getId());
173-
$anotherCustomerQuoteItemId = (int)$anotherCustomerQuote
174-
->getItemByProduct($this->productRepository->get('virtual-product'))
175-
->getId();
176-
177-
$this->expectExceptionMessage(
178-
"The current user cannot perform operations on cart \"$anotherCustomerQuoteMaskedId\""
179-
);
180-
181-
$query = $this->prepareMutationQuery($anotherCustomerQuoteMaskedId, $anotherCustomerQuoteItemId);
89+
$query = $this->getQuery($maskedQuoteId, $notExistentItemId);
18290
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
18391
}
18492

@@ -226,12 +134,77 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array
226134
];
227135
}
228136

137+
/**
138+
* _security
139+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
140+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
141+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
142+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
143+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
144+
*/
145+
public function testRemoveItemIfItemIsNotBelongToCart()
146+
{
147+
$firstQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
148+
$secondQuoteItemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute(
149+
'test_order_with_virtual_product',
150+
'virtual-product'
151+
);
152+
153+
$this->expectExceptionMessage("Cart doesn't contain the {$secondQuoteItemId} item.");
154+
155+
$query = $this->getQuery($firstQuoteMaskedId, $secondQuoteItemId);
156+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
157+
}
158+
159+
/**
160+
* _security
161+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
162+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
163+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
164+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
165+
*/
166+
public function testRemoveItemFromGuestCart()
167+
{
168+
$guestQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
169+
$guestQuoteItemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute('test_quote', 'simple_product');
170+
171+
$this->expectExceptionMessage(
172+
"The current user cannot perform operations on cart \"$guestQuoteMaskedId\""
173+
);
174+
175+
$query = $this->getQuery($guestQuoteMaskedId, $guestQuoteItemId);
176+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
177+
}
178+
179+
/**
180+
* _security
181+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
182+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
183+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
184+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
185+
*/
186+
public function testRemoveItemFromAnotherCustomerCart()
187+
{
188+
$anotherCustomerQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
189+
$anotherCustomerQuoteItemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute(
190+
'test_quote',
191+
'simple_product'
192+
);
193+
194+
$this->expectExceptionMessage(
195+
"The current user cannot perform operations on cart \"$anotherCustomerQuoteMaskedId\""
196+
);
197+
198+
$query = $this->getQuery($anotherCustomerQuoteMaskedId, $anotherCustomerQuoteItemId);
199+
$this->graphQlQuery($query, [], '', $this->getHeaderMap('[email protected]'));
200+
}
201+
229202
/**
230203
* @param string $maskedQuoteId
231204
* @param int $itemId
232205
* @return string
233206
*/
234-
private function prepareMutationQuery(string $maskedQuoteId, int $itemId): string
207+
private function getQuery(string $maskedQuoteId, int $itemId): string
235208
{
236209
return <<<QUERY
237210
mutation {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
13+
use Magento\Quote\Model\QuoteFactory;
14+
15+
/**
16+
* Get quote item id by reserved order id and product sku
17+
*/
18+
class GetQuoteItemIdByReservedQuoteIdAndSku
19+
{
20+
/**
21+
* @var QuoteFactory
22+
*/
23+
private $quoteFactory;
24+
25+
/**
26+
* @var QuoteResource
27+
*/
28+
private $quoteResource;
29+
30+
/**
31+
* @var ProductRepositoryInterface
32+
*/
33+
private $productRepository;
34+
35+
/**
36+
* @param QuoteFactory $quoteFactory
37+
* @param QuoteResource $quoteResource
38+
* @param ProductRepositoryInterface $productRepository
39+
*/
40+
public function __construct(
41+
QuoteFactory $quoteFactory,
42+
QuoteResource $quoteResource,
43+
ProductRepositoryInterface $productRepository
44+
) {
45+
$this->quoteFactory = $quoteFactory;
46+
$this->quoteResource = $quoteResource;
47+
$this->productRepository = $productRepository;
48+
}
49+
50+
/**
51+
* Get quote item id by reserved order id and product sku
52+
*
53+
* @param string $reservedOrderId
54+
* @param string $sku
55+
* @return int
56+
* @throws NoSuchEntityException
57+
*/
58+
public function execute(string $reservedOrderId, string $sku): int
59+
{
60+
$quote = $this->quoteFactory->create();
61+
$this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id');
62+
$product = $this->productRepository->get($sku);
63+
64+
return (int)$quote->getItemByProduct($product)->getId();
65+
}
66+
}

0 commit comments

Comments
 (0)