Skip to content

Commit

Permalink
Fix Subscription::create when not giving every keys (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Minishlink authored May 13, 2018
1 parent 74bc0ac commit 5c2d2a5
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Subscription
/** @var null|string */
private $authToken;

/** @var string */
/** @var null|string */
private $contentEncoding;

/**
Expand All @@ -40,17 +40,20 @@ public function __construct(
string $endpoint,
?string $publicKey = null,
?string $authToken = null,
string $contentEncoding = "aesgcm"
?string $contentEncoding = null
) {
$supportedContentEncodings = ['aesgcm', 'aes128gcm'];
if (!in_array($contentEncoding, $supportedContentEncodings)) {
throw new \ErrorException('This content encoding ('.$contentEncoding.') is not supported.');
}

$this->endpoint = $endpoint;
$this->publicKey = $publicKey;
$this->authToken = $authToken;
$this->contentEncoding = $contentEncoding;

if ($publicKey || $authToken || $contentEncoding) {
$supportedContentEncodings = ['aesgcm', 'aes128gcm'];
if ($contentEncoding && !in_array($contentEncoding, $supportedContentEncodings)) {
throw new \ErrorException('This content encoding ('.$contentEncoding.') is not supported.');
}

$this->publicKey = $publicKey;
$this->authToken = $authToken;
$this->contentEncoding = $contentEncoding ?: "aesgcm";
}
}

/**
Expand All @@ -61,14 +64,18 @@ public function __construct(
* @throws \ErrorException
*/
public static function create(array $associativeArray): Subscription {
$instance = new self(
$associativeArray['endpoint'],
$associativeArray['publicKey'],
$associativeArray['authToken'],
$associativeArray['contentEncoding']
);
if (array_key_exists('publicKey', $associativeArray) || array_key_exists('authToken', $associativeArray) || array_key_exists('contentEncoding', $associativeArray)) {
return new self(
$associativeArray['endpoint'],
$associativeArray['publicKey'] ?? null,
$associativeArray['authToken'] ?? null,
$associativeArray['contentEncoding'] ?? "aesgcm"
);
}

return $instance;
return new self(
$associativeArray['endpoint']
);
}

/**
Expand Down Expand Up @@ -96,9 +103,9 @@ public function getAuthToken(): ?string
}

/**
* @return string
* @return null|string
*/
public function getContentEncoding(): string
public function getContentEncoding(): ?string
{
return $this->contentEncoding;
}
Expand Down
75 changes: 75 additions & 0 deletions tests/SubscriptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

use Minishlink\WebPush\Subscription;

class SubscriptionTest extends PHPUnit\Framework\TestCase
{

public function testCreateMinimal()
{
$subscriptionArray = array(
"endpoint" => "http://toto.com"
);
$subscription = Subscription::create($subscriptionArray);
$this->assertEquals("http://toto.com", $subscription->getEndpoint());
$this->assertEquals(null, $subscription->getPublicKey());
$this->assertEquals(null, $subscription->getAuthToken());
$this->assertEquals(null, $subscription->getContentEncoding());
}

public function testConstructMinimal()
{
$subscription = new Subscription("http://toto.com");
$this->assertEquals("http://toto.com", $subscription->getEndpoint());
$this->assertEquals(null, $subscription->getPublicKey());
$this->assertEquals(null, $subscription->getAuthToken());
$this->assertEquals(null, $subscription->getContentEncoding());
}

public function testCreatePartial()
{
$subscriptionArray = array(
"endpoint" => "http://toto.com",
"publicKey" => "publicKey",
"authToken" => "authToken",
);
$subscription = Subscription::create($subscriptionArray);
$this->assertEquals("http://toto.com", $subscription->getEndpoint());
$this->assertEquals("publicKey", $subscription->getPublicKey());
$this->assertEquals("authToken", $subscription->getAuthToken());
$this->assertEquals("aesgcm", $subscription->getContentEncoding());
}

public function testConstructPartial()
{
$subscription = new Subscription("http://toto.com", "publicKey", "authToken");
$this->assertEquals("http://toto.com", $subscription->getEndpoint());
$this->assertEquals("publicKey", $subscription->getPublicKey());
$this->assertEquals("authToken", $subscription->getAuthToken());
$this->assertEquals("aesgcm", $subscription->getContentEncoding());
}

public function testCreateFull()
{
$subscriptionArray = array(
"endpoint" => "http://toto.com",
"publicKey" => "publicKey",
"authToken" => "authToken",
"contentEncoding" => "aes128gcm",
);
$subscription = Subscription::create($subscriptionArray);
$this->assertEquals("http://toto.com", $subscription->getEndpoint());
$this->assertEquals("publicKey", $subscription->getPublicKey());
$this->assertEquals("authToken", $subscription->getAuthToken());
$this->assertEquals("aes128gcm", $subscription->getContentEncoding());
}

public function testConstructFull()
{
$subscription = new Subscription("http://toto.com", "publicKey", "authToken", "aes128gcm");
$this->assertEquals("http://toto.com", $subscription->getEndpoint());
$this->assertEquals("publicKey", $subscription->getPublicKey());
$this->assertEquals("authToken", $subscription->getAuthToken());
$this->assertEquals("aes128gcm", $subscription->getContentEncoding());
}
}

0 comments on commit 5c2d2a5

Please sign in to comment.