Skip to content

Commit

Permalink
Fix WebPushConfig's TTL not allowing null
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Jul 12, 2022
1 parent ecb7ce4 commit 754248f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
26 changes: 16 additions & 10 deletions src/Firebase/Messaging/WebPushConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @see https://tools.ietf.org/html/rfc8030#section-5.3 Web Push Message Urgency
* @phpstan-type WebPushHeadersShape array{
* TTL?: positive-int|numeric-string,
* TTL?: positive-int|numeric-string|null,
* Urgency?: self::URGENCY_*
* }
*
Expand Down Expand Up @@ -91,6 +91,10 @@ public static function fromArray(array $config): self
{
if (array_key_exists('headers', $config) && is_array($config['headers'])) {
$config['headers'] = self::ensureValidHeaders($config['headers']);

if ($config['headers'] === []) {
unset($config['headers']);
}
}

return new self($config);
Expand All @@ -103,16 +107,18 @@ public static function fromArray(array $config): self
*/
private static function ensureValidHeaders(array $headers): array
{
if (array_key_exists('TTL', $headers) && is_int($headers['TTL'])) {
$headers['TTL'] = (string) $headers['TTL'];
}
if (array_key_exists('TTL', $headers)) {
if (is_int($headers['TTL'])) {
$headers['TTL'] = (string) $headers['TTL'];
}

if (
array_key_exists('TTL', $headers)
&& is_string($headers['TTL'])
&& preg_match('/^[\-0]/', $headers['TTL']) === 1
) {
throw new InvalidArgument('The TTL in the WebPushConfig must must be a positive int');
if (is_string($headers['TTL']) && (preg_match('/^[1-9]\d*$/', $headers['TTL']) !== 1)) {
throw new InvalidArgument('The TTL in the WebPushConfig must must be a positive int');
}

if ($headers['TTL'] === null) {
unset($headers['TTL']);
}
}

if (array_key_exists('Urgency', $headers)) {
Expand Down
17 changes: 15 additions & 2 deletions tests/Unit/Messaging/WebPushConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public function testItCanHaveAPriority(): void
$this->assertSame('high', $config->jsonSerialize()['headers']['Urgency']);
}

/**
* @dataProvider validHeaders
*
* @param WebPushHeadersShape $headers
*/
public function testItAcceptsValidHeaders(array $headers): void
{
WebPushConfig::fromArray(['headers' => $headers]);

$this->addToAssertionCount(1);
}

/**
* @dataProvider invalidHeaders
*
Expand Down Expand Up @@ -82,6 +94,7 @@ public function validHeaders(): array
return [
'positive int ttl' => [['TTL' => 1]],
'positive string ttl' => [['TTL' => '1']],
'null (#719)' => [['TTL' => null]],
];
}

Expand All @@ -93,8 +106,8 @@ public function invalidHeaders(): array
return [
'negative int ttl' => [['TTL' => -1]],
'negative string ttl' => [['TTL' => '-1']],
'zero int ttl' => [['TTL' => -1]],
'zero string ttl' => [['TTL' => '-1']],
'zero int ttl' => [['TTL' => 0]],
'zero string ttl' => [['TTL' => '0']],
'unsupported urgency' => [['Urgency' => 'unsupported']],
];
}
Expand Down

0 comments on commit 754248f

Please sign in to comment.