From c1776ff36e7e5fe6050d230597f4114d8aa6f535 Mon Sep 17 00:00:00 2001 From: Tom Arbesser-Rastburg Date: Thu, 13 Jun 2024 23:25:12 +1000 Subject: [PATCH 1/3] Fix Client::getTime() fixes #298 --- src/Bigcommerce/Api/Client.php | 6 ++++-- test/Unit/Api/ClientTest.php | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 1cd61e9..dda45a3 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -474,7 +474,7 @@ public static function getCustomerLoginToken($id, $redirectUrl = '', $requestIp } /** - * Pings the time endpoint to test the connection to a store. + * Pings the time endpoint to test the connection to the BigCommerce API. * * @return ?DateTime */ @@ -486,7 +486,9 @@ public static function getTime() return null; } - return new DateTime("@{$response}"); + $seconds = floor($response / 1000); + $microseconds = $response % 1000; + return DateTime::createFromFormat('U.u', sprintf('%d.%03d', $seconds, $microseconds)); } /** diff --git a/test/Unit/Api/ClientTest.php b/test/Unit/Api/ClientTest.php index 39354f4..8a5afc7 100644 --- a/test/Unit/Api/ClientTest.php +++ b/test/Unit/Api/ClientTest.php @@ -210,9 +210,9 @@ public function testGetTimeReturnsTheExpectedTime() $this->connection->expects($this->once()) ->method('get') ->with('https://api.bigcommerce.com/time', false) - ->will($this->returnValue($now->format('U'))); + ->will($this->returnValue('1718283600000')); - $this->assertEquals($now->format('U'), Client::getTime()->format('U')); + $this->assertEquals('2024-06-13 13:00:00', Client::getTime()->format('Y-m-d H:i:s')); } public function testGetStoreReturnsTheResultBodyDirectly() From 0b48042caa7c018ca6fac9681ea8e19b62b3ca23 Mon Sep 17 00:00:00 2001 From: Tom Arbesser-Rastburg Date: Fri, 14 Jun 2024 08:14:39 +1000 Subject: [PATCH 2/3] Add Client::getStoreTime() method, update README --- README.md | 8 ++++---- src/Bigcommerce/Api/Client.php | 19 ++++++++++++++++++- test/Unit/Api/ClientTest.php | 21 ++++++++++++++++++++- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 46312ff..d53ff42 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ https://developer.bigcommerce.com/ Requirements ------------ -- PHP 7.1 or greater +- PHP 8.1 or greater - `curl` extension enabled To generate an OAuth API token, [follow this guide.](https://support.bigcommerce.com/s/article/Store-API-Accounts) @@ -96,12 +96,12 @@ Connecting to the store ----------------------- To test that your configuration was correct and you can successfully connect to -the store, ping the getTime method which will return a DateTime object -representing the current timestamp of the store if successful or false if +the store, ping the getStoreTime method which will return a DateTime object +representing the current timestamp of the store if successful or null if unsuccessful: ~~~php -$ping = Bigcommerce::getTime(); +$ping = Bigcommerce::getStoreTime(); if ($ping) echo $ping->format('H:i:s'); ~~~ diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index dda45a3..231bf1d 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -482,15 +482,32 @@ public static function getTime() { $response = self::connection()->get(self::$api_url . '/time'); - if (empty($response)) { + if (empty($response) || !is_numeric($response)) { return null; } + // The response from /time is unix time in milliseconds $seconds = floor($response / 1000); $microseconds = $response % 1000; return DateTime::createFromFormat('U.u', sprintf('%d.%03d', $seconds, $microseconds)); } + /** + * Pings the time endpoint to test the connection to a store. + * + * @return ?DateTime + */ + public static function getStoreTime() + { + $response = self::connection()->get(self::$api_path . '/time'); + + if (!is_object($response) || !property_exists($response, 'time')) { + return null; + } + + return new DateTime("@{$response->time}"); + } + /** * Returns the default collection of products. * diff --git a/test/Unit/Api/ClientTest.php b/test/Unit/Api/ClientTest.php index 8a5afc7..9fb9269 100644 --- a/test/Unit/Api/ClientTest.php +++ b/test/Unit/Api/ClientTest.php @@ -206,7 +206,6 @@ public function testDeleteResourceDeletesToTheRightPlace() public function testGetTimeReturnsTheExpectedTime() { - $now = new \DateTime(); $this->connection->expects($this->once()) ->method('get') ->with('https://api.bigcommerce.com/time', false) @@ -215,6 +214,26 @@ public function testGetTimeReturnsTheExpectedTime() $this->assertEquals('2024-06-13 13:00:00', Client::getTime()->format('Y-m-d H:i:s')); } + public function testGetStoreTimeReturnsTheExpectedTime() + { + $this->connection->expects($this->once()) + ->method('get') + ->with($this->basePath . '/time') + ->will($this->returnValue(json_decode('{"time": 1718283600}'))); + + $this->assertEquals('2024-06-13 13:00:00', Client::getStoreTime()->format('Y-m-d H:i:s')); + } + + public function testGetStoreTimeReturnsNothing() + { + $this->connection->expects($this->once()) + ->method('get') + ->with($this->basePath . '/time') + ->will($this->returnValue(false)); + + $this->assertEquals(null, Client::getStoreTime()); + } + public function testGetStoreReturnsTheResultBodyDirectly() { $body = [random_int(0, mt_getrandmax()) => random_int(0, mt_getrandmax())]; From caea7b0b3e6d7c98941f0748d7d52ab80492ce9d Mon Sep 17 00:00:00 2001 From: Tom Arbesser-Rastburg Date: Fri, 28 Jun 2024 20:08:07 +1000 Subject: [PATCH 3/3] Rename variable $microseconds to $milliseconds --- src/Bigcommerce/Api/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 231bf1d..e64100c 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -488,8 +488,8 @@ public static function getTime() // The response from /time is unix time in milliseconds $seconds = floor($response / 1000); - $microseconds = $response % 1000; - return DateTime::createFromFormat('U.u', sprintf('%d.%03d', $seconds, $microseconds)); + $milliseconds = $response % 1000; + return DateTime::createFromFormat('U.u', sprintf('%d.%03d', $seconds, $milliseconds)); } /**