diff --git a/Example/GroupsExample.php b/Example/GroupsExample.php new file mode 100644 index 0000000..2a7cf00 --- /dev/null +++ b/Example/GroupsExample.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Serhiy\Pushover\Example; + +use Serhiy\Pushover\Api\Groups\Group; +use Serhiy\Pushover\Application; +use Serhiy\Pushover\Client\Response\AddUserToGroupResponse; +use Serhiy\Pushover\Client\Response\DisableUserInGroupResponse; +use Serhiy\Pushover\Client\Response\EnableUserInGroupResponse; +use Serhiy\Pushover\Client\Response\RemoveUserFromGroupResponse; +use Serhiy\Pushover\Client\Response\RenameGroupResponse; +use Serhiy\Pushover\Client\Response\RetrieveGroupResponse; +use Serhiy\Pushover\Recipient; + +/** + * Work with groups example. + * + * @author Serhiy Lunak + */ +class GroupsExample +{ + public function groupsExample() + { + // instantiate pushover application and recipient to verify (can be injected into service using Dependency Injection) + $application = new Application("replace_with_pushover_application_api_token"); + $recipient = new Recipient("replace_with_pushover_user_key"); + + // instantiate pushover group (can be injected into service using Dependency Injection) + $group = new Group("replace_with_pushover_group_key", $application); + + // Retrieve information about the group from the API and populate the object with it. + /** @var RetrieveGroupResponse $retrieveGroupResponse */ + $retrieveGroupResponse = $group->retrieveGroupInformation(); + + // rename the group + /** @var RenameGroupResponse $renameGroupResponse */ + $renameGroupResponse = $group->rename("Rename Group Test"); + + // add user to the group + $recipient->setMemo("This is a test memo"); // optional + $recipient->addDevice("android"); // optional + /** @var AddUserToGroupResponse $addUserToGroupResponse */ + $addUserToGroupResponse = $group->addUser($recipient); + + // remove user from the group + /** @var RemoveUserFromGroupResponse $removeUserFromGroupResponse */ + $removeUserFromGroupResponse = $group->removeUser($recipient); + + // enable user + /** @var EnableUserInGroupResponse $EnableUserInGroupResponse */ + $EnableUserInGroupResponse = $group->enableUser($recipient); + + // disable user + /** @var DisableUserInGroupResponse $disableUserInGroupResponse */ + $disableUserInGroupResponse = $group->disableUser($recipient); + + // work with responses as usually + } +} \ No newline at end of file diff --git a/README.md b/README.md index 48a0199..7f8d5b9 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,11 @@ Light, simple and fast wrapper for the Pushover API. - Receipt API ([Example](Example/ReceiptExample.php)) - Query emergency priority receipt - Cancel emergency priority retry +- Groups API ([Example](Example/GroupsExample.php)) + - Retrieve information about the group + - Add / Remove users + - Enable / Disable users + - Rename the group *Note: Project is in constant development; update to newer versions with caution.* diff --git a/src/Api/Groups/Group.php b/src/Api/Groups/Group.php new file mode 100644 index 0000000..c9090c0 --- /dev/null +++ b/src/Api/Groups/Group.php @@ -0,0 +1,213 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Serhiy\Pushover\Api\Groups; + +use Serhiy\Pushover\Application; +use Serhiy\Pushover\Client\Curl\Curl; +use Serhiy\Pushover\Client\GroupsClient; +use Serhiy\Pushover\Client\Request\Request; +use Serhiy\Pushover\Client\Response\AddUserToGroupResponse; +use Serhiy\Pushover\Client\Response\DisableUserInGroupResponse; +use Serhiy\Pushover\Client\Response\EnableUserInGroupResponse; +use Serhiy\Pushover\Client\Response\RemoveUserFromGroupResponse; +use Serhiy\Pushover\Client\Response\RenameGroupResponse; +use Serhiy\Pushover\Client\Response\RetrieveGroupResponse; +use Serhiy\Pushover\Exception\InvalidArgumentException; +use Serhiy\Pushover\Recipient; + +/** + * @author Serhiy Lunak + */ +class Group +{ + /** + * @var string Group key. + */ + private $key; + + /** + * @var Application Pushover application this group belongs to. + */ + private $application; + + /** + * @var string Name of the group. + */ + private $name; + + /** + * @var Recipient[] Group users. + */ + private $users; + + public function __construct(string $key, Application $application) + { + if (1 != preg_match("/^[a-zA-Z0-9]{30}$/", $key)) { + throw new InvalidArgumentException(sprintf('Group identifiers are 30 characters long, case-sensitive, and may contain the character set [A-Za-z0-9]. "%s" given."', $key)); + } + + $this->key = $key; + $this->application = $application; + } + + /** + * @return string + */ + public function getKey(): string + { + return $this->key; + } + + /** + * @return Application + */ + public function getApplication(): Application + { + return $this->application; + } + + /** + * @return Recipient[] + */ + public function getUsers(): array + { + return $this->users; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * Retrieves information about the group from the API and populates the object with it. + * + * @return RetrieveGroupResponse + */ + public function retrieveGroupInformation(): RetrieveGroupResponse + { + $client = new GroupsClient($this, GroupsClient::ACTION_RETRIEVE_GROUP); + $request = new Request($client->buildApiUrl(), Request::GET); + + $curlResponse = Curl::do($request); + + $response = new RetrieveGroupResponse($curlResponse); + $response->setRequest($request); + + if ($response->isSuccessful()) { + $this->name = $response->getName(); + $this->users = $response->getUsers(); + } + + return $response; + } + + /** + * Adds an existing Pushover user to your Delivery Group. + * + * @param Recipient $recipient + * @return AddUserToGroupResponse + */ + public function addUser(Recipient $recipient) + { + $client = new GroupsClient($this, GroupsClient::ACTION_ADD_USER); + $request = new Request($client->buildApiUrl(), Request::POST, $client->buildCurlPostFields($recipient)); + + $curlResponse = Curl::do($request); + + $response = new AddUserToGroupResponse($curlResponse); + $response->setRequest($request); + + return $response; + } + + /** + * Removes user to from Delivery Group. + * + * @param Recipient $recipient + * @return RemoveUserFromGroupResponse + */ + public function removeUser(Recipient $recipient): RemoveUserFromGroupResponse + { + $client = new GroupsClient($this, GroupsClient::ACTION_REMOVE_USER); + $request = new Request($client->buildApiUrl(), Request::POST, $client->buildCurlPostFields($recipient)); + + $curlResponse = Curl::do($request); + + $response = new RemoveUserFromGroupResponse($curlResponse); + $response->setRequest($request); + + return $response; + } + + /** + * Enables user in Delivery Group. + * + * @param Recipient $recipient + * @return EnableUserInGroupResponse + */ + public function enableUser(Recipient $recipient): EnableUserInGroupResponse + { + $client = new GroupsClient($this, GroupsClient::ACTION_ENABLE_USER); + $request = new Request($client->buildApiUrl(), Request::POST, $client->buildCurlPostFields($recipient)); + + $curlResponse = Curl::do($request); + + $response = new EnableUserInGroupResponse($curlResponse); + $response->setRequest($request); + + return $response; + } + + /** + * Disables user in Delivery Group. + * + * @param Recipient $recipient + * @return DisableUserInGroupResponse + */ + public function disableUser(Recipient $recipient): DisableUserInGroupResponse + { + $client = new GroupsClient($this, GroupsClient::ACTION_DISABLE_USER); + $request = new Request($client->buildApiUrl(), Request::POST, $client->buildCurlPostFields($recipient)); + + $curlResponse = Curl::do($request); + + $response = new DisableUserInGroupResponse($curlResponse); + $response->setRequest($request); + + return $response; + } + + /** + * Renames the group. Reflected in the API and in the group editor on our website. + * + * @param string $name + * @return RenameGroupResponse + */ + public function rename(string $name): RenameGroupResponse + { + $this->name = $name; + + $client = new GroupsClient($this, GroupsClient::ACTION_RENAME_GROUP); + $request = new Request($client->buildApiUrl(), Request::POST, $client->buildCurlPostFields()); + + $curlResponse = Curl::do($request); + + $response = new RenameGroupResponse($curlResponse); + $response->setRequest($request); + + return $response; + } +} diff --git a/src/Client/GroupsClient.php b/src/Client/GroupsClient.php new file mode 100644 index 0000000..fad2b6b --- /dev/null +++ b/src/Client/GroupsClient.php @@ -0,0 +1,115 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Serhiy\Pushover\Client; + +use Serhiy\Pushover\Api\Groups\Group; +use Serhiy\Pushover\Client\Curl\Curl; +use Serhiy\Pushover\Exception\InvalidArgumentException; +use Serhiy\Pushover\Recipient; + +/** + * @author Serhiy Lunak + */ +class GroupsClient extends Client implements ClientInterface +{ + const ACTION_RETRIEVE_GROUP = "retrieve_group"; + const ACTION_ADD_USER = "add_user"; + const ACTION_REMOVE_USER = "delete_user"; + const ACTION_DISABLE_USER = "disable_user"; + const ACTION_ENABLE_USER = "enable_user"; + const ACTION_RENAME_GROUP = "rename"; + + /** + * @var Group + */ + private $group; + + /** + * @var string Action that client performs. + */ + private $action; + + public function __construct(Group $group, string $action) + { + if (!$this->isActionValid($action)) { + throw new InvalidArgumentException("Action argument provided to construct method is invalid."); + } + + $this->group = $group; + $this->action = $action; + } + + /** + * @inheritDoc + */ + public function buildApiUrl() + { + 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(); + } + + /** + * @param Recipient|null $recipient + * @return array + */ + public function buildCurlPostFields(Recipient $recipient = null): array + { + $curlPostFields = 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 + ) { + $curlPostFields["user"] = $recipient->getUserKey(); + } + + 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 ($this->action == self::ACTION_RENAME_GROUP) { + $curlPostFields["name"] = $this->group->getName(); + } + + return $curlPostFields; + } + + /** + * Checks if action that was provided into construct is valid. + * + * @param string $action + * @return bool + */ + private function isActionValid(string $action): bool + { + $oClass = new \ReflectionClass(__CLASS__); + + if (in_array($action, $oClass->getConstants())) { + return true; + } + + return false; + } +} diff --git a/src/Client/Response/AddUserToGroupResponse.php b/src/Client/Response/AddUserToGroupResponse.php new file mode 100644 index 0000000..98939a0 --- /dev/null +++ b/src/Client/Response/AddUserToGroupResponse.php @@ -0,0 +1,40 @@ + + * + * 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 Serhiy Lunak + */ +class AddUserToGroupResponse extends Response +{ + /** + * @param mixed $curlResponse + */ + public function __construct($curlResponse) + { + $this->processCurlResponse($curlResponse); + } + + /** + * @param mixed $curlResponse + */ + private function processCurlResponse($curlResponse): void + { + $this->setCurlResponse($curlResponse); + + $decodedCurlResponse = json_decode($curlResponse); + + $this->processInitialCurlResponse($decodedCurlResponse); + } +} diff --git a/src/Client/Response/Base/Response.php b/src/Client/Response/Base/Response.php index a0a7c3b..d2b001b 100644 --- a/src/Client/Response/Base/Response.php +++ b/src/Client/Response/Base/Response.php @@ -163,4 +163,24 @@ public function setRequest(Request $request): void { $this->request = $request; } + + /** + * Processes initial curl response, common to all response objects. + * + * @param $decodedCurlResponse + */ + protected function processInitialCurlResponse($decodedCurlResponse) + { + $this->setRequestStatus($decodedCurlResponse->status); + + if ($this->getRequestStatus() == 1) { + $this->setIsSuccessful(true); + $this->setRequestToken($decodedCurlResponse->request); + } + + if ($this->getRequestStatus() != 1) { + $this->setErrors($decodedCurlResponse->errors); + $this->setIsSuccessful(false); + } + } } diff --git a/src/Client/Response/DisableUserInGroupResponse.php b/src/Client/Response/DisableUserInGroupResponse.php new file mode 100644 index 0000000..5fb191b --- /dev/null +++ b/src/Client/Response/DisableUserInGroupResponse.php @@ -0,0 +1,40 @@ + + * + * 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 Serhiy Lunak + */ +class DisableUserInGroupResponse extends Response +{ + /** + * @param mixed $curlResponse + */ + public function __construct($curlResponse) + { + $this->processCurlResponse($curlResponse); + } + + /** + * @param mixed $curlResponse + */ + private function processCurlResponse($curlResponse): void + { + $this->setCurlResponse($curlResponse); + + $decodedCurlResponse = json_decode($curlResponse); + + $this->processInitialCurlResponse($decodedCurlResponse); + } +} diff --git a/src/Client/Response/EnableUserInGroupResponse.php b/src/Client/Response/EnableUserInGroupResponse.php new file mode 100644 index 0000000..f214edc --- /dev/null +++ b/src/Client/Response/EnableUserInGroupResponse.php @@ -0,0 +1,40 @@ + + * + * 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 Serhiy Lunak + */ +class EnableUserInGroupResponse extends Response +{ + /** + * @param mixed $curlResponse + */ + public function __construct($curlResponse) + { + $this->processCurlResponse($curlResponse); + } + + /** + * @param mixed $curlResponse + */ + private function processCurlResponse($curlResponse): void + { + $this->setCurlResponse($curlResponse); + + $decodedCurlResponse = json_decode($curlResponse); + + $this->processInitialCurlResponse($decodedCurlResponse); + } +} diff --git a/src/Client/Response/RemoveUserFromGroupResponse.php b/src/Client/Response/RemoveUserFromGroupResponse.php new file mode 100644 index 0000000..8e0b529 --- /dev/null +++ b/src/Client/Response/RemoveUserFromGroupResponse.php @@ -0,0 +1,40 @@ + + * + * 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 Serhiy Lunak + */ +class RemoveUserFromGroupResponse extends Response +{ + /** + * @param mixed $curlResponse + */ + public function __construct($curlResponse) + { + $this->processCurlResponse($curlResponse); + } + + /** + * @param mixed $curlResponse + */ + private function processCurlResponse($curlResponse): void + { + $this->setCurlResponse($curlResponse); + + $decodedCurlResponse = json_decode($curlResponse); + + $this->processInitialCurlResponse($decodedCurlResponse); + } +} diff --git a/src/Client/Response/RenameGroupResponse.php b/src/Client/Response/RenameGroupResponse.php new file mode 100644 index 0000000..acd9ea7 --- /dev/null +++ b/src/Client/Response/RenameGroupResponse.php @@ -0,0 +1,40 @@ + + * + * 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 Serhiy Lunak + */ +class RenameGroupResponse extends Response +{ + /** + * @param mixed $curlResponse + */ + public function __construct($curlResponse) + { + $this->processCurlResponse($curlResponse); + } + + /** + * @param mixed $curlResponse + */ + private function processCurlResponse($curlResponse): void + { + $this->setCurlResponse($curlResponse); + + $decodedCurlResponse = json_decode($curlResponse); + + $this->processInitialCurlResponse($decodedCurlResponse); + } +} diff --git a/src/Client/Response/RetrieveGroupResponse.php b/src/Client/Response/RetrieveGroupResponse.php new file mode 100644 index 0000000..2ee5290 --- /dev/null +++ b/src/Client/Response/RetrieveGroupResponse.php @@ -0,0 +1,103 @@ + + * + * 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; +use Serhiy\Pushover\Recipient; + +/** + * @author Serhiy Lunak + */ +class RetrieveGroupResponse extends Response +{ + /** + * @var string Name of the group. + */ + private $name; + + /** + * @var Recipient[] Array of group users of Recipient object. + */ + private $users; + + /** + * @param mixed $curlResponse + */ + public function __construct($curlResponse) + { + $this->processCurlResponse($curlResponse); + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return Recipient[] + */ + public function getUsers(): array + { + return $this->users; + } + + /** + * Processes curl response. + * + * @param mixed $curlResponse + */ + private function processCurlResponse($curlResponse): void + { + $this->setCurlResponse($curlResponse); + + $decodedCurlResponse = json_decode($curlResponse); + + $this->processInitialCurlResponse($decodedCurlResponse); + + if ($this->getRequestStatus() == 1) { + $this->name = $decodedCurlResponse->name; + $this->users = $this->setUsers($decodedCurlResponse->users); + } + } + + /** + * @param array $users + * @return Recipient[] + */ + private function setUsers(array $users): array + { + $recipients = array(); + + foreach ($users as $user) { + $recipient = new Recipient($user->user); + + if (!empty($user->device)) { + $recipient->addDevice($user->device); + } + + if (!empty($user->memo)) { + $recipient->setMemo($user->memo); + } + + if ($user->disabled) { + $recipient->setIsDisabled(true); + } + + array_push($recipients, $recipient); + } + + return $recipients; + } +} diff --git a/src/Client/Response/UserGroupValidationResponse.php b/src/Client/Response/UserGroupValidationResponse.php index 452cb8e..fda1977 100644 --- a/src/Client/Response/UserGroupValidationResponse.php +++ b/src/Client/Response/UserGroupValidationResponse.php @@ -1,6 +1,6 @@ diff --git a/src/Recipient.php b/src/Recipient.php index 3b9cdbe..d8eb8ae 100644 --- a/src/Recipient.php +++ b/src/Recipient.php @@ -42,14 +42,27 @@ class Recipient */ private $device; + /** + * Used only when sending messages to a Group. If user is disabled in a group, they won't receive messages. + * However if sent to the user directly, they will receive messages. + * + * @var bool + */ + private $isDisabled = false; + + /** + * Used when managing users in a Group. + * A free-text memo used to associate data with the user such as their name or e-mail address, + * viewable through the API and the groups editor on our website (limited to 200 characters) + * + * @var string|null + */ + private $memo; + public function __construct(string $userKey) { - if (1 != preg_match("/^[a-zA-Z0-9]+$/", $userKey)) { - throw new InvalidArgumentException(sprintf('User and group identifiers are 30 characters long, case-sensitive, and may contain the character set [A-Za-z0-9]. "%s" given."', $userKey)); - } - - if (30 != strlen($userKey)) { - throw new InvalidArgumentException(sprintf('User and group identifiers are 30 characters long. "%s" characters identifier provided.', strlen($userKey))); + if (1 != preg_match("/^[a-zA-Z0-9]{30}$/", $userKey)) { + throw new InvalidArgumentException(sprintf('User and group identifiers are 30 characters long, case-sensitive, and may contain the character set [A-Za-z0-9]. "%s" given with "%s" characters.', $userKey, strlen($userKey))); } $this->userKey = $userKey; @@ -98,4 +111,40 @@ public function getDeviceListCommaSeparated(): string { return implode(',', $this->device); } + + /** + * @return bool + */ + public function isDisabled(): bool + { + return $this->isDisabled; + } + + /** + * @param bool $isDisabled + */ + public function setIsDisabled(bool $isDisabled): void + { + $this->isDisabled = $isDisabled; + } + + /** + * @return string|null + */ + public function getMemo(): ?string + { + return $this->memo; + } + + /** + * @param string|null $memo + */ + public function setMemo(?string $memo): void + { + if (strlen($memo) > 200) { + throw new InvalidArgumentException('Memo contained ' . strlen($memo) . ' characters. Memos are limited to 200 characters.'); + } + + $this->memo = $memo; + } } diff --git a/tests/Api/Groups/GroupTest.php b/tests/Api/Groups/GroupTest.php new file mode 100644 index 0000000..79754ee --- /dev/null +++ b/tests/Api/Groups/GroupTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Api\Groups; + +use Serhiy\Pushover\Api\Groups\Group; +use PHPUnit\Framework\TestCase; +use Serhiy\Pushover\Application; + +/** + * @author Serhiy Lunak + */ +class GroupTest extends TestCase +{ + /** + * @return Group + */ + public function testCanBeCreated(): Group + { + $application = new Application("zaGDORePK8gMaC0QOYAMyEEuzJnyUi"); // using dummy token + $group = new Group("uoJCttEFQo8uoZ6REZHMGBjX2pmcdJ", $application); // using dummy group key + + $this->assertInstanceOf(Group::class, $group); + + return $group; + } + + /** + * @depends testCanBeCreated + * @param Group $group + */ + public function testGetApplication(Group $group) + { + $this->assertInstanceOf(Application::class, $group->getApplication()); + $this->assertEquals("zaGDORePK8gMaC0QOYAMyEEuzJnyUi", $group->getApplication()->getToken()); + } + + /** + * @depends testCanBeCreated + * @param Group $group + */ + public function testGetKey(Group $group) + { + $this->assertEquals("uoJCttEFQo8uoZ6REZHMGBjX2pmcdJ", $group->getKey()); + } +} diff --git a/tests/Client/GroupsClientTest.php b/tests/Client/GroupsClientTest.php new file mode 100644 index 0000000..24c9e23 --- /dev/null +++ b/tests/Client/GroupsClientTest.php @@ -0,0 +1,97 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Client; + +use Serhiy\Pushover\Api\Groups\Group; +use Serhiy\Pushover\Application; +use Serhiy\Pushover\Client\GroupsClient; +use PHPUnit\Framework\TestCase; +use Serhiy\Pushover\Recipient; + +/** + * @author Serhiy Lunak + */ +class GroupsClientTest extends TestCase +{ + public function testCanBeCreated() + { + $application = new Application("zaGDORePK8gMaC0QOYAMyEEuzJnyUi"); // using dummy token + $group = new Group("uoJCttEFQo8uoZ6REZHMGBjX2pmcdJ", $application); // using dummy group key + + $client = new GroupsClient($group, GroupsClient::ACTION_RETRIEVE_GROUP); + + $this->assertInstanceOf(GroupsClient::class, $client); + } + + public function testBuildApiUrl() + { + $application = new Application("zaGDORePK8gMaC0QOYAMyEEuzJnyUi"); // using dummy token + $group = new Group("uoJCttEFQo8uoZ6REZHMGBjX2pmcdJ", $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/uoJCttEFQo8uoZ6REZHMGBjX2pmcdJ.json?token=zaGDORePK8gMaC0QOYAMyEEuzJnyUi", $client->buildApiUrl()); + + $client = new GroupsClient($group, GroupsClient::ACTION_ADD_USER); + $this->assertEquals("https://api.pushover.net/1/groups/uoJCttEFQo8uoZ6REZHMGBjX2pmcdJ/add_user.json?token=zaGDORePK8gMaC0QOYAMyEEuzJnyUi", $client->buildApiUrl()); + } + + public function testBuildCurlPostFields() + { + $application = new Application("zaGDORePK8gMaC0QOYAMyEEuzJnyUi"); // using dummy token + $group = new Group("uoJCttEFQo8uoZ6REZHMGBjX2pmcdJ", $application); // using dummy group key + $recipient = new Recipient("uQiRzpo4DXghDmr9QzzfQu27cmVRsG"); // using dummy user key + + // testing various "actions" below + + $client = new GroupsClient($group, GroupsClient::ACTION_ADD_USER); + + $this->assertEquals( + array( + 'token' => 'zaGDORePK8gMaC0QOYAMyEEuzJnyUi', + 'user' => 'uQiRzpo4DXghDmr9QzzfQu27cmVRsG', + ), + $client->buildCurlPostFields($recipient) + ); + + $client = new GroupsClient($group, GroupsClient::ACTION_REMOVE_USER); + + $this->assertEquals( + array( + 'token' => 'zaGDORePK8gMaC0QOYAMyEEuzJnyUi', + 'user' => 'uQiRzpo4DXghDmr9QzzfQu27cmVRsG', + ), + $client->buildCurlPostFields($recipient) + ); + + $client = new GroupsClient($group, GroupsClient::ACTION_ENABLE_USER); + + $this->assertEquals( + array( + 'token' => 'zaGDORePK8gMaC0QOYAMyEEuzJnyUi', + 'user' => 'uQiRzpo4DXghDmr9QzzfQu27cmVRsG', + ), + $client->buildCurlPostFields($recipient) + ); + + $client = new GroupsClient($group, GroupsClient::ACTION_DISABLE_USER); + + $this->assertEquals( + array( + 'token' => 'zaGDORePK8gMaC0QOYAMyEEuzJnyUi', + 'user' => 'uQiRzpo4DXghDmr9QzzfQu27cmVRsG', + ), + $client->buildCurlPostFields($recipient) + ); + } +} diff --git a/tests/Client/Response/AddUserToGroupResponseTest.php b/tests/Client/Response/AddUserToGroupResponseTest.php new file mode 100644 index 0000000..069a70d --- /dev/null +++ b/tests/Client/Response/AddUserToGroupResponseTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Client\Response; + +use Serhiy\Pushover\Client\Response\AddUserToGroupResponse; +use PHPUnit\Framework\TestCase; + +/** + * @author Serhiy Lunak + */ +class AddUserToGroupResponseTest extends TestCase +{ + public function testCenBeCreated() + { + $curlResponse = '{"status":1,"request":"6g890a90-7943-4at2-b739-4aubi545b508"}'; + $response = new AddUserToGroupResponse($curlResponse); + + $this->assertInstanceOf(AddUserToGroupResponse::class, $response); + $this->assertTrue($response->isSuccessful()); + } +} diff --git a/tests/Client/Response/DisableUserInGroupResponseTest.php b/tests/Client/Response/DisableUserInGroupResponseTest.php new file mode 100644 index 0000000..c359abf --- /dev/null +++ b/tests/Client/Response/DisableUserInGroupResponseTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Client\Response; + +use Serhiy\Pushover\Client\Response\DisableUserInGroupResponse; +use PHPUnit\Framework\TestCase; + +/** + * @author Serhiy Lunak + */ +class DisableUserInGroupResponseTest extends TestCase +{ + public function testCenBeCreated() + { + $curlResponse = '{"status":1,"request":"6g890a90-7943-4at2-b739-4aubi545b508"}'; + $response = new DisableUserInGroupResponse($curlResponse); + + $this->assertInstanceOf(DisableUserInGroupResponse::class, $response); + $this->assertTrue($response->isSuccessful()); + } +} diff --git a/tests/Client/Response/EnableUserInGroupResponseTest.php b/tests/Client/Response/EnableUserInGroupResponseTest.php new file mode 100644 index 0000000..74f80f8 --- /dev/null +++ b/tests/Client/Response/EnableUserInGroupResponseTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Client\Response; + +use Serhiy\Pushover\Client\Response\EnableUserInGroupResponse; +use PHPUnit\Framework\TestCase; + +/** + * @author Serhiy Lunak + */ +class EnableUserInGroupResponseTest extends TestCase +{ + public function testCenBeCreated() + { + $curlResponse = '{"status":1,"request":"6g890a90-7943-4at2-b739-4aubi545b508"}'; + $response = new EnableUserInGroupResponse($curlResponse); + + $this->assertInstanceOf(EnableUserInGroupResponse::class, $response); + $this->assertTrue($response->isSuccessful()); + } +} diff --git a/tests/Client/Response/RemoveUserFromGroupResponseTest.php b/tests/Client/Response/RemoveUserFromGroupResponseTest.php new file mode 100644 index 0000000..f04aa39 --- /dev/null +++ b/tests/Client/Response/RemoveUserFromGroupResponseTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Client\Response; + +use Serhiy\Pushover\Client\Response\RemoveUserFromGroupResponse; +use PHPUnit\Framework\TestCase; + +/** + * @author Serhiy Lunak + */ +class RemoveUserFromGroupResponseTest extends TestCase +{ + public function testCenBeCreated() + { + $curlResponse = '{"status":1,"request":"6g890a90-7943-4at2-b739-4aubi545b508"}'; + $response = new RemoveUserFromGroupResponse($curlResponse); + + $this->assertInstanceOf(RemoveUserFromGroupResponse::class, $response); + $this->assertTrue($response->isSuccessful()); + } +} diff --git a/tests/Client/Response/RenameGroupResponseTest.php b/tests/Client/Response/RenameGroupResponseTest.php new file mode 100644 index 0000000..68ec710 --- /dev/null +++ b/tests/Client/Response/RenameGroupResponseTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Client\Response; + +use Serhiy\Pushover\Client\Response\RenameGroupResponse; +use PHPUnit\Framework\TestCase; + +/** + * @author Serhiy Lunak + */ +class RenameGroupResponseTest extends TestCase +{ + public function testCenBeCreated() + { + $curlResponse = '{"status":1,"request":"6g890a90-7943-4at2-b739-4aubi545b508"}'; + $response = new RenameGroupResponse($curlResponse); + + $this->assertInstanceOf(RenameGroupResponse::class, $response); + $this->assertTrue($response->isSuccessful()); + } +} diff --git a/tests/Client/Response/RetrieveGroupResponseTest.php b/tests/Client/Response/RetrieveGroupResponseTest.php new file mode 100644 index 0000000..9053787 --- /dev/null +++ b/tests/Client/Response/RetrieveGroupResponseTest.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Client\Response; + +use Serhiy\Pushover\Client\Response\RetrieveGroupResponse; +use PHPUnit\Framework\TestCase; +use Serhiy\Pushover\Recipient; + +class RetrieveGroupResponseTest extends TestCase +{ + /** + * @return RetrieveGroupResponse + */ + public function testCanBeCreated(): RetrieveGroupResponse + { + $curlResponse = '{"name":"Test Group","users":[{"user":"uQiRzpo4DXghDmr9QzzfQu27cmVRsG","device":"iphone","memo":"This is a test memo","disabled":false}],"status":1,"request":"6g890a90-7943-4at2-b739-4aubi545b508"}'; + $response = new RetrieveGroupResponse($curlResponse); + + $this->assertInstanceOf(RetrieveGroupResponse::class, $response); + + return $response; + } + + /** + * @depends testCanBeCreated + * @param RetrieveGroupResponse $response + */ + public function testGetName(RetrieveGroupResponse $response) + { + $this->assertEquals("Test Group", $response->getName()); + } + + /** + * @depends testCanBeCreated + * @param RetrieveGroupResponse $response + */ + public function testGetUsers(RetrieveGroupResponse $response) + { + $recipient = $response->getUsers()[0]; + + $this->assertInstanceOf(Recipient::class, $recipient); + $this->assertEquals("uQiRzpo4DXghDmr9QzzfQu27cmVRsG", $recipient->getUserKey()); + $this->assertFalse($recipient->isDisabled()); + $this->assertEquals("This is a test memo", $recipient->getMemo()); + $this->assertEquals(array("iphone"), $recipient->getDevice()); + } +}