From 781936d04d2a962a2b6f60ee24e7978c111da20a Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Mon, 27 Feb 2017 15:28:25 +0800 Subject: [PATCH] Fix push_time format error --- src/LeanCloud/Push.php | 10 ++++++++++ test/PushTest.php | 29 +++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/LeanCloud/Push.php b/src/LeanCloud/Push.php index 0bdde59..3012593 100644 --- a/src/LeanCloud/Push.php +++ b/src/LeanCloud/Push.php @@ -146,6 +146,16 @@ public function setExpirationTime(\DateTime $time) { public function encode() { $out = $this->options; $out["data"] = $this->data; + $expire = isset($this->options["expiration_time"]) ? $this->options["expiration_time"] : null; + if (($expire instanceof \DateTime) || + ($expire instanceof \DateTimeImmutable)) { + $out["expiration_time"] = Client::formatDate($expire); + } + $pushTime = isset($this->options["push_time"]) ? $this->options["push_time"] : null; + if (($pushTime instanceof \DateTime) || + ($pushTime instanceof \DateTimeImmutable)){ + $out["push_time"] = Client::formatDate($pushTime); + } if (isset($this->options["where"])) { $query = $this->options["where"]->encode(); $out["where"] = json_decode($query["where"], true); diff --git a/test/PushTest.php b/test/PushTest.php index ad7b289..662a318 100644 --- a/test/PushTest.php +++ b/test/PushTest.php @@ -5,6 +5,16 @@ use LeanCloud\Push; class PushTest extends PHPUnit_Framework_TestCase { + + public function setUp() { + Client::initialize( + getenv("LC_APP_ID"), + getenv("LC_APP_KEY"), + getenv("LC_APP_MASTER_KEY")); + Client::useRegion(getenv("LC_API_REGION")); + Client::useMasterKey(false); + } + public function testMessageEncode() { $data = array( "alert" => "Hello world!", @@ -87,7 +97,7 @@ public function testSetPushTime() { $time = new DateTime(); $push->setPushTime($time); $out = $push->encode(); - $this->assertEquals($time, $out["push_time"]); + $this->assertEquals($time, new DateTime($out["push_time"])); } public function testSetExpirationInterval() { @@ -106,7 +116,7 @@ public function testSetExpirationTime() { $date = new DateTime(); $push->setExpirationTime($date); $out = $push->encode(); - $this->assertEquals($date, $out["expiration_time"]); + $this->assertEquals($date, new DateTime($out["expiration_time"])); } public function testSetWhere() { @@ -124,4 +134,19 @@ public function testSetWhere() { ) ), $out["where"]); } + + public function testSendPush() { + $push = new Push(array( + "alert" => "Hello world!" + )); + $query = new Query("_Installation"); + $query->equalTo("deviceType", "Android"); + $push->setWhere($query); + + $at = new DateTime(); + $at->add(new DateInterval("P1D")); + $push->setPushTime($at); + + // $push->send(); + } }