-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,035 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
...ests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddBundleProductToWishlistTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Wishlist; | ||
|
||
use Exception; | ||
use Magento\Bundle\Model\Option; | ||
use Magento\Bundle\Model\Product\Type; | ||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Framework\Exception\AuthenticationException; | ||
use Magento\Integration\Api\CustomerTokenServiceInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
use Magento\Wishlist\Model\Item; | ||
use Magento\Wishlist\Model\WishlistFactory; | ||
|
||
/** | ||
* Test coverage for adding a bundle product to wishlist | ||
*/ | ||
class AddBundleProductToWishlistTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var CustomerTokenServiceInterface | ||
*/ | ||
private $customerTokenService; | ||
|
||
/** | ||
* @var WishlistFactory | ||
*/ | ||
private $wishlistFactory; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private $productRepository; | ||
|
||
/** | ||
* Set Up | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$objectManager = Bootstrap::getObjectManager(); | ||
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); | ||
$this->wishlistFactory = $objectManager->get(WishlistFactory::class); | ||
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class); | ||
} | ||
|
||
/** | ||
* @magentoConfigFixture default_store wishlist/general/active 1 | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* @magentoApiDataFixture Magento/Bundle/_files/product_1.php | ||
* | ||
* @throws Exception | ||
*/ | ||
public function testAddBundleProductWithOptions(): void | ||
{ | ||
$sku = 'bundle-product'; | ||
$product = $this->productRepository->get($sku); | ||
$customerId = 1; | ||
$qty = 2; | ||
$optionQty = 1; | ||
|
||
/** @var Type $typeInstance */ | ||
$typeInstance = $product->getTypeInstance(); | ||
$typeInstance->setStoreFilter($product->getStoreId(), $product); | ||
/** @var Option $option */ | ||
$option = $typeInstance->getOptionsCollection($product)->getFirstItem(); | ||
/** @var Product $selection */ | ||
$selection = $typeInstance->getSelectionsCollection([$option->getId()], $product)->getFirstItem(); | ||
$optionId = $option->getId(); | ||
$selectionId = $selection->getSelectionId(); | ||
$bundleOptions = $this->generateBundleOptionIdV2((int) $optionId, (int) $selectionId, $optionQty); | ||
|
||
$query = $this->getQuery($sku, $qty, $bundleOptions); | ||
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap()); | ||
$wishlist = $this->wishlistFactory->create()->loadByCustomerId($customerId, true); | ||
/** @var Item $item */ | ||
$item = $wishlist->getItemCollection()->getFirstItem(); | ||
|
||
$this->assertArrayHasKey('addProductsToWishlist', $response); | ||
$this->assertArrayHasKey('wishlist', $response['addProductsToWishlist']); | ||
$response = $response['addProductsToWishlist']['wishlist']; | ||
$this->assertEquals($wishlist->getItemsCount(), $response['items_count']); | ||
$this->assertEquals($wishlist->getSharingCode(), $response['sharing_code']); | ||
$this->assertEquals($wishlist->getUpdatedAt(), $response['updated_at']); | ||
$this->assertEquals($item->getData('qty'), $response['items'][0]['qty']); | ||
$this->assertEquals($item->getDescription(), $response['items'][0]['description']); | ||
$this->assertEquals($item->getAddedAt(), $response['items'][0]['added_at']); | ||
} | ||
|
||
/** | ||
* Authentication header map | ||
* | ||
* @param string $username | ||
* @param string $password | ||
* | ||
* @return array | ||
* | ||
* @throws AuthenticationException | ||
*/ | ||
private function getHeaderMap(string $username = '[email protected]', string $password = 'password'): array | ||
{ | ||
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); | ||
|
||
return ['Authorization' => 'Bearer ' . $customerToken]; | ||
} | ||
|
||
/** | ||
* Returns GraphQl mutation string | ||
* | ||
* @param string $sku | ||
* @param int $qty | ||
* @param string $bundleOptions | ||
* @param int $wishlistId | ||
* | ||
* @return string | ||
*/ | ||
private function getQuery( | ||
string $sku, | ||
int $qty, | ||
string $bundleOptions, | ||
int $wishlistId = 0 | ||
): string { | ||
return <<<MUTATION | ||
mutation { | ||
addProductsToWishlist( | ||
wishlist_id: {$wishlistId}, | ||
wishlist_items: [ | ||
{ | ||
sku: "{$sku}" | ||
quantity: {$qty} | ||
selected_options: [ | ||
"{$bundleOptions}" | ||
] | ||
} | ||
] | ||
) { | ||
userInputErrors { | ||
code | ||
message | ||
} | ||
wishlist { | ||
id | ||
sharing_code | ||
items_count | ||
updated_at | ||
items { | ||
id | ||
description | ||
qty | ||
added_at | ||
} | ||
} | ||
} | ||
} | ||
MUTATION; | ||
} | ||
|
||
/** | ||
* @param int $optionId | ||
* @param int $selectionId | ||
* | ||
* @param int $quantity | ||
* | ||
* @return string | ||
*/ | ||
private function generateBundleOptionIdV2(int $optionId, int $selectionId, int $quantity): string | ||
{ | ||
return base64_encode("bundle/$optionId/$selectionId/$quantity"); | ||
} | ||
} |
Oops, something went wrong.