|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Quote; |
| 9 | + |
| 10 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 11 | +use Magento\Quote\Model\Quote; |
| 12 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 13 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for getting cart information |
| 19 | + */ |
| 20 | +class GetAvailablePaymentMethodsTest extends GraphQlAbstract |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var CustomerTokenServiceInterface |
| 24 | + */ |
| 25 | + private $customerTokenService; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var QuoteResource |
| 29 | + */ |
| 30 | + private $quoteResource; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Quote |
| 34 | + */ |
| 35 | + private $quote; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 39 | + */ |
| 40 | + private $quoteIdToMaskedId; |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritdoc |
| 44 | + */ |
| 45 | + protected function setUp() |
| 46 | + { |
| 47 | + $objectManager = Bootstrap::getObjectManager(); |
| 48 | + $this->quoteResource = $objectManager->create(QuoteResource::class); |
| 49 | + $this->quote = $objectManager->create(Quote::class); |
| 50 | + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); |
| 51 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php |
| 56 | + */ |
| 57 | + public function testGetCartWithPaymentMethodsForRegisteredCustomer() |
| 58 | + { |
| 59 | + $reservedOrderId = 'test_order_item_with_items'; |
| 60 | + $this->quoteResource->load( |
| 61 | + $this->quote, |
| 62 | + $reservedOrderId, |
| 63 | + 'reserved_order_id' |
| 64 | + ); |
| 65 | + |
| 66 | + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); |
| 67 | + $query = $this->prepareGetCartQuery($maskedQuoteId); |
| 68 | + |
| 69 | + $response = $this->sendRequestWithToken($query); |
| 70 | + |
| 71 | + self::assertArrayHasKey('cart', $response); |
| 72 | + self::assertNotEmpty($response['cart']['items']); |
| 73 | + self::assertNotEmpty($response['cart']['available_payment_methods']); |
| 74 | + self::assertCount(1, $response['cart']['available_payment_methods']); |
| 75 | + self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']); |
| 76 | + self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php |
| 81 | + */ |
| 82 | + public function testGetCartWithPaymentMethodsForGuest() |
| 83 | + { |
| 84 | + $reservedOrderId = 'test_order_1'; |
| 85 | + $this->quoteResource->load( |
| 86 | + $this->quote, |
| 87 | + $reservedOrderId, |
| 88 | + 'reserved_order_id' |
| 89 | + ); |
| 90 | + |
| 91 | + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); |
| 92 | + $query = $this->prepareGetCartQuery($maskedQuoteId); |
| 93 | + |
| 94 | + $response = $this->graphQlQuery($query); |
| 95 | + |
| 96 | + self::assertArrayHasKey('cart', $response); |
| 97 | + |
| 98 | + self::assertNotEmpty($response['cart']['available_payment_methods']); |
| 99 | + self::assertCount(2, $response['cart']['available_payment_methods']); |
| 100 | + self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']); |
| 101 | + self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']); |
| 102 | + self::assertEquals('free', $response['cart']['available_payment_methods'][1]['code']); |
| 103 | + self::assertEquals( |
| 104 | + 'No Payment Information Required', |
| 105 | + $response['cart']['available_payment_methods'][1]['title'] |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Generates query for setting the specified shipping method on cart |
| 111 | + * |
| 112 | + * @param string $maskedQuoteId |
| 113 | + * @return string |
| 114 | + */ |
| 115 | + private function prepareGetCartQuery( |
| 116 | + string $maskedQuoteId |
| 117 | + ) : string { |
| 118 | + return <<<QUERY |
| 119 | +{ |
| 120 | + cart(cart_id: "$maskedQuoteId") { |
| 121 | + applied_coupon { |
| 122 | + code |
| 123 | + } |
| 124 | + items { |
| 125 | + id |
| 126 | + } |
| 127 | + available_payment_methods { |
| 128 | + code, |
| 129 | + title |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +
|
| 134 | +QUERY; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Sends a GraphQL request with using a bearer token |
| 139 | + * |
| 140 | + * @param string $query |
| 141 | + * @return array |
| 142 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 143 | + */ |
| 144 | + private function sendRequestWithToken(string $query): array |
| 145 | + { |
| 146 | + |
| 147 | + $customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); |
| 148 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 149 | + |
| 150 | + return $this->graphQlQuery($query, [], '', $headerMap); |
| 151 | + } |
| 152 | +} |
0 commit comments