-
Notifications
You must be signed in to change notification settings - Fork 9.4k
GraphQL GiftMessageGraphQl add coverage for cart #27956
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dbaf8a5
Add GiftMessageGraphQL module
Usik2203 9479c3a
Fixed redundant dependancy issue
Usik2203 9d42238
Merge branch '2.4-develop' into feature-256
Usik2203 003162a
PR 27956 Refactored test
Usik2203 689bbea
Merge branch '2.4-develop' into feature-256
Usik2203 74a95e1
Removed composer.lock from commit
Usik2203 e4d9a05
Revert "Removed composer.lock from commit"
Usik2203 b059e51
Added check, test case and minor fixes
Usik2203 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/GiftMessage.php
This file contains hidden or 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,82 @@ | ||
<?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; | ||
|
||
/** | ||
* 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']; | ||
|
||
try { | ||
$giftCartMessage = $this->cartRepository->get($cart->getId()); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__('Can\'t load cart.')); | ||
} | ||
|
||
if (!isset($giftCartMessage)) { | ||
return null; | ||
} | ||
|
||
return [ | ||
'to' => $giftCartMessage->getRecipient() ?? '', | ||
'from' => $giftCartMessage->getSender() ?? '', | ||
'message'=> $giftCartMessage->getMessage() ?? '' | ||
]; | ||
} | ||
} |
This file contains hidden or 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,3 @@ | ||
# GiftMessageGraphQl | ||
|
||
**GiftMessageGraphQl** provides information about gift messages for cart, cart items, order and order items. |
This file contains hidden or 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,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\\": "" | ||
} | ||
} | ||
} |
This file contains hidden or 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,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> |
This file contains hidden or 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,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") | ||
} |
This file contains hidden or 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,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__ | ||
); |
This file contains hidden or 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
81 changes: 81 additions & 0 deletions
81
dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/GiftMessageTest.php
This file contains hidden or 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,81 @@ | ||
<?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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
{ | ||
/** | ||
* @var GetMaskedQuoteIdByReservedOrderId | ||
*/ | ||
private $getMaskedQuoteIdByReservedOrderId; | ||
|
||
protected function setUp() | ||
{ | ||
$objectManager = Bootstrap::getObjectManager(); | ||
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); | ||
} | ||
|
||
/** | ||
* @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']); | ||
} | ||
|
||
/** | ||
* @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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done