Skip to content

Commit 3352abd

Browse files
committed
GraphQL-544: Replace deprecated fixtures in RemoveItemFromCartTest
1 parent 7e9f507 commit 3352abd

File tree

3 files changed

+212
-206
lines changed

3 files changed

+212
-206
lines changed

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

Lines changed: 84 additions & 125 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,49 +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
/**
5947
* @magentoApiDataFixture Magento/Customer/_files/customer.php
60-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
48+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
6149
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
6250
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
6351
*/
6452
public function testRemoveItemFromCart()
6553
{
66-
$quote = $this->quoteFactory->create();
67-
$this->quoteResource->load($quote, 'test_quote', 'reserved_order_id');
68-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
69-
$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');
7056

71-
$query = $this->prepareMutationQuery($maskedQuoteId, $itemId);
57+
$query = $this->getQuery($maskedQuoteId, $itemId);
7258
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
7359

7460
$this->assertArrayHasKey('removeItemFromCart', $response);
@@ -83,116 +69,24 @@ public function testRemoveItemFromCart()
8369
*/
8470
public function testRemoveItemFromNonExistentCart()
8571
{
86-
$query = $this->prepareMutationQuery('non_existent_masked_id', 1);
72+
$query = $this->getQuery('non_existent_masked_id', 1);
8773
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
8874
}
8975

9076
/**
9177
* @magentoApiDataFixture Magento/Customer/_files/customer.php
92-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
78+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
9379
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
9480
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
9581
*/
9682
public function testRemoveNonExistentItem()
9783
{
98-
$quote = $this->quoteFactory->create();
99-
$this->quoteResource->load($quote, 'test_quote', 'reserved_order_id');
100-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
84+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
10185
$notExistentItemId = 999;
10286

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

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

@@ -240,12 +134,77 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array
240134
];
241135
}
242136

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+
243202
/**
244203
* @param string $maskedQuoteId
245204
* @param int $itemId
246205
* @return string
247206
*/
248-
private function prepareMutationQuery(string $maskedQuoteId, int $itemId): string
207+
private function getQuery(string $maskedQuoteId, int $itemId): string
249208
{
250209
return <<<QUERY
251210
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)