Skip to content

Commit

Permalink
Merge pull request #141 from juvenn/push-time
Browse files Browse the repository at this point in the history
修复推送请求中的时间格式问题
  • Loading branch information
juvenn authored Feb 27, 2017
2 parents eb8267e + 781936d commit 0e58b36
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/LeanCloud/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 27 additions & 2 deletions test/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!",
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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();
}
}

0 comments on commit 0e58b36

Please sign in to comment.