From f5aeeb3b96b6a7d9f6390ec56318645d976bb8e2 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Sat, 17 Mar 2018 13:44:06 +0800 Subject: [PATCH 1/2] Add setServerUrl close #168 #166 #162 --- src/LeanCloud/Client.php | 28 +++++++++++++++++++++++++++- test/ClientTest.php | 11 +++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/LeanCloud/Client.php b/src/LeanCloud/Client.php index 4281805..0cff7b3 100644 --- a/src/LeanCloud/Client.php +++ b/src/LeanCloud/Client.php @@ -62,6 +62,13 @@ class Client { */ private static $appMasterKey; + /** + * Server url + * + * @var string + */ + private static $serverUrl; + /** * Use master key or not * @@ -194,6 +201,18 @@ public static function useMasterKey($flag) { self::$useMasterKey = $flag ? true : false; } + /** + * Set server url + * + * Explicitly set server url with which this client will communicate. + * Url shall be in the form of: `https://api.leancloud.cn` . + * + * @param string $url + */ + public static function setServerUrl($url) { + self::$serverUrl = rtrim($url, "/"); + } + /** * Get API Endpoint * @@ -203,7 +222,14 @@ public static function useMasterKey($flag) { * @return string */ public static function getAPIEndPoint() { - return getenv("LEANCLOUD_API_SERVER") . "/" . self::$apiVersion; + if ($url = self::$serverUrl) { + return $url . "/" . self::$apiVersion; + } else if ($url = getenv("LEANCLOUD_API_SERVER")) { + return $url . "/" . self::$apiVersion; + } else { + throw new \RuntimeException("Server url not set, please set it as:" . + "`Client::setServerUrl('https://{left-8-chars-of-appid}.api.lncld.net}')`"); + } } /** diff --git a/test/ClientTest.php b/test/ClientTest.php index 17243c2..bcc0b9f 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -21,6 +21,17 @@ public function setUp() { Client::useMasterKey(false); } + public function testAPIEndPoint() { + $url = getenv("LEANCLOUD_API_SERVER"); + $this->assertEquals("{$url}/1.1", Client::getAPIEndPoint()); + + Client::setServerURL("https://hello.api.lncld.net"); + $this->assertEquals("https://hello.api.lncld.net/1.1", Client::getAPIEndPoint()); + Client::setServerURL(null); + + $this->assertEquals("{$url}/1.1", Client::getAPIEndPoint()); + } + public function testVerifyKey() { $result = Client::verifyKey( getenv("LEANCLOUD_APP_ID"), From c9ef636ca672a87407a6b35b520d05ef70b4a0b6 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Sat, 17 Mar 2018 12:56:00 +0800 Subject: [PATCH 2/2] (fix) Datetime test --- src/LeanCloud/Client.php | 7 +++++-- test/ClientTest.php | 8 ++++++-- test/ObjectTest.php | 3 ++- test/PushTest.php | 6 ++++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/LeanCloud/Client.php b/src/LeanCloud/Client.php index 0cff7b3..6aaadf8 100644 --- a/src/LeanCloud/Client.php +++ b/src/LeanCloud/Client.php @@ -620,7 +620,8 @@ public static function formatDate($date) { $utc = clone $date; $utc->setTimezone(new \DateTimezone("UTC")); $iso = $utc->format("Y-m-d\TH:i:s.u"); - // chops 3 zeros of microseconds to comply with cloud date format + // Milliseconds precision is required for server to correctly parse time, + // thus we have to chop off last 3 microseconds to milliseconds. $iso = substr($iso, 0, 23) . "Z"; return $iso; } @@ -652,7 +653,9 @@ public static function decode($value, $key) { if ($type === "Date") { // return time in default time zone - return new \DateTime($value["iso"]); + $date = new \DateTime($value["iso"]); + $date->setTimezone(new \DateTimeZone(date_default_timezone_get())); + return $date; } if ($type === "Bytes") { return Bytes::createFromBase64Data($value["base64"]); diff --git a/test/ClientTest.php b/test/ClientTest.php index bcc0b9f..d681ac4 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -175,7 +175,9 @@ public function testDecodeDate() { $date = new DateTime(); $type = array("__type" => "Date", "iso" => Client::formatDate($date)); - $this->assertEquals($date, Client::decode($type, null)); + $date2 = Client::decode($type, null); + $this->assertEquals($date->getTimestamp(), + $date2->getTimestamp()); } public function testDecodeDateWithTimeZone() { @@ -185,7 +187,9 @@ public function testDecodeDateWithTimeZone() { $date = new DateTime("now", new DateTimeZone($zone)); $type = array("__type" => "Date", "iso" => Client::formatDate($date)); - $this->assertEquals($date, Client::decode($type, null)); + $date2 = Client::decode($type, null); + $this->assertEquals($date->getTimestamp(), + $date2->getTimestamp()); } } diff --git a/test/ObjectTest.php b/test/ObjectTest.php index 98dc619..e83f1d7 100644 --- a/test/ObjectTest.php +++ b/test/ObjectTest.php @@ -225,7 +225,8 @@ public function testGetDateShouldReturnDateTime() { $obj2 = new Object("TestObject", $obj->getObjectId()); $obj2->fetch(); $this->assertTrue($obj2->get("release") instanceof DateTime); - $this->assertEquals($obj->get("release"), $obj2->get("release")); + $this->assertEquals($obj->get("release")->getTimestamp(), + $obj2->get("release")->getTimestamp()); $obj2->destroy(); } diff --git a/test/PushTest.php b/test/PushTest.php index 97c4572..409e25b 100644 --- a/test/PushTest.php +++ b/test/PushTest.php @@ -97,7 +97,8 @@ public function testSetPushTime() { $time = new DateTime(); $push->setPushTime($time); $out = $push->encode(); - $this->assertEquals($time, new DateTime($out["push_time"])); + $time2 = new DateTime($out["push_time"]); + $this->assertEquals($time->getTimestamp(), $time2->getTimestamp()); } public function testSetExpirationInterval() { @@ -116,7 +117,8 @@ public function testSetExpirationTime() { $date = new DateTime(); $push->setExpirationTime($date); $out = $push->encode(); - $this->assertEquals($date, new DateTime($out["expiration_time"])); + $date2 = new DateTime($out["expiration_time"]); + $this->assertEquals($date->getTimestamp(), $date2->getTimestamp()); } public function testSetWhere() {