Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RequestSecureTokenTest.php
Go to the documentation of this file.
1 <?php
7 
16 
22 class RequestSecureTokenTest extends \PHPUnit\Framework\TestCase
23 {
27  protected $transparentMock;
28 
32  protected $controller;
33 
37  protected $contextMock;
38 
43 
48 
53 
58 
64  protected function setUp()
65  {
66 
67  $this->contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70  $this->resultJsonFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
71  ->setMethods(['create'])
72  ->disableOriginalConstructor()
73  ->getMock();
74  $this->sessionTransparentMock = $this->getMockBuilder(\Magento\Framework\Session\Generic::class)
75  ->setMethods(['setQuoteId'])
76  ->disableOriginalConstructor()
77  ->getMock();
78  $this->secureTokenServiceMock = $this->getMockBuilder(
79  \Magento\Paypal\Model\Payflow\Service\Request\SecureToken::class
80  )
81  ->setMethods(['requestToken'])
82  ->disableOriginalConstructor()
83  ->getMock();
84  $this->sessionManagerMock = $this->getMockBuilder(\Magento\Framework\Session\SessionManager::class)
85  ->setMethods(['getQuote'])
86  ->disableOriginalConstructor()
87  ->getMock();
88  $this->transparentMock = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\Transparent::class)
89  ->setMethods(['getCode'])
90  ->disableOriginalConstructor()
91  ->getMock();
92 
93  $this->controller = new \Magento\Paypal\Controller\Transparent\RequestSecureToken(
94  $this->contextMock,
95  $this->resultJsonFactoryMock,
96  $this->sessionTransparentMock,
97  $this->secureTokenServiceMock,
98  $this->sessionManagerMock,
99  $this->transparentMock
100  );
101  }
102 
103  public function testExecuteSuccess()
104  {
105  $quoteId = 99;
106  $tokenFields = ['fields-1', 'fields-2', 'fields-3'];
107  $secureToken = 'token_hash';
108  $resultExpectation = [
109  'transparent' => [
110  'fields' => ['fields-1', 'fields-2', 'fields-3']
111  ],
112  'success' => true,
113  'error' => false
114  ];
115 
116  $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
117  ->disableOriginalConstructor()
118  ->getMock();
119  $tokenMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
120  ->disableOriginalConstructor()
121  ->getMock();
122  $jsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
123  ->disableOriginalConstructor()
124  ->getMock();
125 
126  $this->sessionManagerMock->expects($this->atLeastOnce())
127  ->method('getQuote')
128  ->willReturn($quoteMock);
129  $quoteMock->expects($this->once())
130  ->method('getId')
131  ->willReturn($quoteId);
132  $this->sessionTransparentMock->expects($this->once())
133  ->method('setQuoteId')
134  ->with($quoteId);
135  $this->secureTokenServiceMock->expects($this->once())
136  ->method('requestToken')
137  ->with($quoteMock)
138  ->willReturn($tokenMock);
139  $this->transparentMock->expects($this->once())
140  ->method('getCode')
141  ->willReturn('transparent');
142  $tokenMock->expects($this->atLeastOnce())
143  ->method('getData')
144  ->willReturnMap(
145  [
146  ['', null, $tokenFields],
147  ['securetoken', null, $secureToken]
148  ]
149  );
150  $this->resultJsonFactoryMock->expects($this->once())
151  ->method('create')
152  ->willReturn($jsonMock);
153  $jsonMock->expects($this->once())
154  ->method('setData')
155  ->with($resultExpectation)
156  ->willReturnSelf();
157 
158  $this->assertEquals($jsonMock, $this->controller->execute());
159  }
160 
162  {
163  $quoteId = 99;
164  $resultExpectation = [
165  'success' => false,
166  'error' => true,
167  'error_messages' => __('Your payment has been declined. Please try again.')
168  ];
169 
170  $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
171  ->disableOriginalConstructor()
172  ->getMock();
173  $jsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
174  ->disableOriginalConstructor()
175  ->getMock();
176 
177  $this->sessionManagerMock->expects($this->atLeastOnce())
178  ->method('getQuote')
179  ->willReturn($quoteMock);
180  $quoteMock->expects($this->once())
181  ->method('getId')
182  ->willReturn($quoteId);
183  $this->sessionTransparentMock->expects($this->once())
184  ->method('setQuoteId')
185  ->with($quoteId);
186  $this->secureTokenServiceMock->expects($this->once())
187  ->method('requestToken')
188  ->with($quoteMock)
189  ->willThrowException(new \Exception());
190  $this->resultJsonFactoryMock->expects($this->once())
191  ->method('create')
192  ->willReturn($jsonMock);
193  $jsonMock->expects($this->once())
194  ->method('setData')
195  ->with($resultExpectation)
196  ->willReturnSelf();
197 
198  $this->assertEquals($jsonMock, $this->controller->execute());
199  }
200 
201  public function testExecuteEmptyQuoteError()
202  {
203  $resultExpectation = [
204  'success' => false,
205  'error' => true,
206  'error_messages' => __('Your payment has been declined. Please try again.')
207  ];
208 
209  $quoteMock = null;
210  $jsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
211  ->disableOriginalConstructor()
212  ->getMock();
213 
214  $this->sessionManagerMock->expects($this->atLeastOnce())
215  ->method('getQuote')
216  ->willReturn($quoteMock);
217  $this->resultJsonFactoryMock->expects($this->once())
218  ->method('create')
219  ->willReturn($jsonMock);
220  $jsonMock->expects($this->once())
221  ->method('setData')
222  ->with($resultExpectation)
223  ->willReturnSelf();
224 
225  $this->assertEquals($jsonMock, $this->controller->execute());
226  }
227 }
return false
Definition: gallery.phtml:36
__()
Definition: __.php:13