Skip to content

Commit 1f17b9d

Browse files
committed
MC-17592: End-to-end integration to test order with Payflow Link
1 parent 096b288 commit 1f17b9d

File tree

5 files changed

+270
-219
lines changed

5 files changed

+270
-219
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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\Service;
9+
10+
use Magento\Framework\App\Request\Http;
11+
use Magento\Framework\Serialize\SerializerInterface;
12+
use Magento\GraphQl\Controller\GraphQl;
13+
use Magento\Framework\App\Response\Http as HttpResponse;
14+
use Magento\TestFramework\ObjectManager;
15+
use Magento\Framework\Webapi\Request;
16+
17+
/**
18+
* Service class to simplify GraphQl requests for integration tests
19+
*/
20+
class GraphQlRequest
21+
{
22+
/**
23+
* @var string
24+
*/
25+
private $controllerPath = '/graphql';
26+
27+
/**
28+
* @var Http
29+
*/
30+
private $httpRequest;
31+
32+
/**
33+
* @var array
34+
*/
35+
private $defaultHeaders = ['Content-Type' => 'application/json'];
36+
37+
/**
38+
* @var SerializerInterface
39+
*/
40+
private $json;
41+
42+
/**
43+
* @var GraphQl
44+
*/
45+
private $controller;
46+
47+
/**
48+
* @param Http $httpRequest
49+
* @param SerializerInterface $json
50+
* @param GraphQl $controller
51+
*/
52+
public function __construct(
53+
Http $httpRequest,
54+
SerializerInterface $json,
55+
GraphQl $controller
56+
) {
57+
$this->httpRequest = $httpRequest;
58+
$this->json = $json;
59+
$this->controller = $controller;
60+
}
61+
62+
/**
63+
* Send request and return response
64+
*
65+
* @param string $query
66+
* @param array $variables
67+
* @param string $operation
68+
* @param array $headers
69+
* @return HttpResponse
70+
*/
71+
public function send(
72+
string $query,
73+
array $variables = [],
74+
string $operation = '',
75+
array $headers = []
76+
) {
77+
$this->httpRequest->setPathInfo($this->controllerPath);
78+
$this->setQuery($query, $variables, $operation)
79+
->setHeaders($headers);
80+
81+
/** @var HttpResponse $response */
82+
$response = $this->controller->dispatch($this->httpRequest);
83+
return $response;
84+
}
85+
86+
/**
87+
* Set query data on request
88+
*
89+
* @param string $query
90+
* @param array $variables
91+
* @param string $operation
92+
* @return GraphQlRequest
93+
*/
94+
private function setQuery(string $query, array $variables = [], string $operation = ''): self
95+
{
96+
if (strpos(trim($query), 'mutation') === 0) {
97+
$this->httpRequest->setMethod('POST');
98+
$this->setPostContent($query, $variables, $operation);
99+
} else {
100+
$this->httpRequest->setMethod('GET');
101+
$this->setGetContent($query, $variables, $operation);
102+
}
103+
104+
return $this;
105+
}
106+
107+
/**
108+
* Set headers on request
109+
*
110+
* @param array $headers
111+
* @return GraphQlRequest
112+
*/
113+
private function setHeaders(array $headers): self
114+
{
115+
$allHeaders = array_merge($this->defaultHeaders, $headers);
116+
117+
$webApiRequest = ObjectManager::getInstance()->get(Request::class);
118+
$requestHeaders = $webApiRequest->getHeaders();
119+
foreach ($allHeaders as $key => $value) {
120+
$requestHeaders->addHeaderLine($key, $value);
121+
}
122+
123+
$this->httpRequest->setHeaders($webApiRequest->getHeaders());
124+
125+
return $this;
126+
}
127+
128+
/**
129+
* Set POST body for request
130+
*
131+
* @param string $query
132+
* @param array $variables
133+
* @param string $operation
134+
* @return GraphQlRequest
135+
*/
136+
private function setPostContent(string $query, array $variables = [], string $operation = ''): self
137+
{
138+
$content = [
139+
'query' => $query,
140+
'variables' => !empty($variables) ? $this->json->serialize($variables) : null,
141+
'operationName' => !empty($operation) ? $operation : null
142+
];
143+
$this->httpRequest->setContent($this->json->serialize($content));
144+
145+
return $this;
146+
}
147+
148+
/**
149+
* Set GET parameters for request
150+
*
151+
* @param string $query
152+
* @param array $variables
153+
* @param string $operation
154+
* @return GraphQlRequest
155+
*/
156+
private function setGetContent(string $query, array $variables = [], string $operation = ''): self
157+
{
158+
$this->httpRequest->setQueryValue('query', $query);
159+
160+
if (!empty($variables)) {
161+
$this->httpRequest->setQueryValue('variables', $variables);
162+
}
163+
if (!empty($operation)) {
164+
$this->httpRequest->setQueryValue('operationName', $operation);
165+
}
166+
167+
return $this;
168+
}
169+
}

