123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Paypal\Test\Unit\Controller\Transparent;
- use Magento\Framework\App\Action\Context;
- use Magento\Framework\Controller\Result\JsonFactory;
- use Magento\Framework\Session\Generic;
- use Magento\Framework\Session\SessionManager;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Paypal\Controller\Transparent\RequestSecureToken;
- use Magento\Paypal\Model\Payflow\Service\Request\SecureToken;
- use Magento\Paypal\Model\Payflow\Transparent;
- /**
- * Class RequestSecureTokenTest
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class RequestSecureTokenTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Transparent|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $transparentMock;
- /**
- * @var RequestSecureToken|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $controller;
- /**
- * @var Context|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $contextMock;
- /**
- * @var JsonFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultJsonFactoryMock;
- /**
- * @var Generic|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $sessionTransparentMock;
- /**
- * @var SecureToken|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $secureTokenServiceMock;
- /**
- * @var SessionManager|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $sessionManagerMock;
- /**
- * Set up
- *
- * @return void
- */
- protected function setUp()
- {
- $this->contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->resultJsonFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->sessionTransparentMock = $this->getMockBuilder(\Magento\Framework\Session\Generic::class)
- ->setMethods(['setQuoteId'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->secureTokenServiceMock = $this->getMockBuilder(
- \Magento\Paypal\Model\Payflow\Service\Request\SecureToken::class
- )
- ->setMethods(['requestToken'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->sessionManagerMock = $this->getMockBuilder(\Magento\Framework\Session\SessionManager::class)
- ->setMethods(['getQuote'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->transparentMock = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\Transparent::class)
- ->setMethods(['getCode'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->controller = new \Magento\Paypal\Controller\Transparent\RequestSecureToken(
- $this->contextMock,
- $this->resultJsonFactoryMock,
- $this->sessionTransparentMock,
- $this->secureTokenServiceMock,
- $this->sessionManagerMock,
- $this->transparentMock
- );
- }
- public function testExecuteSuccess()
- {
- $quoteId = 99;
- $tokenFields = ['fields-1', 'fields-2', 'fields-3'];
- $secureToken = 'token_hash';
- $resultExpectation = [
- 'transparent' => [
- 'fields' => ['fields-1', 'fields-2', 'fields-3']
- ],
- 'success' => true,
- 'error' => false
- ];
- $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
- ->disableOriginalConstructor()
- ->getMock();
- $tokenMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
- ->disableOriginalConstructor()
- ->getMock();
- $jsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->sessionManagerMock->expects($this->atLeastOnce())
- ->method('getQuote')
- ->willReturn($quoteMock);
- $quoteMock->expects($this->once())
- ->method('getId')
- ->willReturn($quoteId);
- $this->sessionTransparentMock->expects($this->once())
- ->method('setQuoteId')
- ->with($quoteId);
- $this->secureTokenServiceMock->expects($this->once())
- ->method('requestToken')
- ->with($quoteMock)
- ->willReturn($tokenMock);
- $this->transparentMock->expects($this->once())
- ->method('getCode')
- ->willReturn('transparent');
- $tokenMock->expects($this->atLeastOnce())
- ->method('getData')
- ->willReturnMap(
- [
- ['', null, $tokenFields],
- ['securetoken', null, $secureToken]
- ]
- );
- $this->resultJsonFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($jsonMock);
- $jsonMock->expects($this->once())
- ->method('setData')
- ->with($resultExpectation)
- ->willReturnSelf();
- $this->assertEquals($jsonMock, $this->controller->execute());
- }
- public function testExecuteTokenRequestException()
- {
- $quoteId = 99;
- $resultExpectation = [
- 'success' => false,
- 'error' => true,
- 'error_messages' => __('Your payment has been declined. Please try again.')
- ];
- $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
- ->disableOriginalConstructor()
- ->getMock();
- $jsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->sessionManagerMock->expects($this->atLeastOnce())
- ->method('getQuote')
- ->willReturn($quoteMock);
- $quoteMock->expects($this->once())
- ->method('getId')
- ->willReturn($quoteId);
- $this->sessionTransparentMock->expects($this->once())
- ->method('setQuoteId')
- ->with($quoteId);
- $this->secureTokenServiceMock->expects($this->once())
- ->method('requestToken')
- ->with($quoteMock)
- ->willThrowException(new \Exception());
- $this->resultJsonFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($jsonMock);
- $jsonMock->expects($this->once())
- ->method('setData')
- ->with($resultExpectation)
- ->willReturnSelf();
- $this->assertEquals($jsonMock, $this->controller->execute());
- }
- public function testExecuteEmptyQuoteError()
- {
- $resultExpectation = [
- 'success' => false,
- 'error' => true,
- 'error_messages' => __('Your payment has been declined. Please try again.')
- ];
- $quoteMock = null;
- $jsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->sessionManagerMock->expects($this->atLeastOnce())
- ->method('getQuote')
- ->willReturn($quoteMock);
- $this->resultJsonFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($jsonMock);
- $jsonMock->expects($this->once())
- ->method('setData')
- ->with($resultExpectation)
- ->willReturnSelf();
- $this->assertEquals($jsonMock, $this->controller->execute());
- }
- }
|