12
12
use Magento \Braintree \Model \Ui \ConfigProvider ;
13
13
use Magento \Framework \Api \SearchCriteriaBuilder ;
14
14
use Magento \Framework \App \State ;
15
+ use Magento \Framework \App \Area ;
15
16
use Magento \Quote \Api \CartRepositoryInterface ;
16
17
use Magento \Quote \Api \Data \CartInterface ;
17
18
use Magento \Quote \Api \Data \PaymentInterface ;
20
21
use PHPUnit \Framework \TestCase ;
21
22
use PHPUnit_Framework_MockObject_MockObject as MockObject ;
22
23
24
+ /**
25
+ * Class PaymentInformationManagementTest
26
+ *
27
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28
+ */
23
29
class PaymentInformationManagementTest extends TestCase
24
30
{
25
31
/**
@@ -64,9 +70,16 @@ protected function tearDown()
64
70
* @magentoConfigFixture current_store payment/braintree/active 1
65
71
* @dataProvider getErrorPerAreaDataProvider
66
72
* @expectedException \Magento\Framework\Exception\CouldNotSaveException
73
+ * @param string $area
74
+ * @param array $testErrorCodes
75
+ * @param string $expectedOutput
76
+ * @throws \Magento\Framework\Exception\LocalizedException
67
77
*/
68
- public function testSavePaymentInformationAndPlaceOrderWithErrors (string $ area , string $ errorMessage )
69
- {
78
+ public function testSavePaymentInformationAndPlaceOrderWithErrors (
79
+ string $ area ,
80
+ array $ testErrorCodes ,
81
+ string $ expectedOutput
82
+ ) {
70
83
/** @var State $state */
71
84
$ state = $ this ->objectManager ->get (State::class);
72
85
$ state ->setAreaCode ($ area );
@@ -77,31 +90,18 @@ public function testSavePaymentInformationAndPlaceOrderWithErrors(string $area,
77
90
$ payment = $ this ->objectManager ->create (PaymentInterface::class);
78
91
$ payment ->setMethod (ConfigProvider::CODE );
79
92
80
- $ errors = [
81
- 'errors ' => [
82
- [
83
- 'code ' => 'fake_code ' ,
84
- 'attribute ' => 'base ' ,
85
- 'message ' => 'Error message should not be mapped. '
86
- ],
87
- [
88
- 'code ' => 81802 ,
89
- 'attribute ' => 'base ' ,
90
- 'message ' => 'Company is too long. '
91
- ],
92
- [
93
- 'code ' => 91511 ,
94
- 'attribute ' => 'base ' ,
95
- 'message ' => 'Customer does not have any credit cards. '
96
- ]
97
- ]
98
- ];
93
+ $ errors = ['errors ' => []];
94
+
95
+ foreach ($ testErrorCodes as $ testErrorCode ) {
96
+ array_push ($ errors ['errors ' ], ['code ' => $ testErrorCode ]);
97
+ }
98
+
99
99
$ response = new Error (['errors ' => $ errors ]);
100
100
101
101
$ this ->client ->method ('placeRequest ' )
102
102
->willReturn (['object ' => $ response ]);
103
103
104
- $ this ->expectExceptionMessage ($ errorMessage );
104
+ $ this ->expectExceptionMessage ($ expectedOutput );
105
105
106
106
/** @var PaymentInformationManagementInterface $paymentInformationManagement */
107
107
$ paymentInformationManagement = $ this ->objectManager ->get (PaymentInformationManagementInterface::class);
@@ -118,10 +118,24 @@ public function testSavePaymentInformationAndPlaceOrderWithErrors(string $area,
118
118
*/
119
119
public function getErrorPerAreaDataProvider ()
120
120
{
121
- $ globalAreaError = 'Company is too long. ' ;
121
+ $ testErrorGlobal = ['code ' => 81802 , 'message ' => 'Company is too long. ' ];
122
+ $ testErrorAdmin = ['code ' => 91511 , 'message ' => 'Customer does not have any credit cards. ' ];
123
+ $ testErrorFake = ['code ' => 'fake_code ' , 'message ' => 'Error message should not be mapped. ' ];
124
+
122
125
return [
123
- ['area ' => 'frontend ' , 'error ' => $ globalAreaError ],
124
- ['area ' => 'adminhtml ' , 'error ' => $ globalAreaError . PHP_EOL . 'Customer does not have any credit cards. ' ],
126
+ [
127
+ Area::AREA_FRONTEND ,
128
+ [$ testErrorAdmin ['code ' ], $ testErrorFake ['code ' ]],
129
+ 'Transaction has been declined. Please try again later. '
130
+ ], [
131
+ Area::AREA_FRONTEND ,
132
+ [$ testErrorGlobal ['code ' ], $ testErrorAdmin ['code ' ], $ testErrorFake ['code ' ]],
133
+ $ testErrorGlobal ['message ' ]
134
+ ], [
135
+ Area::AREA_ADMINHTML ,
136
+ [$ testErrorGlobal ['code ' ], $ testErrorAdmin ['code ' ], $ testErrorFake ['code ' ]],
137
+ $ testErrorGlobal ['message ' ] . PHP_EOL . $ testErrorAdmin ['message ' ]
138
+ ],
125
139
];
126
140
}
127
141
@@ -131,7 +145,7 @@ public function getErrorPerAreaDataProvider()
131
145
* @param string $reservedOrderId
132
146
* @return CartInterface
133
147
*/
134
- private function getQuote (string $ reservedOrderId ) : CartInterface
148
+ private function getQuote (string $ reservedOrderId ): CartInterface
135
149
{
136
150
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
137
151
$ searchCriteriaBuilder = $ this ->objectManager ->get (SearchCriteriaBuilder::class);
0 commit comments