dev/tests/integration/testsuite/Magento/PaypalGraphQl/Model/Resolver/Customer/PlaceOrderWithPayflowLinkTest.php

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
namespace Magento\PaypalGraphQl\Model\Resolver\Customer;
99

1010
use Magento\Framework\App\ProductMetadataInterface;
11-
use Magento\Framework\App\Request\Http;
1211
use Magento\Framework\DataObject;
1312
use Magento\Framework\Math\Random;
1413
use Magento\Framework\Serialize\SerializerInterface;
15-
use Magento\GraphQl\Controller\GraphQl;
1614
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
15+
use Magento\GraphQl\Service\GraphQlRequest;
16+
use Magento\Integration\Model\Oauth\Token;
1717
use Magento\Paypal\Model\Payflow\Request;
1818
use Magento\Paypal\Model\Payflow\Service\Gateway;
1919
use Magento\Quote\Model\QuoteIdToMaskedQuoteId;
@@ -22,6 +22,7 @@
2222
use Magento\TestFramework\ObjectManager;
2323
use PHPUnit\Framework\MockObject\MockObject;
2424
use PHPUnit\Framework\TestCase;
25+
use Magento\Paypal\Model\Payflow\RequestFactory;
2526

2627
/**
2728
* End to end place order test using payflow_link via graphql endpoint for registered customer
@@ -31,50 +32,39 @@
3132
*/
3233
class PlaceOrderWithPayflowLinkTest extends TestCase
3334
{
34-
/**
35-
* @var Http
36-
*/
37-
private $request;
35+
/** @var GraphQlRequest */
36+
private $graphQlRequest;
3837

39-
/**
40-
* @var SerializerInterface
41-
*/
38+
/** @var SerializerInterface */
4239
private $json;
4340

44-
/**
45-
* @var QuoteIdToMaskedQuoteId
46-
*/
41+
/** @var QuoteIdToMaskedQuoteId */
4742
private $quoteIdToMaskedId;
4843

49-
/** @var GetMaskedQuoteIdByReservedOrderId */
44+
/** @var GetMaskedQuoteIdByReservedOrderId */
5045
private $getMaskedQuoteIdByReservedOrderId;
5146

52-
/** @var ObjectManager */
53-
protected $objectManager;
54-
55-
/** @var GraphQl */
56-
protected $graphqlController;
47+
/** @var ObjectManager */
48+
private $objectManager;
5749

58-
/** @var Gateway|MockObject */
50+
/** @var Gateway|MockObject */
5951
private $gateway;
6052

61-
/** @var Random|MockObject */
53+
/** @var Random|MockObject */
6254
private $mathRandom;
6355

64-
/** @var Request|MockObject */
56+
/** @var Request|MockObject */
6557
private $payflowRequest;
6658

6759
protected function setUp()
6860
{
6961
parent::setUp();
7062

7163
$this->objectManager = Bootstrap::getObjectManager();
72-
$this->request = $this->objectManager->create(Http::class);
7364
$this->json = $this->objectManager->get(SerializerInterface::class);
7465
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
7566
$this->quoteIdToMaskedId = $this->objectManager->get(QuoteIdToMaskedQuoteId::class);
76-
77-
$this->graphqlController = $this->objectManager->get(GraphQl::class);
67+
$this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class);
7868

7969
$this->mathRandom = $this->getMockBuilder(Random::class)
8070
->getMock();
@@ -83,12 +73,12 @@ protected function setUp()
8373
->setMethods(['postRequest'])
8474
->getMock();
8575

86-
$requestFactory = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\RequestFactory::class)
76+
$requestFactory = $this->getMockBuilder(RequestFactory::class)
8777
->setMethods(['create'])
8878
->disableOriginalConstructor()
8979
->getMock();
9080

