Skip to content

[WIP] Add GraphQl coverage for GiftMessage module for cart items #28070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/Model/Resolver/GiftMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GiftMessageGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GiftMessage\Api\CartRepositoryInterface;

/**
* Class provides ability to get GiftMessage for cart
*/
class GiftMessage implements ResolverInterface
{
/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* GiftMessage constructor.
*
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
CartRepositoryInterface $cartRepository
) {
$this->cartRepository = $cartRepository;
}

/**
* Return information about Gift message of cart
*
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
*
* @return array|Value|mixed
*
* @throws GraphQlInputException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($value['model'])) {
throw new GraphQlInputException(__('"model" value should be specified'));
}
$cart = $value['model'];

$giftCartMessage = $this->cartRepository->get($cart->getId());

if ($giftCartMessage === null) {
throw new GraphQlInputException(__("Gift message doesn't exist for current cart"));
}

return [
'to' => $giftCartMessage->getRecipient() ?? '',
'from' => $giftCartMessage->getSender() ?? '',
'message'=> $giftCartMessage->getMessage() ?? ''
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GiftMessageGraphQl\Model\Resolver;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GiftMessage\Api\ItemRepositoryInterface;

/**
* Class provides ability to get GiftMessage for cart item
*/
class GiftMessageItem implements ResolverInterface
{
/**
* @var ItemRepositoryInterface
*/
private $itemRepository;

/**
* GiftMessageItem constructor.
*
* @param ItemRepositoryInterface $itemRepository
*/
public function __construct(
ItemRepositoryInterface $itemRepository
) {
$this->itemRepository = $itemRepository;
}

/**
* Return information about Gift message for item cart
*
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
*
* @return array|Value|mixed
* @throws GraphQlInputException
* @throws NoSuchEntityException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($value['model'])) {
throw new GraphQlInputException(__('"model" value should be specified'));
}

$quoteItem = $value['model'];

$giftItemMessage = $this->itemRepository->get($quoteItem->getQuoteId(), $quoteItem->getItemId());

return [
'to' => $giftItemMessage !== null ? $giftItemMessage->getRecipient() : '',
'from' => $giftItemMessage !== null ? $giftItemMessage->getSender() : '',
'message'=> $giftItemMessage !== null ? $giftItemMessage->getMessage() : ''
];
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# GiftMessageGraphQl

**GiftMessageGraphQl**
22 changes: 22 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "magento/module-gift-message-graph-ql",
"description": "N/A",
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0||~7.3.0",
"magento/framework": "*",
"magento/module-gift-message": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\GiftMessageGraphQl\\": ""
}
}
}
11 changes: 11 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_GiftMessageGraphQl"/>
</config>
36 changes: 36 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type Cart {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\GiftMessage") @doc(description: "The entered gift message for the cart")
}

type SimpleCartItem {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\GiftMessageItem") @doc(description: "The entered gift message for the cart item")
}

type ConfigurableCartItem {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\GiftMessageItem") @doc(description: "The entered gift message for the cart item")
}

type BundleCartItem {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\GiftMessageItem") @doc(description: "The entered gift message for the cart item")
}

type GiftCardCartItem {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\GiftMessageItem") @doc(description: "The entered gift message for the cart item")
}
# TODO gift message for the order and order item
type SalesItemInterface {
gift_message: GiftMessage @doc(description: "The entered gift message for the order item")
}

type CustomerOrder {
gift_message: GiftMessage @doc(description: "The entered gift message for the order")
}

type GiftMessage {
to: String! @doc(description: "Recepient name")
from: String! @doc(description: "Sender name")
message: String! @doc(description: "Gift message text")
}
13 changes: 13 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Magento_GiftMessageGraphQl',
__DIR__
);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
"magento/module-encryption-key": "*",
"magento/module-fedex": "*",
"magento/module-gift-message": "*",
"magento/module-gift-message-graph-ql": "*",
"magento/module-google-adwords": "*",
"magento/module-google-analytics": "*",
"magento/module-google-optimizer": "*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\GiftMessage;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

class GiftMessageTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}

/**
* @magentoApiDataFixture Magento/GiftMessage/_files/quote_with_message.php
*/
public function testGiftMessageForCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('message_order_21');
$query = <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
gift_message {
to
from
message
}
}
}
QUERY;
$response = $this->graphQlQuery($query);
self::assertArrayHasKey('gift_message', $response['cart']);
self::assertArrayHasKey('to', $response['cart']['gift_message']);
self::assertArrayHasKey('from', $response['cart']['gift_message']);
self::assertArrayHasKey('message', $response['cart']['gift_message']);
}
}