Skip to content

Commit

Permalink
Enhancement: Ensure Recipient
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark committed Sep 2, 2024
1 parent de4a95f commit 98f8a19
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 81 deletions.
62 changes: 24 additions & 38 deletions src/Client/GroupsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,23 @@ class GroupsClient extends Client implements ClientInterface
public const ACTION_LIST_GROUPS = 'list';

/**
* @param string $action Action that client performs
* @param self::ACTION_* $action Action that client performs
*/
public function __construct(
private readonly Group $group,
private readonly string $action,
) {
if (!$this->isActionValid($action)) {
throw new InvalidArgumentException('Action argument provided to construct method is invalid.');
}
$this->validateAction($action);
}

public function buildApiUrl(): string
{
if ($this->action === self::ACTION_CREATE_GROUP) {
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();
}

return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups/'.$this->group->getKey().'/'.$this->action.'.json?token='.$this->group->getApplication()->getToken();
return match ($this->action) {
self::ACTION_CREATE_GROUP => sprintf('%s/%s/groups.json', Curl::API_BASE_URL, Curl::API_VERSION),
self::ACTION_LIST_GROUPS => sprintf('%s/%s/groups.json?token=%s', Curl::API_BASE_URL, Curl::API_VERSION, $this->group->getApplication()->getToken()),
self::ACTION_RETRIEVE_GROUP => sprintf('%s/%s/groups/%s.json?token=%s', Curl::API_BASE_URL, Curl::API_VERSION, $this->group->getKey(), $this->group->getApplication()->getToken()),
default => sprintf('%s/%s/groups/%s/%s.json?token=%s', Curl::API_BASE_URL, Curl::API_VERSION, $this->group->getKey(), $this->action, $this->group->getApplication()->getToken()),
};
}

/**
Expand All @@ -72,28 +63,25 @@ public function buildCurlPostFields(?Recipient $recipient = null): array
'token' => $this->group->getApplication()->getToken(),
];

if (
$this->action === self::ACTION_ADD_USER
|| $this->action === self::ACTION_REMOVE_USER
|| $this->action === self::ACTION_DISABLE_USER
|| $this->action === self::ACTION_ENABLE_USER
) {
if (\in_array($this->action, [self::ACTION_ADD_USER, self::ACTION_REMOVE_USER, self::ACTION_DISABLE_USER, self::ACTION_ENABLE_USER], true)) {
if (!$recipient instanceof Recipient) {
throw new \LogicException('Recipient object must be provided for this action.');
}

$curlPostFields['user'] = $recipient->getUserKey();
}

if ($this->action === self::ACTION_ADD_USER) {
if (!empty($recipient->getDevice())) {
$curlPostFields['device'] = $recipient->getDevice()[0];
}
if ($this->action === self::ACTION_ADD_USER) {
if (!empty($recipient->getDevice())) {
$curlPostFields['device'] = $recipient->getDevice()[0];
}

if (null !== $recipient->getMemo()) {
$curlPostFields['memo'] = $recipient->getMemo();
if (null !== $recipient->getMemo()) {
$curlPostFields['memo'] = $recipient->getMemo();
}
}
}

if ($this->action === self::ACTION_RENAME_GROUP
|| $this->action === self::ACTION_CREATE_GROUP
) {
if (\in_array($this->action, [self::ACTION_RENAME_GROUP, self::ACTION_CREATE_GROUP], true)) {
$curlPostFields['name'] = $this->group->getName();
}

Expand All @@ -103,14 +91,12 @@ public function buildCurlPostFields(?Recipient $recipient = null): array
/**
* Checks if action that was provided into construct is valid.
*/
private function isActionValid(string $action): bool
private function validateAction(string $action): void
{
$oClass = new \ReflectionClass(self::class);

if (\in_array($action, $oClass->getConstants(), true)) {
return true;
if (!\in_array($action, $oClass->getConstants(), true)) {
throw new InvalidArgumentException('Action argument provided to construct method is invalid.');
}

return false;
}
}
121 changes: 78 additions & 43 deletions tests/Client/GroupsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,69 +34,104 @@ public function testCanBeConstructed(): void
$this->assertInstanceOf(GroupsClient::class, $client);
}

