|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EasyPost\Test; |
| 4 | + |
| 5 | +use EasyPost\EasyPostClient; |
| 6 | +use EasyPost\Exception\General\EndOfPaginationException; |
| 7 | +use EasyPost\Claim; |
| 8 | +use Exception; |
| 9 | + |
| 10 | +class ClaimTest extends \PHPUnit\Framework\TestCase |
| 11 | +{ |
| 12 | + private static EasyPostClient $client; |
| 13 | + |
| 14 | + /** |
| 15 | + * Setup the testing environment for this file. |
| 16 | + */ |
| 17 | + public static function setUpBeforeClass(): void |
| 18 | + { |
| 19 | + TestUtil::setupVcrTests(); |
| 20 | + self::$client = new EasyPostClient(getenv('EASYPOST_TEST_API_KEY')); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Cleanup the testing environment once finished. |
| 25 | + */ |
| 26 | + public static function tearDownAfterClass(): void |
| 27 | + { |
| 28 | + TestUtil::teardownVcrTests(); |
| 29 | + } |
| 30 | + |
| 31 | +/** |
| 32 | + * Helper method to create and purchase an insured shipment. |
| 33 | + * |
| 34 | + * @param string $amount The amount of insurance for the shipment. |
| 35 | + * @return \EasyPost\Shipment The purchased shipment object. |
| 36 | + */ |
| 37 | + private static function createAndBuyShipment(string $amount): \EasyPost\Shipment |
| 38 | + { |
| 39 | + $shipment = self::$client->shipment->create(Fixture::fullShipment()); |
| 40 | + return self::$client->shipment->buy( |
| 41 | + $shipment->id, |
| 42 | + [ |
| 43 | + 'rate' => $shipment->lowestRate(), |
| 44 | + 'insurance' => $amount, |
| 45 | + ] |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * Test creating an claim object. |
| 52 | + */ |
| 53 | + public function testCreate(): void |
| 54 | + { |
| 55 | + TestUtil::setupCassette('claim/create.yml'); |
| 56 | + $amount = '100'; |
| 57 | + $purchasedShipment = self::createAndBuyShipment($amount); |
| 58 | + |
| 59 | + $claimData = Fixture::basicClaimData(); |
| 60 | + $claimData['tracking_code'] = $purchasedShipment->tracking_code; |
| 61 | + $claimData['amount'] = $amount; |
| 62 | + |
| 63 | + $claim = self::$client->claim->create($claimData); |
| 64 | + |
| 65 | + $this->assertInstanceOf(Claim::class, $claim); |
| 66 | + $this->assertStringMatchesFormat('clm_%s', $claim->id); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Test retrieving an claim object. |
| 71 | + */ |
| 72 | + public function testRetrieve(): void |
| 73 | + { |
| 74 | + TestUtil::setupCassette('claim/retrieve.yml'); |
| 75 | + $amount = '100'; |
| 76 | + $purchasedShipment = self::createAndBuyShipment($amount); |
| 77 | + |
| 78 | + $claimData = Fixture::basicClaimData(); |
| 79 | + $claimData['tracking_code'] = $purchasedShipment->tracking_code; |
| 80 | + $claimData['amount'] = $amount; |
| 81 | + |
| 82 | + $claim = self::$client->claim->create($claimData); |
| 83 | + $retrievedClaim = self::$client->claim->retrieve($claim->id); |
| 84 | + |
| 85 | + $this->assertInstanceOf(Claim::class, $claim); |
| 86 | + $this->assertStringMatchesFormat('clm_%s', $claim->id); |
| 87 | + $this->assertEquals($claim->id, $retrievedClaim->id); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test retrieving all claims. |
| 92 | + */ |
| 93 | + public function testAll(): void |
| 94 | + { |
| 95 | + TestUtil::setupCassette('claim/all.yml'); |
| 96 | + |
| 97 | + $claim = self::$client->claim->all([ |
| 98 | + 'page_size' => Fixture::pageSize(), |
| 99 | + ]); |
| 100 | + |
| 101 | + $claimArray = $claim['claims']; |
| 102 | + |
| 103 | + $this->assertLessThanOrEqual($claimArray, Fixture::pageSize()); |
| 104 | + $this->assertNotNull($claim['has_more']); |
| 105 | + $this->assertContainsOnlyInstancesOf(Claim::class, $claimArray); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Test retrieving next page. |
| 110 | + */ |
| 111 | + public function testGetNextPage(): void |
| 112 | + { |
| 113 | + TestUtil::setupCassette('claim/getNextPage.yml'); |
| 114 | + |
| 115 | + try { |
| 116 | + $claims = self::$client->claim->all([ |
| 117 | + 'page_size' => Fixture::pageSize(), |
| 118 | + ]); |
| 119 | + $nextPage = self::$client->claim->getNextPage($claims, Fixture::pageSize()); |
| 120 | + |
| 121 | + $firstIdOfFirstPage = $claims['claims'][0]->id; |
| 122 | + $secondIdOfSecondPage = $nextPage['claims'][0]->id; |
| 123 | + |
| 124 | + $this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage); |
| 125 | + $this->assertNotNull($nextPage['_params']); |
| 126 | + } catch (EndOfPaginationException $error) { |
| 127 | + // There's no second page, that's not a failure |
| 128 | + $this->assertTrue(true); |
| 129 | + } catch (Exception $error) { |
| 130 | + throw $error; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Test cancelling a filed claim. |
| 136 | + */ |
| 137 | + public function testRefund(): void |
| 138 | + { |
| 139 | + TestUtil::setupCassette('claim/cancel.yml'); |
| 140 | + $amount = '100'; |
| 141 | + $purchasedShipment = self::createAndBuyShipment($amount); |
| 142 | + |
| 143 | + $claimData = Fixture::basicClaimData(); |
| 144 | + $claimData['tracking_code'] = $purchasedShipment->tracking_code; |
| 145 | + $claimData['amount'] = $amount; |
| 146 | + |
| 147 | + $claim = self::$client->claim->create($claimData); |
| 148 | + $cancelledClaim = self::$client->claim->cancel($claim->id); |
| 149 | + |
| 150 | + $this->assertInstanceOf(Claim::class, $cancelledClaim); |
| 151 | + $this->assertStringMatchesFormat('clm_%s', $cancelledClaim->id); |
| 152 | + $this->assertEquals('cancelled', $cancelledClaim->status); |
| 153 | + } |
| 154 | +} |
0 commit comments