Skip to content

Commit

Permalink
PR 28072 Add coverage for cart items
Browse files Browse the repository at this point in the history
  • Loading branch information
Usik2203 committed May 20, 2020
1 parent b059e51 commit 034af40
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 0 deletions.
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() : ''
];
}
}
16 changes: 16 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ type Cart {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\GiftMessage") @doc(description: "The entered gift message for the cart")
}

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

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

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

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

type GiftMessage {
to: String! @doc(description: "Recepient name")
from: String! @doc(description: "Sender name")
Expand Down
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']);
}
}
}
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);
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);

0 comments on commit 034af40

Please sign in to comment.