Skip to content

Commit

Permalink
fix: corrects JSON encoding for various data types (#118)
Browse files Browse the repository at this point in the history
* fix: corrects JSON encoding for various data types

* fix: address feedback

* fix: address feedback

* fix: remove extra line

* fix: address feedback

* fix: remove trailing ? from GET urls
  • Loading branch information
Justintime50 committed Oct 20, 2021
1 parent e49dd50 commit 14d3220
Show file tree
Hide file tree
Showing 25 changed files with 702 additions and 587 deletions.
24 changes: 11 additions & 13 deletions lib/EasyPost/Requestor.php
Expand Up @@ -55,11 +55,9 @@ public static function utf8($value)
*/
private static function _encodeObjects($data)
{
if (!$data) {
$data = array();
}

if ($data instanceof EasypostResource) {
if (is_null($data)) {
return array();
} elseif ($data instanceof EasypostResource) {
return array("id" => self::utf8($data->id));
} elseif ($data === true) {
return 'true';
Expand All @@ -68,12 +66,14 @@ private static function _encodeObjects($data)
} elseif (is_array($data)) {
$resource = array();
foreach ($data as $k => $v) {
$resource[$k] = self::_encodeObjects($v);
if (!is_null($v) and ($v !== "") and (!is_array($v) or !empty($v))) {
$resource[$k] = self::_encodeObjects($v);
}
}

return array_filter($resource);
return $resource;
} else {
return self::utf8($data);
return self::utf8(strval($data));
}
}

Expand Down Expand Up @@ -197,7 +197,7 @@ private function _curlRequest($method, $absUrl, $headers, $params, $myApiKey)
// Setup the HTTP method and params to use on the request
if ($method == 'get') {
$curlOptions[CURLOPT_HTTPGET] = 1;
if (count($params) > 0) {
if (isset($params) && !empty($params)) {
$urlParams = self::_urlEncode($params);
$absUrl = "$absUrl?$urlParams";
}
Expand All @@ -206,12 +206,10 @@ private function _curlRequest($method, $absUrl, $headers, $params, $myApiKey)
$curlOptions[CURLOPT_POSTFIELDS] = json_encode($params);
} elseif ($method == 'patch' || $method == 'put') {
$curlOptions[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
if (count($params) > 0) {
$curlOptions[CURLOPT_POSTFIELDS] = json_encode($params);
}
$curlOptions[CURLOPT_POSTFIELDS] = json_encode($params);
} elseif ($method == 'delete') {
$curlOptions[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
if (count($params) > 0) {
if (isset($params) && !empty($params)) {
$urlParams = self::_urlEncode($params);
$absUrl = "$absUrl?$urlParams";
}
Expand Down
28 changes: 28 additions & 0 deletions test/EasyPost/Test/AddressTest.php
Expand Up @@ -74,4 +74,32 @@ public function testRetrieve(Address $address)
$this->assertEquals($retrieved_address->id, $address->id);
$this->assertEquals($retrieved_address, $address);
}

/**
* Test the creation of a verified address
* We purposefully pass in slightly incorrect data to get the corrected address back once verified
*
* @return void
*/
public function testCreateVerify()
{
VCR::insertCassette('addresses/createVerify.yml');

$address = Address::create(array(
"verify" => array(true),
"street1" => "417 montgomery streat",
"street2" => "FL 5",
"city" => "San Francisco",
"state" => "CA",
"zip" => "94104",
"country" => "US",
"company" => "EasyPost",
"phone" => "415-123-4567"
));

$this->assertInstanceOf('\EasyPost\Address', $address);
$this->assertIsString($address->id);
$this->assertStringMatchesFormat('adr_%s', $address->id);
$this->assertEquals($address->street1, '417 MONTGOMERY ST STE 500');
}
}
4 changes: 2 additions & 2 deletions test/EasyPost/Test/ParcelTest.php
Expand Up @@ -44,13 +44,13 @@ public function testCreate()
"length" => "10",
"width" => "8",
"height" => "4",
"weight" => "15",
"weight" => 15.4,
));

$this->assertInstanceOf('\EasyPost\Parcel', $parcel);
$this->assertIsString($parcel->id);
$this->assertStringMatchesFormat('prcl_%s', $parcel->id);
$this->assertEquals($parcel->weight, '15');
$this->assertEquals($parcel->weight, 15.4);

// Return so the `retrieve` test can reuse this object
return $parcel;
Expand Down
22 changes: 12 additions & 10 deletions test/EasyPost/Test/ReportTest.php
Expand Up @@ -7,6 +7,8 @@
use EasyPost\EasyPost;

EasyPost::setApiKey(getenv('API_KEY'));
define('REPORT_START_DATE', '2021-01-03');
define('REPORT_END_DATE', '2021-01-04');

class ReportTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -41,8 +43,8 @@ public function testCreatePaymentLogReport()
VCR::insertCassette('reports/createPaymentLogReport.yml');

$payment_log_report = Report::create(array(
"start_date" => "2021-01-02",
"end_date" => "2021-01-03",
"start_date" => REPORT_START_DATE,
"end_date" => REPORT_END_DATE,
"type" => "payment_log"
));

Expand All @@ -64,8 +66,8 @@ public function testCreateRefundReport()
VCR::insertCassette('reports/createRefundReport.yml');

$refund_report = Report::create(array(
"start_date" => "2021-01-02",
"end_date" => "2021-01-03",
"start_date" => REPORT_START_DATE,
"end_date" => REPORT_END_DATE,
"type" => "refund"
));

Expand All @@ -87,8 +89,8 @@ public function testCreateShipmentReport()
VCR::insertCassette('reports/createShipmentReport.yml');

$shipment_report = Report::create(array(
"start_date" => "2021-01-02",
"end_date" => "2021-01-03",
"start_date" => REPORT_START_DATE,
"end_date" => REPORT_END_DATE,
"type" => "shipment"
));

Expand All @@ -110,8 +112,8 @@ public function testCreateShipmentInvoiceReport()
VCR::insertCassette('reports/createShipmentInvoiceReport.yml');

$shipment_invoice_report = Report::create(array(
"start_date" => "2021-01-02",
"end_date" => "2021-01-03",
"start_date" => REPORT_START_DATE,
"end_date" => REPORT_END_DATE,
"type" => "shipment_invoice"
));

Expand All @@ -133,8 +135,8 @@ public function testCreateTrackerReport()
VCR::insertCassette('reports/createTrackerReport.yml');

$tracker_report = Report::create(array(
"start_date" => "2021-01-02",
"end_date" => "2021-01-03",
"start_date" => REPORT_START_DATE,
"end_date" => REPORT_END_DATE,
"type" => "tracker"
));

Expand Down
16 changes: 11 additions & 5 deletions test/EasyPost/Test/ShipmentTest.php
Expand Up @@ -81,14 +81,18 @@ public function testCreate()
),
),
"options" => array(
"label_format" => "PDF",
"label_format" => "PDF",
"invoice_number" => 123 // Tests that we encode integers to strings where appropriate
),
"reference" => 123 // Tests that we encode integers to strings where appropriate
));

