14 use Magento\Payment\Gateway\Command\Result\ArrayResultFactory;
18 use PHPUnit_Framework_MockObject_MockObject as MockObject;
40 private $tokenManagementMock;
45 private $paymentTokenMock;
50 private $resultFactoryMock;
55 private $subjectReaderMock;
60 private $responseValidatorMock;
65 private $validationResultMock;
67 protected function setUp()
69 $this->paymentTokenMock = $this->getMockBuilder(PaymentToken::class)
70 ->disableOriginalConstructor()
71 ->setMethods([
'getGatewayToken'])
74 $this->tokenManagementMock = $this->getMockBuilder(PaymentTokenManagement::class)
75 ->disableOriginalConstructor()
76 ->setMethods([
'getByPublicHash'])
79 $this->adapterMock = $this->getMockBuilder(BraintreeAdapter::class)
80 ->disableOriginalConstructor()
81 ->setMethods([
'createNonce'])
84 $adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
85 ->disableOriginalConstructor()
87 $adapterFactoryMock->expects(self::any())
89 ->willReturn($this->adapterMock);
91 $this->resultFactoryMock = $this->getMockBuilder(ArrayResultFactory::class)
92 ->disableOriginalConstructor()
93 ->setMethods([
'create'])
96 $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
97 ->disableOriginalConstructor()
98 ->setMethods([
'readPublicHash',
'readCustomerId'])
101 $this->validationResultMock = $this->getMockBuilder(ResultInterface::class)
102 ->setMethods([
'isValid',
'getFailsDescription',
'getErrorCodes'])
105 $this->responseValidatorMock = $this->getMockBuilder(PaymentNonceResponseValidator::class)
106 ->disableOriginalConstructor()
107 ->setMethods([
'validate',
'isValid',
'getFailsDescription'])
111 $this->tokenManagementMock,
113 $this->resultFactoryMock,
114 $this->subjectReaderMock,
115 $this->responseValidatorMock
126 $exception = new \InvalidArgumentException(
'The "publicHash" field does not exists');
128 $this->subjectReaderMock->expects(static::once())
129 ->method(
'readPublicHash')
130 ->willThrowException($exception);
132 $this->subjectReaderMock->expects(self::never())
133 ->method(
'readCustomerId');
135 $this->command->execute([]);
145 $publicHash =
'3wv2m24d2er3';
147 $this->subjectReaderMock->expects(static::once())
148 ->method(
'readPublicHash')
149 ->willReturn($publicHash);
151 $exception = new \InvalidArgumentException(
'The "customerId" field does not exists');
152 $this->subjectReaderMock->expects(static::once())
153 ->method(
'readCustomerId')
154 ->willThrowException($exception);
156 $this->tokenManagementMock->expects(static::never())
157 ->method(
'getByPublicHash');
159 $this->command->execute([
'publicHash' => $publicHash]);
169 $publicHash =
'3wv2m24d2er3';
172 $this->subjectReaderMock->expects(static::once())
173 ->method(
'readPublicHash')
174 ->willReturn($publicHash);
176 $this->subjectReaderMock->expects(static::once())
177 ->method(
'readCustomerId')
180 $exception = new \Exception(
'No available payment tokens');
181 $this->tokenManagementMock->expects(static::once())
182 ->method(
'getByPublicHash')
183 ->willThrowException($exception);
185 $this->paymentTokenMock->expects(self::never())
186 ->method(
'getGatewayToken');
188 $this->command->execute([
'publicHash' => $publicHash,
'customerId' =>
$customerId]);
198 $publicHash =
'3wv2m24d2er3';
202 $this->subjectReaderMock->expects(static::once())
203 ->method(
'readPublicHash')
204 ->willReturn($publicHash);
206 $this->subjectReaderMock->expects(static::once())
207 ->method(
'readCustomerId')
210 $this->tokenManagementMock->expects(static::once())
211 ->method(
'getByPublicHash')
213 ->willReturn($this->paymentTokenMock);
215 $this->paymentTokenMock->expects(static::once())
216 ->method(
'getGatewayToken')
219 $obj = new \stdClass();
220 $obj->success =
false;
221 $this->adapterMock->expects(static::once())
222 ->method(
'createNonce')
226 $this->responseValidatorMock->expects(static::once())
228 ->with([
'response' => [
'object' => $obj]])
229 ->willReturn($this->validationResultMock);
231 $this->validationResultMock->expects(static::once())
235 $this->validationResultMock->expects(static::once())
236 ->method(
'getFailsDescription')
237 ->willReturn([
'Payment method nonce can\'t be retrieved.']);
239 $this->resultFactoryMock->expects(static::never())
242 $this->command->execute([
'publicHash' => $publicHash,
'customerId' =>
$customerId]);
250 $publicHash =
'3wv2m24d2er3';
255 $this->subjectReaderMock->expects(static::once())
256 ->method(
'readPublicHash')
257 ->willReturn($publicHash);
259 $this->subjectReaderMock->expects(static::once())
260 ->method(
'readCustomerId')
263 $this->tokenManagementMock->expects(static::once())
264 ->method(
'getByPublicHash')
266 ->willReturn($this->paymentTokenMock);
268 $this->paymentTokenMock->expects(static::once())
269 ->method(
'getGatewayToken')
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')
281 $this->responseValidatorMock->expects(static::once())
283 ->with([
'response' => [
'object' => $obj]])
284 ->willReturn($this->validationResultMock);
286 $this->validationResultMock->expects(static::once())
290 $this->validationResultMock->expects(self::never())
291 ->method(
'getFailsDescription');
293 $expected = $this->getMockBuilder(ArrayResult::class)
294 ->disableOriginalConstructor()
295 ->setMethods([
'get'])
297 $expected->expects(static::once())
299 ->willReturn([
'paymentMethodNonce' => $nonce]);
300 $this->resultFactoryMock->expects(static::once())
302 ->willReturn($expected);
304 $actual = $this->command->execute([
'publicHash' => $publicHash,
'customerId' =>
$customerId]);
305 self::assertEquals($expected, $actual);
306 self::assertEquals($nonce, $actual->get()[
'paymentMethodNonce']);
testExecuteWithExceptionForTokenManagement()
testExecuteWithExceptionForPublicHash()
testExecuteWithExceptionForCustomerId()
testExecuteWithFailedValidation()