PluginProbe
PayPlug for WooCommerce (Official) / 1.0.21
PayPlug for WooCommerce (Official) v1.0.21
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 / PaymentTest.php

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

750 lines 29.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
5 /**
6 * @group unit
7 * @group ci
8 * @group recommended
9 */
10 class PaymentTest extends \PHPUnit_Framework_TestCase
11 {
12 private $_requestMock;
13 private $_configuration;
14
15 protected function setUp()
16 {
17 $this->_configuration = new Payplug\Payplug('abc');
18 Payplug\Payplug::setDefaultConfiguration($this->_configuration);
19
20 $this->_requestMock = $this->getMock('\Payplug\Core\IHttpRequest');
21 Payplug\Core\HttpClient::$REQUEST_HANDLER = $this->_requestMock;
22 }
23
24 public function testRetroCreatePaymentFromAttributes()
25 {
26 $payment = Payment::fromAttributes(array(
27 'id' => 'pay_490329',
28 'object' => 'payment',
29 'is_live' => true,
30 'amount' => 3300,
31 'amount_refunded' => 0,
32 'currency' => 'EUR',
33 'created_at' => 1410437760,
34 'is_paid' => true,
35 'is_refunded' => false,
36 'is_3ds' => false,
37 'save_card' => false,
38 'card' => array(
39 'last4' => '1800',
40 'country' => 'FR',
41 'exp_year' => 2017,
42 'exp_month' => 9,
43 'brand' => 'Mastercard'
44 ),
45 'customer' => array(
46 'email' => '[email protected]',
47 'first_name' => 'John',
48 'last_name' => 'Doe'
49 ),
50 'hosted_payment' => array(
51 'payment_url' => 'https://www.payplug.com/p/b9868d18546711e490c612314307c934',
52 'return_url' => 'http://yourwebsite.com/payplug_return?someid=11235',
53 'cancel_url' => 'http://yourwebsite.com/payplug_cancel?someid=81321',
54 'paid_at' => 1410437806
55 ),
56 'notification' => array(
57 'url' => 'http://yourwebsite.com/payplug_ipn',
58 'response_code' => 200
59 ),
60 'failure' => null,
61 'metadata' => array(
62 'a_custom_field' => 'a custom value',
63 'another_key' => 'another value'
64 )
65 ));
66
67 $this->assertEquals('pay_490329', $payment->id);
68 $this->assertEquals('payment', $payment->object);
69 $this->assertEquals(true, $payment->is_live);
70 $this->assertEquals(3300, $payment->amount);
71 $this->assertEquals(0, $payment->amount_refunded);
72 $this->assertEquals('EUR', $payment->currency);
73 $this->assertEquals(1410437760, $payment->created_at);
74 $this->assertEquals(true, $payment->is_paid);
75 $this->assertEquals(false, $payment->is_refunded);
76 $this->assertEquals(false, $payment->is_3ds);
77 $this->assertEquals(false, $payment->save_card);
78
79 $this->assertEquals('1800', $payment->card->last4);
80 $this->assertEquals('FR', $payment->card->country);
81 $this->assertEquals(2017, $payment->card->exp_year);
82 $this->assertEquals(9, $payment->card->exp_month);
83 $this->assertEquals('Mastercard', $payment->card->brand);
84
85 $this->assertEquals('[email protected]', $payment->customer->email);
86 $this->assertEquals('John', $payment->customer->first_name);
87 $this->assertEquals('Doe', $payment->customer->last_name);
88
89 $this->assertEquals('https://www.payplug.com/p/b9868d18546711e490c612314307c934', $payment->hosted_payment->payment_url);
90
91 $this->assertEquals('http://yourwebsite.com/payplug_return?someid=11235', $payment->hosted_payment->return_url);
92 $this->assertEquals('http://yourwebsite.com/payplug_cancel?someid=81321', $payment->hosted_payment->cancel_url);
93 $this->assertEquals(1410437806, $payment->hosted_payment->paid_at);
94 $this->assertEquals('http://yourwebsite.com/payplug_ipn', $payment->notification->url);
95 $this->assertEquals(200, $payment->notification->response_code);
96
97 $this->assertNull($payment->failure);
98
99 $this->assertEquals('a custom value', $payment->metadata['a_custom_field']);
100 $this->assertEquals('another value', $payment->metadata['another_key']);
101 }
102
103 public function testCompleteRetroCreatePaymentFromAttributes()
104 {
105 $payment = Payment::fromAttributes(array(
106 'id' => 'pay_490329',
107 'object' => 'payment',
108 'is_live' => true,
109 'amount' => 3300,
110 'amount_refunded' => 0,
111 'currency' => 'EUR',
112 'created_at' => 1410437760,
113 'is_paid' => true,
114 'is_refunded' => false,
115 'is_3ds' => false,
116 'save_card' => false,
117 'card' => array(
118 'last4' => '1800',
119 'country' => 'FR',
120 'exp_year' => 2017,
121 'exp_month' => 9,
122 'brand' => 'Mastercard'
123 ),
124 'customer' => array(
125 'email' => '[email protected]',
126 'first_name' => 'John',
127 'last_name' => 'Doe'
128 ),
129 'billing' => array(
130 "title" => "Mr",
131 "first_name" => "John",
132 "last_name" => "Doe",
133 "email" => "[email protected]",
134 "phone_number" => "0123456789",
135 "address1" => "77 rue la Boétie",
136 "address2" => null,
137 "company_name" => "PayPlug",
138 "postcode" => "75008",
139 "city" => "Paris",
140 "state" => null,
141 "country" => "FR",
142 "language" => "fr"
143 ),
144 'shipping' => array(
145 "title" => "Mr",
146 "first_name" => "John",
147 "last_name" => "Doe",
148 "email" => "[email protected]",
149 "phone_number" => "0123456789",
150 "address1" => "77 rue la Boétie",
151 "address2" => null,
152 "company_name" => "PayPlug",
153 "postcode" => "75008",
154 "city" => "Paris",
155 "state" => null,
156 "country" => "FR",
157 "language" => "fr"
158 ),
159 'hosted_payment' => array(
160 'payment_url' => 'https://www.payplug.com/p/b9868d18546711e490c612314307c934',
161 'return_url' => 'http://yourwebsite.com/payplug_return?someid=11235',
162 'cancel_url' => 'http://yourwebsite.com/payplug_cancel?someid=81321',
163 'paid_at' => 1410437806
164 ),
165 'notification' => array(
166 'url' => 'http://yourwebsite.com/payplug_ipn',
167 'response_code' => 200
168 ),
169 'failure' => null,
170 'metadata' => array(
171 'a_custom_field' => 'a custom value',
172 'another_key' => 'another value'
173 )
174 ));
175
176 $this->assertEquals('pay_490329', $payment->id);
177 $this->assertEquals('payment', $payment->object);
178 $this->assertEquals(true, $payment->is_live);
179 $this->assertEquals(3300, $payment->amount);
180 $this->assertEquals(0, $payment->amount_refunded);
181 $this->assertEquals('EUR', $payment->currency);
182 $this->assertEquals(1410437760, $payment->created_at);
183 $this->assertEquals(true, $payment->is_paid);
184 $this->assertEquals(false, $payment->is_refunded);
185 $this->assertEquals(false, $payment->is_3ds);
186 $this->assertEquals(false, $payment->save_card);
187
188 $this->assertEquals('1800', $payment->card->last4);
189 $this->assertEquals('FR', $payment->card->country);
190 $this->assertEquals(2017, $payment->card->exp_year);
191 $this->assertEquals(9, $payment->card->exp_month);
192 $this->assertEquals('Mastercard', $payment->card->brand);
193
194 $this->assertEquals('[email protected]', $payment->customer->email);
195 $this->assertEquals('John', $payment->customer->first_name);
196 $this->assertEquals('Doe', $payment->customer->last_name);
197
198 // Billing / Shiping
199 $this->assertEquals('Mr', $payment->shipping->title);
200 $this->assertEquals('John', $payment->billing->first_name);
201 $this->assertEquals('Doe', $payment->shipping->last_name);
202 $this->assertEquals('[email protected]', $payment->billing->email);
203 $this->assertEquals('0123456789', $payment->shipping->phone_number);
204 $this->assertEquals('77 rue la Boétie', $payment->billing->address1);
205 $this->assertEquals(null, $payment->shipping->address2);
206 $this->assertEquals('PayPlug', $payment->billing->company_name);
207 $this->assertEquals('75008', $payment->shipping->postcode);
208 $this->assertEquals('Paris', $payment->billing->city);
209 $this->assertEquals(null, $payment->billing->state);
210 $this->assertEquals('FR', $payment->shipping->country);
211 $this->assertEquals('fr', $payment->billing->language);
212
213 $this->assertEquals('https://www.payplug.com/p/b9868d18546711e490c612314307c934', $payment->hosted_payment->payment_url);
214
215 $this->assertEquals('http://yourwebsite.com/payplug_return?someid=11235', $payment->hosted_payment->return_url);
216 $this->assertEquals('http://yourwebsite.com/payplug_cancel?someid=81321', $payment->hosted_payment->cancel_url);
217 $this->assertEquals(1410437806, $payment->hosted_payment->paid_at);
218 $this->assertEquals('http://yourwebsite.com/payplug_ipn', $payment->notification->url);
219 $this->assertEquals(200, $payment->notification->response_code);
220
221 $this->assertNull($payment->failure);
222
223 $this->assertEquals('a custom value', $payment->metadata['a_custom_field']);
224 $this->assertEquals('another value', $payment->metadata['another_key']);
225 }
226
227 public function testCreatePaymentFromAttributes()
228 {
229 $payment = Payment::fromAttributes(array(
230 'id' => 'pay_490329',
231 'object' => 'payment',
232 'is_live' => true,
233 'amount' => 3300,
234 'amount_refunded' => 0,
235 'currency' => 'EUR',
236 'created_at' => 1410437760,
237 'is_paid' => true,
238 'is_refunded' => false,
239 'is_3ds' => false,
240 'save_card' => false,
241 'card' => array(
242 'last4' => '1800',
243 'country' => 'FR',
244 'exp_year' => 2017,
245 'exp_month' => 9,
246 'brand' => 'Mastercard'
247 ),
248 'billing' => array(
249 "title" => "Mr",
250 "first_name" => "John",
251 "last_name" => "Doe",
252 "email" => "[email protected]",
253 "phone_number" => "0123456789",
254 "address1" => "77 rue la Boétie",
255 "address2" => null,
256 "company_name" => "PayPlug",
257 "postcode" => "75008",
258 "city" => "Paris",
259 "state" => null,
260 "country" => "FR",
261 "language" => "fr"
262 ),
263 'shipping' => array(
264 "title" => "Mr",
265 "first_name" => "John",
266 "last_name" => "Doe",
267 "email" => "[email protected]",
268 "phone_number" => "0123456789",
269 "address1" => "77 rue la Boétie",
270 "address2" => null,
271 "company_name" => "PayPlug",
272 "postcode" => "75008",
273 "city" => "Paris",
274 "state" => null,
275 "country" => "FR",
276 "language" => "fr"
277 ),
278 'hosted_payment' => array(
279 'payment_url' => 'https://www.payplug.com/p/b9868d18546711e490c612314307c934',
280 'return_url' => 'http://yourwebsite.com/payplug_return?someid=11235',
281 'cancel_url' => 'http://yourwebsite.com/payplug_cancel?someid=81321',
282 'paid_at' => 1410437806
283 ),
284 'notification' => array(
285 'url' => 'http://yourwebsite.com/payplug_ipn',
286 'response_code' => 200
287 ),
288 'failure' => null,
289 'metadata' => array(
290 'a_custom_field' => 'a custom value',
291 'another_key' => 'another value'
292 )
293 ));
294
295 $this->assertEquals('pay_490329', $payment->id);
296 $this->assertEquals('payment', $payment->object);
297 $this->assertEquals(true, $payment->is_live);
298 $this->assertEquals(3300, $payment->amount);
299 $this->assertEquals(0, $payment->amount_refunded);
300 $this->assertEquals('EUR', $payment->currency);
301 $this->assertEquals(1410437760, $payment->created_at);
302 $this->assertEquals(true, $payment->is_paid);
303 $this->assertEquals(false, $payment->is_refunded);
304 $this->assertEquals(false, $payment->is_3ds);
305 $this->assertEquals(false, $payment->save_card);
306
307 $this->assertEquals('1800', $payment->card->last4);
308 $this->assertEquals('FR', $payment->card->country);
309 $this->assertEquals(2017, $payment->card->exp_year);
310 $this->assertEquals(9, $payment->card->exp_month);
311 $this->assertEquals('Mastercard', $payment->card->brand);
312
313
314 // Billing / Shiping
315 $this->assertEquals('Mr', $payment->shipping->title);
316 $this->assertEquals('John', $payment->billing->first_name);
317 $this->assertEquals('Doe', $payment->shipping->last_name);
318 $this->assertEquals('[email protected]', $payment->billing->email);
319 $this->assertEquals('0123456789', $payment->shipping->phone_number);
320 $this->assertEquals('77 rue la Boétie', $payment->billing->address1);
321 $this->assertEquals(null, $payment->shipping->address2);
322 $this->assertEquals('PayPlug', $payment->billing->company_name);
323 $this->assertEquals('75008', $payment->shipping->postcode);
324 $this->assertEquals('Paris', $payment->billing->city);
325 $this->assertEquals(null, $payment->billing->state);
326 $this->assertEquals('FR', $payment->shipping->country);
327 $this->assertEquals('fr', $payment->billing->language);
328
329 $this->assertEquals('https://www.payplug.com/p/b9868d18546711e490c612314307c934', $payment->hosted_payment->payment_url);
330
331 $this->assertEquals('http://yourwebsite.com/payplug_return?someid=11235', $payment->hosted_payment->return_url);
332 $this->assertEquals('http://yourwebsite.com/payplug_cancel?someid=81321', $payment->hosted_payment->cancel_url);
333 $this->assertEquals(1410437806, $payment->hosted_payment->paid_at);
334 $this->assertEquals('http://yourwebsite.com/payplug_ipn', $payment->notification->url);
335 $this->assertEquals(200, $payment->notification->response_code);
336
337 $this->assertNull($payment->failure);
338
339 $this->assertEquals('a custom value', $payment->metadata['a_custom_field']);
340 $this->assertEquals('another value', $payment->metadata['another_key']);
341 }
342
343 public function testPaymentCreate()
344 {
345 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = null;
346
347 $this->_requestMock
348 ->expects($this->once())
349 ->method('exec')
350 ->will($this->returnValue('{"status":"ok"}'));
351
352 $this->_requestMock
353 ->expects($this->atLeastOnce())
354 ->method('setopt')
355 ->will($this->returnCallback(function($option, $value = null) {
356 switch($option) {
357 case CURLOPT_POSTFIELDS:
358 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = json_decode($value, true);
359 return true;
360 }
361 return true;
362 }));
363 $this->_requestMock
364 ->expects($this->any())
365 ->method('getinfo')
366 ->will($this->returnCallback(function($option) {
367 switch($option) {
368 case CURLINFO_HTTP_CODE:
369 return 200;
370 }
371 return null;
372 }));
373
374 $payment = Payment::create(array(
375 'amount' => 999,
376 'currency' => 'EUR',
377 'customer' => array(
378 'email' => '[email protected]',
379 'first_name' => 'John',
380 'last_name' => 'Doe'
381 ),
382 'billing' => array(
383 'title' => 'Mr',
384 'first_name' => 'John',
385 'last_name' => 'Doe',
386 'email' => '[email protected]',
387 'phone_number' => '0123456789',
388 'address1' => '77 rue la Boétie',
389 'address2' => 'ul',
390 'company_name' => 'PayPlug',
391 'postcode' => '75008',
392 'city' => 'Paris',
393 'state' => 'ul',
394 'country' => 'FR',
395 'language' => 'fr'
396 ),
397 'shipping' => array(
398 'title' => 'Mr',
399 'first_name' => 'John',
400 'last_name' => 'Doe',
401 'email' => '[email protected]',
402 'phone_number' => '0123456789',
403 'address1' => '77 rue la Boétie',
404 'address2' => 'ul',
405 'company_name' => 'PayPlug',
406 'postcode' => '75008',
407 'city' => 'Paris',
408 'state' => 'ul',
409 'country' => 'FR',
410 'language' => 'fr'
411 ),
412 'hosted_payment' => array(
413 'return_url' => 'https://www.example.com/thank_you_for_your_payment.html',
414 'cancel_url' => 'https://www.example.com/so_bad_it_didnt_make_it.html'
415 ),
416 'notification_url' => 'http://www.example.org/callbackURL',
417 'force_3ds' => false
418 ));
419
420 $this->assertTrue(is_array($GLOBALS['CURLOPT_POSTFIELDS_DATA']));
421 $this->assertEquals('ok', $payment->status);
422
423 unset($GLOBALS['CURLOPT_POSTFIELDS_DATA']);
424 }
425
426 public function testPaymentAbort()
427 {
428 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = null;
429
430 $this->_requestMock
431 ->expects($this->once())
432 ->method('exec')
433 ->will($this->returnValue('{"status":"ok"}'));
434
435 $this->_requestMock
436 ->expects($this->atLeastOnce())
437 ->method('setopt')
438 ->will($this->returnCallback(function($option, $value = null) {
439 switch($option) {
440 case CURLOPT_POSTFIELDS:
441 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = json_decode($value, true);
442 return true;
443 }
444 return true;
445 }));
446 $this->_requestMock
447 ->expects($this->any())
448 ->method('getinfo')
449 ->will($this->returnCallback(function($option) {
450 switch($option) {
451 case CURLINFO_HTTP_CODE:
452 return 200;
453 }
454 return null;
455 }));
456
457 $payment = Payplug\Payment::abort('a_payment_id');
458
459 $this->assertTrue(is_array($GLOBALS['CURLOPT_POSTFIELDS_DATA']));
460 $this->assertTrue($GLOBALS['CURLOPT_POSTFIELDS_DATA'] === array('aborted' => true));
461 $this->assertEquals('ok', $payment->status);
462
463 unset($GLOBALS['CURLOPT_POSTFIELDS_DATA']);
464 }
465
466 public function testPaymentCapture()
467 {
468 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = null;
469
470 $this->_requestMock
471 ->expects($this->once())
472 ->method('exec')
473 ->will($this->returnValue('{"status":"ok"}'));
474
475 $this->_requestMock
476 ->expects($this->atLeastOnce())
477 ->method('setopt')
478 ->will($this->returnCallback(function($option, $value = null) {
479 switch($option) {
480 case CURLOPT_POSTFIELDS:
481 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = json_decode($value, true);
482 return true;
483 }
484 return true;
485 }));
486 $this->_requestMock
487 ->expects($this->any())
488 ->method('getinfo')
489 ->will($this->returnCallback(function($option) {
490 switch($option) {
491 case CURLINFO_HTTP_CODE:
492 return 200;
493 }
494 return null;
495 }));
496
497 $payment = Payplug\Payment::capture('a_payment_id');
498
499 $this->assertTrue(is_array($GLOBALS['CURLOPT_POSTFIELDS_DATA']));
500 $this->assertTrue($GLOBALS['CURLOPT_POSTFIELDS_DATA'] === array('captured' => true));
501 $this->assertEquals('ok', $payment->status);
502
503 unset($GLOBALS['CURLOPT_POSTFIELDS_DATA']);
504 }
505
506 public function testPaymentRetrieve()
507 {
508 $GLOBALS['CURLOPT_URL_DATA'] = null;
509
510 $this->_requestMock
511 ->expects($this->once())
512 ->method('exec')
513 ->will($this->returnValue('{"status":"ok"}'));
514
515 $this->_requestMock
516 ->expects($this->any())
517 ->method('getinfo')
518 ->will($this->returnCallback(function($option) {
519 switch($option) {
520 case CURLINFO_HTTP_CODE:
521 return 200;
522 }
523 return null;
524 }));
525 $this->_requestMock
526 ->expects($this->any())
527 ->method('setopt')
528 ->will($this->returnCallback(function($option, $value = null) {
529 switch($option) {
530 case CURLOPT_URL:
531 $GLOBALS['CURLOPT_URL_DATA'] = $value;
532 return true;
533 }
534 return true;
535 }));
536
537 $payment = Payment::retrieve('a_payment_id');
538
539 $this->assertStringEndsWith('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
540 $this->assertEquals('ok', $payment->status);
541
542 unset($GLOBALS['CURLOPT_URL_DATA']);
543 }
544
545 public function testPaymentList()
546 {
547 $GLOBALS['CURLOPT_URL_DATA'] = null;
548
549 $this->_requestMock
550 ->expects($this->once())
551 ->method('exec')
552 ->will($this->returnValue('{"data":[{"id": "payment1"}, {"id": "payment2"}]}'));
553
554 $this->_requestMock
555 ->expects($this->any())
556 ->method('getinfo')
557 ->will($this->returnCallback(function($option) {
558 switch($option) {
559 case CURLINFO_HTTP_CODE:
560 return 200;
561 }
562 return null;
563 }));
564
565 $payments = Payment::listPayments();
566 $this->assertEquals(2, count($payments));
567 $this->assertTrue($payments[0]->id === 'payment1' || $payments[0]->id === 'payment2');
568 $this->assertTrue($payments[1]->id === 'payment1' || $payments[1]->id === 'payment2');
569 $this->assertTrue($payments[0]->id !== $payments[1]->id);
570
571
572 unset($GLOBALS['CURLOPT_URL_DATA']);
573 }
574
575 public function testPaymentPaginationList()
576 {
577 $GLOBALS['CURLOPT_URL_DATA'] = null;
578
579 $this->_requestMock
580 ->expects($this->once())
581 ->method('exec')
582 ->will($this->returnValue('{"per_page": 1, "page": 0, "data":[{"id": "payment1"}]}'));
583
584 $this->_requestMock
585 ->expects($this->any())
586 ->method('getinfo')
587 ->will($this->returnCallback(function($option) {
588 switch($option) {
589 case CURLINFO_HTTP_CODE:
590 return 200;
591 }
592 return null;
593 }));
594
595 $perPage = 1;
596 $page = 0;
597 $payments = Payment::listPayments($perPage, $page);
598
599 $this->assertEquals(1, count($payments));
600 $this->assertTrue($payments[0]->id == 'payment1');
601
602
603 unset($GLOBALS['CURLOPT_URL_DATA']);
604 }
605
606 public function testPaymentRefundWhenPaymentIsInvalid()
607 {
608 $this->setExpectedException('\PayPlug\Exception\InvalidPaymentException');
609
610 $payment = Payment::fromAttributes(array('fake' => 'payment'));
611 $payment->refund(array('amount' => 3300));
612 }
613
614 public function testPaymentRefund()
615 {
616 $GLOBALS['CURLOPT_URL_DATA'] = null;
617
618 $this->_requestMock
619 ->expects($this->once())
620 ->method('exec')
621 ->will($this->returnValue('{"status":"ok"}'));
622
623 $this->_requestMock
624 ->expects($this->any())
625 ->method('getinfo')
626 ->will($this->returnCallback(function($option) {
627 switch($option) {
628 case CURLINFO_HTTP_CODE:
629 return 200;
630 }
631 return null;
632 }));
633 $this->_requestMock
634 ->expects($this->any())
635 ->method('setopt')
636 ->will($this->returnCallback(function($option, $value = null) {
637 switch($option) {
638 case CURLOPT_URL:
639 $GLOBALS['CURLOPT_URL_DATA'] = $value;
640 return true;
641 }
642 return true;
643 }));
644
645 $payment = Payment::fromAttributes(array('id' => 'a_payment_id'));
646 $payment->refund(array('amount' => 3300));
647
648 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
649
650 unset($GLOBALS['CURLOPT_URL_DATA']);
651 }
652
653 public function testPaymentListRefundsWhenPaymentIsInvalid()
654 {
655 $this->setExpectedException('\PayPlug\Exception\InvalidPaymentException');
656
657 $payment = Payment::fromAttributes(array('fake' => 'payment'));
658 $payment->listRefunds();
659 }
660
661 public function testPaymentListRefunds()
662 {
663 $GLOBALS['CURLOPT_URL_DATA'] = null;
664
665 $this->_requestMock
666 ->expects($this->once())
667 ->method('exec')
668 ->will($this->returnValue('{"data":[{"id": "refund1"}, {"id": "refund2"}]}'));
669
670 $this->_requestMock
671 ->expects($this->any())
672 ->method('getinfo')
673 ->will($this->returnCallback(function($option) {
674 switch($option) {
675 case CURLINFO_HTTP_CODE:
676 return 200;
677 }
678 return null;
679 }));
680 $this->_requestMock
681 ->expects($this->any())
682 ->method('setopt')
683 ->will($this->returnCallback(function($option, $value = null) {
684 switch($option) {
685 case CURLOPT_URL:
686 $GLOBALS['CURLOPT_URL_DATA'] = $value;
687 return true;
688 }
689 return true;
690 }));
691
692 $payment = Payment::fromAttributes(array('id' => 'a_payment_id'));
693 $refunds = $payment->listRefunds();
694
695 $this->assertEquals(2, count($refunds));
696 $this->assertTrue($refunds[0]->id === 'refund1' || $refunds[0]->id === 'refund2');
697 $this->assertTrue($refunds[1]->id === 'refund1' || $refunds[1]->id === 'refund2');
698 $this->assertTrue($refunds[0]->id !== $refunds[1]->id);
699
700 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
701
702 unset($GLOBALS['CURLOPT_URL_DATA']);
703 }
704
705 public function testRetrieveConsistentPaymentWhenIdIsUndefined()
706 {
707 $this->setExpectedException('\PayPlug\Exception\UndefinedAttributeException');
708
709 $payment = Payment::fromAttributes(array('this_payment' => 'has_no_id'));
710 $payment->getConsistentResource();
711 }
712
713 public function testRetrieveConsistentPayment()
714 {
715 function testRetrieveConsistentPayment_getinfo($option) {
716 switch($option) {
717 case CURLINFO_HTTP_CODE:
718 return 200;
719 }
720 return null;
721 }
722
723 $this->_requestMock
724 ->expects($this->once())
725 ->method('exec')
726 ->will($this->returnValue('{"id": "pay_345"}'));
727
728 $this->_requestMock
729 ->expects($this->any())
730 ->method('setopt')
731 ->will($this->returnValue(true));
732 $this->_requestMock
733 ->expects($this->any())
734 ->method('getinfo')
735 ->will($this->returnCallback(function($option) {
736 switch($option) {
737 case CURLINFO_HTTP_CODE:
738 return 200;
739 }
740 return null;
741 }));
742
743 $payment1 = Payment::fromAttributes(array('id' => 'pay_123'));
744 $payment2 = $payment1->getConsistentResource($this->_configuration);
745
746 $this->assertEquals('pay_123', $payment1->id);
747 $this->assertEquals('pay_345', $payment2->id);
748 }
749 }
750