Skip to content

Commit 0cc1f1d

Browse files
authored
ENGCOM-5006: Added Test coverage for "Get Selected payment method" #653
2 parents 1170d5c + 7a4dff2 commit 0cc1f1d

File tree

2 files changed

+248
-0
lines changed

2 files changed

+248
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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\Customer;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\Integration\Api\CustomerTokenServiceInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
/**
16+
* Test for getting selected payment method from cart
17+
*/
18+
class GetSelectedPaymentMethodTest extends GraphQlAbstract
19+
{
20+
/**
21+
* @var GetMaskedQuoteIdByReservedOrderId
22+
*/
23+
private $getMaskedQuoteIdByReservedOrderId;
24+
25+
/**
26+
* @var CustomerTokenServiceInterface
27+
*/
28+
private $customerTokenService;
29+
30+
/**
31+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
32+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
33+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
34+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
35+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
36+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
37+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
38+
*/
39+
public function testGetSelectedPaymentMethod()
40+
{
41+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
42+
43+
$query = $this->getQuery($maskedQuoteId);
44+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
45+
46+
$this->assertArrayHasKey('cart', $response);
47+
$this->assertArrayHasKey('selected_payment_method', $response['cart']);
48+
$this->assertArrayHasKey('code', $response['cart']['selected_payment_method']);
49+
$this->assertEquals('checkmo', $response['cart']['selected_payment_method']['code']);
50+
}
51+
52+
/**
53+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
54+
* @expectedException \Exception
55+
*/
56+
public function testGetSelectedPaymentMethodFromNonExistentCart()
57+
{
58+
$maskedQuoteId = 'non_existent_masked_id';
59+
$query = $this->getQuery($maskedQuoteId);
60+
61+
$this->expectExceptionMessage(
62+
'Could not find a cart with ID "non_existent_masked_id"'
63+
);
64+
65+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
66+
}
67+
68+
/**
69+
* _security
70+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
71+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
72+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
73+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
74+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
75+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
76+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
77+
*/
78+
public function testGetSelectedPaymentMethodFromGuestCart()
79+
{
80+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
81+
$query = $this->getQuery($maskedQuoteId);
82+
83+
$this->expectExceptionMessage(
84+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
85+
);
86+
87+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
88+
}
89+
90+
/**
91+
* _security
92+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
93+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
94+
*/
95+
public function testGetSelectedPaymentMethodFromAnotherCustomerCart()
96+
{
97+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
98+
$query = $this->getQuery($maskedQuoteId);
99+
100+
$this->expectExceptionMessage(
101+
"The current user cannot perform operations on cart \"{$maskedQuoteId}\""
102+
);
103+
104+
$this->graphQlQuery($query, [], '', $this->getHeaderMap('[email protected]'));
105+
}
106+
107+
protected function setUp()
108+
{
109+
$objectManager = Bootstrap::getObjectManager();
110+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
111+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
112+
}
113+
114+
/**
115+
* @param string $maskedQuoteId
116+
* @return string
117+
*/
118+
private function getQuery(string $maskedQuoteId): string
119+
{
120+
return <<<QUERY
121+
{
122+
cart(cart_id:"$maskedQuoteId") {
123+
selected_payment_method {
124+
code
125+
}
126+
}
127+
}
128+
QUERY;
129+
}
130+
131+
/**
132+
* @param string $username
133+
* @param string $password
134+
* @return array
135+
* @throws \Magento\Framework\Exception\AuthenticationException
136+
*/
137+
private function getHeaderMap(string $username = '[email protected]', string $password = 'password'): array
138+
{
139+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
140+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
141+
return $headerMap;
142+
}
143+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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\Guest;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
14+
/**
15+
* Test for getting selected payment method from cart
16+
*/
17+
class GetSelectedPaymentMethodTest extends GraphQlAbstract
18+
{
19+
/**
20+
* @var GetMaskedQuoteIdByReservedOrderId
21+
*/
22+
private $getMaskedQuoteIdByReservedOrderId;
23+
24+
protected function setUp()
25+
{
26+
$objectManager = Bootstrap::getObjectManager();
27+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
28+
}
29+
30+
/**
31+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
32+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
33+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
34+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
35+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
36+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
37+
*
38+
*/
39+
public function testGetSelectedPaymentMethod()
40+
{
41+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
42+
43+
$query = $this->getQuery($maskedQuoteId);
44+
$response = $this->graphQlQuery($query);
45+
46+
$this->assertArrayHasKey('cart', $response);
47+
$this->assertArrayHasKey('selected_payment_method', $response['cart']);
48+
$this->assertArrayHasKey('code', $response['cart']['selected_payment_method']);
49+
$this->assertEquals('checkmo', $response['cart']['selected_payment_method']['code']);
50+
}
51+
52+
/**
53+
* @expectedException \Exception
54+
*/
55+
public function testGetSelectedPaymentMethodFromNonExistentCart()
56+
{
57+
$maskedQuoteId = 'non_existent_masked_id';
58+
$query = $this->getQuery($maskedQuoteId);
59+
60+
$this->expectExceptionMessage(
61+
'Could not find a cart with ID "non_existent_masked_id"'
62+
);
63+
64+
$this->graphQlQuery($query);
65+
}
66+
67+
/**
68+
* _security
69+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
70+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
71+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
72+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
73+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
74+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
75+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
76+
*/
77+
public function testGetSelectedPaymentMethodFromCustomerCart()
78+
{
79+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
80+
$query = $this->getQuery($maskedQuoteId);
81+
82+
$this->expectExceptionMessage(
83+
"The current user cannot perform operations on cart \"{$maskedQuoteId}\""
84+
);
85+
86+
$this->graphQlQuery($query);
87+
}
88+
89+
/**
90+
* @param string $maskedQuoteId
91+
* @return string
92+
*/
93+
private function getQuery(string $maskedQuoteId): string
94+
{
95+
return <<<QUERY
96+
{
97+
cart(cart_id:"$maskedQuoteId") {
98+
selected_payment_method {
99+
code
100+
}
101+
}
102+
}
103+
QUERY;
104+
}
105+
}

0 commit comments

Comments
 (0)