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()); } }