Skip to content

Commit 3b9aee5

Browse files
committed
MC-16922: Create an end-to-end test SetPaymentMethodOnCart for the authorize.net payment method
- integration test for end to end place order with authorizenet MC-16922: Create an end-to-end test SetPaymentMethodOnCart for the authorize.net payment method - integration test for end to end place order for customer with authorizenet MC-16922: Create an end-to-end test SetPaymentMethodOnCart for the authorize.net payment method - squash commits MC-16922: Create an end-to-end test SetPaymentMethodOnCart for the authorize.net payment method - review comments
1 parent e3e468c commit 3b9aee5

16 files changed

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

@@ -37,9 +37,26 @@ class SetAuthorizenetPaymentMethodOnCustomerCartTest extends \PHPUnit\Framework\
3737
/** @var CustomerTokenServiceInterface */
3838
private $customerTokenService;
3939

40+
/**
41+
* @var \Magento\Framework\App\Cache */
42+
private $appCache;
43+
4044
/** @var Http */
4145
private $request;
4246

47+
public static function setUpBeforeClass()
48+
{
49+
$db = Bootstrap::getInstance()->getBootstrap()
50+
->getApplication()
51+
->getDbInstance();
52+
if (!$db->isDbDumpExists()) {
53+
throw new \LogicException('DB dump does not exist.');
54+
}
55+
$db->restoreFromDbDump();
56+
57+
parent::setUpBeforeClass();
58+
}
59+
4360
protected function setUp() : void
4461
{
4562
$this->objectManager = Bootstrap::getObjectManager();

0 commit comments

Comments
 (0)