Skip to content

Commit 5b35864

Browse files
ACQE-8324: Using rest api update guest cart of configurable product
- Updated code to use latest data fixtures
1 parent b040348 commit 5b35864

File tree

1 file changed

+93
-64
lines changed

1 file changed

+93
-64
lines changed

dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartConfigurableItemRepositoryTest.php

Lines changed: 93 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88

99
namespace Magento\Quote\Api;
1010

11-
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
13+
use Magento\ConfigurableProduct\Test\Fixture\Attribute as AttributeFixture;
14+
use Magento\ConfigurableProduct\Test\Fixture\Product as ConfigurableProductFixture;
15+
use Magento\Framework\Exception\NoSuchEntityException;
1216
use Magento\Framework\Webapi\Rest\Request;
1317
use Magento\Integration\Api\AdminTokenServiceInterface;
1418
use Magento\Integration\Model\AdminTokenService;
1519
use Magento\Integration\Model\Oauth\Token as TokenModel;
16-
use Magento\TestFramework\Bootstrap as TestBootstrap;
20+
use Magento\TestFramework\Fixture\DataFixture;
21+
use Magento\TestFramework\Fixture\DataFixtureStorage;
22+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
1723
use Magento\TestFramework\Helper\Bootstrap;
1824
use Magento\TestFramework\TestCase\WebapiAbstract;
1925
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
@@ -53,6 +59,11 @@ class GuestCartConfigurableItemRepositoryTest extends WebapiAbstract
5359
*/
5460
private $simpleProductSkus=[];
5561

62+
/**
63+
* @var DataFixtureStorage
64+
*/
65+
private $fixtures;
66+
5667
/**
5768
* @inheritdoc
5869
*/
@@ -62,55 +73,45 @@ protected function setUp(): void
6273
$this->tokenService = Bootstrap::getObjectManager()->get(AdminTokenService::class);
6374
$this->tokenModel = Bootstrap::getObjectManager()->get(TokenModel::class);
6475
$this->userModel = Bootstrap::getObjectManager()->get(User::class);
76+
$this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage();
6577
}
6678

6779
/**
68-
* @magentoApiDataFixture Magento/Webapi/_files/webapi_user.php
69-
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
80+
* Test guest cart update configurable item using modern fixtures
7081
*/
82+
#[
83+
DataFixture(ProductFixture::class, ['price' => 10, 'sku' => 'simple-10'], as: 'p1'),
84+
DataFixture(ProductFixture::class, ['price' => 20, 'sku' => 'simple-20'], as: 'p2'),
85+
DataFixture(AttributeFixture::class, ['attribute_code' => 'test_configurable'], as: 'attr'),
86+
DataFixture(
87+
ConfigurableProductFixture::class,
88+
[
89+
'sku' => 'configurable',
90+
'name' => 'Configurable Product',
91+
'_options' => ['$attr$'],
92+
'_links' => ['$p1$', '$p2$']
93+
],
94+
'configurableProduct'
95+
)
96+
]
7197
public function testGuestCartUpdateConfigurableItem()
7298
{
73-
$adminToken = $this->createAdminAccessToken();
74-
$guestCartId = $this->createGuestCart($adminToken);
75-
$response = $this->addConfigurableProductToCart($guestCartId, $adminToken);
76-
$this->updateConfigurableProductInCart($guestCartId, $adminToken, $response['item_id']);
77-
$this->verifyCartItems($guestCartId, $adminToken, $response['item_id']);
78-
}
79-
80-
private function createAdminAccessToken()
81-
{
82-
$adminUser = 'webapi_user';
83-
84-
$serviceInfo = [
85-
'rest' => [
86-
'resourcePath' => self::RESOURCE_PATH_ADMIN_TOKEN,
87-
'httpMethod' => Request::HTTP_METHOD_POST,
88-
],
89-
];
90-
$requestData = [
91-
'username' => $adminUser,
92-
'password' => TestBootstrap::ADMIN_PASSWORD,
93-
];
94-
$accessToken = $this->_webApiCall($serviceInfo, $requestData);
95-
$this->assertNotNull($accessToken);
96-
return $accessToken;
99+
$guestCartId = $this->createGuestCart();
100+
$response = $this->addConfigurableProductToCart($guestCartId);
101+
$this->updateConfigurableProductInCart($guestCartId, $response['item_id']);
102+
$this->verifyCartItems($guestCartId, $response['item_id']);
97103
}
98104

