Skip to content

Commit 6f120a3

Browse files
authored
Merge pull request #4289 from magento-honey-badgers/MC-16922
PR-authorizenet-test
2 parents 98703f6 + 4f1802b commit 6f120a3

12 files changed

+711
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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\AuthorizenetGraphQl\Model\Resolver\Customer;
9+
10+
use Magento\Framework\App\Request\Http;
11+
use Magento\Framework\Serialize\SerializerInterface;
12+
use Magento\GraphQl\Controller\GraphQl;
13+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
14+
use Magento\Integration\Api\CustomerTokenServiceInterface;
15+
use Magento\Framework\Webapi\Request;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\Framework\HTTP\ZendClient;
18+
use Magento\Framework\HTTP\ZendClientFactory;
19+
use Magento\TestFramework\ObjectManager;
20+
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
21+
use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
22+
use PHPUnit\Framework\MockObject\MockObject;
23+
use Magento\Quote\Model\Quote\PaymentFactory;
24+
use PHPUnit\Framework\TestCase;
25+
use Zend_Http_Response;
26+
27+
/**
28+
* Tests end to end Place Order process for customer via authorizeNet
29+
*
30+
* @magentoAppArea graphql
31+
* @magentoDbIsolation disabled
32+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
33+
*/
34+
class PlaceOrderWithAuthorizeNetTest extends TestCase
35+
{
36+
const CONTENT_TYPE = 'application/json';
37+
38+
/** @var ObjectManager */
39+
private $objectManager;
40+
41+
/** @var GetMaskedQuoteIdByReservedOrderId */
42+
private $getMaskedQuoteIdByReservedOrderId;
43+
44+
/** @var SerializerInterface */
45+
private $jsonSerializer;
46+
47+
/** @var Http */
48+
private $request;
49+
50+
/** @var ZendClient|MockObject|InvocationMocker */
51+
private $clientMock;
52+
53+
/** @var CustomerTokenServiceInterface */
54+
private $customerTokenService;
55+
56+
/** @var Zend_Http_Response */
57+
protected $responseMock;
58+
59+
/** @var PaymentFactory */
60+
private $paymentFactory;
61+
62+
protected function setUp() : void
63+
{
64+
$this->objectManager = Bootstrap::getObjectManager();
65+
$this->jsonSerializer = $this->objectManager->get(SerializerInterface::class);
66+
$this->request = $this->objectManager->get(Http::class);
67+
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
68+
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
69+
$this->clientMock = $this->createMock(ZendClient::class);
70+
$this->responseMock = $this->createMock(Zend_Http_Response::class);
71+
$this->clientMock->method('request')
72+
->willReturn($this->responseMock);
73+
$this->clientMock->method('setUri')
74+
->with('https://apitest.authorize.net/xml/v1/request.api');
75+
$clientFactoryMock = $this->createMock(ZendClientFactory::class);
76+
$clientFactoryMock->method('create')
77+
->willReturn($this->clientMock);
78+
/** @var PaymentDataObjectFactory $paymentFactory */
79+
$this->paymentFactory = $this->objectManager->get(PaymentDataObjectFactory::class);
80+
$this->objectManager->addSharedInstance($clientFactoryMock, ZendClientFactory::class);
81+
}
82+
83+
/**
84+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/active 1
85+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/environment sandbox
86+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/login someusername
87+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/trans_key somepassword
88+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/trans_signature_key abc
89+
* @magentoDataFixture Magento/Sales/_files/default_rollback.php
90+
* @magentoDataFixture Magento/Customer/_files/customer.php
91+
* @magentoDataFixture Magento/AuthorizenetGraphQl/_files/simple_product_authorizenet.php
92+
* @magentoDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
93+
* @magentoDataFixture Magento/AuthorizenetGraphQl/_files/set_new_shipping_address_authorizenet.php
94+
* @magentoDataFixture Magento/AuthorizenetGraphQl/_files/set_new_billing_address_authorizenet.php
95+
* @magentoDataFixture Magento/AuthorizenetGraphQl/_files/add_simple_products_authorizenet.php
96+
* @magentoDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
97+
*/
98+
public function testDispatchToPlaceOrderWithRegisteredCustomer(): void
99+
{
100+
$paymentMethod = 'authorizenet_acceptjs';
101+
$cartId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
102+
$query
103+
= <<<QUERY
104+
mutation {
105+
setPaymentMethodOnCart(input: {
106+
cart_id: "$cartId"
107+
payment_method: {
108+
code: "$paymentMethod"
109+
additional_data:
110+
{authorizenet_acceptjs:
111+
{opaque_data_descriptor: "mydescriptor",
112+
opaque_data_value: "myvalue",
113+
cc_last_4: 1111}}
114+
}
115+
}) {
116+
cart {
117+
selected_payment_method {
118+
code
119+
}
120+
}
121+
}
122+
placeOrder(input: {cart_id: "$cartId"}) {
123+
order {
124+
order_id
125+
}
126+
}
127+
}
128+
QUERY;
129+
$postData = [
130+
'query' => $query,
131+
'variables' => null,
132+
'operationName' => null
133+
];
134+
$this->request->setPathInfo('/graphql');
135+
$this->request->setMethod('POST');
136+
$this->request->setContent($this->jsonSerializer->serialize($postData));
137+
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
138+
$bearerCustomerToken = 'Bearer ' . $customerToken;
139+
$webApiRequest = $this->objectManager->get(Request::class);
140+
$webApiRequest->getHeaders()->addHeaderLine('Content-Type', 'application/json')
141+
->addHeaderLine('Accept', 'application/json')
142+
->addHeaderLine('Authorization', $bearerCustomerToken);
143+
$this->request->setHeaders($webApiRequest->getHeaders());
144+
$graphql = $this->objectManager->get(\Magento\GraphQl\Controller\GraphQl::class);
145+
146+
// phpcs:ignore Magento2.Security.IncludeFile
147+
$expectedRequest = include __DIR__ . '/../../../_files/request_authorize_customer.php';
148+
// phpcs:ignore Magento2.Security.IncludeFile
149+
$authorizeResponse = include __DIR__ . '/../../../_files/response_authorize.php';
150+
151+
$this->clientMock->method('setRawData')
152+
->with(json_encode($expectedRequest), 'application/json');
153+
154+
$this->responseMock->method('getBody')->willReturn(json_encode($authorizeResponse));
155+
156+
$response = $graphql->dispatch($this->request);
157+
$responseData = $this->jsonSerializer->unserialize($response->getContent());
158+
159+
$this->assertArrayNotHasKey('errors', $responseData, 'Response has errors');
160+
$this->assertTrue(
161+
isset($responseData['data']['setPaymentMethodOnCart']['cart']['selected_payment_method']['code'])
162+
);
163+
$this->assertEquals(
164+
$paymentMethod,
165+
$responseData['data']['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']
166+
);
167+
168+
$this->assertTrue(
169+
isset($responseData['data']['placeOrder']['order']['order_id'])
170+
);
171+
172+
$this->assertEquals(
173+
'test_quote',
174+
$responseData['data']['placeOrder']['order']['order_id']
175+
);
176+
}
177+
178+
protected function tearDown()
179+
{
180+
$this->objectManager->removeSharedInstance(ZendClientFactory::class);
181+
parent::tearDown();
182+
}
183+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\GraphQl\Quote\Customer;
8+
namespace Magento\AuthorizenetGraphQl\Model\Resolver\Customer;
99

1010
use Magento\Framework\App\Request\Http;
1111
use Magento\Framework\Serialize\SerializerInterface;
1212
use Magento\Framework\Webapi\Request;
1313
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
1414
use Magento\Integration\Api\CustomerTokenServiceInterface;
1515
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
1617

1718
/**
1819
* Tests SetPaymentMethod mutation for customer via authorizeNet payment
@@ -21,7 +22,7 @@
2122
* @magentoDbIsolation disabled
2223
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2324
*/
24-
class SetAuthorizenetPaymentMethodOnCustomerCartTest extends \PHPUnit\Framework\TestCase
25+
class SetAuthorizeNetPaymentMethodOnCartTest extends TestCase
2526
{
2627
const CONTENT_TYPE = 'application/json';
2728

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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\AuthorizenetGraphQl\Model\Resolver\Guest;
9+
10+
use Magento\Framework\App\Request\Http;
11+
use Magento\Framework\Serialize\SerializerInterface;
12+
use Magento\GraphQl\Controller\GraphQl;
13+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\Framework\HTTP\ZendClient;
16+
use Magento\Framework\HTTP\ZendClientFactory;
17+
use Magento\TestFramework\ObjectManager;
18+
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
19+
use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use Magento\Quote\Model\Quote\PaymentFactory;
22+
use PHPUnit\Framework\TestCase;
23+
use Zend_Http_Response;
24+
25+
/**
26+
* Tests end to end Place Order process for non logged in customer using authorizeNet payment
27+
*
28+
* @magentoAppArea graphql
29+
* @magentoDbIsolation disabled
30+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31+
*/
32+
class PlaceOrderWithAuthorizeNetTest extends TestCase
33+
{
34+
const CONTENT_TYPE = 'application/json';
35+
36+
/** @var ObjectManager */
37+
private $objectManager;
38+
39+
/** @var GetMaskedQuoteIdByReservedOrderId */
40+
private $getMaskedQuoteIdByReservedOrderId;
41+
42+
/** @var GraphQl */
43+
private $graphql;
44+
45+
/** @var SerializerInterface */
46+
private $jsonSerializer;
47+
48+
/** @var Http */
49+
private $request;
50+
51+
/** @var ZendClient|MockObject|InvocationMocker */
52+
private $clientMock;
53+
54+
/** @var Zend_Http_Response */
55+
protected $responseMock;
56+
57+
/** @var PaymentFactory */
58+
private $paymentFactory;
59+
60+
protected function setUp() : void
61+
{
62+
$this->objectManager = Bootstrap::getObjectManager();
63+
$this->graphql = $this->objectManager->get(\Magento\GraphQl\Controller\GraphQl::class);
64+
$this->jsonSerializer = $this->objectManager->get(SerializerInterface::class);
65+
$this->request = $this->objectManager->get(Http::class);
66+
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
67+
$this->clientMock = $this->createMock(ZendClient::class);
68+
$this->responseMock = $this->createMock(Zend_Http_Response::class);
69+
$this->clientMock->method('request')
70+
->willReturn($this->responseMock);
71+
$this->clientMock->method('setUri')
72+
->with('https://apitest.authorize.net/xml/v1/request.api');
73+
$clientFactoryMock = $this->createMock(ZendClientFactory::class);
74+
$clientFactoryMock->method('create')
75+
->willReturn($this->clientMock);
76+
/** @var PaymentDataObjectFactory $paymentFactory */
77+
$this->paymentFactory = $this->objectManager->get(PaymentDataObjectFactory::class);
78+
$this->objectManager->addSharedInstance($clientFactoryMock, ZendClientFactory::class);
79+
}
80+
81+
/**
82+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/active 1
83+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/environment sandbox
84+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/login someusername
85+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/trans_key somepassword
86+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/trans_signature_key abc
87+
* @magentoDataFixture Magento/Sales/_files/default_rollback.php
88+
* @magentoDataFixture Magento/AuthorizenetGraphQl/_files/simple_product_authorizenet.php
89+
* @magentoDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
90+
* @magentoDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
91+
* @magentoDataFixture Magento/AuthorizenetGraphQl/_files/set_new_shipping_address_authorizenet.php
92+
* @magentoDataFixture Magento/AuthorizenetGraphQl/_files/set_new_billing_address_authorizenet.php
93+
* @magentoDataFixture Magento/AuthorizenetGraphQl/_files/add_simple_products_authorizenet.php
94+
* @magentoDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
95+
*/
96+
public function testDispatchToPlaceAnOrderWithAuthorizenet(): void
97+
{
98+
$paymentMethod = 'authorizenet_acceptjs';
99+
$cartId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
100+
$query
101+
= <<<QUERY
102+
mutation {
103+
setPaymentMethodOnCart(input: {
104+
cart_id: "$cartId"
105+
payment_method: {
106+
code: "$paymentMethod"
107+
additional_data:
108+
{authorizenet_acceptjs:
109+
{opaque_data_descriptor: "mydescriptor",
110+
opaque_data_value: "myvalue",
111+
cc_last_4: 1111}}
112+
}
113+
}) {
114+
cart {
115+
selected_payment_method {
116+
code
117+
}
118+
}
119+
}
120+
placeOrder(input: {cart_id: "$cartId"}) {
121+
order {
122+
order_id
123+
}
124+
}
125+
}
126+
QUERY;
127+
$postData = [
128+
'query' => $query,
129+
'variables' => null,
130+
'operationName' => null
131+
];
132+
$this->request->setPathInfo('/graphql');
133+
$this->request->setMethod('POST');
134+
$this->request->setContent(json_encode($postData));
135+
$headers = $this->objectManager->create(\Zend\Http\Headers::class)
136+
->addHeaders(['Content-Type' => 'application/json']);
137+
$this->request->setHeaders($headers);
138+
// phpcs:ignore Magento2.Security.IncludeFile
139+
$expectedRequest = include __DIR__ . '/../../../_files/request_authorize.php';
140+
// phpcs:ignore Magento2.Security.IncludeFile
141+
$authorizeResponse = include __DIR__ . '/../../../_files/response_authorize.php';
142+
143+
$this->clientMock->method('setRawData')
144+
->with(json_encode($expectedRequest), 'application/json');
145+
146+
$this->responseMock->method('getBody')->willReturn(json_encode($authorizeResponse));
147+
148+
$response = $this->graphql->dispatch($this->request);
149+
$responseData = $this->jsonSerializer->unserialize($response->getContent());
150+
151+
$this->assertArrayNotHasKey('errors', $responseData, 'Response has errors');
152+
$this->assertTrue(
153+
isset($responseData['data']['setPaymentMethodOnCart']['cart']['selected_payment_method']['code'])
154+
);
155+
$this->assertEquals(
156+
$paymentMethod,
157+
$responseData['data']['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']
158+
);
159+
160+
$this->assertTrue(
161+
isset($responseData['data']['placeOrder']['order']['order_id'])
162+
);
163+
164+
$this->assertEquals(
165+
'test_quote',
166+
$responseData['data']['placeOrder']['order']['order_id']
167+
);
168+
}
169+
170+
protected function tearDown()
171+
{
172+
$this->objectManager->removeSharedInstance(ZendClientFactory::class);
173+
parent::tearDown();
174+
}
175+
}

0 commit comments

Comments
 (0)