Skip to content

Commit fd98e21

Browse files
authored
Merge pull request #2471 from magento-mpi/MAGETWO-83340
[MPI] Delivery
2 parents f658498 + 0ba409d commit fd98e21

28 files changed

+913
-441
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Braintree\Gateway\Validator;
9+
10+
use Braintree\Error\ErrorCollection;
11+
use Braintree\Error\Validation;
12+
use Braintree\Result\Error;
13+
use Braintree\Result\Successful;
14+
15+
/**
16+
* Processes errors codes from Braintree response.
17+
*/
18+
class ErrorCodeProvider
19+
{
20+
/**
21+
* Retrieves list of error codes from Braintree response.
22+
*
23+
* @param Successful|Error $response
24+
* @return array
25+
*/
26+
public function getErrorCodes($response): array
27+
{
28+
$result = [];
29+
if (!$response instanceof Error) {
30+
return $result;
31+
}
32+
33+
/** @var ErrorCollection $collection */
34+
$collection = $response->errors;
35+
36+
/** @var Validation $error */
37+
foreach ($collection->deepAll() as $error) {
38+
$result[] = $error->code;
39+
}
40+
41+
return $result;
42+
}
43+
}

app/code/Magento/Braintree/Gateway/Validator/GeneralResponseValidator.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,26 @@ class GeneralResponseValidator extends AbstractValidator
1818
*/
1919
protected $subjectReader;
2020

