-
-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix broken apns background message data detection
- Loading branch information
1 parent
cb66574
commit f7e6074
Showing
4 changed files
with
316 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
tests/Unit/Messaging/Processor/SetApnsContentAvailableIfNeededTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreait\Firebase\Tests\Unit\Messaging\Processor; | ||
|
||
use Beste\Json; | ||
use Kreait\Firebase\Messaging\CloudMessage; | ||
use Kreait\Firebase\Messaging\Message; | ||
use Kreait\Firebase\Messaging\Processor\SetApnsContentAvailableIfNeeded; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class SetApnsContentAvailableIfNeededTest extends TestCase | ||
{ | ||
private SetApnsContentAvailableIfNeeded $processor; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->processor = new SetApnsContentAvailableIfNeeded(); | ||
} | ||
|
||
/** | ||
* @dataProvider provideMessagesWithExpectedContentAvailable | ||
* | ||
* @param array<mixed> $messageData | ||
* | ||
* @see https://github.com/kreait/firebase-php/pull/762 | ||
*/ | ||
public function testItSetsTheExpectedPushType(bool $expected, array $messageData): void | ||
{ | ||
$message = CloudMessage::fromArray($messageData); | ||
|
||
$processed = Json::decode(Json::encode(($this->processor)($message)), true); | ||
|
||
if ($expected === true) { | ||
$this->assertTrue(isset($processed['apns']['payload']['aps']['content-available'])); | ||
$this->assertSame(1, $processed['apns']['payload']['aps']['content-available']); | ||
} else { | ||
$this->assertFalse(isset($processed['apns']['payload']['aps']['content-available'])); | ||
} | ||
} | ||
|
||
/** | ||
* @return iterable<string, array{0: bool, 1: array<mixed>}> | ||
*/ | ||
public function provideMessagesWithExpectedContentAvailable(): iterable | ||
{ | ||
yield 'message data at root level -> true' => [ | ||
true, | ||
[ | ||
'data' => [ | ||
'key' => 'value', | ||
], | ||
], | ||
]; | ||
|
||
yield 'message data at apns level -> true' => [ | ||
true, | ||
[ | ||
'apns' => [ | ||
'payload' => [ | ||
'data' => [ | ||
'key' => 'value', | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
yield 'both message and apns data -> true' => [ | ||
true, | ||
[ | ||
'data' => [ | ||
'key' => 'value', | ||
], | ||
'apns' => [ | ||
'payload' => [ | ||
'data' => [ | ||
'key' => 'value', | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
yield 'no data -> false' => [ | ||
false, | ||
[ | ||
'data' => [], | ||
'apns' => [ | ||
'payload' => [ | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param non-empty-string $type | ||
*/ | ||
private function assertMessageHasPushType(Message $message, string $type): void | ||
{ | ||
$processed = Json::decode(Json::encode(($this->processor)($message)), true); | ||
|
||
$this->assertTrue(isset($processed['apns']['headers']['apns-push-type'])); | ||
$this->assertSame($type, $processed['apns']['headers']['apns-push-type']); | ||
} | ||
|
||
private function assertMessageHasNoPushType(Message $message): void | ||
{ | ||
$processed = Json::decode(Json::encode(($this->processor)($message)), true); | ||
|
||
$this->assertFalse(isset($processed['apns']['headers']['apns-push-type'])); | ||
} | ||
} |
190 changes: 190 additions & 0 deletions
190
tests/Unit/Messaging/Processor/SetApnsPushTypeIfNeededTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreait\Firebase\Tests\Unit\Messaging\Processor; | ||
|
||
use Beste\Json; | ||
use Kreait\Firebase\Messaging\CloudMessage; | ||
use Kreait\Firebase\Messaging\Message; | ||
use Kreait\Firebase\Messaging\Processor\SetApnsPushTypeIfNeeded; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class SetApnsPushTypeIfNeededTest extends TestCase | ||
{ | ||
private SetApnsPushTypeIfNeeded $processor; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->processor = new SetApnsPushTypeIfNeeded(); | ||
} | ||
|
||
/** | ||
* @dataProvider provideMessagesWithExpectedPushType | ||
* | ||
* @param non-empty-string|null $expected | ||
* @param array<mixed> $messageData | ||
*/ | ||
public function testItSetsTheExpectedPushType(?string $expected, array $messageData): void | ||
{ | ||
$message = CloudMessage::fromArray($messageData); | ||
|
||
if ($expected === null) { | ||
$this->assertMessageHasNoPushType($message); | ||
} else { | ||
$this->assertMessageHasPushType($message, $expected); | ||
} | ||
} | ||
|
||
/** | ||
* @return iterable<string, array{0: non-empty-string|null, 1: array<mixed>}> | ||
*/ | ||
public function provideMessagesWithExpectedPushType(): iterable | ||
{ | ||
yield 'message data at root level -> background' => [ | ||
'background', | ||
[ | ||
'data' => [ | ||
'key' => 'value', | ||
], | ||
], | ||
]; | ||
|
||
yield 'message data at apns level -> background' => [ | ||
'background', | ||
[ | ||
'apns' => [ | ||
'payload' => [ | ||
'data' => [ | ||
'key' => 'value', | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
yield 'both message and apns data -> background' => [ | ||
'background', | ||
[ | ||
'data' => [ | ||
'key' => 'value', | ||
], | ||
'apns' => [ | ||
'payload' => [ | ||
'data' => [ | ||
'key' => 'value', | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
yield 'notification at root level -> alert' => [ | ||
'alert', | ||
[ | ||
'notification' => [ | ||
'title' => 'Alert', | ||
], | ||
], | ||
]; | ||
|
||
yield 'notification at apns level -> alert' => [ | ||
'alert', | ||
[ | ||
'apns' => [ | ||
'payload' => [ | ||
'aps' => [ | ||
'alert' => [ | ||
'title' => 'Alert', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
yield 'notification both at root apns level -> alert' => [ | ||
'alert', | ||
[ | ||
'notification' => [ | ||
'title' => 'Alert', | ||
], | ||
'apns' => [ | ||
'payload' => [ | ||
'aps' => [ | ||
'alert' => [ | ||
'title' => 'Alert', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
yield 'data at root, notification at apns level -> alert' => [ | ||
'alert', | ||
[ | ||
'data' => [ | ||
'key' => 'value', | ||
], | ||
'apns' => [ | ||
'payload' => [ | ||
'aps' => [ | ||
'alert' => [ | ||
'title' => 'Alert', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
yield 'notification at root, data at apns level -> alert' => [ | ||
'alert', | ||
[ | ||
'notification' => [ | ||
'title' => 'Alert', | ||
], | ||
'apns' => [ | ||
'payload' => [ | ||
'data' => [ | ||
'key' => 'value', | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
yield 'no data -> none' => [ | ||
null, | ||
[ | ||
'data' => [], | ||
'apns' => [ | ||
'payload' => [ | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param non-empty-string $type | ||
*/ | ||
private function assertMessageHasPushType(Message $message, string $type): void | ||
{ | ||
$processed = Json::decode(Json::encode(($this->processor)($message)), true); | ||
|
||
$this->assertTrue(isset($processed['apns']['headers']['apns-push-type'])); | ||
$this->assertSame($type, $processed['apns']['headers']['apns-push-type']); | ||
} | ||
|
||
private function assertMessageHasNoPushType(Message $message): void | ||
{ | ||
$processed = Json::decode(Json::encode(($this->processor)($message)), true); | ||
|
||
$this->assertFalse(isset($processed['apns']['headers']['apns-push-type'])); | ||
} | ||
} |