99-
private function createGuestCart(string $adminToken)
105+
/**
106+
* @return string
107+
*/
108+
private function createGuestCart(): string
100109
{
101110
$serviceInfo = [
102111
'rest' => [
103112
'resourcePath' => self::RESOURCE_PATH_GUEST_CART,
104-
'httpMethod' => Request::HTTP_METHOD_POST,
105-
'headers' => [
106-
'Authorization' => 'Bearer ' . $adminToken
107-
]
108-
],
109-
'soap' => [
110-
'service' => self::SERVICE_NAME_GUEST_CART,
111-
'serviceVersion' => self::SERVICE_VERSION_GUEST_CART,
112-
'operation' => self::SERVICE_NAME_GUEST_CART . 'CreateEmptyCart',
113-
],
113+
'httpMethod' => Request::HTTP_METHOD_POST
114+
]
114115
];
115116

116117
$requestData = ['storeId' => 1];
@@ -119,9 +120,13 @@ private function createGuestCart(string $adminToken)
119120
return $quoteId;
120121
}
121122

122-
private function addConfigurableProductToCart(string $guestCartId, string $adminToken)
123+
/**
124+
* @param string $guestCartId
125+
* @return array
126+
*/
127+
private function addConfigurableProductToCart(string $guestCartId): array
123128
{
124-
$configurableProduct = $this->getConfigurableProduct('configurable');
129+
$configurableProduct = $this->getConfigurableProduct();
125130
$optionData = $this->getConfigurableOptionData($configurableProduct);
126131

127132
$requestData = $this->buildCartItemRequestData(
@@ -131,7 +136,7 @@ private function addConfigurableProductToCart(string $guestCartId, string $admin
131136
$optionData['option_id']
132137
);
133138

134-
$serviceInfo = $this->getCartServiceInfo($guestCartId, $adminToken, 'add');
139+
$serviceInfo = $this->getCartServiceInfo($guestCartId, 'add');
135140

136141
$response = $this->_webApiCall($serviceInfo, $requestData);
137142
$this->assertNotNull($response['item_id']);
@@ -145,9 +150,14 @@ private function addConfigurableProductToCart(string $guestCartId, string $admin
145150
return $response;
146151
}
147152

148-
private function verifyCartItems(string $guestCartId, string $adminToken, int $expectedItemId)
153+
/**
154+
* @param string $guestCartId
155+
* @param int $expectedItemId
156+
* @return void
157+
*/
158+
private function verifyCartItems(string $guestCartId, int $expectedItemId): void
149159
{
150-
$serviceInfo = $this->getCartServiceInfo($guestCartId, $adminToken, 'get');
160+
$serviceInfo = $this->getCartServiceInfo($guestCartId, 'get');
151161
$response = $this->_webApiCall($serviceInfo, []);
152162
$this->assertIsArray($response);
153163
$this->assertGreaterThan(0, count($response), 'Cart should contain at least one item');
@@ -169,9 +179,14 @@ private function verifyCartItems(string $guestCartId, string $adminToken, int $e
169179
$this->assertTrue($foundItem, 'Expected cart item not found in cart items list');
170180
}
171181

172-
private function updateConfigurableProductInCart(string $guestCartId, string $adminToken, int $itemId)
182+
/**
183+
* @param string $guestCartId
184+
* @param int $itemId
185+
* @return void
186+
*/
187+
private function updateConfigurableProductInCart(string $guestCartId, int $itemId): void
173188
{
174-
$configurableProduct = $this->getConfigurableProduct('configurable');
189+
$configurableProduct = $this->getConfigurableProduct();
175190
$optionData = $this->getConfigurableOptionData($configurableProduct);
176191
$requestData = $this->buildCartItemRequestData(
177192
$guestCartId,
@@ -180,7 +195,7 @@ private function updateConfigurableProductInCart(string $guestCartId, string $ad
180195
$optionData['option_id']
181196
);
182197
$requestData['cartItem']['item_id'] = $itemId;
183-
$serviceInfo = $this->getCartServiceInfo($guestCartId, $adminToken, 'update', $itemId);
198+
$serviceInfo = $this->getCartServiceInfo($guestCartId, 'update', $itemId);
184199
$response = $this->_webApiCall($serviceInfo, $requestData);
185200
$this->assertNotNull($response['item_id']);
186201
$this->assertEquals(Configurable::TYPE_CODE, $response['product_type']);
@@ -192,20 +207,30 @@ private function updateConfigurableProductInCart(string $guestCartId, string $ad
192207
);
193208
}
194209

195-
private function getConfigurableProduct(string $sku)
210+
/**
211+
* Get configurable product from fixtures
212+
*
213+
* @return ProductInterface
214+
* @throws NoSuchEntityException
215+
*/
216+
private function getConfigurableProduct(): ProductInterface
196217
{
197-
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
198-
$configurableProduct = $productRepository->get($sku);
218+
$configurableProduct = $this->fixtures->get('configurableProduct');
199219
$simpleProducts = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
200220
foreach ($simpleProducts as $simpleProduct) {
201221
$this->simpleProductSkus[] = $simpleProduct->getSku();
202222
}
203223
return $configurableProduct;
204224
}
205225

206-
private function getConfigurableOptionData($configurableProduct, $selectedOption = null)
226+
/**
227+
* @param $configurableProduct
228+
* @param $selectedOption
229+
* @return array
230+
*/
231+
private function getConfigurableOptionData($configurableProduct, $selectedOption = null): array
207232
{
208-
$configOptions = $configurableProduct->getExtensionAttributes()->getconfigOptions();
233+
$configOptions = $configurableProduct->getExtensionAttributes()->getConfigurableProductOptions();
209234

210235
$options = $configOptions[0]->getOptions();
211236
$optionKey = (isset($selectedOption) && isset($options[$selectedOption])) ? $selectedOption : 0;
@@ -216,7 +241,14 @@ private function getConfigurableOptionData($configurableProduct, $selectedOption
216241
];
217242
}
218243

219-
private function buildCartItemRequestData(string $cartId, string $sku, int $attributeId, int $optionId): array
244+
/**
245+
* @param string $cartId
246+
* @param string $sku
247+
* @param string $attributeId
248+
* @param string $optionId
249+
* @return array[]
250+
*/
251+
private function buildCartItemRequestData(string $cartId, string $sku, string $attributeId, string $optionId): array
220252
{
221253
return [
222254
'cartItem' => [
@@ -237,9 +269,14 @@ private function buildCartItemRequestData(string $cartId, string $sku, int $attr
237269
];
238270
}
239271

272+
/**
273+
* @param string $cartId
274+
* @param string $action
275+
* @param int|null $itemId
276+
* @return array[]
277+
*/
240278
private function getCartServiceInfo(
241279
string $cartId,
242-
string $adminToken,
243280
string $action = 'add',
244281
?int $itemId = null
245282
): array {
@@ -258,16 +295,8 @@ private function getCartServiceInfo(
258295
return [
259296
'rest' => [
260297
'resourcePath' => $resourcePath,
261-
'httpMethod' => $httpMethod,
262-
'headers' => [
263-
'Authorization' => 'Bearer ' . $adminToken
264-
]
265-
],
266-
'soap' => [
267-
'service' => self::SERVICE_NAME_GUEST_CART,
268-
'serviceVersion' => self::SERVICE_VERSION_GUEST_CART,
269-
'operation' => self::SERVICE_NAME_GUEST_CART . 'Save',
270-
],
298+
'httpMethod' => $httpMethod
299+
]
271300
];
272301
}
273302
}

0 commit comments

Comments
 (0)