5
5
*/
6
6
namespace Magento \Braintree \Test \Unit \Gateway \Validator ;
7
7
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 ;
9
12
use Magento \Framework \Phrase ;
10
- use Magento \Payment \Gateway \Validator \ResultInterface ;
13
+ use Magento \Payment \Gateway \Validator \Result ;
11
14
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 ;
14
16
15
17
class GeneralResponseValidatorTest extends \PHPUnit \Framework \TestCase
16
18
{
@@ -20,14 +22,9 @@ class GeneralResponseValidatorTest extends \PHPUnit\Framework\TestCase
20
22
private $ responseValidator ;
21
23
22
24
/**
23
- * @var ResultInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
24
- */
25
- private $ resultInterfaceFactoryMock ;
26
-
27
- /**
28
- * @var SubjectReader|\PHPUnit_Framework_MockObject_MockObject
25
+ * @var ResultInterfaceFactory|MockObject
29
26
*/
30
- private $ subjectReaderMock ;
27
+ private $ resultInterfaceFactory ;
31
28
32
29
/**
33
30
* Set up
@@ -36,85 +33,105 @@ class GeneralResponseValidatorTest extends \PHPUnit\Framework\TestCase
36
33
*/
37
34
protected function setUp ()
38
35
{
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)
45
37
->disableOriginalConstructor ()
38
+ ->setMethods (['create ' ])
46
39
->getMock ();
47
40
48
41
$ this ->responseValidator = new GeneralResponseValidator (
49
- $ this ->resultInterfaceFactoryMock ,
50
- $ this ->subjectReaderMock
42
+ $ this ->resultInterfaceFactory ,
43
+ new SubjectReader (),
44
+ new ErrorCodeProvider ()
51
45
);
52
46
}
53
47
54
48
/**
55
- * Run test for validate method
49
+ * Checks a case when the validator processes successful and failed transactions.
56
50
*
57
51
* @param array $validationSubject
58
52
* @param bool $isValid
59
53
* @param Phrase[] $messages
54
+ * @param array $errorCodes
60
55
* @return void
61
56
*
62
57
* @dataProvider dataProviderTestValidate
63
58
*/
64
- public function testValidate (array $ validationSubject , $ isValid , $ messages )
59
+ public function testValidate (array $ validationSubject , bool $ isValid , $ messages, array $ errorCodes )
65
60
{
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 );
73
62
74
- $ this ->resultInterfaceFactoryMock ->expects (self ::once ())
75
- ->method ('create ' )
63
+ $ this ->resultInterfaceFactory ->method ('create ' )
76
64
->with ([
77
65
'isValid ' => $ isValid ,
78
- 'failsDescription ' => $ messages
66
+ 'failsDescription ' => $ messages ,
67
+ 'errorCodes ' => $ errorCodes
79
68
])
80
- ->willReturn ($ resultMock );
69
+ ->willReturn ($ result );
81
70
82
- $ actualMock = $ this ->responseValidator ->validate ($ validationSubject );
71
+ $ actual = $ this ->responseValidator ->validate ($ validationSubject );
83
72
84
- self ::assertEquals ($ resultMock , $ actualMock );
73
+ self ::assertEquals ($ result , $ actual );
85
74
}
86
75
87
76
/**
77
+ * Gets variations for different type of response.
78
+ *
88
79
* @return array
89
80
*/
90
81
public function dataProviderTestValidate ()
91
82
{
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. ' ;
94
89
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 ]);
97
100
98
101
return [
99
102
[
100
103
'validationSubject ' => [
101
104
'response ' => [
102
- 'object ' => $ successTrue
105
+ 'object ' => $ successTransaction
103
106
],
104
107
],
105
108
'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 ' => []
107
123
],
108
124
[
109
125
'validationSubject ' => [
110
126
'response ' => [
111
- 'object ' => $ successFalse
127
+ 'object ' => $ errorTransaction
112
128
]
113
129
],
114
130
'isValid ' => false ,
115
131
[
116
132
__ ('Braintree error response. ' )
117
- ]
133
+ ],
134
+ 'errorCodes ' => ['81804 ' ]
118
135
]
119
136
];
120
137
}
0 commit comments