Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group List implementation fixes #29 #30

Merged
merged 33 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
57f37f1
Group List implementation #29
Vitexus Aug 12, 2024
2061543
Process only correct response #29
Vitexus Aug 12, 2024
6301ee8
missing use added #29
Vitexus Aug 12, 2024
12ad4c5
Update tests/Client/GroupsClientTest.php
Vitexus Aug 12, 2024
1e4e28b
ListGroupResponse.php renamed to ListGroupsResponse.php
Vitexus Aug 12, 2024
0115681
php-cs-fixer fix src used
Vitexus Aug 12, 2024
6f9e012
Group and GroupTest fix
Vitexus Aug 12, 2024
0c32fd9
The api response parsing to array of groups test.
Vitexus Aug 12, 2024
9a601ce
cs fixer used
Vitexus Aug 12, 2024
959246b
returned class fix
Vitexus Aug 12, 2024
ba9e3c7
argument type fix
Vitexus Aug 12, 2024
92b74de
Merge branch 'master' into listgroups
Vitexus Aug 25, 2024
8ebf1f6
Update PHPDoc for Group.php
Vitexus Aug 26, 2024
df1719c
comments must be max 80 characters per line long
Vitexus Aug 26, 2024
ec4fb11
missing variable for $group added
Vitexus Aug 26, 2024
5da5336
ListGroupsResponseTest phpdoc fix
Vitexus Aug 26, 2024
bf1bea5
Merge branch 'master' into listgroups
Vitexus Aug 28, 2024
cb274ca
php-cs-fixer used
Vitexus Aug 28, 2024
1bee04e
Merge branch 'master' of github.com:Vitexus/pushover-php into listgroups
Vitexus Aug 28, 2024
9ed03b2
Update Example/GroupsExample.php
Vitexus Aug 28, 2024
2579c06
Update tests/Client/Response/ListGroupsResponseTest.php
Vitexus Aug 28, 2024
c7d8ba6
Update src/Client/GroupsClient.php
Vitexus Aug 28, 2024
dcfcf5b
Update tests/Client/Response/ListGroupsResponseTest.php
Vitexus Aug 28, 2024
3a2b4bd
Update tests/Client/Response/ListGroupsResponseTest.php
Vitexus Aug 28, 2024
a0f23ac
Update src/Client/Response/ListGroupsResponse.php
Vitexus Aug 28, 2024
8a03435
Update tests/Client/Response/ListGroupsResponseTest.php
Vitexus Aug 28, 2024
9429cce
Update src/Client/Response/ListGroupsResponse.php
Vitexus Aug 28, 2024
162e911
Update src/Client/Response/ListGroupsResponse.php
Vitexus Aug 28, 2024
cba4a05
Update src/Client/Response/ListGroupsResponse.php
Vitexus Aug 28, 2024
0e16a78
Update src/Client/GroupsClient.php
slunak Aug 28, 2024
2b65e6c
Merge branch 'master' of https://github.com/slunak/pushover-php into …
slunak Aug 28, 2024
f79c224
cs-fixer
slunak Aug 28, 2024
7cad5e4
Update src/Client/Response/Base/Response.php
slunak Aug 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Example/GroupsExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function groupsExample()
$createGroupResponse = $group->create('Test');
$newGroupKey = $createGroupResponse->getGroupKey();

// Obtain list of all groups
$listGroupsResponse = $group->list();
/** @var array<string, string> $groups ['name' => 'key', 'name2' => 'key2'] */
$groups = $listGroupsResponse->getGroups();

// Retrieve information about the group from the API and populate the object with it.
/** @var RetrieveGroupResponse $retrieveGroupResponse */
$retrieveGroupResponse = $group->retrieveGroupInformation();
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Light, simple and fast, yet comprehensive wrapper for the [Pushover](https://pus
- Cancel emergency priority retry
- Groups API ([Example](Example/GroupsExample.php))
- Create a group
- List groups
- Retrieve information about the group
- Add / Remove users
- Enable / Disable users
Expand Down
32 changes: 25 additions & 7 deletions src/Api/Groups/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Serhiy\Pushover\Client\Response\CreateGroupResponse;
use Serhiy\Pushover\Client\Response\DisableUserInGroupResponse;
use Serhiy\Pushover\Client\Response\EnableUserInGroupResponse;
use Serhiy\Pushover\Client\Response\ListGroupsResponse;
use Serhiy\Pushover\Client\Response\RemoveUserFromGroupResponse;
use Serhiy\Pushover\Client\Response\RenameGroupResponse;
use Serhiy\Pushover\Client\Response\RetrieveGroupResponse;
Expand All @@ -30,10 +31,13 @@
/**
* Pushover Delivery Group.
*
* Delivery Groups allow broadcasting the same Pushover message to a number of different users at once with just a single group token,
* used in place of a user token (or in place of specifying multiple user keys in every request). For situations where subscriptions are not appropriate,
* or where automated manipulation of group members is required, such as changing an on-call notification group,
* or syncing with an external directory system, our Groups API is available.
* Delivery Groups allow broadcasting the same Pushover message to a number of
* different users at once with just a single group token, used in place of a
* user token (or in place of specifying multiple user keys in every request).
* For situations where subscriptions are not appropriate, or where automated
* manipulation of group members is required, such as changing an on-call
* notification group, or syncing with an external directory system, our Groups
* API is available.
*
* @author Serhiy Lunak
*/
Expand Down Expand Up @@ -136,12 +140,26 @@ public function create(string $name): CreateGroupResponse
return $response;
}

