|
| 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 | +} |
0 commit comments