| 1 |
<?php |
| 2 |
namespace Payplug; |
| 3 |
|
| 4 |
class APIResourceMock extends Resource\APIResource |
| 5 |
{ |
| 6 |
static function fromAttributes(array $attributes) |
| 7 |
{ |
| 8 |
$object = new APIResourceMock(); |
| 9 |
$object->initialize($attributes); |
| 10 |
return $object; |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* @group unit |
| 16 |
* @group ci |
| 17 |
* @group recommended |
| 18 |
*/ |
| 19 |
class APIResourceTest extends \PHPUnit_Framework_TestCase |
| 20 |
{ |
| 21 |
private $_myApiResource = null; |
| 22 |
|
| 23 |
protected function setUp() |
| 24 |
{ |
| 25 |
$this->_myApiResource = APIResourceMock::fromAttributes(array( |
| 26 |
'attr1' => 'val_attr1', |
| 27 |
'attr2' => 'val_attr2' |
| 28 |
)); |
| 29 |
} |
| 30 |
|
| 31 |
public function testThrowsExceptionWhenKeyDoesNotExist() |
| 32 |
{ |
| 33 |
$this->setExpectedException('\PayPlug\Exception\UndefinedAttributeException'); |
| 34 |
$this->_myApiResource->an_undefined_attribute; |
| 35 |
} |
| 36 |
|
| 37 |
public function testIssetReturnsTrueWhenPropertyExists() |
| 38 |
{ |
| 39 |
$this->assertTrue(isset($this->_myApiResource->attr1)); |
| 40 |
} |
| 41 |
|
| 42 |
public function testIssetReturnsFalseWhenPropertyDoesNotExist() |
| 43 |
{ |
| 44 |
$this->assertFalse(isset($this->_myApiResource->an_undefined_attribute)); |
| 45 |
} |
| 46 |
|
| 47 |
public function testPaymentFromAPIResourceFactory() |
| 48 |
{ |
| 49 |
$attributes = array( |
| 50 |
'id' => 'pay_123', |
| 51 |
'object' => 'payment' |
| 52 |
); |
| 53 |
$payment = Resource\APIResource::factory($attributes); |
| 54 |
$this->assertTrue($payment instanceof Resource\Payment); |
| 55 |
$this->assertEquals('pay_123', $payment->id); |
| 56 |
} |
| 57 |
|
| 58 |
public function testRefundFromAPIResourceFactory() |
| 59 |
{ |
| 60 |
$attributes = array( |
| 61 |
'id' => 're_123', |
| 62 |
'object' => 'refund' |
| 63 |
); |
| 64 |
$refund = Resource\APIResource::factory($attributes); |
| 65 |
$this->assertTrue($refund instanceof Resource\Refund); |
| 66 |
$this->assertEquals('re_123', $refund->id); |
| 67 |
} |
| 68 |
|
| 69 |
public function testAPIResourceFactoryWhenObjectIsNotDefined() |
| 70 |
{ |
| 71 |
$this->setExpectedException('\PayPlug\Exception\UnknownAPIResourceException'); |
| 72 |
$attributes = array( |
| 73 |
'id' => 'a_random_object' |
| 74 |
); |
| 75 |
Resource\APIResource::factory($attributes); |
| 76 |
} |
| 77 |
|
| 78 |
public function testAPIResourceFactoryWhenObjectIsUnknown() |
| 79 |
{ |
| 80 |
$this->setExpectedException('\PayPlug\Exception\UnknownAPIResourceException'); |
| 81 |
$attributes = array( |
| 82 |
'id' => 'a_random_object', |
| 83 |
'object' => 'an_unknown_object' |
| 84 |
); |
| 85 |
Resource\APIResource::factory($attributes); |
| 86 |
} |
| 87 |
} |
| 88 |
|