Skip to content

Commit

Permalink
Add tests for client and providers
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-b committed Sep 9, 2020
1 parent 1b9a788 commit cde8610
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 9 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/vendor/
.idea
/example/PushCert.pem
/example/AuthKey_GVN5Q3V6GL.p8
/tests/certs
3 changes: 1 addition & 2 deletions example/certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
use SasaB\Apns\Notification;
use \Ramsey\Uuid\Uuid;

$certificate = Certificate::fromFile('./PushCert.pem');
$certificate = Certificate::fromFile('../tests/certs/PushCert.pem');

$client = Client::auth($certificate);

// 51d5f3696c9cc62caf322fbcfd0b25a455697b1c3261eb4ed085041c6e895bdb
$notification = new Notification("51d5f3696c9cc62caf322fbcfd0b25a455697b1c3261eb4ed085041c6e895bdb");

$notification->setApsId($apsId = Uuid::uuid4());
Expand Down
8 changes: 5 additions & 3 deletions example/token.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
use SasaB\Apns\Provider\JWT;
use SasaB\Apns\Provider\TokenKey;

$keyId = file_get_contents('../tests/certs/key-id.txt');
$teamId = file_get_contents('../tests/certs/team-id.txt');

$tokenKey = new TokenKey('GVN5Q3V6GL');
$tokenKey->loadFromFile('AuthKey_GVN5Q3V6GL.p8');
$tokenKey = new TokenKey($keyId);
$tokenKey->loadFromFile('../tests/certs/AuthKey.p8');

$jwt = JWT::new('X7XGQHWDNM', $tokenKey);
$jwt = JWT::new($teamId, $tokenKey);

if ($jwt->hasExpired()) {
$jwt->refresh($tokenKey);
Expand Down
5 changes: 5 additions & 0 deletions src/Provider/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function getAuthOptions(): array

return $options;
}

public function getFilePath(): string
{
return $this->file;
}
}
40 changes: 40 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,56 @@


use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
use SasaB\Apns\Client;
use SasaB\Apns\Notification;
use SasaB\Apns\Provider\JWT;

class ClientTest extends TestCase
{
use CreateCertificateTrust, CreateTokenKeyTrust;

public function testItCanSendNotification()
{
$certificate = $this->makeCertificate();

$client = Client::auth($certificate);

$notification = new Notification("51d5f3696c9cc62caf322fbcfd0b25a455697b1c3261eb4ed085041c6e895bdb");

$notification->setApsId($apsId = Uuid::uuid4());

$notification->setCustomKey('mdm', '4DA9FEC7-5443-48B3-9491-892F1147BE47');

$response = $client->send($notification);

$this->assertSame((string) $apsId, (string) $response->getApnsId());
$this->assertSame(200, $response->getCode());
}

public function testItCanSendBatchOfNotifications()
{
$tokenKey = $this->makeTokenKey();

$jwt = JWT::new($this->teamId, $tokenKey);

if ($jwt->hasExpired()) {
$jwt->refresh($tokenKey);
}

$client = Client::auth($jwt);

$notification = new Notification("51d5f3696c9cc62caf322fbcfd0b25a455697b1c3261eb4ed085041c6e895bdb");

$notification->setApsId($apsId = Uuid::uuid4());

$notification->setPushTopic(file_get_contents('certs/apns-topic.txt'));

$notification->setCustomKey('mdm', '4DA9FEC7-5443-48B3-9491-892F1147BE47');

$response = $client->send($notification);

$this->assertSame((string) $apsId, (string) $response->getApnsId());
$this->assertSame(200, $response->getCode());
}
}
22 changes: 22 additions & 0 deletions tests/CreateCertificateTrust.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: sasa.blagojevic@mail.com
* Date: 09/09/2020
* Time: 18:08
*/

namespace SasaB\Tests;


use SasaB\Apns\Provider\Certificate;

trait CreateCertificateTrust
{
protected function makeCertificate(): Certificate
{
$filePath = 'certs/PushCert.pem';

return Certificate::fromFile($filePath);
}
}
28 changes: 28 additions & 0 deletions tests/CreateTokenKeyTrust.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Created by PhpStorm.
* User: sasa.blagojevic@mail.com
* Date: 09/09/2020
* Time: 18:07
*/

namespace SasaB\Tests;


use SasaB\Apns\Provider\TokenKey;

trait CreateTokenKeyTrust
{
protected $teamId;

protected function makeTokenKey(): TokenKey
{
$keyId = file_get_contents('certs/key-id.txt');
$this->teamId = file_get_contents('certs/team-id.txt');

$tokenKey = new TokenKey($keyId);
$tokenKey->loadFromFile('certs/AuthKey.p8');

return $tokenKey;
}
}
20 changes: 18 additions & 2 deletions tests/ProviderTest/CertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,29 @@

use PHPUnit\Framework\TestCase;
use SasaB\Apns\Provider\Certificate;
use SasaB\Tests\CreateCertificateTrust;

class CertificateTest extends TestCase
{
use CreateCertificateTrust;

public function testItCanBeCreatedFromFile()
{
$file = '';
$certificate = Certificate::fromFile($file);
$certificate = $this->makeCertificate();

$authOptions = $certificate->getAuthOptions();

$this->assertArraySubset(['cert' => $certificate->getFilePath()], $authOptions);

$this->assertArrayHasKey('headers', $authOptions);

$this->assertArrayHasKey('apns-topic', $authOptions['headers']);
}

public function testItThrowsWhenCertFileIsNotFound()
{
$this->expectException(\InvalidArgumentException::class);

Certificate::fromFile('/no-file.txt');
}
}
38 changes: 38 additions & 0 deletions tests/ProviderTest/TokenKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,47 @@
namespace SasaB\Tests\ProviderTest;


use Lcobucci\JWT\Token;
use SasaB\Apns\Provider\JWT;
use SasaB\Apns\Provider\TokenKey;

use PHPUnit\Framework\TestCase;
use SasaB\Tests\CreateTokenKeyTrust;

class TokenKeyTest extends TestCase
{
use CreateTokenKeyTrust;

private $teamId;

public function testItCanCreateValidJWT()
{
$tokenKey = $this->makeTokenKey();

$jwt = JWT::new($this->teamId, $tokenKey);

$this->assertInstanceOf(Token::class, $jwt->getToken());
$this->assertRegExp('/^[a-zA-Z0-9\-_]+?\.[a-zA-Z0-9\-_]+?\.([a-zA-Z0-9\-_]+)?$/', (string) $jwt->getToken());
}

public function testItCanProvideAuthHeader()
{
$tokenKey = $this->makeTokenKey();

$jwt = JWT::new($this->teamId, $tokenKey);

$this->assertSame([
'headers' => ['authorization' => 'Bearer '.$jwt->getToken()]
], $jwt->getAuthOptions());
}

public function testItCanCheckIfTokenExpired()
{
$tokenKey = $this->makeTokenKey();

$jwt = JWT::new($this->teamId, $tokenKey);

$this->assertFalse($jwt->hasExpired());
}

}

0 comments on commit cde8610

Please sign in to comment.