public function testBuildApiUrl(): void
/**
* @dataProvider buildApiUrlProvider
*/
public function testBuildApiUrl(string $expected, string $action): void
{
$application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token
$group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application); // using dummy group key

// testing various "actions" below

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

$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, $action);
$this->assertSame($expected, $client->buildApiUrl());
}

$client = new GroupsClient($group, GroupsClient::ACTION_LIST_GROUPS);
$this->assertSame('https://api.pushover.net/1/groups.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl());
/**
* @return iterable<array{string, string}>
*/
public static function buildApiUrlProvider(): iterable
{
yield [
'https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66.json?token=cccc3333CCCC3333dddd4444DDDD44',
GroupsClient::ACTION_RETRIEVE_GROUP,
];

yield [
'https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66/add_user.json?token=cccc3333CCCC3333dddd4444DDDD44',
GroupsClient::ACTION_ADD_USER,
];

yield [
'https://api.pushover.net/1/groups.json?token=cccc3333CCCC3333dddd4444DDDD44',
GroupsClient::ACTION_LIST_GROUPS,
];
}

public function testBuildCurlPostFields(): void
/**
* @dataProvider buildCurlPostFieldsProvider
*
* @param array<string, string> $expected
*/
public function testBuildCurlPostFields(array $expected, string $action): void
{
$application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token
$group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application); // using dummy group key
$recipient = new Recipient('aaaa1111AAAA1111bbbb2222BBBB22'); // using dummy user key

// testing various "actions" below
$client = new GroupsClient($group, $action);

$client = new GroupsClient($group, GroupsClient::ACTION_ADD_USER);

$this->assertEquals(
[
'token' => 'cccc3333CCCC3333dddd4444DDDD44',
'user' => 'aaaa1111AAAA1111bbbb2222BBBB22',
],
$this->assertSame(
$expected,
$client->buildCurlPostFields($recipient),
);
}

$client = new GroupsClient($group, GroupsClient::ACTION_REMOVE_USER);
/**
* @return iterable<array{array<string, string>, string}>
*/
public static function buildCurlPostFieldsProvider(): iterable
{
$base = [
'token' => 'cccc3333CCCC3333dddd4444DDDD44',
'user' => 'aaaa1111AAAA1111bbbb2222BBBB22',
];

foreach ([
GroupsClient::ACTION_ADD_USER,
GroupsClient::ACTION_REMOVE_USER,
GroupsClient::ACTION_ENABLE_USER,
GroupsClient::ACTION_DISABLE_USER,
] as $action) {
yield [$base, $action];
}
}

$this->assertEquals(
[
'token' => 'cccc3333CCCC3333dddd4444DDDD44',
'user' => 'aaaa1111AAAA1111bbbb2222BBBB22',
],
$client->buildCurlPostFields($recipient),
);
/**
* @dataProvider actionsNeedRecipientProvider
*/
public function testBuildCurlPostFieldsThrowsExceptionWhenRecipientIsNotProvided(string $action): void
{
$application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token
$group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application); // using dummy group key

$client = new GroupsClient($group, GroupsClient::ACTION_ENABLE_USER);
$client = new GroupsClient($group, $action);

$this->assertEquals(
[
'token' => 'cccc3333CCCC3333dddd4444DDDD44',
'user' => 'aaaa1111AAAA1111bbbb2222BBBB22',
],
$client->buildCurlPostFields($recipient),
);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Recipient object must be provided for this action.');

$client = new GroupsClient($group, GroupsClient::ACTION_DISABLE_USER);
$client->buildCurlPostFields();
}

$this->assertEquals(
[
'token' => 'cccc3333CCCC3333dddd4444DDDD44',
'user' => 'aaaa1111AAAA1111bbbb2222BBBB22',
],
$client->buildCurlPostFields($recipient),
);
/**
* @return iterable<list<string>>
*/
public static function actionsNeedRecipientProvider(): iterable
{
return [
[GroupsClient::ACTION_ADD_USER],
[GroupsClient::ACTION_REMOVE_USER],
[GroupsClient::ACTION_ENABLE_USER],
[GroupsClient::ACTION_DISABLE_USER],
];
}
}

0 comments on commit 98f8a19

Please sign in to comment.