-
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.
ENGCOM-4352: graphQl-292: payment method list coverage #327
- Loading branch information
Showing
3 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
app/code/Magento/QuoteGraphQl/Model/Resolver/AvailablePaymentMethods.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,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; | ||
} | ||
} |
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
152 changes: 152 additions & 0 deletions
152
dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailablePaymentMethodsTest.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,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); | ||
} | ||
} |