Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
Test for offline payment methods
Browse files Browse the repository at this point in the history
Fixes #601
  • Loading branch information
pmclain committed Apr 25, 2019
1 parent e73db01 commit 97c416c
Show file tree
Hide file tree
Showing 2 changed files with 315 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote\Customer;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\OfflinePayments\Model\Banktransfer;
use Magento\OfflinePayments\Model\Cashondelivery;
use Magento\OfflinePayments\Model\Checkmo;
use Magento\OfflinePayments\Model\Purchaseorder;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for setting offline payment methods on cart
*/
class SetOfflinePaymentMethodsOnCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
*
* @param string $methodCode
* @dataProvider offlinePaymentMethodDataProvider
*/
public function testSetOfflinePaymentMethod(string $methodCode)
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$query = $this->getQuery(
$maskedQuoteId,
$methodCode
);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertArrayHasKey('code', $response['setPaymentMethodOnCart']['cart']['selected_payment_method']);
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
*
* @param string $methodCode
*/
public function testSetPurchaseOrderPaymentMethod()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
$poNumber = 'abc123';

$query = <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "{$maskedQuoteId}",
payment_method: {
code: "{$methodCode}"
purchase_order_number: "{$poNumber}"
}
}) {
cart {
selected_payment_method {
code
purchase_order_number
}
}
}
}
QUERY;

$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertArrayHasKey('code', $response['setPaymentMethodOnCart']['cart']['selected_payment_method']);
self::assertArrayHasKey('purchase_order_number', $response['setPaymentMethodOnCart']['cart']['selected_payment_method']);
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
self::assertEquals($poNumber, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['purchase_order_number']);
}

/**
* @return array
*/
public function offlinePaymentMethodDataProvider(): array
{
return [
'check_mo' => [Checkmo::PAYMENT_METHOD_CHECKMO_CODE],
'bank_transfer' => [Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE],
'cash_on_delivery' => [Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE],
];
}

/**
* @param string $maskedQuoteId
* @param string $methodCode
* @return string
*/
private function getQuery(
string $maskedQuoteId,
string $methodCode
) : string {
return <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "{$maskedQuoteId}",
payment_method: {
code: "{$methodCode}"
}
}) {
cart {
selected_payment_method {
code
}
}
}
}
QUERY;
}

/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote\Guest;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\OfflinePayments\Model\Banktransfer;
use Magento\OfflinePayments\Model\Cashondelivery;
use Magento\OfflinePayments\Model\Checkmo;
use Magento\OfflinePayments\Model\Purchaseorder;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for setting offline payment methods on cart
*/
class SetOfflinePaymentMethodsOnCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
*
* @param string $methodCode
* @dataProvider offlinePaymentMethodDataProvider
*/
public function testSetOfflinePaymentMethod(string $methodCode)
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$query = $this->getQuery(
$maskedQuoteId,
$methodCode
);
$response = $this->graphQlMutation($query);

self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertArrayHasKey('code', $response['setPaymentMethodOnCart']['cart']['selected_payment_method']);
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
*
* @param string $methodCode
*/
public function testSetPurchaseOrderPaymentMethod()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
$poNumber = 'abc123';

$query = <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "{$maskedQuoteId}",
payment_method: {
code: "{$methodCode}"
purchase_order_number: "{$poNumber}"
}
}) {
cart {
selected_payment_method {
code
purchase_order_number
}
}
}
}
QUERY;

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

self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertArrayHasKey('code', $response['setPaymentMethodOnCart']['cart']['selected_payment_method']);
self::assertArrayHasKey('purchase_order_number', $response['setPaymentMethodOnCart']['cart']['selected_payment_method']);
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
self::assertEquals($poNumber, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['purchase_order_number']);
}

/**
* @return array
*/
public function offlinePaymentMethodDataProvider(): array
{
return [
'check_mo' => [Checkmo::PAYMENT_METHOD_CHECKMO_CODE],
'bank_transfer' => [Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE],
'cash_on_delivery' => [Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE],
];
}

/**
* @param string $maskedQuoteId
* @param string $methodCode
* @return string
*/
private function getQuery(
string $maskedQuoteId,
string $methodCode
) : string {
return <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "{$maskedQuoteId}",
payment_method: {
code: "{$methodCode}"
}
}) {
cart {
selected_payment_method {
code
}
}
}
}
QUERY;
}
}

0 comments on commit 97c416c

Please sign in to comment.