ClientTest.php 6.64 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

declare(strict_types=1);

namespace Magento\AuthorizenetAcceptjs\Test\Unit\Gateway\Http;

use Magento\AuthorizenetAcceptjs\Gateway\Http\Client;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\HTTP\ZendClientFactory;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Payment\Gateway\Http\TransferInterface;
use Magento\Payment\Model\Method\Logger;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Zend_Http_Client;
use Zend_Http_Response;

class ClientTest extends TestCase
{
    /**
     * @var ObjectManager
     */
    private $objectManager;

    /**
     * @var Logger
     */
    private $paymentLogger;

    /**
     * @var ZendClientFactory
     */
    private $httpClientFactory;

    /**
     * @var Zend_Http_Client
     */
    private $httpClient;

    /**
     * @var Zend_Http_Response
     */
    private $httpResponse;

    /**
     * @var LoggerInterface
     */
    private $logger;

    protected function setUp()
    {
        $this->objectManager = new ObjectManager($this);
        $this->paymentLogger = $this->createMock(Logger::class);
        $this->httpClientFactory = $this->createMock(ZendClientFactory::class);
        $this->httpClient = $this->createMock(Zend_Http_Client::class);
        $this->httpResponse = $this->createMock(Zend_Http_Response::class);
        $this->httpClientFactory->method('create')->will($this->returnValue($this->httpClient));
        $this->httpClient->method('request')
            ->willReturn($this->httpResponse);
        /** @var MockObject $logger */
        $this->logger = $this->createMock(LoggerInterface::class);
    }

    public function testCanSendRequest()
    {
        // Assert the raw data was set on the client
        $this->httpClient->expects($this->once())
            ->method('setRawData')
            ->with(
                '{"doSomeThing":{"foobar":"baz"}}',
                'application/json'
            );

        $request = [
            'payload_type' => 'doSomeThing',
            'foobar' => 'baz'
        ];
        // Authorize.net returns a BOM and refuses to fix it
        $response = pack('CCC', 0xef, 0xbb, 0xbf) . '{"foo":{"bar":"baz"}}';

        $this->httpResponse->method('getBody')
            ->willReturn($response);

        // Assert the logger was given the data
        $this->paymentLogger->expects($this->once())
            ->method('debug')
            ->with(['request' => $request, 'response' => '{"foo":{"bar":"baz"}}']);

        /**
         * @var $apiClient Client
         */
        $apiClient = $this->objectManager->getObject(Client::class, [
            'httpClientFactory' => $this->httpClientFactory,
            'paymentLogger' => $this->paymentLogger,
            'json' => new Json()
        ]);

        $result = $apiClient->placeRequest($this->getTransferObjectMock($request));

        $this->assertSame('baz', $result['foo']['bar']);
    }

    /**
     * @expectedException \Magento\Framework\Exception\LocalizedException
     * @expectedExceptionMessage Something went wrong in the payment gateway.
     */
    public function testExceptionIsThrownWhenEmptyResponseIsReceived()
    {
        // Assert the client has the raw data set
        $this->httpClient->expects($this->once())
            ->method('setRawData')
            ->with(
                '{"doSomeThing":{"foobar":"baz"}}',
                'application/json'
            );

        $this->httpResponse->method('getBody')
            ->willReturn('');

        // Assert the exception is given to the logger
        $this->logger->expects($this->once())
            ->method('critical')
            ->with($this->callback(function ($e) {
                return $e instanceof \Exception
                    && $e->getMessage() === 'Invalid JSON was returned by the gateway';
            }));

        $request = [
            'payload_type' => 'doSomeThing',
            'foobar' => 'baz'
        ];

        // Assert the logger was given the data
        $this->paymentLogger->expects($this->once())
            ->method('debug')
            ->with(['request' => $request, 'response' => '']);

        /**
         * @var $apiClient Client
         */
        $apiClient = $this->objectManager->getObject(Client::class, [
            'httpClientFactory' => $this->httpClientFactory,
            'paymentLogger' => $this->paymentLogger,
            'logger' => $this->logger,
            'json' => new Json()
        ]);

        $apiClient->placeRequest($this->getTransferObjectMock($request));
    }

    /**
     * @expectedException \Magento\Framework\Exception\LocalizedException
     * @expectedExceptionMessage Something went wrong in the payment gateway.
     */
    public function testExceptionIsThrownWhenInvalidResponseIsReceived()
    {
        // Assert the client was given the raw data
        $this->httpClient->expects($this->once())
            ->method('setRawData')
            ->with(
                '{"doSomeThing":{"foobar":"baz"}}',
                'application/json'
            );

        $this->httpResponse->method('getBody')
            ->willReturn('bad');

        $request = [
            'payload_type' => 'doSomeThing',
            'foobar' => 'baz'
        ];

        // Assert the logger was given the data
        $this->paymentLogger->expects($this->once())
            ->method('debug')
            ->with(['request' => $request, 'response' => 'bad']);

        // Assert the exception was given to the logger
        $this->logger->expects($this->once())
            ->method('critical')
            ->with($this->callback(function ($e) {
                return $e instanceof \Exception
                    && $e->getMessage() === 'Invalid JSON was returned by the gateway';
            }));

        /**
         * @var $apiClient Client
         */
        $apiClient = $this->objectManager->getObject(Client::class, [
            'httpClientFactory' => $this->httpClientFactory,
            'paymentLogger' => $this->paymentLogger,
            'logger' => $this->logger,
            'json' => new Json()
        ]);

        $apiClient->placeRequest($this->getTransferObjectMock($request));
    }

    /**
     * Creates mock object for TransferInterface.
     *
     * @return TransferInterface|MockObject
     */
    private function getTransferObjectMock(array $data)
    {
        $transferObjectMock = $this->createMock(TransferInterface::class);
        $transferObjectMock->method('getBody')
            ->willReturn($data);

        return $transferObjectMock;
    }
}