Skip to content

Commit 7f19de5

Browse files
committed
MC-17875: Refactor GraphQl Integration tests
- refactor payment tests
1 parent c3f29ce commit 7f19de5

14 files changed

+106
-266
lines changed

dev/tests/integration/testsuite/Magento/AuthorizenetGraphQl/Model/Resolver/Customer/PlaceOrderWithAuthorizeNetTest.php

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77

88
namespace Magento\AuthorizenetGraphQl\Model\Resolver\Customer;
99

10-
use Magento\Framework\App\Request\Http;
1110
use Magento\Framework\Serialize\SerializerInterface;
12-
use Magento\GraphQl\Controller\GraphQl;
1311
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
12+
use Magento\GraphQl\Service\GraphQlRequest;
1413
use Magento\Integration\Api\CustomerTokenServiceInterface;
15-
use Magento\Framework\Webapi\Request;
1614
use Magento\TestFramework\Helper\Bootstrap;
1715
use Magento\Framework\HTTP\ZendClient;
1816
use Magento\Framework\HTTP\ZendClientFactory;
@@ -33,8 +31,6 @@
3331
*/
3432
class PlaceOrderWithAuthorizeNetTest extends TestCase
3533
{
36-
const CONTENT_TYPE = 'application/json';
37-
3834
/** @var ObjectManager */
3935
private $objectManager;
4036

@@ -44,8 +40,8 @@ class PlaceOrderWithAuthorizeNetTest extends TestCase
4440
/** @var SerializerInterface */
4541
private $jsonSerializer;
4642

47-
/** @var Http */
48-
private $request;
43+
/** @var GraphQlRequest */
44+
private $graphQlRequest;
4945

5046
/** @var ZendClient|MockObject|InvocationMocker */
5147
private $clientMock;
@@ -63,7 +59,7 @@ protected function setUp() : void
6359
{
6460
$this->objectManager = Bootstrap::getObjectManager();
6561
$this->jsonSerializer = $this->objectManager->get(SerializerInterface::class);
66-
$this->request = $this->objectManager->get(Http::class);
62+
$this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class);
6763
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
6864
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
6965
$this->clientMock = $this->createMock(ZendClient::class);
@@ -126,23 +122,12 @@ public function testDispatchToPlaceOrderWithRegisteredCustomer(): void
126122
}
127123
}
128124
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);
145125

126+
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
127+
$requestHeaders = [
128+
'Accept' => 'application/json',
129+
'Authorization' => 'Bearer ' . $customerToken
130+
];
146131
// phpcs:ignore Magento2.Security.IncludeFile
147132
$expectedRequest = include __DIR__ . '/../../../_files/request_authorize_customer.php';
148133
// phpcs:ignore Magento2.Security.IncludeFile
@@ -153,7 +138,7 @@ public function testDispatchToPlaceOrderWithRegisteredCustomer(): void
153138

154139
$this->responseMock->method('getBody')->willReturn(json_encode($authorizeResponse));
155140

156-
$response = $graphql->dispatch($this->request);
141+
$response = $this->graphQlRequest->send($query, [], '', $requestHeaders);
157142
$responseData = $this->jsonSerializer->unserialize($response->getContent());
158143

159144
$this->assertArrayNotHasKey('errors', $responseData, 'Response has errors');
@@ -178,6 +163,5 @@ public function testDispatchToPlaceOrderWithRegisteredCustomer(): void
178163
protected function tearDown()
179164
{
180165
$this->objectManager->removeSharedInstance(ZendClientFactory::class);
181-
parent::tearDown();
182166
}
183167
}

dev/tests/integration/testsuite/Magento/AuthorizenetGraphQl/Model/Resolver/Customer/SetAuthorizeNetPaymentMethodOnCartTest.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
namespace Magento\AuthorizenetGraphQl\Model\Resolver\Customer;
99