21+
/**
22+
* @var ErrorCodeProvider
23+
*/
24+
private $errorCodeProvider;
25+
2126
/**
2227
* Constructor
2328
*
2429
* @param ResultInterfaceFactory $resultFactory
2530
* @param SubjectReader $subjectReader
31+
* @param ErrorCodeProvider $errorCodeProvider
2632
*/
27-
public function __construct(ResultInterfaceFactory $resultFactory, SubjectReader $subjectReader)
28-
{
33+
public function __construct(
34+
ResultInterfaceFactory $resultFactory,
35+
SubjectReader $subjectReader,
36+
ErrorCodeProvider $errorCodeProvider
37+
) {
2938
parent::__construct($resultFactory);
3039
$this->subjectReader = $subjectReader;
40+
$this->errorCodeProvider = $errorCodeProvider;
3141
}
3242

3343
/**
@@ -49,8 +59,9 @@ public function validate(array $validationSubject)
4959
$errorMessages = array_merge($errorMessages, $validationResult[1]);
5060
}
5161
}
62+
$errorCodes = $this->errorCodeProvider->getErrorCodes($response);
5263

53-
return $this->createResult($isValid, $errorMessages);
64+
return $this->createResult($isValid, $errorMessages, $errorCodes);
5465
}
5566

5667
/**
@@ -62,7 +73,7 @@ protected function getResponseValidators()
6273
function ($response) {
6374
return [
6475
property_exists($response, 'success') && $response->success === true,
65-
[__('Braintree error response.')]
76+
[$response->message ?? __('Braintree error response.')]
6677
];
6778
}
6879
];

app/code/Magento/Braintree/Test/Unit/Gateway/Command/GetPaymentNonceCommandTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected function setUp()
9999
->getMock();
100100

101101
$this->validationResultMock = $this->getMockBuilder(ResultInterface::class)
102-
->setMethods(['isValid', 'getFailsDescription'])
102+
->setMethods(['isValid', 'getFailsDescription', 'getErrorCodes'])
103103
->getMock();
104104

105105
$this->responseValidatorMock = $this->getMockBuilder(PaymentNonceResponseValidator::class)

app/code/Magento/Braintree/Test/Unit/Gateway/Validator/GeneralResponseValidatorTest.php

+59-42
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
*/
66
namespace Magento\Braintree\Test\Unit\Gateway\Validator;
77

8-
use Braintree\Transaction;
8+
use Braintree\Result\Error;
9+
use Magento\Braintree\Gateway\SubjectReader;
10+
use Magento\Braintree\Gateway\Validator\ErrorCodeProvider;
11+
use Magento\Braintree\Gateway\Validator\GeneralResponseValidator;
912
use Magento\Framework\Phrase;
10-
use Magento\Payment\Gateway\Validator\ResultInterface;
13+
use Magento\Payment\Gateway\Validator\Result;
1114
use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
12-
use Magento\Braintree\Gateway\Validator\GeneralResponseValidator;
13-
use Magento\Braintree\Gateway\SubjectReader;
15+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1416

1517
class GeneralResponseValidatorTest extends \PHPUnit\Framework\TestCase
1618
{
@@ -20,14 +22,9 @@ class GeneralResponseValidatorTest extends \PHPUnit\Framework\TestCase
2022
private $responseValidator;
2123

2224
/**
23-
* @var ResultInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
24-
*/
25-
private $resultInterfaceFactoryMock;
26-
27-
/**
28-
* @var SubjectReader|\PHPUnit_Framework_MockObject_MockObject
25+
* @var ResultInterfaceFactory|MockObject
2926
*/
30-
private $subjectReaderMock;
27+
private $resultInterfaceFactory;
3128

3229
/**
3330
* Set up
@@ -36,85 +33,105 @@ class GeneralResponseValidatorTest extends \PHPUnit\Framework\TestCase
3633
*/
3734
protected function setUp()
3835
{
39-
$this->resultInterfaceFactoryMock = $this->getMockBuilder(
40-
\Magento\Payment\Gateway\Validator\ResultInterfaceFactory::class
41-
)->disableOriginalConstructor()
42-
->setMethods(['create'])
43-
->getMock();
44-
$this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
36+
$this->resultInterfaceFactory = $this->getMockBuilder(ResultInterfaceFactory::class)
4537
->disableOriginalConstructor()
38+
->setMethods(['create'])
4639
->getMock();
4740

4841
$this->responseValidator = new GeneralResponseValidator(
49-
$this->resultInterfaceFactoryMock,
50-
$this->subjectReaderMock
42+
$this->resultInterfaceFactory,
43+
new SubjectReader(),
44+
new ErrorCodeProvider()
5145
);
5246
}
5347

5448
/**
55-
* Run test for validate method
49+
* Checks a case when the validator processes successful and failed transactions.
5650
*
5751
* @param array $validationSubject
5852
* @param bool $isValid
5953
* @param Phrase[] $messages
54+
* @param array $errorCodes
6055
* @return void
6156
*
6257
* @dataProvider dataProviderTestValidate
6358
*/
64-
public function testValidate(array $validationSubject, $isValid, $messages)
59+
public function testValidate(array $validationSubject, bool $isValid, $messages, array $errorCodes)
6560
{
66-
/** @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject $resultMock */
67-
$resultMock = $this->createMock(ResultInterface::class);
68-
69-
$this->subjectReaderMock->expects(self::once())
70-
->method('readResponseObject')
71-
->with($validationSubject)
72-
->willReturn($validationSubject['response']['object']);
61+
$result = new Result($isValid, $messages);
7362

74-
$this->resultInterfaceFactoryMock->expects(self::once())
75-
->method('create')
63+
$this->resultInterfaceFactory->method('create')
7664
->with([
7765
'isValid' => $isValid,
78-
'failsDescription' => $messages
66+
'failsDescription' => $messages,
67+
'errorCodes' => $errorCodes
7968
])
80-
->willReturn($resultMock);
69+
->willReturn($result);
8170

82-
$actualMock = $this->responseValidator->validate($validationSubject);
71+
$actual = $this->responseValidator->validate($validationSubject);
8372

84-
self::assertEquals($resultMock, $actualMock);
73+
self::assertEquals($result, $actual);
8574
}
8675

8776
/**
77+
* Gets variations for different type of response.
78+
*
8879
* @return array
8980
*/
9081
public function dataProviderTestValidate()
9182
{
92-
$successTrue = new \stdClass();
93-
$successTrue->success = true;
83+
$successTransaction = new \stdClass();
84+
$successTransaction->success = true;
85+
86+
$failureTransaction = new \stdClass();
87+
$failureTransaction->success = false;
88+
$failureTransaction->message = 'Transaction was failed.';
9489

95-
$successFalse = new \stdClass();
96-
$successFalse->success = false;
90+
$errors = [
91+
'errors' => [
92+
[
93+
'code' => 81804,
94+
'attribute' => 'base',
95+
'message' => 'Cannot process transaction.'
96+
]
97+
]
98+
];
99+
$errorTransaction = new Error(['errors' => $errors]);
97100

98101
return [
99102
[
100103
'validationSubject' => [
101104
'response' => [
102-
'object' => $successTrue
105+
'object' => $successTransaction
103106
],
104107
],
105108
'isValid' => true,
106-
[]
109+
[],
110+
'errorCodes' => []
111+
],
112+
[
113+
'validationSubject' => [
114+
'response' => [
115+
'object' => $failureTransaction
116+
]
117+
],
118+
'isValid' => false,
119+
[
120+
__('Transaction was failed.')
121+
],
122+
'errorCodes' => []
107123
],
108124
[
109125
'validationSubject' => [
110126
'response' => [
111-
'object' => $successFalse
127+
'object' => $errorTransaction
112128
]
113129
],
114130
'isValid' => false,
115131
[
116132
__('Braintree error response.')
117-
]
133+
],
134+
'errorCodes' => ['81804']
118135
]
119136
];
120137
}

app/code/Magento/Braintree/Test/Unit/Gateway/Validator/PaymentNonceResponseValidatorTest.php

+13-46
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
*/
66
namespace Magento\Braintree\Test\Unit\Gateway\Validator;
77

8-
use Braintree\Transaction;
8+
use Magento\Braintree\Gateway\SubjectReader;
9+
use Magento\Braintree\Gateway\Validator\ErrorCodeProvider;
910
use Magento\Braintree\Gateway\Validator\PaymentNonceResponseValidator;
10-
use Magento\Payment\Gateway\Validator\ResultInterface;
11+
use Magento\Payment\Gateway\Validator\Result;
1112
use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
12-
use Magento\Braintree\Gateway\SubjectReader;
13+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1314

14-
/**
15-
* Class PaymentNonceResponseValidatorTest
16-
*/
1715
class PaymentNonceResponseValidatorTest extends \PHPUnit\Framework\TestCase
1816
{
1917
/**
@@ -22,35 +20,24 @@ class PaymentNonceResponseValidatorTest extends \PHPUnit\Framework\TestCase
2220
private $validator;
2321

2422
/**
25-
* @var ResultInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
23+
* @var ResultInterfaceFactory|MockObject
2624
*/
2725
private $resultInterfaceFactory;
2826

29-
/**
30-
* @var SubjectReader|\PHPUnit_Framework_MockObject_MockObject
31-
*/
32-
private $subjectReader;
33-
3427
protected function setUp()
3528
{
3629
$this->resultInterfaceFactory = $this->getMockBuilder(ResultInterfaceFactory::class)
3730
->disableOriginalConstructor()
3831
->setMethods(['create'])
3932
->getMock();
40-
$this->subjectReader = $this->getMockBuilder(SubjectReader::class)
41-
->disableOriginalConstructor()
42-
->setMethods(['readResponseObject'])
43-
->getMock();
4433

4534
$this->validator = new PaymentNonceResponseValidator(
4635
$this->resultInterfaceFactory,
47-
$this->subjectReader
36+
new SubjectReader(),
37+
new ErrorCodeProvider()
4838
);
4939
}
5040

51-
/**
52-
* @covers \Magento\Braintree\Gateway\Validator\PaymentNonceResponseValidator::validate
53-
*/
5441
public function testFailedValidate()
5542
{
5643
$obj = new \stdClass();
@@ -61,23 +48,12 @@ public function testFailedValidate()
6148
]
6249
];
6350

64-
$this->subjectReader->expects(static::once())
65-
->method('readResponseObject')
66-
->willReturn($obj);
67-
68-
$result = $this->createMock(ResultInterface::class);
69-
$this->resultInterfaceFactory->expects(self::once())
70-
->method('create')
71-
->with([
72-
'isValid' => false,
73-
'failsDescription' => [
74-
__('Payment method nonce can\'t be retrieved.')
75-
]
76-
])
51+
$result = new Result(false, [__('Payment method nonce can\'t be retrieved.')]);
52+
$this->resultInterfaceFactory->method('create')
7753
->willReturn($result);
7854

7955
$actual = $this->validator->validate($subject);
80-
static::assertEquals($result, $actual);
56+
self::assertEquals($result, $actual);
8157
}
8258

8359
public function testValidateSuccess()
@@ -93,20 +69,11 @@ public function testValidateSuccess()
9369
]
9470
];
9571

96-
$this->subjectReader->expects(static::once())
97-
->method('readResponseObject')
98-
->willReturn($obj);
99-
100-
$result = $this->createMock(ResultInterface::class);
101-
$this->resultInterfaceFactory->expects(self::once())
102-
->method('create')
103-
->with([
104-
'isValid' => true,
105-
'failsDescription' => []
106-
])
72+
$result = new Result(true);
73+
$this->resultInterfaceFactory->method('create')
10774
->willReturn($result);
10875

10976
$actual = $this->validator->validate($subject);
110-
static::assertEquals($result, $actual);
77+
self::assertEquals($result, $actual);
11178
}
11279
}

0 commit comments

Comments
 (0)