Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetPaymentNonceCommandTest.php
Go to the documentation of this file.
1 <?php
7 
14 use Magento\Payment\Gateway\Command\Result\ArrayResultFactory;
18 use PHPUnit_Framework_MockObject_MockObject as MockObject;
19 
25 class GetPaymentNonceCommandTest extends \PHPUnit\Framework\TestCase
26 {
30  private $command;
31 
35  private $adapterMock;
36 
40  private $tokenManagementMock;
41 
45  private $paymentTokenMock;
46 
50  private $resultFactoryMock;
51 
55  private $subjectReaderMock;
56 
60  private $responseValidatorMock;
61 
65  private $validationResultMock;
66 
67  protected function setUp()
68  {
69  $this->paymentTokenMock = $this->getMockBuilder(PaymentToken::class)
70  ->disableOriginalConstructor()
71  ->setMethods(['getGatewayToken'])
72  ->getMock();
73 
74  $this->tokenManagementMock = $this->getMockBuilder(PaymentTokenManagement::class)
75  ->disableOriginalConstructor()
76  ->setMethods(['getByPublicHash'])
77  ->getMock();
78 
79  $this->adapterMock = $this->getMockBuilder(BraintreeAdapter::class)
80  ->disableOriginalConstructor()
81  ->setMethods(['createNonce'])
82  ->getMock();
84  $adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87  $adapterFactoryMock->expects(self::any())
88  ->method('create')
89  ->willReturn($this->adapterMock);
90 
91  $this->resultFactoryMock = $this->getMockBuilder(ArrayResultFactory::class)
92  ->disableOriginalConstructor()
93  ->setMethods(['create'])
94  ->getMock();
95 
96  $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
97  ->disableOriginalConstructor()
98  ->setMethods(['readPublicHash', 'readCustomerId'])
99  ->getMock();
100 
101  $this->validationResultMock = $this->getMockBuilder(ResultInterface::class)
102  ->setMethods(['isValid', 'getFailsDescription', 'getErrorCodes'])
103  ->getMock();
104 
105  $this->responseValidatorMock = $this->getMockBuilder(PaymentNonceResponseValidator::class)
106  ->disableOriginalConstructor()
107  ->setMethods(['validate', 'isValid', 'getFailsDescription'])
108  ->getMock();
109 
110  $this->command = new GetPaymentNonceCommand(
111  $this->tokenManagementMock,
112  $adapterFactoryMock,
113  $this->resultFactoryMock,
114  $this->subjectReaderMock,
115  $this->responseValidatorMock
116  );
117  }
118 
125  {
126  $exception = new \InvalidArgumentException('The "publicHash" field does not exists');
127 
128  $this->subjectReaderMock->expects(static::once())
129  ->method('readPublicHash')
130  ->willThrowException($exception);
131 
132  $this->subjectReaderMock->expects(self::never())
133  ->method('readCustomerId');
134 
135  $this->command->execute([]);
136  }
137 
144  {
145  $publicHash = '3wv2m24d2er3';
146 
147  $this->subjectReaderMock->expects(static::once())
148  ->method('readPublicHash')
149  ->willReturn($publicHash);
150 
151  $exception = new \InvalidArgumentException('The "customerId" field does not exists');
152  $this->subjectReaderMock->expects(static::once())
153  ->method('readCustomerId')
154  ->willThrowException($exception);
155 
156  $this->tokenManagementMock->expects(static::never())
157  ->method('getByPublicHash');
158 
159  $this->command->execute(['publicHash' => $publicHash]);
160  }
161 
168  {
169  $publicHash = '3wv2m24d2er3';
170  $customerId = 1;
171 
172  $this->subjectReaderMock->expects(static::once())
173  ->method('readPublicHash')
174  ->willReturn($publicHash);
175 
176  $this->subjectReaderMock->expects(static::once())
177  ->method('readCustomerId')
178  ->willReturn($customerId);
179 
180  $exception = new \Exception('No available payment tokens');
181  $this->tokenManagementMock->expects(static::once())
182  ->method('getByPublicHash')
183  ->willThrowException($exception);
184 
185  $this->paymentTokenMock->expects(self::never())
186  ->method('getGatewayToken');
187 
188  $this->command->execute(['publicHash' => $publicHash, 'customerId' => $customerId]);
189  }
190 
197  {
198  $publicHash = '3wv2m24d2er3';
199  $customerId = 1;
200  $token = 'jd2vnq';
201 
202  $this->subjectReaderMock->expects(static::once())
203  ->method('readPublicHash')
204  ->willReturn($publicHash);
205 
206  $this->subjectReaderMock->expects(static::once())
207  ->method('readCustomerId')
208  ->willReturn($customerId);
209 
210  $this->tokenManagementMock->expects(static::once())
211  ->method('getByPublicHash')
212  ->with($publicHash, $customerId)
213  ->willReturn($this->paymentTokenMock);
214 
215  $this->paymentTokenMock->expects(static::once())
216  ->method('getGatewayToken')
217  ->willReturn($token);
218 
219  $obj = new \stdClass();
220  $obj->success = false;
221  $this->adapterMock->expects(static::once())
222  ->method('createNonce')
223  ->with($token)
224  ->willReturn($obj);
225 
226  $this->responseValidatorMock->expects(static::once())
227  ->method('validate')
228  ->with(['response' => ['object' => $obj]])
229  ->willReturn($this->validationResultMock);
230 
231  $this->validationResultMock->expects(static::once())
232  ->method('isValid')
233  ->willReturn(false);
234 
235  $this->validationResultMock->expects(static::once())
236  ->method('getFailsDescription')
237  ->willReturn(['Payment method nonce can\'t be retrieved.']);
238 
239  $this->resultFactoryMock->expects(static::never())
240  ->method('create');
241 
242  $this->command->execute(['publicHash' => $publicHash, 'customerId' => $customerId]);
243  }
244 
248  public function testExecute()
249  {
250  $publicHash = '3wv2m24d2er3';
251  $customerId = 1;
252  $token = 'jd2vnq';
253  $nonce = 's1dj23';
254 
255  $this->subjectReaderMock->expects(static::once())
256  ->method('readPublicHash')
257  ->willReturn($publicHash);
258 
259  $this->subjectReaderMock->expects(static::once())
260  ->method('readCustomerId')
261  ->willReturn($customerId);
262 
263  $this->tokenManagementMock->expects(static::once())
264  ->method('getByPublicHash')
265  ->with($publicHash, $customerId)
266  ->willReturn($this->paymentTokenMock);
267 
268  $this->paymentTokenMock->expects(static::once())
269  ->method('getGatewayToken')
270  ->willReturn($token);
271 
272  $obj = new \stdClass();
273  $obj->success = true;
274  $obj->paymentMethodNonce = new \stdClass();
275  $obj->paymentMethodNonce->nonce = $nonce;
276  $this->adapterMock->expects(static::once())
277  ->method('createNonce')
278  ->with($token)
279  ->willReturn($obj);
280 
281  $this->responseValidatorMock->expects(static::once())
282  ->method('validate')
283  ->with(['response' => ['object' => $obj]])
284  ->willReturn($this->validationResultMock);
285 
286  $this->validationResultMock->expects(static::once())
287  ->method('isValid')
288  ->willReturn(true);
289 
290  $this->validationResultMock->expects(self::never())
291  ->method('getFailsDescription');
292 
293  $expected = $this->getMockBuilder(ArrayResult::class)
294  ->disableOriginalConstructor()
295  ->setMethods(['get'])
296  ->getMock();
297  $expected->expects(static::once())
298  ->method('get')
299  ->willReturn(['paymentMethodNonce' => $nonce]);
300  $this->resultFactoryMock->expects(static::once())
301  ->method('create')
302  ->willReturn($expected);
303 
304  $actual = $this->command->execute(['publicHash' => $publicHash, 'customerId' => $customerId]);
305  self::assertEquals($expected, $actual);
306  self::assertEquals($nonce, $actual->get()['paymentMethodNonce']);
307  }
308 }