$this->assertInstanceOf('\EasyPost\Shipment', $shipment);
$this->assertIsString($shipment->id);
$this->assertStringMatchesFormat('shp_%s', $shipment->id);
$this->assertEquals($shipment->options->label_format, 'PDF');
$this->assertEquals($shipment->options->invoice_number, '123');
$this->assertEquals($shipment->reference, '123');

// Return so the `retrieve` test can reuse this object
return $shipment;
Expand Down Expand Up @@ -172,11 +176,11 @@ public function testSmartrate()
$this->assertEquals($shipment->rates[0]['id'], $smartrates[0]['id']);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_50'], 1);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_75'], 1);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_85'], 1);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_85'], 2);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_90'], 2);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_95'], 2);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_97'], 3);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_99'], 6);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_99'], 3);
}

/**
Expand Down Expand Up @@ -214,13 +218,15 @@ public function testCreateEmptyObjects()
),
"options" => null,
"tax_identifiers" => null,
"reference" => "",
));

$this->assertInstanceOf('\EasyPost\Shipment', $shipment);
$this->assertIsString($shipment->id);
$this->assertStringMatchesFormat('shp_%s', $shipment->id);
$this->assertNotEmpty($shipment->options); // The EasyPost API populates some default values here
$this->assertNull($shipment->customs_info);
$this->assertEmpty($shipment->customs_info);
$this->assertNull($shipment->reference);
}

/**
Expand Down Expand Up @@ -266,6 +272,6 @@ public function testCreateTaxIdentifiers()
$this->assertInstanceOf('\EasyPost\Shipment', $shipment);
$this->assertIsString($shipment->id);
$this->assertStringMatchesFormat('shp_%s', $shipment->id);
$this->assertNotEmpty($shipment->tax_identifiers);
$this->assertEquals($shipment->tax_identifiers[0]['tax_id_type'], 'IOSS');
}
}
52 changes: 26 additions & 26 deletions test/cassettes/addresses/create.yml
Expand Up @@ -8,7 +8,7 @@
Accept: application/json
Authorization: ''
Content-Type: application/json
User-Agent: 'EasyPost/v2 PhpClient/3.6.0'
User-Agent: ''
X-Client-User-Agent: ''
EasyPost-Version: '2'
body: '{"address":{"street1":"388 Townsend St","street2":"Apt 20","city":"San Francisco","state":"CA","zip":"94107"}}'
Expand All @@ -24,22 +24,22 @@
x-download-options: noopen
x-permitted-cross-domain-policies: none
referrer-policy: strict-origin-when-cross-origin
x-ep-request-uuid: 8d29b69361578bbbe78bb42b003aed86
x-ep-request-uuid: bee465606170496ce786b3e00015d209
cache-control: 'no-cache, no-store'
pragma: no-cache
expires: '0'
location: /api/v2/addresses/adr_059da1d447734818bee9a1a2fa429230
location: /api/v2/addresses/adr_aef9c7338a384dd0b75c0e0239cd35a1
content-type: 'application/json; charset=utf-8'
content-length: '429'
etag: 'W/"8a84928b0f256fa81df9d80b69ef7c60"'
x-request-id: 025de43a-2840-4d07-bdd0-99543de1234c
x-runtime: '0.033085'
x-node: bigweb5nuq
x-version-label: easypost-202110011858-559f609973-master
etag: 'W/"2e12bf9f14c69813dba0b8e1710f2d10"'
x-request-id: 75eca947-e794-4f43-ba59-b60e34338bd2
x-runtime: '0.048301'
x-node: bigweb4nuq
x-version-label: easypost-202110192143-cc149f31de-master
x-backend: easypost
x-proxied: ['intlb1nuq d40607e4ab', 'extlb2nuq d40607e4ab']
x-proxied: ['intlb2nuq d40607e4ab', 'extlb1nuq d40607e4ab']
strict-transport-security: 'max-age=31536000; includeSubDomains; preload'
body: '{"id":"adr_059da1d447734818bee9a1a2fa429230","object":"Address","created_at":"2021-10-01T22:29:15+00:00","updated_at":"2021-10-01T22:29:15+00:00","name":null,"company":null,"street1":"388 Townsend St","street2":"Apt 20","city":"San Francisco","state":"CA","zip":"94107","country":"US","phone":null,"email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}}'
body: '{"id":"adr_aef9c7338a384dd0b75c0e0239cd35a1","object":"Address","created_at":"2021-10-20T16:53:00+00:00","updated_at":"2021-10-20T16:53:00+00:00","name":null,"company":null,"street1":"388 Townsend St","street2":"Apt 20","city":"San Francisco","state":"CA","zip":"94107","country":"US","phone":null,"email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}}'
curl_info:
url: 'https://api.easypost.com/v2/addresses'
content_type: 'application/json; charset=utf-8'
Expand All @@ -49,32 +49,32 @@
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 0.290406
namelookup_time: 0.044905
connect_time: 0.109719
pretransfer_time: 0.194584
total_time: 0.349258
namelookup_time: 0.051463
connect_time: 0.144801
pretransfer_time: 0.219276
size_upload: !!float 110
size_download: !!float 429
speed_download: !!float 1477
speed_upload: !!float 378
speed_download: !!float 1228
speed_upload: !!float 314
download_content_length: !!float 429
upload_content_length: !!float 110
starttransfer_time: 0.290299
starttransfer_time: 0.349206
redirect_time: !!float 0
redirect_url: ''
primary_ip: 169.62.110.130
primary_ip: 169.62.110.131
certinfo: { }
primary_port: 443
local_ip: 10.130.6.13
local_port: 59817
local_ip: 10.130.6.4
local_port: 50428
http_version: 2
protocol: 2
ssl_verifyresult: 0
scheme: HTTPS
appconnect_time_us: 194515
connect_time_us: 109719
namelookup_time_us: 44905
pretransfer_time_us: 194584
appconnect_time_us: 219198
connect_time_us: 144801
namelookup_time_us: 51463
pretransfer_time_us: 219276
redirect_time_us: 0
starttransfer_time_us: 290299
total_time_us: 290406
starttransfer_time_us: 349206
total_time_us: 349258

0 comments on commit 14d3220

Please sign in to comment.