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