PluginProbe
PayPlug for WooCommerce (Official) / 1.10.0
PayPlug for WooCommerce (Official) v1.10.0
3.1.0 3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 All 102 releases
payplug / vendor / payplug / payplug-php / tests / unit_tests / Resource / RefundTest.php

RefundTest.php in PayPlug for WooCommerce (Official) 1.10.0, at vendor/payplug/payplug-php/tests/unit_tests/Resource/RefundTest.php

404 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Payplug\Resource;
3 use Payplug;
4 use Payplug\Core\HttpClient;
5
6 /**
7 * @group unit
8 * @group ci
9 * @group recommended
10 */
11 class RefundTest extends \PHPUnit\Framework\TestCase
12 {
13 private $_requestMock;
14 private $_configuration;
15
16 /**
17 * @before
18 */
19 protected function setUpTest()
20 {
21 $this->_configuration = new Payplug\Payplug('abc');
22 Payplug\Payplug::setDefaultConfiguration($this->_configuration);
23
24 $this->_requestMock = $this->createMock('\Payplug\Core\IHttpRequest');
25 Payplug\Core\HttpClient::$REQUEST_HANDLER = $this->_requestMock;
26 }
27
28 protected function setUpTwice()
29 {
30 $this->_configuration = new Payplug\Payplug('abc','1970-01-01');
31 Payplug\Payplug::setDefaultConfiguration($this->_configuration);
32
33 $this->_requestMock = $this->createMock('\Payplug\Core\IHttpRequest');
34 Payplug\Core\HttpClient::$REQUEST_HANDLER = $this->_requestMock;
35 }
36
37 public function testCreateRefundFromAttributes()
38 {
39 $refund = Refund::fromAttributes(array(
40 'id' => 're_390312',
41 'payment_id' => 'pay_490329',
42 'object' => 'refund',
43 'amount' => 3300,
44 'currency' => 'EUR',
45 'created_at' => 1410437760
46 ));
47
48 $this->assertEquals('re_390312', $refund->id);
49 $this->assertEquals('pay_490329', $refund->payment_id);
50 $this->assertEquals('refund', $refund->object);
51 $this->assertEquals(3300, $refund->amount);
52 $this->assertEquals('EUR', $refund->currency);
53 $this->assertEquals(1410437760, $refund->created_at);
54 }
55
56 public function testRefundCreateFromPaymentId()
57 {
58 $GLOBALS['CURLOPT_URL_DATA'] = null;
59
60 $this->_requestMock
61 ->expects($this->once())
62 ->method('exec')
63 ->will($this->returnValue('{"status":"ok"}'));
64
65 $this->_requestMock
66 ->expects($this->any())
67 ->method('getinfo')
68 ->will($this->returnCallback(function($option) {
69 switch($option) {
70 case CURLINFO_HTTP_CODE:
71 return 200;
72 }
73 return null;
74 }));
75 $this->_requestMock
76 ->expects($this->any())
77 ->method('setopt')
78 ->will($this->returnCallback(function($option, $value = null) {
79 switch($option) {
80 case CURLOPT_URL:
81 $GLOBALS['CURLOPT_URL_DATA'] = $value;
82 return true;
83 }
84 return true;
85 }));
86
87 $refund = Refund::create('a_payment_id', array('amount' => 3300));
88
89 $this->assertEquals('ok', $refund->status);
90 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
91
92 unset($GLOBALS['CURLOPT_URL_DATA']);
93 }
94
95 public function testRefundCreateFromPaymentObject()
96 {
97 $GLOBALS['CURLOPT_URL_DATA'] = null;
98
99 $this->_requestMock
100 ->expects($this->once())
101 ->method('exec')
102 ->will($this->returnValue('{"status":"ok"}'));
103
104 $this->_requestMock
105 ->expects($this->any())
106 ->method('getinfo')
107 ->will($this->returnCallback(function($option) {
108 switch($option) {
109 case CURLINFO_HTTP_CODE:
110 return 200;
111 }
112 return null;
113 }));
114 $this->_requestMock
115 ->expects($this->any())
116 ->method('setopt')
117 ->will($this->returnCallback(function($option, $value = null) {
118 switch($option) {
119 case CURLOPT_URL:
120 $GLOBALS['CURLOPT_URL_DATA'] = $value;
121 return true;
122 }
123 return true;
124 }));
125
126 $refund = Refund::create(
127 Payment::fromAttributes(array('id' => 'a_payment_id')),
128 array('amount' => 3300)
129 );
130
131 $this->assertEquals('ok', $refund->status);
132 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
133
134 unset($GLOBALS['CURLOPT_URL_DATA']);
135 }
136
137 public function testRefundRetrieveFromPaymentId()
138 {
139 function testRefundRetrieveFromPaymentId_getinfo($option) {
140 switch($option) {
141 case CURLINFO_HTTP_CODE:
142 return 200;
143 }
144 return null;
145 }
146 $GLOBALS['CURLOPT_URL_DATA'] = null;
147 function testRefundRetrieveFromPaymentId_setopt($option, $value = null) {
148 switch($option) {
149 case CURLOPT_URL:
150 $GLOBALS['CURLOPT_URL_DATA'] = $value;
151 return true;
152 }
153 return true;
154 }
155
156 $this->_requestMock
157 ->expects($this->once())
158 ->method('exec')
159 ->will($this->returnValue('{"status":"ok"}'));
160
161 $this->_requestMock
162 ->expects($this->any())
163 ->method('getinfo')
164 ->will($this->returnCallback(function($option) {
165 switch($option) {
166 case CURLINFO_HTTP_CODE:
167 return 200;
168 }
169 return null;
170 }));
171 $this->_requestMock
172 ->expects($this->any())
173 ->method('setopt')
174 ->will($this->returnCallback(function($option, $value = null) {
175 switch($option) {
176 case CURLOPT_URL:
177 $GLOBALS['CURLOPT_URL_DATA'] = $value;
178 return true;
179 }
180 return true;
181 }));
182
183 $refund = Refund::retrieve('a_payment_id', 'a_refund_id');
184
185 $this->assertEquals('ok', $refund->status);
186 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
187 $this->assertStringEndsWith('a_refund_id', $GLOBALS['CURLOPT_URL_DATA']);
188
189 unset($GLOBALS['CURLOPT_URL_DATA']);
190 }
191
192 public function testRefundRetrieveFromPaymentObject()
193 {
194 $GLOBALS['CURLOPT_URL_DATA'] = null;
195
196
197 $this->_requestMock
198 ->expects($this->once())
199 ->method('exec')
200 ->will($this->returnValue('{"status":"ok"}'));
201
202 $this->_requestMock
203 ->expects($this->any())
204 ->method('getinfo')
205 ->will($this->returnCallback(function($option) {
206 switch($option) {
207 case CURLINFO_HTTP_CODE:
208 return 200;
209 }
210 return null;
211 }));
212 $this->_requestMock
213 ->expects($this->any())
214 ->method('setopt')
215 ->will($this->returnCallback(function($option, $value = null) {
216 switch($option) {
217 case CURLOPT_URL:
218 $GLOBALS['CURLOPT_URL_DATA'] = $value;
219 return true;
220 }
221 return true;
222 }));
223
224 $refund = Refund::retrieve(
225 Payment::fromAttributes(array('id' => 'a_payment_id')),
226 'a_refund_id'
227 );
228
229 $this->assertEquals('ok', $refund->status);
230 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
231 $this->assertStringEndsWith('a_refund_id', $GLOBALS['CURLOPT_URL_DATA']);
232
233 unset($GLOBALS['CURLOPT_URL_DATA']);
234 }
235
236 public function testRefundsListThrowsExceptionOnWongAPIResponse()
237 {
238 $this->expectException('\PayPlug\Exception\UnexpectedAPIResponseException');
239
240 $this->_requestMock
241 ->expects($this->once())
242 ->method('exec')
243 ->will($this->returnValue('{"status":"this_is_an_invalid_response"}'));
244
245 $this->_requestMock
246 ->expects($this->any())
247 ->method('getinfo')
248 ->will($this->returnCallback(function($option) {
249 switch($option) {
250 case CURLINFO_HTTP_CODE:
251 return 200;
252 }
253 return null;
254 }));
255
256 Refund::listRefunds('a_payment_id');
257 }
258
259 public function testRefundsListFromPaymentId()
260 {
261 $GLOBALS['CURLOPT_URL_DATA'] = null;
262
263 $this->_requestMock
264 ->expects($this->once())
265 ->method('exec')
266 ->will($this->returnValue('{"data":[{"id": "refund1"}, {"id": "refund2"}]}'));
267
268 $this->_requestMock
269 ->expects($this->any())
270 ->method('getinfo')
271 ->will($this->returnCallback(function($option) {
272 switch($option) {
273 case CURLINFO_HTTP_CODE:
274 return 200;
275 }
276 return null;
277 }));
278 $this->_requestMock
279 ->expects($this->any())
280 ->method('setopt')
281 ->will($this->returnCallback(function($option, $value = null) {
282 switch($option) {
283 case CURLOPT_URL:
284 $GLOBALS['CURLOPT_URL_DATA'] = $value;
285 return true;
286 }
287 return true;
288 }));
289
290 $refunds = Refund::listRefunds('a_payment_id');
291
292 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
293 $this->assertEquals(2, count($refunds));
294 $this->assertTrue('refund1' === $refunds[0]->id || 'refund2' === $refunds[1]->id);
295 $this->assertTrue(
296 (('refund1' === $refunds[1]->id) || ('refund2' === $refunds[1]->id))
297 && ($refunds[0]->id !== $refunds[1]->id)
298 );
299
300 unset($GLOBALS['CURLOPT_URL_DATA']);
301 }
302
303 public function testRefundsListFromPaymentObject()
304 {
305 $GLOBALS['CURLOPT_URL_DATA'] = null;
306
307 $this->_requestMock
308 ->expects($this->once())
309 ->method('exec')
310 ->will($this->returnValue('{"data":[{"id": "refund1"}, {"id": "refund2"}]}'));
311
312 $this->_requestMock
313 ->expects($this->any())
314 ->method('getinfo')
315 ->will($this->returnCallback(function($option) {
316 switch($option) {
317 case CURLINFO_HTTP_CODE:
318 return 200;
319 }
320 return null;
321 }));
322 $this->_requestMock
323 ->expects($this->any())
324 ->method('setopt')
325 ->will($this->returnCallback(function($option, $value = null) {
326 switch($option) {
327 case CURLOPT_URL:
328 $GLOBALS['CURLOPT_URL_DATA'] = $value;
329 return true;
330 }
331 return true;
332 }));
333
334 $refunds = Refund::listRefunds(
335 Payment::fromAttributes(array('id' => 'a_payment_id'))
336 );
337
338 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
339 $this->assertEquals(2, count($refunds));
340 $this->assertTrue('refund1' === $refunds[0]->id || 'refund2' === $refunds[1]->id);
341 $this->assertTrue(
342 (('refund1' === $refunds[1]->id) || ('refund2' === $refunds[1]->id))
343 && ($refunds[0]->id !== $refunds[1]->id)
344 );
345
346 unset($GLOBALS['CURLOPT_URL_DATA']);
347 }
348
349 public function testRetrieveConsistentRefundWhenIdIsUndefined()
350 {
351 $this->expectException('\PayPlug\Exception\UndefinedAttributeException');
352
353 $payment = Refund::fromAttributes(array('this_refund' => 'has_no_id', 'payment_id' => 'pay_id'));
354 $payment->getConsistentResource();
355 }
356
357 public function testRetrieveConsistentRefundWhenPaymentIdIsUndefined()
358 {
359 $this->expectException('\PayPlug\Exception\UndefinedAttributeException');
360
361 $payment = Refund::fromAttributes(array('id' => 'an_id', 'no_payment_id' => ''));
362 $payment->getConsistentResource();
363 }
364
365 public function testRetrieveConsistentRefund()
366 {
367 function testRetrieveConsistentRefund_getinfo($option) {
368 switch($option) {
369 case CURLINFO_HTTP_CODE:
370 return 200;
371 }
372 return null;
373 }
374
375 $this->_requestMock
376 ->expects($this->once())
377 ->method('exec')
378 ->will($this->returnValue('{"id": "re_345", "payment_id": "pay_789"}'));
379
380 $this->_requestMock
381 ->expects($this->any())
382 ->method('setopt')
383 ->will($this->returnValue(true));
384 $this->_requestMock
385 ->expects($this->any())
386 ->method('getinfo')
387 ->will($this->returnCallback(function($option) {
388 switch($option) {
389 case CURLINFO_HTTP_CODE:
390 return 200;
391 }
392 return null;
393 }));
394
395 $refund1 = Refund::fromAttributes(array('id' => 're_123', 'payment_id' => 'pay_321'));
396 $refund2 = $refund1->getConsistentResource($this->_configuration);
397
398 $this->assertEquals('re_123', $refund1->id);
399 $this->assertEquals('pay_321', $refund1->payment_id);
400 $this->assertEquals('re_345', $refund2->id);
401 $this->assertEquals('pay_789', $refund2->payment_id);
402 }
403 }
404