10-
use Magento\Framework\App\Request\Http;
1110
use Magento\Framework\Serialize\SerializerInterface;
12-
use Magento\Framework\Webapi\Request;
1311
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
12+
use Magento\GraphQl\Service\GraphQlRequest;
1413
use Magento\Integration\Api\CustomerTokenServiceInterface;
1514
use Magento\TestFramework\Helper\Bootstrap;
1615
use PHPUnit\Framework\TestCase;
@@ -24,8 +23,6 @@
2423
*/
2524
class SetAuthorizeNetPaymentMethodOnCartTest extends TestCase
2625
{
27-
const CONTENT_TYPE = 'application/json';
28-
2926
/** @var \Magento\Framework\ObjectManagerInterface */
3027
private $objectManager;
3128

@@ -38,14 +35,14 @@ class SetAuthorizeNetPaymentMethodOnCartTest extends TestCase
3835
/** @var CustomerTokenServiceInterface */
3936
private $customerTokenService;
4037

41-
/** @var Http */
42-
private $request;
38+
/** @var GraphQlRequest */
39+
private $graphQlRequest;
4340

4441
protected function setUp() : void
4542
{
4643
$this->objectManager = Bootstrap::getObjectManager();
4744
$this->jsonSerializer = $this->objectManager->get(SerializerInterface::class);
48-
$this->request = $this->objectManager->get(Http::class);
45+
$this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class);
4946
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
5047
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
5148
}
@@ -88,24 +85,16 @@ public function testDispatchToSetPaymentMethodWithAuthorizenet(): void
8885
}
8986
}
9087
QUERY;
91-
$postData = [
92-
'query' => $query,
93-
'variables' => null,
94-
'operationName' => null
95-
];
96-
$this->request->setPathInfo('/graphql');
97-
$this->request->setMethod('POST');
98-
$this->request->setContent($this->jsonSerializer->serialize($postData));
88+
9989
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
100-
$bearerCustomerToken = 'Bearer ' . $customerToken;
101-
$contentType ='application/json';
102-
$webApiRequest = $this->objectManager->get(Request::class);
103-
$webApiRequest->getHeaders()->addHeaderLine('Content-Type', $contentType)
104-
->addHeaderLine('Accept', $contentType)
105-
->addHeaderLine('Authorization', $bearerCustomerToken);
106-
$this->request->setHeaders($webApiRequest->getHeaders());
107-
$graphql = $this->objectManager->get(\Magento\GraphQl\Controller\GraphQl::class);
108-
$response = $graphql->dispatch($this->request);
90+
91+
$requestHeaders = [
92+
'Accept' => 'application/json',
93+
'Authorization' => 'Bearer ' . $customerToken
94+
];
95+
96+
$response = $this->graphQlRequest->send($query, [], '', $requestHeaders);
97+
10998
$output = $this->jsonSerializer->unserialize($response->getContent());
11099
$this->assertArrayNotHasKey('errors', $output, 'Response has errors');
111100
$this->assertArrayHasKey('setPaymentMethodOnCart', $output['data']);

dev/tests/integration/testsuite/Magento/AuthorizenetGraphQl/Model/Resolver/Guest/PlaceOrderWithAuthorizeNetTest.php

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
use Magento\Framework\App\Request\Http;
1111
use Magento\Framework\Serialize\SerializerInterface;
12-
use Magento\GraphQl\Controller\GraphQl;
1312
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
13+
use Magento\GraphQl\Service\GraphQlRequest;
1414
use Magento\TestFramework\Helper\Bootstrap;
1515
use Magento\Framework\HTTP\ZendClient;
1616
use Magento\Framework\HTTP\ZendClientFactory;
@@ -31,22 +31,17 @@
3131
*/
3232
class PlaceOrderWithAuthorizeNetTest extends TestCase
3333
{
34-
const CONTENT_TYPE = 'application/json';
35-
3634
/** @var ObjectManager */
3735
private $objectManager;
3836

3937
/** @var GetMaskedQuoteIdByReservedOrderId */
4038
private $getMaskedQuoteIdByReservedOrderId;
4139

42-
/** @var GraphQl */
43-
private $graphql;
44-
4540
/** @var SerializerInterface */
4641
private $jsonSerializer;
4742

48-
/** @var Http */
49-
private $request;
43+
/** @var GraphQlRequest */
44+
private $graphQlRequest;
5045

5146
/** @var ZendClient|MockObject|InvocationMocker */
5247
private $clientMock;
@@ -64,6 +59,7 @@ protected function setUp() : void
6459
$this->jsonSerializer = $this->objectManager->get(SerializerInterface::class);
6560
$this->request = $this->objectManager->get(Http::class);
6661
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
62+
$this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class);
6763
$this->clientMock = $this->createMock(ZendClient::class);
6864
$this->responseMock = $this->createMock(Zend_Http_Response::class);
6965
$this->clientMock->method('request')
@@ -78,6 +74,11 @@ protected function setUp() : void
7874
$this->objectManager->addSharedInstance($clientFactoryMock, ZendClientFactory::class);
7975
}
8076