91-
$this->payflowRequest = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\Request::class)
81+
$this->payflowRequest = $this->getMockBuilder(Request::class)
9282
->disableOriginalConstructor()
9383
->getMock();
9484
$requestFactory->expects($this->any())->method('create')->will($this->returnValue($this->payflowRequest));
@@ -110,7 +100,6 @@ protected function setUp()
110100
* @magentoDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
111101
* @magentoDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
112102
* @return void
113-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
114103
*/
115104
public function testResolvePlaceOrderWithPayflowLinkForCustomer(): void
116105
{
@@ -150,22 +139,6 @@ public function testResolvePlaceOrderWithPayflowLinkForCustomer(): void
150139
}
151140
QUERY;
152141

153-
$postData = $this->json->serialize(['query' => $query]);
154-
$this->request->setPathInfo('/graphql');
155-
$this->request->setMethod('POST');
156-
$this->request->setContent($postData);
157-
158-
/** @var \Magento\Integration\Model\Oauth\Token $tokenModel */
159-
$tokenModel = $this->objectManager->create(\Magento\Integration\Model\Oauth\Token::class);
160-
$customerToken = $tokenModel->createCustomerToken(1)->getToken();
161-
/** @var \Magento\Framework\Webapi\Request $webApiRequest */
162-
$webApiRequest = $this->objectManager->get(\Magento\Framework\Webapi\Request::class);
163-
$webApiRequest->getHeaders()
164-
->addHeaderLine('Content-Type', 'application/json')
165-
->addHeaderLine('Accept', 'application/json')
166-
->addHeaderLine('Authorization', 'Bearer ' . $customerToken);
167-
$this->request->setHeaders($webApiRequest->getHeaders());
168-
169142
$productMetadata = ObjectManager::getInstance()->get(ProductMetadataInterface::class);
170143
$button = 'Magento_Cart_' . $productMetadata->getEdition();
171144

@@ -197,24 +170,29 @@ public function testResolvePlaceOrderWithPayflowLinkForCustomer(): void
197170
$this->returnSelf()
198171
],
199172
['USER1', 1, $this->returnSelf()],
200-
['USER2', '4b102efb018ad34bacea669f401fc8cb', $this->returnSelf()]
173+
['USER2', 'USER2SilentPostHash', $this->returnSelf()]
201174
);
202175

203-
$response = $this->graphqlController->dispatch($this->request);
204-
$responseData = $this->json->unserialize($response->getContent());
176+
/** @var Token $tokenModel */
177+
$tokenModel = $this->objectManager->create(Token::class);
178+
$customerToken = $tokenModel->createCustomerToken(1)->getToken();
205179

180+
$requestHeaders = [
181+
'Content-Type' => 'application/json',
182+
'Accept' => 'application/json',
183+
'Authorization' => 'Bearer ' . $customerToken
184+
];
185+
$response = $this->graphQlRequest->send($query, [], '', $requestHeaders);
186+
$responseData = $this->json->unserialize($response->getContent());
206187
$this->assertArrayNotHasKey('errors', $responseData);
207188
$this->assertArrayHasKey('data', $responseData);
208-
209189
$this->assertEquals(
210190
$paymentMethod,
211191
$responseData['data']['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']
212192
);
213-
214193
$this->assertTrue(
215194
isset($responseData['data']['placeOrder']['order']['order_id'])
216195
);
217-
218196
$this->assertEquals(
219197
'test_quote',
220198
$responseData['data']['placeOrder']['order']['order_id']

0 commit comments

Comments
 (0)