Skip to content

Commit

Permalink
ENGCOM-4352: graphQl-292: payment method list coverage #327
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav authored Feb 21, 2019
2 parents 310648c + 8f8fca1 commit 5842b9e
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Checkout\Api\PaymentInformationManagementInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\Data\CartInterface;

/**
* Get list of active payment methods resolver.
*/
class AvailablePaymentMethods implements ResolverInterface
{
/**
* @var PaymentInformationManagementInterface
*/
private $informationManagement;

/**
* @param PaymentInformationManagementInterface $informationManagement
*/
public function __construct(PaymentInformationManagementInterface $informationManagement)
{
$this->informationManagement = $informationManagement;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

$cart = $value['model'];
return $this->getPaymentMethodsData($cart);
}

/**
* Collect and return information about available payment methods
*
* @param CartInterface $cart
* @return array
*/
private function getPaymentMethodsData(CartInterface $cart): array
{
$paymentInformation = $this->informationManagement->getPaymentInformation($cart->getId());
$paymentMethods = $paymentInformation->getPaymentMethods();

$paymentMethodsData = [];
foreach ($paymentMethods as $paymentMethod) {
$paymentMethodsData[] = [
'title' => $paymentMethod->getTitle(),
'code' => $paymentMethod->getCode(),
];
}
return $paymentMethodsData;
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Cart {
applied_coupon: AppliedCoupon
shipping_addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
available_payment_methods : [PaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
}

type CartAddress {
Expand Down Expand Up @@ -155,6 +156,11 @@ type AvailableShippingMethod {
price_incl_tax: Float!
}

type PaymentMethod {
code: String @doc(description: "The payment method code")
title: String @doc(description: "The payment method title.")
}

enum AdressTypeEnum {
SHIPPING
BILLING
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote;

use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for getting cart information
*/
class GetAvailablePaymentMethodsTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @var QuoteResource
*/
private $quoteResource;

/**
* @var Quote
*/
private $quote;

/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedId;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->create(QuoteResource::class);
$this->quote = $objectManager->create(Quote::class);
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
*/
public function testGetCartWithPaymentMethodsForRegisteredCustomer()
{
$reservedOrderId = 'test_order_item_with_items';
$this->quoteResource->load(
$this->quote,
$reservedOrderId,
'reserved_order_id'
);

$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
$query = $this->prepareGetCartQuery($maskedQuoteId);

$response = $this->sendRequestWithToken($query);

self::assertArrayHasKey('cart', $response);
self::assertNotEmpty($response['cart']['items']);
self::assertNotEmpty($response['cart']['available_payment_methods']);
self::assertCount(1, $response['cart']['available_payment_methods']);
self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']);
self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
public function testGetCartWithPaymentMethodsForGuest()
{
$reservedOrderId = 'test_order_1';
$this->quoteResource->load(
$this->quote,
$reservedOrderId,
'reserved_order_id'
);

$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
$query = $this->prepareGetCartQuery($maskedQuoteId);

$response = $this->graphQlQuery($query);

self::assertArrayHasKey('cart', $response);

self::assertNotEmpty($response['cart']['available_payment_methods']);
self::assertCount(2, $response['cart']['available_payment_methods']);
self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']);
self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']);
self::assertEquals('free', $response['cart']['available_payment_methods'][1]['code']);
self::assertEquals(
'No Payment Information Required',
$response['cart']['available_payment_methods'][1]['title']
);
}

/**
* Generates query for setting the specified shipping method on cart
*
* @param string $maskedQuoteId
* @return string
*/
private function prepareGetCartQuery(
string $maskedQuoteId
) : string {
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
applied_coupon {
code
}
items {
id
}
available_payment_methods {
code,
title
}
}
}
QUERY;
}

/**
* Sends a GraphQL request with using a bearer token
*
* @param string $query
* @return array
* @throws \Magento\Framework\Exception\AuthenticationException
*/
private function sendRequestWithToken(string $query): array
{

$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];

return $this->graphQlQuery($query, [], '', $headerMap);
}
}

0 comments on commit 5842b9e

Please sign in to comment.