Skip to content

Commit

Permalink
Added tests to phpstan checking
Browse files Browse the repository at this point in the history
  • Loading branch information
dutchie027 committed Apr 10, 2022
1 parent 3f2b7b0 commit 2f3f3e0
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 75 deletions.
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ parameters:
treatPhpDocTypesAsCertain: false
paths:
- ./src
- ./tests
2 changes: 1 addition & 1 deletion src/AuthProvider/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private function __construct(array $options)
/**
* Create Token Auth Provider.
*
* @param array<string> $options
* @param array<array-key, string> $options
*/
public static function create(array $options): Token
{
Expand Down
8 changes: 5 additions & 3 deletions src/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public function setUrlArgs(array $value): Payload
/**
* Set custom value for Payload.
*
* @param string $value
* @param mixed $value either string or array depending on key
*
* @throws InvalidPayloadException
*/
Expand All @@ -305,7 +305,7 @@ public function setCustomValue(string $key, $value): Payload
/**
* Merges custom value for Payload.
*
* @param string $value
* @param mixed $value either string or array depending on key
*
* @throws InvalidPayloadException
*/
Expand All @@ -322,8 +322,10 @@ public function addCustomValue(string $key, $value): Payload
* @param string $key
*
* @throws InvalidPayloadException
*
* @return mixed either string or array based on key
*/
public function getCustomValue($key): string
public function getCustomValue($key)
{
if (!array_key_exists($key, $this->customValues)) {
throw InvalidPayloadException::notExistingCustomValue($key);
Expand Down
15 changes: 10 additions & 5 deletions tests/AuthProvider/CertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

class CertificateTest extends TestCase
{
public function testCreatingCertificateAuthProvider()
public function testCreatingCertificateAuthProvider(): void
{
$options = $this->getOptions();
$authProvider = Certificate::create($options);

self::assertInstanceOf(AuthProviderInterface::class, $authProvider);
}

public function testAuthenticatingClient()
public function testAuthenticatingClient(): void
{
$options = $this->getOptions();
$authProvider = Certificate::create($options);
Expand All @@ -30,7 +30,7 @@ public function testAuthenticatingClient()
self::assertSame($request->getOptions()[CURLOPT_SSLCERTPASSWD], $options['certificate_secret']);
}

public function testVoipApnsTopic()
public function testVoipApnsTopic(): void
{
$options = $this->getOptions();
$authProvider = Certificate::create($options);
Expand All @@ -41,7 +41,7 @@ public function testVoipApnsTopic()
self::assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.voip');
}

public function testComplicationApnsTopic()
public function testComplicationApnsTopic(): void
{
$options = $this->getOptions();
$authProvider = Certificate::create($options);
Expand All @@ -52,7 +52,7 @@ public function testComplicationApnsTopic()
self::assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.complication');
}

public function testFileproviderApnsTopic()
public function testFileproviderApnsTopic(): void
{
$options = $this->getOptions();
$authProvider = Certificate::create($options);
Expand All @@ -63,6 +63,11 @@ public function testFileproviderApnsTopic()
self::assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.pushkit.fileprovider');
}

/**
* Undocumented function
*
* @return array<string,string>
*/
private function getOptions()
{
return [
Expand Down
21 changes: 13 additions & 8 deletions tests/AuthProvider/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class TokenTest extends TestCase
{
public function testCreatingTokenAuthProviderWithKeyPath()
public function testCreatingTokenAuthProviderWithKeyPath(): void
{
$options = $this->getOptions();
$options['private_key_path'] = __DIR__ . '/../files/private_key.p8';
Expand All @@ -20,27 +20,27 @@ public function testCreatingTokenAuthProviderWithKeyPath()
self::assertIsString($authProvider->get());
}

public function testCreatingTokenAuthProviderWithKeyContent()
public function testCreatingTokenAuthProviderWithKeyContent(): void
{
$options = $this->getOptions();
$options['private_key_content'] = file_get_contents(
implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'files', 'private_key.p8'])
);
) ?: '';
$authProvider = Token::create($options);

self::assertInstanceOf(AuthProviderInterface::class, $authProvider);
self::assertIsString($authProvider->get());
}

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

$options = $this->getOptions();
Token::create($options);
}

public function testUseExistingToken()
public function testUseExistingToken(): void
{
$token = 'eyJhbGciOiJFUzI1NiIsImtpZCI6IjEyMzQ1Njc4OTAifQ.eyJpc3MiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNDc4NTE0NDk4fQ.' .
'YxR8Hw--Hp8YH8RF2QDiwOjmGhTC_7g2DpbnzKQZ8Sj20-q12LrLrAMafcuxf97CTHl9hCVere0vYrzcLmGV-A';
Expand All @@ -52,7 +52,7 @@ public function testUseExistingToken()
self::assertEquals($token, $authProvider->get());
}

public function testVoipApnsTopic()
public function testVoipApnsTopic(): void
{
$options = $this->getOptions();
$options['private_key_path'] = __DIR__ . '/../files/private_key.p8';
Expand All @@ -64,7 +64,7 @@ public function testVoipApnsTopic()
self::assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.voip');
}

public function testComplicationApnsTopic()
public function testComplicationApnsTopic(): void
{
$options = $this->getOptions();
$options['private_key_path'] = __DIR__ . '/../files/private_key.p8';
Expand All @@ -76,7 +76,7 @@ public function testComplicationApnsTopic()
self::assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.complication');
}

public function testFileproviderApnsTopic()
public function testFileproviderApnsTopic(): void
{
$options = $this->getOptions();
$options['private_key_path'] = __DIR__ . '/../files/private_key.p8';
Expand All @@ -88,6 +88,11 @@ public function testFileproviderApnsTopic()
self::assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.pushkit.fileprovider');
}

/**
* Set Options
*
* @return array<string,string>
*/
private function getOptions()
{
return [
Expand Down
4 changes: 2 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ClientTest extends TestCase
{
public function testAmountOfAddedMessages()
public function testAmountOfAddedMessages(): void
{
$notification = $this->createMock(Notification::class);
$notification2 = $this->createMock(Notification::class);
Expand All @@ -32,7 +32,7 @@ private function getClient(): Client
return new Client($authProvider, $production = false);
}

public function testPrepareHandle()
public function testPrepareHandle(): void
{
$client = $this->getClient();

Expand Down
4 changes: 2 additions & 2 deletions tests/InvalidPayloadExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

class InvalidPayloadExceptionTest extends TestCase
{
public function testReservedKey()
public function testReservedKey(): void
{
$exception = InvalidPayloadException::reservedKey();

self::assertTrue(is_a($exception, \Exception::class));
self::assertStringContainsString(Payload::PAYLOAD_ROOT_KEY, $exception->getMessage());
}

public function testNotExistingCustomValue()
public function testNotExistingCustomValue(): void
{
$key = 'this is a string';

Expand Down
17 changes: 7 additions & 10 deletions tests/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

class NotificationTest extends TestCase
{
public function testGetDeviceToken()
public function testGetDeviceToken(): void
{
$message = new Notification(Payload::create(), 'deviceTokenString');

self::assertSame('deviceTokenString', $message->getDeviceToken());
}

public function testGetPayload()
public function testGetPayload(): void
{
$payload = Payload::create();

Expand All @@ -24,7 +24,7 @@ public function testGetPayload()
self::assertSame($payload, $message->getPayload());
}

public function testId()
public function testId(): void
{
$message = new Notification(Payload::create(), 'deviceTokenString');

Expand All @@ -34,7 +34,7 @@ public function testId()
self::assertSame($id, $message->getId());
}

public function testExpirationAt()
public function testExpirationAt(): void
{
$message = new Notification(Payload::create(), 'deviceTokenString');

Expand All @@ -43,13 +43,10 @@ public function testExpirationAt()

$message->setExpirationAt($expire);

// Change object to see unwanted behaviour with object references
$expire->modify('+2 days');

self::assertSame($expected, $message->getExpirationAt()->getTimestamp());
self::assertSame($expected, $message->getExpirationAt()->getTimestamp()); /** @phpstan-ignore-line */
}

public function testPriority()
public function testPriority(): void
{
$message = new Notification(Payload::create(), 'deviceTokenString');

Expand All @@ -60,7 +57,7 @@ public function testPriority()
self::assertSame(Notification::PRIORITY_LOW, $message->getPriority());
}

public function testCollapseId()
public function testCollapseId(): void
{
$message = new Notification(Payload::create(), 'deviceTokenString');

Expand Down
20 changes: 10 additions & 10 deletions tests/Payload/AlertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,70 @@

class AlertTest extends TestCase
{
public function testSetTitle()
public function testSetTitle(): void
{
$alert = Alert::create()->setTitle('title');

self::assertEquals('title', $alert->getTitle());
}

public function testSetSubtitle()
public function testSetSubtitle(): void
{
$alert = Alert::create()->setSubtitle('subtitle');

self::assertEquals('subtitle', $alert->getSubtitle());
}

public function testSetBody()
public function testSetBody(): void
{
$alert = Alert::create()->setBody('body');

self::assertEquals('body', $alert->getBody());
}

public function testSetTitleLocKey()
public function testSetTitleLocKey(): void
{
$alert = Alert::create()->setTitleLocKey('title-loc-key');

self::assertEquals('title-loc-key', $alert->getTitleLocKey());
}

public function testSetTitleLocArgs()
public function testSetTitleLocArgs(): void
{
$alert = Alert::create()->setTitleLocArgs(['title1', 'title2']);

self::assertEquals(['title1', 'title2'], $alert->getTitleLocArgs());
}

public function testSetActionLocKey()
public function testSetActionLocKey(): void
{
$alert = Alert::create()->setActionLocKey('action-loc-key');

self::assertEquals('action-loc-key', $alert->getActionLocKey());
}

public function testSetLocKey()
public function testSetLocKey(): void
{
$alert = Alert::create()->setLocKey('loc-key');

self::assertEquals('loc-key', $alert->getLocKey());
}

public function testSetLocArgs()
public function testSetLocArgs(): void
{
$alert = Alert::create()->setLocArgs(['loc-arg1', 'loc-arg2']);

self::assertEquals(['loc-arg1', 'loc-arg2'], $alert->getLocArgs());
}

public function testSetLaunchImage()
public function testSetLaunchImage(): void
{
$alert = Alert::create()->setLaunchImage('launch-image');

self::assertEquals('launch-image', $alert->getLaunchImage());
}

public function testAlertConvertingToJson()
public function testAlertConvertingToJson(): void
{
$alert = Alert::create()
->setTitle('title')
Expand Down
8 changes: 4 additions & 4 deletions tests/Payload/SoundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@

class SoundTest extends TestCase
{
public function testSetCritical()
public function testSetCritical(): void
{
$sound = Sound::create()->setCritical(1);

self::assertEquals(1, $sound->getCritical());
}

public function testSetName()
public function testSetName(): void
{
$sound = Sound::create()->setName('soundName');

self::assertEquals('soundName', $sound->getName());
}

public function testSetVolume()
public function testSetVolume(): void
{
$sound = Sound::create()->setVolume(1.0);

self::assertEquals(1.0, $sound->getVolume());
}

public function testSoundConvertingToJson()
public function testSoundConvertingToJson(): void
{
$sound = Sound::create()
->setCritical(1)
Expand Down
Loading

0 comments on commit 2f3f3e0

Please sign in to comment.