Skip to content

Commit

Permalink
Test coverage for Wishlist GraphQl
Browse files Browse the repository at this point in the history
  • Loading branch information
eduard13 committed May 22, 2020
1 parent eddb4eb commit 7488a36
Show file tree
Hide file tree
Showing 10 changed files with 1,035 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
use Magento\Wishlist\Model\Wishlist\AddProductsToWishlist;
use Magento\Wishlist\Model\Wishlist\AddProductsToWishlist as AddProductsToWishlistModel;
use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig;
use Magento\Wishlist\Model\Wishlist\Data\Error;
use Magento\Wishlist\Model\Wishlist\Data\WishlistItemFactory;
Expand All @@ -23,10 +23,10 @@
/**
* Adding products to wishlist resolver
*/
class AddProductsToWishlistResolver implements ResolverInterface
class AddProductsToWishlist implements ResolverInterface
{
/**
* @var AddProductsToWishlist
* @var AddProductsToWishlistModel
*/
private $addProductsToWishlist;

Expand Down Expand Up @@ -54,14 +54,14 @@ class AddProductsToWishlistResolver implements ResolverInterface
* @param WishlistResourceModel $wishlistResource
* @param WishlistFactory $wishlistFactory
* @param WishlistConfig $wishlistConfig
* @param AddProductsToWishlist $addProductsToWishlist
* @param AddProductsToWishlistModel $addProductsToWishlist
* @param WishlistDataMapper $wishlistDataMapper
*/
public function __construct(
WishlistResourceModel $wishlistResource,
WishlistFactory $wishlistFactory,
WishlistConfig $wishlistConfig,
AddProductsToWishlist $addProductsToWishlist,
AddProductsToWishlistModel $addProductsToWishlist,
WishlistDataMapper $wishlistDataMapper
) {
$this->wishlistResource = $wishlistResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig;
use Magento\Wishlist\Model\Wishlist\Data\Error;
use Magento\Wishlist\Model\Wishlist\RemoveProductsFromWishlist;
use Magento\Wishlist\Model\Wishlist\RemoveProductsFromWishlist as RemoveProductsFromWishlistModel;
use Magento\Wishlist\Model\WishlistFactory;
use Magento\WishlistGraphQl\Mapper\WishlistDataMapper;

/**
* Removing products from wishlist resolver
*/
class RemoveProductsFromWishlistResolver implements ResolverInterface
class RemoveProductsFromWishlist implements ResolverInterface
{
/**
* @var WishlistDataMapper
*/
private $wishlistDataMapper;

/**
* @var RemoveProductsFromWishlist
* @var RemoveProductsFromWishlistModel
*/
private $removeProductsFromWishlist;

Expand All @@ -54,14 +54,14 @@ class RemoveProductsFromWishlistResolver implements ResolverInterface
* @param WishlistResourceModel $wishlistResource
* @param WishlistConfig $wishlistConfig
* @param WishlistDataMapper $wishlistDataMapper
* @param RemoveProductsFromWishlist $removeProductsFromWishlist
* @param RemoveProductsFromWishlistModel $removeProductsFromWishlist
*/
public function __construct(
WishlistFactory $wishlistFactory,
WishlistResourceModel $wishlistResource,
WishlistConfig $wishlistConfig,
WishlistDataMapper $wishlistDataMapper,
RemoveProductsFromWishlist $removeProductsFromWishlist
RemoveProductsFromWishlistModel $removeProductsFromWishlist
) {
$this->wishlistResource = $wishlistResource;
$this->wishlistConfig = $wishlistConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig;
use Magento\Wishlist\Model\Wishlist\Data\Error;
use Magento\Wishlist\Model\Wishlist\Data\WishlistItemFactory;
use Magento\Wishlist\Model\Wishlist\UpdateProductsInWishlist;
use Magento\Wishlist\Model\Wishlist\UpdateProductsInWishlist as UpdateProductsInWishlistModel;
use Magento\Wishlist\Model\WishlistFactory;
use Magento\WishlistGraphQl\Mapper\WishlistDataMapper;

/**
* Update wishlist items resolver
*/
class UpdateProductsInWishlistResolver implements ResolverInterface
class UpdateProductsInWishlist implements ResolverInterface
{
/**
* @var UpdateProductsInWishlist
* @var UpdateProductsInWishlistModel
*/
private $updateProductsInWishlist;

Expand Down Expand Up @@ -54,14 +54,14 @@ class UpdateProductsInWishlistResolver implements ResolverInterface
* @param WishlistResourceModel $wishlistResource
* @param WishlistFactory $wishlistFactory
* @param WishlistConfig $wishlistConfig
* @param UpdateProductsInWishlist $updateProductsInWishlist
* @param UpdateProductsInWishlistModel $updateProductsInWishlist
* @param WishlistDataMapper $wishlistDataMapper
*/
public function __construct(
WishlistResourceModel $wishlistResource,
WishlistFactory $wishlistFactory,
WishlistConfig $wishlistConfig,
UpdateProductsInWishlist $updateProductsInWishlist,
UpdateProductsInWishlistModel $updateProductsInWishlist,
WishlistDataMapper $wishlistDataMapper
) {
$this->wishlistResource = $wishlistResource;
Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/WishlistGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type WishlistItem {
}

type Mutation {
addProductsToWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemInput!]!): AddProductsToWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\AddProductsToWishlistResolver")
removeProductsFromWishlist(wishlist_id: ID!, wishlist_items_ids: [ID!]!): RemoveProductsFromWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\RemoveProductsFromWishlistResolver")
updateProductsInWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemUpdateInput!]!): UpdateProductsInWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\UpdateProductsInWishlistResolver")
addProductsToWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemInput!]!): AddProductsToWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\AddProductsToWishlist")
removeProductsFromWishlist(wishlist_id: ID!, wishlist_items_ids: [ID!]!): RemoveProductsFromWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\RemoveProductsFromWishlist")
updateProductsInWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemUpdateInput!]!): UpdateProductsInWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\UpdateProductsInWishlist")
}

input WishlistItemInput {
Expand Down
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");
}
}
Loading

0 comments on commit 7488a36

Please sign in to comment.