/**
* List groups.
*/
public function list(): ListGroupsResponse
{
$client = new GroupsClient($this, GroupsClient::ACTION_LIST_GROUPS);
$request = new Request($client->buildApiUrl(), Request::GET);

$curlResponse = Curl::do($request);

$response = new ListGroupsResponse($curlResponse);
$response->setRequest($request);

return $response;
}

/**
* Adds an existing Pushover user to your Delivery Group.
*
* @return AddUserToGroupResponse
*/
public function addUser(Recipient $recipient)
public function addUser(Recipient $recipient): AddUserToGroupResponse
{
$client = new GroupsClient($this, GroupsClient::ACTION_ADD_USER);
$request = new Request($client->buildApiUrl(), Request::POST, $client->buildCurlPostFields($recipient));
Expand Down
1 change: 0 additions & 1 deletion src/Client/Curl/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
class Curl
{
public const API_BASE_URL = 'https://api.pushover.net';

public const API_VERSION = '1';

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Client/GroupsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ class GroupsClient extends Client implements ClientInterface
public const ACTION_ENABLE_USER = 'enable_user';
public const ACTION_RENAME_GROUP = 'rename';
public const ACTION_CREATE_GROUP = 'create';
private Group $group;
public const ACTION_LIST_GROUPS = 'list';
Vitexus marked this conversation as resolved.
Show resolved Hide resolved

/**
* Action that client performs.
*/
private string $action;
private Group $group;

public function __construct(Group $group, string $action)
{
Expand All @@ -53,6 +54,10 @@ public function buildApiUrl(): string
return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups.json';
}

if ($this->action === self::ACTION_LIST_GROUPS) {
return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups.json?token='.$this->group->getApplication()->getToken();
}

if ($this->action === self::ACTION_RETRIEVE_GROUP) {
return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups/'.$this->group->getKey().'.json?token='.$this->group->getApplication()->getToken();
}
Expand Down
59 changes: 59 additions & 0 deletions src/Client/Response/ListGroupsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Pushover package.
*
* (c) Serhiy Lunak <https://github.com/slunak>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Serhiy\Pushover\Client\Response;

use Serhiy\Pushover\Client\Response\Base\Response;

/**
* @author Vítězslav Dvořák
*/
final class ListGroupsResponse extends Response
{
/**
* @var array<string, string>
*/
public array $groups = [];
Vitexus marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param mixed $curlResponse
*/
public function __construct($curlResponse)
{
$this->processCurlResponse($curlResponse);
}

/**
* List of groups.
*
* @return array<string, string> group names with keys eg. ['name' => 'key', ...]
*/
public function getGroups(): array
{
return $this->groups;
}

/**
* @param mixed $curlResponse
*/
private function processCurlResponse($curlResponse): void
{
$decodedCurlResponse = $this->processInitialCurlResponse($curlResponse);

if (property_exists($decodedCurlResponse, 'groups')) {
foreach ($decodedCurlResponse->groups as $grp) {
$this->groups[$grp->name] = $grp->group;
}
}
}
}
14 changes: 14 additions & 0 deletions tests/Api/Groups/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Serhiy\Pushover\Api\Groups\Group;
use Serhiy\Pushover\Application;
use Serhiy\Pushover\Client\Response\CreateGroupResponse;
use Serhiy\Pushover\Client\Response\ListGroupsResponse;
use Serhiy\Pushover\Client\Response\RetrieveGroupResponse;

/**
Expand Down Expand Up @@ -76,4 +77,17 @@ public function testCreate(): void

$this->assertInstanceOf(CreateGroupResponse::class, $response);
}

/**
* @group Integration
*/
public function testList(): void
{
$application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token
$group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application);

$response = $group->list();

$this->assertInstanceOf(ListGroupsResponse::class, $response);
}
}
3 changes: 3 additions & 0 deletions tests/Client/GroupsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function testBuildApiUrl(): void

$client = new GroupsClient($group, GroupsClient::ACTION_ADD_USER);
$this->assertEquals('https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66/add_user.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl());

$client = new GroupsClient($group, GroupsClient::ACTION_LIST_GROUPS);
$this->assertSame('https://api.pushover.net/1/groups.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl());
}

public function testBuildCurlPostFields(): void
Expand Down
41 changes: 41 additions & 0 deletions tests/Client/Response/ListGroupsResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Pushover package.
*
* (c) Serhiy Lunak <https://github.com/slunak>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Client\Response;

use PHPUnit\Framework\TestCase;
use Serhiy\Pushover\Client\Response\ListGroupsResponse;

final class ListGroupsResponseTest extends TestCase
{
public function testCanBeCreated(): ListGroupsResponse
{
$successfulCurlResponse = '{"groups":[{"group":"111111111111111111111111111111","name":"Group1"},{"group":"222222222222222222222222222222","name":"group2"},{"group":"333333333333333333333333333333","name":"Group 3"}],"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}';
$response = new ListGroupsResponse($successfulCurlResponse);

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

return $response;
}

/**
* @depends testCanBeCreated
*/
public function testGetGroups(ListGroupsResponse $response): void
{
$groups = $response->getGroups();
$this->assertSame($groups, ['Group1' => '111111111111111111111111111111', 'group2' => '222222222222222222222222222222', 'Group 3' => '333333333333333333333333333333']);
}
}
Loading