77+
protected function tearDown()
78+
{
79+
$this->objectManager->removeSharedInstance(ZendClientFactory::class);
80+
}
81+
8182
/**
8283
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/active 1
8384
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/environment sandbox
@@ -124,17 +125,7 @@ public function testDispatchToPlaceAnOrderWithAuthorizenet(): void
124125
}
125126
}
126127
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);
128+
138129
// phpcs:ignore Magento2.Security.IncludeFile
139130
$expectedRequest = include __DIR__ . '/../../../_files/request_authorize.php';
140131
// phpcs:ignore Magento2.Security.IncludeFile
@@ -145,7 +136,7 @@ public function testDispatchToPlaceAnOrderWithAuthorizenet(): void
145136

146137
$this->responseMock->method('getBody')->willReturn(json_encode($authorizeResponse));
147138

148-
$response = $this->graphql->dispatch($this->request);
139+
$response = $this->graphQlRequest->send($query);
149140
$responseData = $this->jsonSerializer->unserialize($response->getContent());
150141

151142
$this->assertArrayNotHasKey('errors', $responseData, 'Response has errors');
@@ -166,10 +157,4 @@ public function testDispatchToPlaceAnOrderWithAuthorizenet(): void
166157
$responseData['data']['placeOrder']['order']['order_id']
167158
);
168159
}
169-
170-
protected function tearDown()
171-
{
172-
$this->objectManager->removeSharedInstance(ZendClientFactory::class);
173-
parent::tearDown();
174-
}
175160
}

dev/tests/integration/testsuite/Magento/AuthorizenetGraphQl/Model/Resolver/Guest/SetAuthorizeNetPaymentMethodOnCartTest.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
namespace Magento\AuthorizenetGraphQl\Model\Resolver\Guest;
99

10-
use Magento\Framework\App\Request\Http;
1110
use Magento\Framework\Serialize\SerializerInterface;
12-
use Magento\GraphQl\Controller\GraphQl;
1311
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
12+
use Magento\GraphQl\Service\GraphQlRequest;
1413
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\ObjectManager;
1515
use PHPUnit\Framework\TestCase;
1616

1717
/**
@@ -23,30 +23,24 @@
2323
*/
2424
class SetAuthorizeNetPaymentMethodOnCartTest extends TestCase
2525
{
26-
const CONTENT_TYPE = 'application/json';
27-
28-
/** @var \Magento\Framework\ObjectManagerInterface */
26+
/** @var ObjectManager */
2927
private $objectManager;
3028

3129
/** @var GetMaskedQuoteIdByReservedOrderId */
3230
private $getMaskedQuoteIdByReservedOrderId;
3331

34-
/** @var GraphQl */
35-
private $graphql;
36-
3732
/** @var SerializerInterface */
3833
private $jsonSerializer;
3934

40-
/** @var Http */
41-
private $request;
35+
/** @var GraphQlRequest */
36+
private $graphQlRequest;
4237

4338
protected function setUp() : void
4439
{
4540
$this->objectManager = Bootstrap::getObjectManager();
46-
$this->graphql = $this->objectManager->get(\Magento\GraphQl\Controller\GraphQl::class);
4741
$this->jsonSerializer = $this->objectManager->get(SerializerInterface::class);
48-
$this->request = $this->objectManager->get(Http::class);
4942
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
43+
$this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class);
5044
}
5145

5246
/**
@@ -86,18 +80,8 @@ public function testDispatchToSetPaymentMethodWithAuthorizenet(): void
8680
}
8781
}
8882
QUERY;
89-
$postData = [
90-
'query' => $query,
91-
'variables' => null,
92-
'operationName' => null
93-
];
94-
$this->request->setPathInfo('/graphql');
95-
$this->request->setMethod('POST');
96-
$this->request->setContent(json_encode($postData));
97-
$headers = $this->objectManager->create(\Zend\Http\Headers::class)
98-
->addHeaders(['Content-Type' => 'application/json']);
99-
$this->request->setHeaders($headers);
100-
$response = $this->graphql->dispatch($this->request);
83+
84+
$response = $this->graphQlRequest->send($query);
10185
$output = $this->jsonSerializer->unserialize($response->getContent());
10286
$this->assertArrayNotHasKey('errors', $output, 'Response has errors');
10387
$this->assertArrayHasKey('setPaymentMethodOnCart', $output['data']);

0 commit comments

Comments
 (0)