|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EasyPost\Test; |
| 4 | + |
| 5 | +use EasyPost\Constant\Constants; |
| 6 | +use EasyPost\EasyPostClient; |
| 7 | +use EasyPost\EasyPostObject; |
| 8 | +use EasyPost\Test\Mocking\MockingUtility; |
| 9 | +use EasyPost\Test\Mocking\MockRequest; |
| 10 | +use EasyPost\Test\Mocking\MockRequestMatchRule; |
| 11 | +use EasyPost\Test\Mocking\MockRequestResponseInfo; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class FedExRegistrationTest extends TestCase |
| 15 | +{ |
| 16 | + private static EasyPostClient $client; |
| 17 | + |
| 18 | + /** |
| 19 | + * Setup the testing environment for this file. |
| 20 | + */ |
| 21 | + public static function setUpBeforeClass(): void |
| 22 | + { |
| 23 | + $mockingUtility = new MockingUtility( |
| 24 | + [ |
| 25 | + new MockRequest( |
| 26 | + new MockRequestMatchRule( |
| 27 | + 'post', |
| 28 | + '/v2\/fedex_registrations\/\S*\/address$/' |
| 29 | + ), |
| 30 | + new MockRequestResponseInfo( |
| 31 | + 200, |
| 32 | + '{"email_address":null,"options":["SMS","CALL","INVOICE"],"phone_number":"***-***-9721"}' |
| 33 | + ) |
| 34 | + ), |
| 35 | + new MockRequest( |
| 36 | + new MockRequestMatchRule( |
| 37 | + 'post', |
| 38 | + '/v2\/fedex_registrations\/\S*\/pin$/' |
| 39 | + ), |
| 40 | + new MockRequestResponseInfo( |
| 41 | + 200, |
| 42 | + '{"message":"sent secured Pin"}' |
| 43 | + ) |
| 44 | + ), |
| 45 | + new MockRequest( |
| 46 | + new MockRequestMatchRule( |
| 47 | + 'post', |
| 48 | + '/v2\/fedex_registrations\/\S*\/pin\/validate$/' |
| 49 | + ), |
| 50 | + new MockRequestResponseInfo( |
| 51 | + 200, |
| 52 | + '{"id":"ca_123","type":"FedexAccount",' . |
| 53 | + '"credentials":{"account_number":"123456789","mfa_key":"123456789-XXXXX"}}' |
| 54 | + ) |
| 55 | + ), |
| 56 | + new MockRequest( |
| 57 | + new MockRequestMatchRule( |
| 58 | + 'post', |
| 59 | + '/v2\/fedex_registrations\/\S*\/invoice$/' |
| 60 | + ), |
| 61 | + new MockRequestResponseInfo( |
| 62 | + 200, |
| 63 | + '{"id":"ca_123","type":"FedexAccount",' . |
| 64 | + '"credentials":{"account_number":"123456789","mfa_key":"123456789-XXXXX"}}' |
| 65 | + ) |
| 66 | + ), |
| 67 | + ] |
| 68 | + ); |
| 69 | + |
| 70 | + self::$client = new EasyPostClient( |
| 71 | + (string)getenv('EASYPOST_TEST_API_KEY'), |
| 72 | + Constants::TIMEOUT, |
| 73 | + Constants::API_BASE, |
| 74 | + $mockingUtility |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test registering a billing address. |
| 80 | + */ |
| 81 | + public function testRegisterAddress(): void |
| 82 | + { |
| 83 | + $fedexAccountNumber = '123456789'; |
| 84 | + $params = [ |
| 85 | + 'address_validation' => [ |
| 86 | + 'name' => 'BILLING NAME', |
| 87 | + 'street1' => '1234 BILLING STREET', |
| 88 | + 'city' => 'BILLINGCITY', |
| 89 | + 'state' => 'ST', |
| 90 | + 'postal_code' => '12345', |
| 91 | + 'country_code' => 'US', |
| 92 | + ], |
| 93 | + 'easypost_details' => [ |
| 94 | + 'carrier_account_id' => 'ca_123', |
| 95 | + ], |
| 96 | + ]; |
| 97 | + |
| 98 | + $response = self::$client->fedexRegistration->registerAddress($fedexAccountNumber, $params); |
| 99 | + |
| 100 | + $this->assertInstanceOf(EasyPostObject::class, $response); |
| 101 | + $this->assertNull($response->email_address); // @phpstan-ignore-line |
| 102 | + $this->assertNotNull($response->options); // @phpstan-ignore-line |
| 103 | + $this->assertContains('SMS', $response->options); |
| 104 | + $this->assertContains('CALL', $response->options); |
| 105 | + $this->assertContains('INVOICE', $response->options); |
| 106 | + $this->assertEquals('***-***-9721', $response->phone_number); // @phpstan-ignore-line |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Test requesting a pin. |
| 111 | + */ |
| 112 | + public function testRequestPin(): void |
| 113 | + { |
| 114 | + $fedexAccountNumber = '123456789'; |
| 115 | + |
| 116 | + $response = self::$client->fedexRegistration->requestPin($fedexAccountNumber, 'SMS'); |
| 117 | + |
| 118 | + $this->assertInstanceOf(EasyPostObject::class, $response); |
| 119 | + $this->assertEquals('sent secured Pin', $response->message); // @phpstan-ignore-line |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Test validating a pin. |
| 124 | + */ |
| 125 | + public function testValidatePin(): void |
| 126 | + { |
| 127 | + $fedexAccountNumber = '123456789'; |
| 128 | + $params = [ |
| 129 | + 'pin_validation' => [ |
| 130 | + 'pin_code' => '123456', |
| 131 | + 'name' => 'BILLING NAME', |
| 132 | + ], |
| 133 | + 'easypost_details' => [ |
| 134 | + 'carrier_account_id' => 'ca_123', |
| 135 | + ], |
| 136 | + ]; |
| 137 | + |
| 138 | + $response = self::$client->fedexRegistration->validatePin($fedexAccountNumber, $params); |
| 139 | + |
| 140 | + $this->assertInstanceOf(EasyPostObject::class, $response); |
| 141 | + $this->assertEquals('ca_123', $response->id); // @phpstan-ignore-line |
| 142 | + $this->assertEquals('FedexAccount', $response->type); // @phpstan-ignore-line |
| 143 | + $this->assertNotNull($response->credentials); // @phpstan-ignore-line |
| 144 | + $this->assertEquals('123456789', $response->credentials['account_number']); |
| 145 | + $this->assertEquals('123456789-XXXXX', $response->credentials['mfa_key']); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Test submitting details about an invoice. |
| 150 | + */ |
| 151 | + public function testSubmitInvoice(): void |
| 152 | + { |
| 153 | + $fedexAccountNumber = '123456789'; |
| 154 | + $params = [ |
| 155 | + 'invoice_validation' => [ |
| 156 | + 'name' => 'BILLING NAME', |
| 157 | + 'invoice_number' => 'INV-12345', |
| 158 | + 'invoice_date' => '2025-12-08', |
| 159 | + 'invoice_amount' => '100.00', |
| 160 | + 'invoice_currency' => 'USD', |
| 161 | + ], |
| 162 | + 'easypost_details' => [ |
| 163 | + 'carrier_account_id' => 'ca_123', |
| 164 | + ], |
| 165 | + ]; |
| 166 | + |
| 167 | + $response = self::$client->fedexRegistration->submitInvoice($fedexAccountNumber, $params); |
| 168 | + |
| 169 | + $this->assertInstanceOf(EasyPostObject::class, $response); |
| 170 | + $this->assertEquals('ca_123', $response->id); // @phpstan-ignore-line |
| 171 | + $this->assertEquals('FedexAccount', $response->type); // @phpstan-ignore-line |
| 172 | + $this->assertNotNull($response->credentials); // @phpstan-ignore-line |
| 173 | + $this->assertEquals('123456789', $response->credentials['account_number']); |
| 174 | + $this->assertEquals('123456789-XXXXX', $response->credentials['mfa_key']); |
| 175 | + } |
| 176 | +} |
0 commit comments