-
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.
PR 28072 Add coverage for cart items
- Loading branch information
Showing
5 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.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,79 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GiftMessageGraphQl\Model\Resolver\Cart\Item; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
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 GiftMessage 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 | ||
* @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']; | ||
|
||
try { | ||
$giftItemMessage = $this->itemRepository->get($quoteItem->getQuoteId(), $quoteItem->getItemId()); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__('Can\'t load cart item')); | ||
} | ||
|
||
return [ | ||
'to' => isset($giftItemMessage) ? $giftItemMessage->getRecipient() : '', | ||
'from' => isset($giftItemMessage) ? $giftItemMessage->getSender() : '', | ||
'message'=> isset($giftItemMessage) ? $giftItemMessage->getMessage() : '' | ||
]; | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.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,63 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\GiftMessage\Cart\Item; | ||
|
||
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 | ||
{ | ||
/** | ||
* @var GetMaskedQuoteIdByReservedOrderId | ||
*/ | ||
private $getMaskedQuoteIdByReservedOrderId; | ||
|
||
protected function setUp() | ||
{ | ||
$objectManager = Bootstrap::getObjectManager(); | ||
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/GiftMessage/_files/guest/quote_with_item_message.php | ||
* @throws NoSuchEntityException | ||
* @throws Exception | ||
*/ | ||
public function testGiftMessageCartForItem() | ||
{ | ||
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_guest_order_with_gift_message'); | ||
$query = <<<QUERY | ||
{ | ||
cart(cart_id: "$maskedQuoteId") { | ||
items { | ||
product { | ||
name | ||
} | ||
... on SimpleCartItem { | ||
gift_message { | ||
to | ||
from | ||
message | ||
} | ||
} | ||
} | ||
} | ||
} | ||
QUERY; | ||
$response = $this->graphQlQuery($query); | ||
foreach ($response['cart']['items'] as $item) { | ||
self::assertArrayHasKey('gift_message', $item); | ||
self::assertArrayHasKey('to', $item['gift_message']); | ||
self::assertArrayHasKey('from', $item['gift_message']); | ||
self::assertArrayHasKey('message', $item['gift_message']); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.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,63 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
require __DIR__ . '/../../../../Magento/Catalog/_files/products.php'; | ||
|
||
use Magento\Catalog\Model\Product; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\GiftMessage\Model\Message; | ||
use Magento\GiftMessage\Model\ResourceModel\Message as MessageResource; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\Quote\Model\QuoteIdMask; | ||
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; | ||
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResource; | ||
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMaskFactory; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
|
||
/** @var ObjectManagerInterface $objectManager */ | ||
$objectManager = Bootstrap::getObjectManager(); | ||
|
||
/** @var QuoteResource $quote */ | ||
$quote = $objectManager->create(QuoteResource::class); | ||
|
||
/** @var Quote $quoteModel */ | ||
$quoteModel = $objectManager->create(Quote::class); | ||
$quoteModel->setData(['store_id' => 1, 'is_active' => 1, 'is_multi_shipping' => 0]); | ||
$quote->save($quoteModel); | ||
|
||
$product = $objectManager->create(Product::class); | ||
$quoteProduct = $product->load($product->getIdBySku('simple')); | ||
|
||
$quoteModel->setReservedOrderId('test_guest_order_with_gift_message') | ||
->addProduct($product->load($product->getIdBySku('simple')), 1); | ||
$quoteModel->collectTotals(); | ||
$quote->save($quoteModel); | ||
|
||
/** @var MessageResource $message */ | ||
$message = $objectManager->create(MessageResource::class); | ||
|
||
/** @var Message $message */ | ||
$messageModel = $objectManager->create(Message::class); | ||
|
||
$messageModel->setSender('John Doe'); | ||
$messageModel->setRecipient('Jane Roe'); | ||
$messageModel->setMessage('Gift Message Text'); | ||
$message->save($messageModel); | ||
|
||
$quoteModel->getItemByProduct($quoteProduct)->setGiftMessageId($messageModel->getId()); | ||
$quote->save($quoteModel); | ||
|
||
/** @var QuoteIdMaskResource $quoteIdMask */ | ||
$quoteIdMask = Bootstrap::getObjectManager() | ||
->create(QuoteIdMaskFactory::class) | ||
->create(); | ||
|
||
/** @var QuoteIdMask $quoteIdMaskModel */ | ||
$quoteIdMaskModel = $objectManager->create(QuoteIdMask::class); | ||
|
||
$quoteIdMaskModel->setQuoteId($quoteModel->getId()); | ||
$quoteIdMaskModel->setDataChanges(true); | ||
$quoteIdMask->save($quoteIdMaskModel); |
32 changes: 32 additions & 0 deletions
32
...tegration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message_rollback.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,32 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
use Magento\Catalog\Model\Product; | ||
use Magento\Framework\Registry; | ||
use Magento\GiftMessage\Model\Message; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
|
||
$registry = Bootstrap::getObjectManager()->get(Registry::class); | ||
$registry->unregister('isSecureArea'); | ||
$registry->register('isSecureArea', true); | ||
$objectManager = Bootstrap::getObjectManager(); | ||
$quote = $objectManager->create(Quote::class); | ||
$quote->load('test_guest_order_with_gift_message', 'reserved_order_id'); | ||
$message = $objectManager->create(Message::class); | ||
$product = $objectManager->create(Product::class); | ||
foreach ($quote->getAllItems() as $item) { | ||
$message->load($item->getGiftMessageId()); | ||
$message->delete(); | ||
$sku = $item->getSku(); | ||
$product->load($product->getIdBySku($sku)); | ||
if ($product->getId()) { | ||
$product->delete(); | ||
} | ||
} | ||
$quote->delete(); | ||
$registry->unregister('isSecureArea'); | ||
$registry->register('isSecureArea', false); |