Skip to content

Commit 64d051c

Browse files
committed
Merge branch '2.2-develop-mainline' into ISSUE-12889
2 parents 18d7890 + a39e37f commit 64d051c

File tree

37 files changed

+900
-469
lines changed

37 files changed

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

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

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

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

3343
/**
@@ -62,9 +72,10 @@ protected function getResponseValidators()
6272
function ($response) {
6373
return [
6474
property_exists($response, 'success') && $response->success === true,
65-
[__('Braintree error response.')]
75+
[$response->message ?? __('Braintree error response.')]
6676
];
67-
}
77+
},
78+
$this->errorCodeValidator
6879
];
6980
}
7081
}

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

Lines changed: 53 additions & 40 deletions
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\ErrorCodeValidator;
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,23 +33,20 @@ 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 ErrorCodeValidator()
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
@@ -61,45 +55,52 @@ protected function setUp()
6155
*
6256
* @dataProvider dataProviderTestValidate
6357
*/
64-
public function testValidate(array $validationSubject, $isValid, $messages)
58+
public function testValidate(array $validationSubject, bool $isValid, $messages)
6559
{
66-
/** @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject $resultMock */
67-
$resultMock = $this->createMock(ResultInterface::class);
60+
$result = new Result($isValid, $messages);
6861

69-
$this->subjectReaderMock->expects(self::once())
70-
->method('readResponseObject')
71-
->with($validationSubject)
72-
->willReturn($validationSubject['response']['object']);
73-
74-
$this->resultInterfaceFactoryMock->expects(self::once())
75-
->method('create')
62+
$this->resultInterfaceFactory->method('create')
7663
->with([
7764
'isValid' => $isValid,
7865
'failsDescription' => $messages
7966
])
80-
->willReturn($resultMock);
67+
->willReturn($result);
8168

82-
$actualMock = $this->responseValidator->validate($validationSubject);
69+
$actual = $this->responseValidator->validate($validationSubject);
8370

84-
self::assertEquals($resultMock, $actualMock);
71+
self::assertEquals($result, $actual);
8572
}
8673

8774
/**
75+
* Gets variations for different type of response.
76+
*
8877
* @return array
8978
*/
9079
public function dataProviderTestValidate()
9180
{
92-
$successTrue = new \stdClass();
93-
$successTrue->success = true;
81+
$successTransaction = new \stdClass();
82+
$successTransaction->success = true;
83+
84+
$failureTransaction = new \stdClass();
85+
$failureTransaction->success = false;
86+
$failureTransaction->message = 'Transaction was failed.';
9487

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

9899
return [
99100
[
100101
'validationSubject' => [
101102
'response' => [
102-
'object' => $successTrue
103+
'object' => $successTransaction
103104
],
104105
],
105106
'isValid' => true,
@@ -108,12 +109,24 @@ public function dataProviderTestValidate()
108109
[
109110
'validationSubject' => [
110111
'response' => [
111-
'object' => $successFalse
112+
'object' => $failureTransaction
113+
]
114+
],
115+
'isValid' => false,
116+
[
117+
__('Transaction was failed.')
118+
]
119+
],
120+
[
121+
'validationSubject' => [
122+
'response' => [
123+
'object' => $errorTransaction
112124
]
113125
],
114126
'isValid' => false,
115127
[
116-
__('Braintree error response.')
128+
__('Braintree error response.'),
129+
81804
117130
]
118131
]
119132
];

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

Lines changed: 13 additions & 46 deletions
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\ErrorCodeValidator;
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 ErrorCodeValidator()
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)