Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CancelResponseValidatorTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Braintree\Result\Error;
12 use PHPUnit\Framework\TestCase;
16 use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
17 use PHPUnit_Framework_MockObject_MockObject as MockObject;
18 
22 class CancelResponseValidatorTest extends TestCase
23 {
27  private $validator;
28 
32  private $generalValidator;
33 
37  private $resultFactory;
38 
42  protected function setUp()
43  {
44  $this->generalValidator = $this->getMockBuilder(GeneralResponseValidator::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47 
48  $this->resultFactory = $this->getMockBuilder(ResultInterfaceFactory::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51 
52  $this->validator = new CancelResponseValidator(
53  $this->resultFactory,
54  $this->generalValidator,
55  new SubjectReader()
56  );
57  }
58 
64  public function testValidateSuccessfulTransaction(): void
65  {
67  $result = $this->getMockForAbstractClass(ResultInterface::class);
68  $result->method('isValid')->willReturn(true);
69  $this->generalValidator->method('validate')->willReturn($result);
70  $actual = $this->validator->validate([]);
71 
72  $this->assertSame($result, $actual);
73  }
74 
81  public function testValidateExpiredTransaction(): void
82  {
84  $result = $this->getMockForAbstractClass(ResultInterface::class);
85  $result->method('isValid')->willReturn(false);
86  $this->generalValidator->method('validate')->willReturn($result);
87 
88  $expected = $this->getMockForAbstractClass(ResultInterface::class);
89  $expected->method('isValid')->willReturn(true);
90  $this->resultFactory->method('create')
91  ->with(
92  [
93  'isValid' => true,
94  'failsDescription' => ['Transaction is cancelled offline.'],
95  'errorCodes' => []
96  ]
97  )->willReturn($expected);
98 
99  $errors = [
100  'errors' => [
101  [
102  'code' => 91504,
103  'message' => 'Transaction can only be voided if status is authorized.',
104  ],
105  ],
106  ];
107  $buildSubject = [
108  'response' => [
109  'object' => new Error(['errors' => $errors]),
110  ],
111  ];
112 
113  $actual = $this->validator->validate($buildSubject);
114 
115  $this->assertSame($expected, $actual);
116  }
117 
125  public function testValidateWithMultipleErrors(array $responseErrors): void
126  {
128  $result = $this->getMockForAbstractClass(ResultInterface::class);
129  $result->method('isValid')->willReturn(false);
130 
131  $this->generalValidator->method('validate')->willReturn($result);
132 
133  $this->resultFactory->expects($this->never())->method('create');
134 
135  $errors = [
136  'errors' => $responseErrors,
137  ];
138  $buildSubject = [
139  'response' => [
140  'object' => new Error(['errors' => $errors]),
141  ]
142  ];
143 
144  $actual = $this->validator->validate($buildSubject);
145 
146  $this->assertSame($result, $actual);
147  }
148 
154  public function getErrorsDataProvider(): array
155  {
156  return [
157  [
158  'errors' => [
159  [
160  'code' => 91734,
161  'message' => 'Credit card type is not accepted by this merchant account.',
162  ],
163  [
164  'code' => 91504,
165  'message' => 'Transaction can only be voided if status is authorized.',
166  ],
167  ],
168  ],
169  [
170  'errors' => [
171  [
172  'code' => 91734,
173  'message' => 'Credit card type is not accepted by this merchant account.',
174  ],
175  ],
176  ],
177  ];
178  }
179 }
$errors
Definition: overview.phtml:9