From 3a94a87a533c0aa44b4e21e96580f64721df7a6f Mon Sep 17 00:00:00 2001 From: Tom Arbesser-Rastburg Date: Wed, 17 May 2023 19:52:46 +1000 Subject: [PATCH] Fix Client::getTime() --- .phpstan/baseline.neon | 12 +----------- src/Bigcommerce/Api/Client.php | 15 ++++++++------- test/Unit/Api/ClientTest.php | 8 ++++---- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/.phpstan/baseline.neon b/.phpstan/baseline.neon index 2b91ca5..4259a42 100644 --- a/.phpstan/baseline.neon +++ b/.phpstan/baseline.neon @@ -385,11 +385,6 @@ parameters: count: 1 path: ../src/Bigcommerce/Api/Client.php - - - message: "#^Method Bigcommerce\\\\Api\\\\Client\\:\\:getRequestsRemaining\\(\\) should return int but returns false\\.$#" - count: 1 - path: ../src/Bigcommerce/Api/Client.php - - message: "#^Method Bigcommerce\\\\Api\\\\Client\\:\\:getRulesByProduct\\(\\) has parameter \\$filter with no value type specified in iterable type array\\.$#" count: 1 @@ -450,11 +445,6 @@ parameters: count: 1 path: ../src/Bigcommerce/Api/Client.php - - - message: "#^Method Bigcommerce\\\\Api\\\\Client\\:\\:getTime\\(\\) should return DateTime but returns array\\|float\\|int\\|string\\|false\\|null\\.$#" - count: 1 - path: ../src/Bigcommerce/Api/Client.php - - message: "#^Method Bigcommerce\\\\Api\\\\Client\\:\\:mapCollection\\(\\) has parameter \\$object with no value type specified in iterable type array\\.$#" count: 1 @@ -547,7 +537,7 @@ parameters: - message: "#^Negated boolean expression is always false\\.$#" - count: 2 + count: 1 path: ../src/Bigcommerce/Api/Client.php - diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 3826195..99f2fb3 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -2,7 +2,8 @@ namespace Bigcommerce\Api; -use \Exception as Exception; +use DateTime; +use Exception; use Firebase\JWT\JWT; /** @@ -452,17 +453,17 @@ public static function getCustomerLoginToken($id, $redirectUrl = '', $requestIp /** * Pings the time endpoint to test the connection to a store. * - * @return \DateTime + * @return ?DateTime */ public static function getTime() { - $response = self::connection()->get(self::$api_path . '/time'); + $response = self::connection()->get(self::$api_url . '/time'); - if ($response == false || is_string($response)) { - return $response; + if (empty($response)) { + return null; } - return new \DateTime("@{$response->time}"); + return new DateTime("@{$response}"); } /** @@ -1333,7 +1334,7 @@ public static function getStore() * last request that was fetched within the current script. If no * requests have been made, pings the time endpoint to get the value. * - * @return int + * @return int|false */ public static function getRequestsRemaining() { diff --git a/test/Unit/Api/ClientTest.php b/test/Unit/Api/ClientTest.php index c84c14e..5c5d7d5 100644 --- a/test/Unit/Api/ClientTest.php +++ b/test/Unit/Api/ClientTest.php @@ -244,8 +244,8 @@ public function testGetTimeReturnsTheExpectedTime() $now = new \DateTime(); $this->connection->expects($this->once()) ->method('get') - ->with($this->basePath . '/time', false) - ->will($this->returnValue((object)array('time' => $now->format('U')))); + ->with('https://api.bigcommerce.com/time', false) + ->will($this->returnValue($now->format('U'))); $this->assertEquals($now->format('U'), Client::getTime()->format('U')); } @@ -278,8 +278,8 @@ public function testGetRequestsRemainingRequestsTimeWhenNoValueAvailable() $this->connection->expects($this->once()) ->method('get') - ->with($this->basePath . '/time', false) - ->will($this->returnValue((object)array('time' => time()))); + ->with('https://api.bigcommerce.com/time', false) + ->will($this->returnValue(time())); $this->assertSame(12345, Client::getRequestsRemaining()); }