-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Claims service * fix lint
- Loading branch information
Showing
15 changed files
with
1,312 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace EasyPost; | ||
|
||
/** | ||
* @package EasyPost | ||
* @property string $id | ||
* @property string $approved_amount | ||
* @property string[] $attachments | ||
* @property string $check_delivery_address | ||
* @property string $contact_email | ||
* @property string $description | ||
* @property object $history | ||
* @property string $insurance_amount | ||
* @property string $insurance_id | ||
* @property string $payment_method | ||
* @property string $recipient_name | ||
* @property string $requested_amount | ||
* @property string $salvage_value | ||
* @property string $shipment_id | ||
* @property string $status | ||
* @property string $status_detail | ||
* @property string $status_timestamp | ||
* @property string $tracking_code | ||
* @property string $type | ||
*/ | ||
class Claim extends EasyPostObject | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace EasyPost\Service; | ||
|
||
use EasyPost\Http\Requestor; | ||
use EasyPost\Util\InternalUtil; | ||
|
||
/** | ||
* Claim service containing all the logic to make API calls. | ||
*/ | ||
class ClaimService extends BaseService | ||
{ | ||
/** | ||
* Create an claim object. | ||
* | ||
* @param mixed $params | ||
* @return mixed | ||
*/ | ||
public function create(mixed $params = null): mixed | ||
{ | ||
return self::createResource(self::serviceModelClassName(self::class), $params); | ||
} | ||
|
||
/** | ||
* Retrieve an claim object. | ||
* | ||
* @param string $id | ||
* @return mixed | ||
*/ | ||
public function retrieve(string $id): mixed | ||
{ | ||
return self::retrieveResource(self::serviceModelClassName(self::class), $id); | ||
} | ||
|
||
/** | ||
* Retrieve all claim objects. | ||
* | ||
* @param mixed $params | ||
* @return mixed | ||
*/ | ||
public function all(mixed $params = null): mixed | ||
{ | ||
return self::allResources(self::serviceModelClassName(self::class), $params); | ||
} | ||
|
||
/** | ||
* Retrieve the next page of claim collection. | ||
* | ||
* @param mixed $claims | ||
* @param int|null $pageSize | ||
* @return mixed | ||
*/ | ||
public function getNextPage(mixed $claims, ?int $pageSize = null): mixed | ||
{ | ||
return $this->getNextPageResources(self::serviceModelClassName(self::class), $claims, $pageSize); | ||
} | ||
|
||
/** | ||
* Cancel a claim object. | ||
* | ||
* @param string $id | ||
* @return mixed | ||
*/ | ||
public function cancel(string $id): mixed | ||
{ | ||
$response = Requestor::request($this->client, 'post', "/claims/{$id}/cancel"); | ||
|
||
return InternalUtil::convertToEasyPostObject($this->client, $response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
namespace EasyPost\Test; | ||
|
||
use EasyPost\EasyPostClient; | ||
use EasyPost\Exception\General\EndOfPaginationException; | ||
use EasyPost\Claim; | ||
use Exception; | ||
|
||
class ClaimTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
private static EasyPostClient $client; | ||
|
||
/** | ||
* Setup the testing environment for this file. | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
TestUtil::setupVcrTests(); | ||
self::$client = new EasyPostClient(getenv('EASYPOST_TEST_API_KEY')); | ||
} | ||
|
||
/** | ||
* Cleanup the testing environment once finished. | ||
*/ | ||
public static function tearDownAfterClass(): void | ||
{ | ||
TestUtil::teardownVcrTests(); | ||
} | ||
|
||
/** | ||
* Helper method to create and purchase an insured shipment. | ||
* | ||
* @param string $amount The amount of insurance for the shipment. | ||
* @return \EasyPost\Shipment The purchased shipment object. | ||
*/ | ||
private static function createAndBuyShipment(string $amount): \EasyPost\Shipment | ||
{ | ||
$shipment = self::$client->shipment->create(Fixture::fullShipment()); | ||
return self::$client->shipment->buy( | ||
$shipment->id, | ||
[ | ||
'rate' => $shipment->lowestRate(), | ||
'insurance' => $amount, | ||
] | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Test creating an claim object. | ||
*/ | ||
public function testCreate(): void | ||
{ | ||
TestUtil::setupCassette('claim/create.yml'); | ||
$amount = '100'; | ||
$purchasedShipment = self::createAndBuyShipment($amount); | ||
|
||
$claimData = Fixture::basicClaimData(); | ||
$claimData['tracking_code'] = $purchasedShipment->tracking_code; | ||
$claimData['amount'] = $amount; | ||
|
||
$claim = self::$client->claim->create($claimData); | ||
|
||
$this->assertInstanceOf(Claim::class, $claim); | ||
$this->assertStringMatchesFormat('clm_%s', $claim->id); | ||
} | ||
|
||
/** | ||
* Test retrieving an claim object. | ||
*/ | ||
public function testRetrieve(): void | ||
{ | ||
TestUtil::setupCassette('claim/retrieve.yml'); | ||
$amount = '100'; | ||
$purchasedShipment = self::createAndBuyShipment($amount); | ||
|
||
$claimData = Fixture::basicClaimData(); | ||
$claimData['tracking_code'] = $purchasedShipment->tracking_code; | ||
$claimData['amount'] = $amount; | ||
|
||
$claim = self::$client->claim->create($claimData); | ||
$retrievedClaim = self::$client->claim->retrieve($claim->id); | ||
|
||
$this->assertInstanceOf(Claim::class, $claim); | ||
$this->assertStringMatchesFormat('clm_%s', $claim->id); | ||
$this->assertEquals($claim->id, $retrievedClaim->id); | ||
} | ||
|
||
/** | ||
* Test retrieving all claims. | ||
*/ | ||
public function testAll(): void | ||
{ | ||
TestUtil::setupCassette('claim/all.yml'); | ||
|
||
$claim = self::$client->claim->all([ | ||
'page_size' => Fixture::pageSize(), | ||
]); | ||
|
||
$claimArray = $claim['claims']; | ||
|
||
$this->assertLessThanOrEqual($claimArray, Fixture::pageSize()); | ||
$this->assertNotNull($claim['has_more']); | ||
$this->assertContainsOnlyInstancesOf(Claim::class, $claimArray); | ||
} | ||
|
||
/** | ||
* Test retrieving next page. | ||
*/ | ||
public function testGetNextPage(): void | ||
{ | ||
TestUtil::setupCassette('claim/getNextPage.yml'); | ||
|
||
try { | ||
$claims = self::$client->claim->all([ | ||
'page_size' => Fixture::pageSize(), | ||
]); | ||
$nextPage = self::$client->claim->getNextPage($claims, Fixture::pageSize()); | ||
|
||
$firstIdOfFirstPage = $claims['claims'][0]->id; | ||
$secondIdOfSecondPage = $nextPage['claims'][0]->id; | ||
|
||
$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage); | ||
$this->assertNotNull($nextPage['_params']); | ||
} catch (EndOfPaginationException $error) { | ||
// There's no second page, that's not a failure | ||
$this->assertTrue(true); | ||
} catch (Exception $error) { | ||
throw $error; | ||
} | ||
} | ||
|
||
/** | ||
* Test cancelling a filed claim. | ||
*/ | ||
public function testRefund(): void | ||
{ | ||
TestUtil::setupCassette('claim/cancel.yml'); | ||
$amount = '100'; | ||
$purchasedShipment = self::createAndBuyShipment($amount); | ||
|
||
$claimData = Fixture::basicClaimData(); | ||
$claimData['tracking_code'] = $purchasedShipment->tracking_code; | ||
$claimData['amount'] = $amount; | ||
|
||
$claim = self::$client->claim->create($claimData); | ||
$cancelledClaim = self::$client->claim->cancel($claim->id); | ||
|
||
$this->assertInstanceOf(Claim::class, $cancelledClaim); | ||
$this->assertStringMatchesFormat('clm_%s', $cancelledClaim->id); | ||
$this->assertEquals('cancelled', $cancelledClaim->status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.