Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark committed Sep 17, 2024
1 parent cbe441d commit cd26c85
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 140 deletions.
12 changes: 6 additions & 6 deletions src/Client/MessageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function buildApiUrl(): string
/**
* Builds array for CURLOPT_POSTFIELDS curl argument.
*
* @return array<string, (null|\CURLFile|string)>
* @return array<string, (null|\CURLFile|int|string)>
*/
public function buildCurlPostFields(Notification $notification): array
{
Expand Down Expand Up @@ -73,11 +73,11 @@ public function buildCurlPostFields(Notification $notification): array
}

if (null !== $notification->getMessage()->getPriority()) {
$curlPostFields['priority'] = (string) $notification->getMessage()->getPriority()->getPriority();
$curlPostFields['priority'] = $notification->getMessage()->getPriority()->getPriority();

if (Priority::EMERGENCY === $notification->getMessage()->getPriority()->getPriority()) {
$curlPostFields['retry'] = (string) $notification->getMessage()->getPriority()->getRetry();
$curlPostFields['expire'] = (string) $notification->getMessage()->getPriority()->getExpire();
$curlPostFields['retry'] = $notification->getMessage()->getPriority()->getRetry();
$curlPostFields['expire'] = $notification->getMessage()->getPriority()->getExpire();

if (null !== $notification->getMessage()->getPriority()->getCallback()) {
$curlPostFields['callback'] = $notification->getMessage()->getPriority()->getCallback();
Expand All @@ -86,11 +86,11 @@ public function buildCurlPostFields(Notification $notification): array
}

if (true === $notification->getMessage()->getIsHtml()) {
$curlPostFields['html'] = '1';
$curlPostFields['html'] = 1;
}

if (null !== $notification->getMessage()->getTtl()) {
$curlPostFields['ttl'] = (string) $notification->getMessage()->getTtl();
$curlPostFields['ttl'] = $notification->getMessage()->getTtl();
}

if (null !== $notification->getSound()) {
Expand Down
10 changes: 5 additions & 5 deletions tests/Api/Glances/GlanceDataFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function setTitleProvider(): iterable
yield ['', ''];
}

public function testSetTitleThrowsInvalidArgumentExeptionIfValueIsAbove100(): void
public function testSetTitleThrowsInvalidArgumentExceptionIfValueIsAbove100(): void
{
$this->expectException(InvalidArgumentException::class);
(new GlanceDataFields())->setTitle(str_repeat('a', 101));
Expand All @@ -71,7 +71,7 @@ public static function setTextProvider(): iterable
yield ['', ''];
}

public function testSetTextThrowsInvalidArgumentExeptionIfValueIsAbove100(): void
public function testSetTextThrowsInvalidArgumentExceptionIfValueIsAbove100(): void
{
$this->expectException(InvalidArgumentException::class);
(new GlanceDataFields())->setText(str_repeat('a', 101));
Expand All @@ -96,7 +96,7 @@ public static function setSubtextProvider(): iterable
yield ['', ''];
}

public function testSetSubtextThrowsInvalidArgumentExeptionIfValueIsAbove100(): void
public function testSetSubtextThrowsInvalidArgumentExceptionIfValueIsAbove100(): void
{
$this->expectException(InvalidArgumentException::class);
(new GlanceDataFields())->setSubtext(str_repeat('a', 101));
Expand All @@ -121,13 +121,13 @@ public static function setCountProvider(): iterable
yield [1000];
}

public function testSetPercentThrowsInvalidArgumentExeptionIfValueIsAbove100(): void
public function testSetPercentThrowsInvalidArgumentExceptionIfValueIsAbove100(): void
{
$this->expectException(InvalidArgumentException::class);
(new GlanceDataFields())->setPercent(101);
}

public function testSetPercentThrowsInvalidArgumentExeptionIfValueIsBelowMinus1(): void
public function testSetPercentThrowsInvalidArgumentExceptionIfValueIsBelowMinus1(): void
{
$this->expectException(InvalidArgumentException::class);
(new GlanceDataFields())->setPercent(-1);
Expand Down
17 changes: 8 additions & 9 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
* file that was distributed with this source code.
*/

use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;
use Serhiy\Pushover\Application;
use Serhiy\Pushover\Exception\InvalidArgumentException;

final class ApplicationTest extends TestCase
{
public function testCanBeConstructed(): Application
public function testCanBeConstructed(): void
{
$application = new Application('cccc3333CCCC3333dddd4444DDDD44');

$this->assertInstanceOf(Application::class, $application);

return $application;
$this->assertInstanceOf(
Application::class,
new Application('cccc3333CCCC3333dddd4444DDDD44'),
);
}

public function testCannotBeConstructed(): void
Expand All @@ -45,9 +43,10 @@ public function testCannotBeConstructedFromShortApiToken(): void
new Application('token');
}

#[Depends('testCanBeConstructed')]
public function testGetToken(Application $application): void
public function testGetToken(): void
{
$application = new Application('cccc3333CCCC3333dddd4444DDDD44');

$this->assertSame('cccc3333CCCC3333dddd4444DDDD44', $application->getToken());
}
}
10 changes: 5 additions & 5 deletions tests/Client/MessageClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public function testBuildCurlPostFields(): void
$this->assertSame('This is a title of the message', $curlPostFields['title']);
$this->assertSame('https://www.example.com', $curlPostFields['url']);
$this->assertSame('Example.com', $curlPostFields['url_title']);
$this->assertSame('2', $curlPostFields['priority']);
$this->assertSame('30', $curlPostFields['retry']);
$this->assertSame('300', $curlPostFields['expire']);
$this->assertSame(2, $curlPostFields['priority']);
$this->assertSame(30, $curlPostFields['retry']);
$this->assertSame(300, $curlPostFields['expire']);
$this->assertSame('https://callback.example.com', $curlPostFields['callback']);
$this->assertSame('1', $curlPostFields['html']);
$this->assertSame('86400', $curlPostFields['ttl']);
$this->assertSame(1, $curlPostFields['html']);
$this->assertSame(86400, $curlPostFields['ttl']);
$this->assertSame('pushover', $curlPostFields['sound']);
}
}
8 changes: 2 additions & 6 deletions tests/Client/Response/AddUserToGroupResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ final class AddUserToGroupResponseTest extends TestCase
{
public function testCenBeCreated(): void
{
$successfulCurlResponse = '{"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new AddUserToGroupResponse($successfulCurlResponse);

$response = new AddUserToGroupResponse('{"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(AddUserToGroupResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());

$unSuccessfulCurlResponse = '{"user":"invalid","errors":["user is already a member of this group"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new AddUserToGroupResponse($unSuccessfulCurlResponse);

$response = new AddUserToGroupResponse('{"user":"invalid","errors":["user is already a member of this group"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(AddUserToGroupResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());
Expand Down
8 changes: 2 additions & 6 deletions tests/Client/Response/CancelRetryResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ final class CancelRetryResponseTest extends TestCase
{
public function testCenBeCreated(): void
{
$successfulCurlResponse = '{"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new CancelRetryResponse($successfulCurlResponse);

$response = new CancelRetryResponse('{"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(CancelRetryResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());

$unSuccessfulCurlResponse = '{"receipt":"not found","errors":["receipt not found; may be invalid or expired"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new CancelRetryResponse($unSuccessfulCurlResponse);

$response = new CancelRetryResponse('{"receipt":"not found","errors":["receipt not found; may be invalid or expired"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(CancelRetryResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());
Expand Down
8 changes: 2 additions & 6 deletions tests/Client/Response/DisableUserInGroupResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ final class DisableUserInGroupResponseTest extends TestCase
{
public function testCenBeCreated(): void
{
$successfulCurlResponse = '{"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new DisableUserInGroupResponse($successfulCurlResponse);

$response = new DisableUserInGroupResponse('{"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(DisableUserInGroupResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());

$unSuccessfulCurlResponse = '{"user":"invalid","errors":["user is not a member of this group"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new DisableUserInGroupResponse($unSuccessfulCurlResponse);

$response = new DisableUserInGroupResponse('{"user":"invalid","errors":["user is not a member of this group"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(DisableUserInGroupResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());
Expand Down
8 changes: 2 additions & 6 deletions tests/Client/Response/EnableUserInGroupResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ final class EnableUserInGroupResponseTest extends TestCase
{
public function testCenBeCreated(): void
{
$successfulCurlResponse = '{"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new EnableUserInGroupResponse($successfulCurlResponse);

$response = new EnableUserInGroupResponse('{"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(EnableUserInGroupResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());

$unSuccessfulCurlResponse = '{"user":"invalid","errors":["user is not a member of this group"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new EnableUserInGroupResponse($unSuccessfulCurlResponse);

$response = new EnableUserInGroupResponse('{"user":"invalid","errors":["user is not a member of this group"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(EnableUserInGroupResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());
Expand Down
8 changes: 2 additions & 6 deletions tests/Client/Response/GlancesResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ final class GlancesResponseTest extends TestCase
{
public function testCanBeConstructed(): void
{
$successfulCurlResponse = '{"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new GlancesResponse($successfulCurlResponse);

$response = new GlancesResponse('{"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(GlancesResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());

$unSuccessfulCurlResponse = '{"token":"invalid","errors":["application token is invalid"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new GlancesResponse($unSuccessfulCurlResponse);

$response = new GlancesResponse('{"token":"invalid","errors":["application token is invalid"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(GlancesResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());
Expand Down
11 changes: 3 additions & 8 deletions tests/Client/Response/LicenseResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ final class LicenseResponseTest extends TestCase
{
public function testCanBeConstructed(): void
{
$successfulCurlResponse = '{"credits":5,"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new LicenseResponse($successfulCurlResponse);

$response = new LicenseResponse('{"credits":5,"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(LicenseResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());
$this->assertSame(5, $response->getCredits());

$unSuccessfulCurlResponse = '{"token":"is out of available license credits","errors":["application is out of available license credits"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new LicenseResponse($unSuccessfulCurlResponse);

$response = new LicenseResponse('{"token":"is out of available license credits","errors":["application is out of available license credits"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');
$this->assertInstanceOf(LicenseResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());
Expand All @@ -42,8 +38,7 @@ public function testCanBeConstructed(): void

public function testGetCredits(): void
{
$curlResponse = '{"credits":5,"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new LicenseResponse($curlResponse);
$response = new LicenseResponse('{"credits":5,"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');

$this->assertSame(5, $response->getCredits());
}
Expand Down
18 changes: 7 additions & 11 deletions tests/Client/Response/ListGroupsResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,11 @@ final class ListGroupsResponseTest extends TestCase
{
public function testCanBeConstructed(): ListGroupsResponse
{
$response = new ListGroupsResponse(json_encode([
'groups' => [
['group' => '111111111111111111111111111111', 'name' => 'Group1'],
['group' => '222222222222222222222222222222', 'name' => 'group2'],
['group' => '333333333333333333333333333333', 'name' => 'Group 3'],
],
'status' => 1,
'request' => $request = 'aaaaaaaa-1111-bbbb-2222-cccccccccccc',
]));
$response = new ListGroupsResponse('{"groups":[{"group":"111111111111111111111111111111","name":"Group1"},{"group":"222222222222222222222222222222","name":"group2"},{"group":"333333333333333333333333333333","name":"Group 3"}],"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');

$this->assertInstanceOf(ListGroupsResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame($request, $response->getRequestToken());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());

return $response;
}
Expand All @@ -42,7 +34,11 @@ public function testCanBeConstructed(): ListGroupsResponse
public function testGetGroups(ListGroupsResponse $response): void
{
$this->assertSame(
['Group1' => '111111111111111111111111111111', 'group2' => '222222222222222222222222222222', 'Group 3' => '333333333333333333333333333333'],
[
'Group1' => '111111111111111111111111111111',
'group2' => '222222222222222222222222222222',
'Group 3' => '333333333333333333333333333333',
],
$response->getGroups(),
);
}
Expand Down
37 changes: 10 additions & 27 deletions tests/Client/Response/MessageResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,52 +23,35 @@ final class MessageResponseTest extends TestCase
{
public function testCanBeCreatedWithSuccessfulCurlResponse(): void
{
$response = new MessageResponse(json_encode([
'receipt' => $receipt = 'gggg7777GGGG7777hhhh8888HHHH88',
'status' => 1,
'request' => $request = 'aaaaaaaa-1111-bbbb-2222-cccccccccccc',
]));
$response = new MessageResponse('{"receipt":"gggg7777GGGG7777hhhh8888HHHH88","status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');

$this->assertInstanceOf(MessageResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame($request, $response->getRequestToken());
$this->assertSame($receipt, $response->getReceipt());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());
$this->assertSame('gggg7777GGGG7777hhhh8888HHHH88', $response->getReceipt());
}

public function testCanBeCreatedWitUnsuccessfulCurlResponse(): void
{
$response = new MessageResponse(json_encode([
'user' => 'invalid',
'errors' => $errors = ['user identifier is not a valid user, group, or subscribed user key'],
'status' => 0,
'request' => $request = 'aaaaaaaa-1111-bbbb-2222-cccccccccccc',
]));
$response = new MessageResponse('{"user":"invalid","errors":["user identifier is not a valid user, group, or subscribed user key"],"status":0,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');

$this->assertInstanceOf(MessageResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame($request, $response->getRequestToken());
$this->assertSame($errors, $response->getErrors());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());
$this->assertSame(['user identifier is not a valid user, group, or subscribed user key'], $response->getErrors());
}

public function testGetRequestToken(): void
{
$response = new MessageResponse(json_encode([
'receipt' => $receipt = 'gggg7777GGGG7777hhhh8888HHHH88',
'status' => 1,
'request' => $request = 'aaaaaaaa-1111-bbbb-2222-cccccccccccc',
]));
$response = new MessageResponse('{"receipt":"gggg7777GGGG7777hhhh8888HHHH88","status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');

$this->assertSame($request, $response->getRequestToken());
$this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken());
}

public function testGetReceipt(): void
{
$response = new MessageResponse(json_encode([
'receipt' => $receipt = 'gggg7777GGGG7777hhhh8888HHHH88',
'status' => 1,
'request' => 'aaaaaaaa-1111-bbbb-2222-cccccccccccc',
]));
$response = new MessageResponse('{"receipt":"gggg7777GGGG7777hhhh8888HHHH88","status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}');

$this->assertSame($receipt, $response->getReceipt());
$this->assertSame('gggg7777GGGG7777hhhh8888HHHH88', $response->getReceipt());
}
}
Loading

0 comments on commit cd26c85

Please sign in to comment.