Skip to content
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

GraphQL GiftMessageGraphQl add coverage for cart #27956

Merged
merged 8 commits into from
Jun 5, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GiftMessageGraphQl\Model\Resolver\Cart;

use Magento\Framework\Exception\LocalizedException;
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;
use Magento\GiftMessage\Helper\Message as GiftMessageHelper;

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

/**
* @var GiftMessageHelper
*/
private $giftMessageHelper;

/**
* @param CartRepositoryInterface $cartRepository
* @param GiftMessageHelper $giftMessageHelper
*/
public function __construct(
CartRepositoryInterface $cartRepository,
GiftMessageHelper $giftMessageHelper
) {
$this->cartRepository = $cartRepository;
$this->giftMessageHelper = $giftMessageHelper;
}

/**
* 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'];

if (!$this->giftMessageHelper->isMessagesAllowed('order', $cart)) {
return null;
}

try {
$giftCartMessage = $this->cartRepository->get($cart->getId());
} catch (LocalizedException $e) {
throw new GraphQlInputException(__('Can\'t load cart.'));
}

if (!isset($giftCartMessage)) {
return null;
}

return [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is better to return null if the cart does not have a gift message, rather than returning an object with empty fields. This would align more with schema as you have specified theses fields as non-nullable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

'to' => $giftCartMessage->getRecipient() ?? '',
'from' => $giftCartMessage->getSender() ?? '',
'message'=> $giftCartMessage->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** provides information about gift messages for cart, cart items, order and order items.
25 changes: 25 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "magento/module-gift-message-graph-ql",
"description": "N/A",
"type": "magento2-module",
"require": {
lenaorobei marked this conversation as resolved.
Show resolved Hide resolved
"php": "~7.3.0||~7.4.0",
"magento/framework": "*",
"magento/module-gift-message": "*"
},
"suggest": {
"magento/module-graph-ql": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\GiftMessageGraphQl\\": ""
}
}
}
15 changes: 15 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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">
<sequence>
<module name="Magento_GiftMessage"/>
</sequence>
</module>
</config>
12 changes: 12 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

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

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,96 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\GiftMessage\Cart;

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

class GiftMessageTest extends GraphQlAbstract
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a negative test case? Where you attempt to retrieve gift message for a cart that has none.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

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

/**
* @magentoConfigFixture default_store sales/gift_options/allow_order 1
* @magentoApiDataFixture Magento/GiftMessage/_files/quote_with_message.php
* @throws NoSuchEntityException
* @throws Exception
*/
public function testGiftMessageForCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('message_order_21');
$response = $this->requestCartAndAssertResult($maskedQuoteId);
self::assertArrayHasKey('gift_message', $response['cart']);
self::assertSame('Mercutio', $response['cart']['gift_message']['to']);
self::assertSame('Romeo', $response['cart']['gift_message']['from']);
self::assertSame('I thought all for the best.', $response['cart']['gift_message']['message']);
}

/**
* @magentoConfigFixture default_store sales/gift_options/allow_order 0
* @magentoApiDataFixture Magento/GiftMessage/_files/quote_with_message.php
* @throws NoSuchEntityException
* @throws Exception
*/
public function testGiftMessageForCartWithNotAllow()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('message_order_21');
$response = $this->requestCartAndAssertResult($maskedQuoteId);
self::assertArrayHasKey('gift_message', $response['cart']);
self::assertNull($response['cart']['gift_message']);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @throws NoSuchEntityException
* @throws Exception
*/
public function testGiftMessageForCartWithoutMessage()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$response = $this->requestCartAndAssertResult($maskedQuoteId);
self::assertArrayHasKey('gift_message', $response['cart']);
self::assertNull($response['cart']['gift_message']);
}

/**
* Get Gift Message Assertion
*
* @param string $quoteId
*
* @return array
* @throws Exception
*/
private function requestCartAndAssertResult(string $quoteId)
{
$query = <<<QUERY
{
cart(cart_id: "$quoteId") {
gift_message {
to
from
message
}
}
}
QUERY;
return $this->graphQlQuery($query);
}
}