PluginProbe
PayPlug for WooCommerce (Official) / 1.0.8
PayPlug for WooCommerce (Official) v1.0.8
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.0.8, at vendor/payplug/payplug-php/tests/unit_tests/Resource/RefundTest.php

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