VaultDetailsHandlerTest.php 6.91 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 219 220 221 222 223 224 225 226 227 228
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Braintree\Test\Unit\Gateway\Response\PayPal;

use Braintree\Result\Successful;
use Braintree\Transaction;
use Braintree\Transaction\PayPalDetails;
use Magento\Braintree\Gateway\SubjectReader;
use Magento\Braintree\Gateway\Response\PayPal\VaultDetailsHandler;
use Magento\Framework\Intl\DateTimeFactory;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
use Magento\Sales\Model\Order\Payment;
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Model\PaymentToken;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
 * Tests \Magento\Braintree\Gateway\Response\PayPal\VaultDetailsHandler.
 *
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class VaultDetailsHandlerTest extends TestCase
{
    private static $transactionId = '1n2suy';

    private static $token = 'rc39al';

    private static $payerEmail = 'john.doe@example.com';

    /**
     * @var PaymentDataObjectInterface|MockObject
     */
    private $paymentDataObjectMock;

    /**
     * @var Payment|MockObject
     */
    private $paymentInfoMock;

    /**
     * @var PaymentTokenFactoryInterface|MockObject
     */
    private $paymentTokenFactoryMock;

    /**
     * @var PaymentTokenInterface|MockObject
     */
    protected $paymentTokenMock;

    /**
     * @var OrderPaymentExtension|MockObject
     */
    private $paymentExtensionMock;

    /**
     * @var OrderPaymentExtensionInterfaceFactory|MockObject
     */
    private $paymentExtensionFactoryMock;

    /**
     * @var VaultDetailsHandler
     */
    private $handler;

    /**
     * @var DateTimeFactory|MockObject
     */
    private $dateTimeFactoryMock;

    /**
     * @var array
     */
    private $subject = [];

    protected function setUp()
    {
        $objectManager = new ObjectManager($this);

        $this->paymentDataObjectMock = $this->getMockForAbstractClass(PaymentDataObjectInterface::class);

        $this->paymentInfoMock = $this->getMockBuilder(Payment::class)
            ->disableOriginalConstructor()
            ->setMethods(['__wakeup', 'getExtensionAttributes'])
            ->getMock();

        $this->paymentTokenMock = $objectManager->getObject(PaymentToken::class);

        $this->paymentTokenFactoryMock = $this->getMockBuilder(PaymentTokenFactoryInterface::class)
            ->setMethods(['create'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->paymentExtensionMock = $this->getMockBuilder(OrderPaymentExtensionInterface::class)
            ->setMethods(['setVaultPaymentToken', 'getVaultPaymentToken'])
            ->disableOriginalConstructor()
            ->getMock();
        $this->paymentExtensionFactoryMock = $this->getMockBuilder(OrderPaymentExtensionInterfaceFactory::class)
            ->disableOriginalConstructor()
            ->setMethods(['create'])
            ->getMock();

        $this->paymentInfoMock->expects(self::any())
            ->method('getExtensionAttributes')
            ->willReturn($this->paymentExtensionMock);

        $this->subject = [
            'payment' => $this->paymentDataObjectMock,
        ];

        $this->dateTimeFactoryMock = $this->getMockBuilder(DateTimeFactory::class)
            ->disableOriginalConstructor()
            ->setMethods(['create'])
            ->getMock();
        
        $this->handler = new VaultDetailsHandler(
            $this->paymentTokenFactoryMock,
            $this->paymentExtensionFactoryMock,
            new SubjectReader(),
            $this->dateTimeFactoryMock
        );
    }

    public function testHandle()
    {
        $transaction = $this->getTransaction();
        $response = [
            'object' => $transaction
        ];

        $this->paymentExtensionMock->method('setVaultPaymentToken')
            ->with($this->paymentTokenMock);
        $this->paymentExtensionMock->method('getVaultPaymentToken')
            ->willReturn($this->paymentTokenMock);
        
        $this->paymentDataObjectMock->method('getPayment')
            ->willReturn($this->paymentInfoMock);

        $this->paymentTokenFactoryMock->method('create')
            ->with(PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT)
            ->willReturn($this->paymentTokenMock);

        $this->paymentExtensionFactoryMock->method('create')
            ->willReturn($this->paymentExtensionMock);

        $dateTime = new \DateTime('2016-07-05 00:00:00', new \DateTimeZone('UTC'));
        $expirationDate = '2017-07-05 00:00:00';
        $this->dateTimeFactoryMock->method('create')
            ->willReturn($dateTime);
        
        $this->handler->handle($this->subject, $response);

        $extensionAttributes = $this->paymentInfoMock->getExtensionAttributes();
        $paymentToken = $extensionAttributes->getVaultPaymentToken();
        self::assertNotNull($paymentToken);

        $tokenDetails = json_decode($paymentToken->getTokenDetails(), true);

        self::assertSame($this->paymentTokenMock, $paymentToken);
        self::assertEquals(self::$token, $paymentToken->getGatewayToken());
        self::assertEquals(self::$payerEmail, $tokenDetails['payerEmail']);
        self::assertEquals($expirationDate, $paymentToken->getExpiresAt());
    }

    public function testHandleWithoutToken()
    {
        $transaction = $this->getTransaction();
        $transaction->transaction->paypalDetails->token = null;

        $response = [
            'object' => $transaction
        ];

        $this->paymentDataObjectMock->method('getPayment')
            ->willReturn($this->paymentInfoMock);

        $this->paymentTokenFactoryMock->expects(self::never())
            ->method('create');

        $this->dateTimeFactoryMock->expects(self::never())
            ->method('create');

        $this->handler->handle($this->subject, $response);
        self::assertNotNull($this->paymentInfoMock->getExtensionAttributes());
    }

    /**
     * Creates Braintree transaction.
     *
     * @return Successful
     */
    private function getTransaction(): Successful
    {
        $attributes = [
            'id' => self::$transactionId,
            'paypalDetails' => $this->getPayPalDetails()
        ];

        $transaction = Transaction::factory($attributes);
        $result = new Successful(['transaction' => $transaction]);

        return $result;
    }

    /**
     * Gets PayPal transaction details.
     *
     * @return PayPalDetails
     */
    private function getPayPalDetails(): PayPalDetails
    {
        $attributes = [
            'token' => self::$token,
            'payerEmail' => self::$payerEmail
        ];

        $details = new PayPalDetails($attributes);

        return $details;
    }
}