From 9240949c05d1abc0ab491ac2ce620449099cb9db Mon Sep 17 00:00:00 2001 From: The Test Coder Date: Sun, 12 Jun 2022 17:24:54 +0530 Subject: [PATCH 1/2] add return value in sendNotificationToUser method --- src/OneSignalClient.php | 158 +++++++++++++++++++++++----------------- 1 file changed, 91 insertions(+), 67 deletions(-) diff --git a/src/OneSignalClient.php b/src/OneSignalClient.php index 1a18adf..752d431 100644 --- a/src/OneSignalClient.php +++ b/src/OneSignalClient.php @@ -63,7 +63,7 @@ public function async($on = true) * @param Callable $requestCallback * @return $this */ - public function callback(Callable $requestCallback) + public function callback(callable $requestCallback) { $this->requestCallback = $requestCallback; return $this; @@ -89,7 +89,8 @@ public function __construct($appId, $restApiKey, $userAuthKey, $guzzleClientTime $this->additionalParams = []; } - private function createGuzzleHandler() { + private function createGuzzleHandler() + { return tap(HandlerStack::create(new CurlHandler()), function (HandlerStack $handlerStack) { $handlerStack->push(Middleware::retry(function ($retries, Psr7Request $request, Psr7Response $response = null, RequestException $exception = null) { if ($retries >= $this->maxRetries) { @@ -109,19 +110,23 @@ private function createGuzzleHandler() { }); } - public function testCredentials() { - return "APP ID: ".$this->appId." REST: ".$this->restApiKey; + public function testCredentials() + { + return "APP ID: " . $this->appId . " REST: " . $this->restApiKey; } - private function requiresAuth() { - $this->headers['headers']['Authorization'] = 'Basic '.$this->restApiKey; + private function requiresAuth() + { + $this->headers['headers']['Authorization'] = 'Basic ' . $this->restApiKey; } - private function requiresUserAuth() { - $this->headers['headers']['Authorization'] = 'Basic '.$this->userAuthKey; + private function requiresUserAuth() + { + $this->headers['headers']['Authorization'] = 'Basic ' . $this->userAuthKey; } - private function usesJSON() { + private function usesJSON() + { $this->headers['headers']['Content-Type'] = 'application/json'; } @@ -139,7 +144,8 @@ public function setParam($key, $value) return $this; } - public function sendNotificationToUser($message, $userId, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) { + public function sendNotificationToUser($message, $userId, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) + { $contents = array( "en" => $message ); @@ -162,23 +168,23 @@ public function sendNotificationToUser($message, $userId, $url = null, $data = n $params['buttons'] = $buttons; } - if(isset($schedule)){ + if (isset($schedule)) { $params['send_after'] = $schedule; } - if(isset($headings)){ + if (isset($headings)) { $params['headings'] = array( "en" => $headings ); } - - if(isset($subtitle)){ + + if (isset($subtitle)) { $params['subtitle'] = array( "en" => $subtitle ); } - $this->sendNotificationCustom($params); + return $this->sendNotificationCustom($params); } /** @@ -191,7 +197,8 @@ public function sendNotificationToUser($message, $userId, $url = null, $data = n * @param null $headings * @param null $subtitle */ - public function sendNotificationToExternalUser($message, $userId, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) { + public function sendNotificationToExternalUser($message, $userId, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) + { $contents = array( "en" => $message ); @@ -214,17 +221,17 @@ public function sendNotificationToExternalUser($message, $userId, $url = null, $ $params['buttons'] = $buttons; } - if(isset($schedule)){ + if (isset($schedule)) { $params['send_after'] = $schedule; } - if(isset($headings)){ + if (isset($headings)) { $params['headings'] = array( "en" => $headings ); } - if(isset($subtitle)){ + if (isset($subtitle)) { $params['subtitle'] = array( "en" => $subtitle ); @@ -232,7 +239,9 @@ public function sendNotificationToExternalUser($message, $userId, $url = null, $ $this->sendNotificationCustom($params); } - public function sendNotificationUsingTags($message, $tags, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) { + + public function sendNotificationUsingTags($message, $tags, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) + { $contents = array( "en" => $message ); @@ -255,17 +264,17 @@ public function sendNotificationUsingTags($message, $tags, $url = null, $data = $params['buttons'] = $buttons; } - if(isset($schedule)){ + if (isset($schedule)) { $params['send_after'] = $schedule; } - if(isset($headings)){ + if (isset($headings)) { $params['headings'] = array( "en" => $headings ); } - - if(isset($subtitle)){ + + if (isset($subtitle)) { $params['subtitle'] = array( "en" => $subtitle ); @@ -274,7 +283,8 @@ public function sendNotificationUsingTags($message, $tags, $url = null, $data = $this->sendNotificationCustom($params); } - public function sendNotificationToAll($message, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) { + public function sendNotificationToAll($message, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) + { $contents = array( "en" => $message ); @@ -297,17 +307,17 @@ public function sendNotificationToAll($message, $url = null, $data = null, $butt $params['buttons'] = $buttons; } - if(isset($schedule)){ + if (isset($schedule)) { $params['send_after'] = $schedule; } - if(isset($headings)){ + if (isset($headings)) { $params['headings'] = array( "en" => $headings ); } - - if(isset($subtitle)){ + + if (isset($subtitle)) { $params['subtitle'] = array( "en" => $subtitle ); @@ -316,7 +326,8 @@ public function sendNotificationToAll($message, $url = null, $data = null, $butt $this->sendNotificationCustom($params); } - public function sendNotificationToSegment($message, $segment, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) { + public function sendNotificationToSegment($message, $segment, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) + { $contents = array( "en" => $message ); @@ -339,17 +350,17 @@ public function sendNotificationToSegment($message, $segment, $url = null, $data $params['buttons'] = $buttons; } - if(isset($schedule)){ + if (isset($schedule)) { $params['send_after'] = $schedule; } - if(isset($headings)){ + if (isset($headings)) { $params['headings'] = array( "en" => $headings ); } - - if(isset($subtitle)){ + + if (isset($subtitle)) { $params['subtitle'] = array( "en" => $subtitle ); @@ -358,10 +369,11 @@ public function sendNotificationToSegment($message, $segment, $url = null, $data $this->sendNotificationCustom($params); } - public function deleteNotification($notificationId, $appId = null) { + public function deleteNotification($notificationId, $appId = null) + { $this->requiresAuth(); - if(!$appId) + if (!$appId) $appId = $this->appId; $notificationCancelNode = "/$notificationId?app_id=$this->appId"; return $this->delete(self::ENDPOINT_NOTIFICATIONS . $notificationCancelNode); @@ -374,12 +386,13 @@ public function deleteNotification($notificationId, $appId = null) { * @param array $parameters * @return mixed */ - public function sendNotificationCustom($parameters = []){ + public function sendNotificationCustom($parameters = []) + { $this->requiresAuth(); $this->usesJSON(); if (isset($parameters['api_key'])) { - $this->headers['headers']['Authorization'] = 'Basic '.$parameters['api_key']; + $this->headers['headers']['Authorization'] = 'Basic ' . $parameters['api_key']; } // Make sure to use app_id @@ -400,50 +413,54 @@ public function sendNotificationCustom($parameters = []){ return $this->post(self::ENDPOINT_NOTIFICATIONS); } - public function getNotification($notification_id, $app_id = null) { + public function getNotification($notification_id, $app_id = null) + { $this->requiresAuth(); $this->usesJSON(); - if(!$app_id) + if (!$app_id) $app_id = $this->appId; - return $this->get(self::ENDPOINT_NOTIFICATIONS . '/'.$notification_id . '?app_id='.$app_id); + return $this->get(self::ENDPOINT_NOTIFICATIONS . '/' . $notification_id . '?app_id=' . $app_id); } - public function getNotifications($app_id = null, $limit = null, $offset = null) { + public function getNotifications($app_id = null, $limit = null, $offset = null) + { $this->requiresAuth(); $this->usesJSON(); $endpoint = self::ENDPOINT_NOTIFICATIONS; - - if(!$app_id) { + + if (!$app_id) { $app_id = $this->appId; } - $endpoint.='?app_id='.$app_id; + $endpoint .= '?app_id=' . $app_id; - if($limit) { - $endpoint.="&limit=".$limit; + if ($limit) { + $endpoint .= "&limit=" . $limit; } - if($offset) { - $endpoint.="&offset=".$$offset; + if ($offset) { + $endpoint .= "&offset=" . $$offset; } return $this->get($endpoint); } - public function getApp($app_id = null) { + public function getApp($app_id = null) + { $this->requiresUserAuth(); $this->usesJSON(); - if(!$app_id) + if (!$app_id) $app_id = $this->appId; - return $this->get(self::ENDPOINT_APPS . '/'.$app_id); + return $this->get(self::ENDPOINT_APPS . '/' . $app_id); } - public function getApps() { + public function getApps() + { $this->requiresUserAuth(); $this->usesJSON(); @@ -457,8 +474,9 @@ public function getApps() { * @return mixed * @throws \Exception */ - public function createPlayer(Array $parameters) { - if(!isset($parameters['device_type']) or !is_numeric($parameters['device_type'])) { + public function createPlayer(array $parameters) + { + if (!isset($parameters['device_type']) or !is_numeric($parameters['device_type'])) { throw new \Exception('The `device_type` param is required as integer to create a player(device)'); } return $this->sendPlayer($parameters, 'POST', self::ENDPOINT_PLAYERS); @@ -470,16 +488,18 @@ public function createPlayer(Array $parameters) { * @param array $parameters * @return mixed */ - public function editPlayer(Array $parameters) { + public function editPlayer(array $parameters) + { return $this->sendPlayer($parameters, 'PUT', self::ENDPOINT_PLAYERS . '/' . $parameters['id']); } - public function requestPlayersCSV($app_id = null, Array $parameters = null) { + public function requestPlayersCSV($app_id = null, array $parameters = null) + { $this->requiresAuth(); $this->usesJSON(); - $endpoint = self::ENDPOINT_PLAYERS."/csv_export?"; - $endpoint .= "app_id" . $app_id?$app_id:$this->appId; + $endpoint = self::ENDPOINT_PLAYERS . "/csv_export?"; + $endpoint .= "app_id" . $app_id ? $app_id : $this->appId; return $this->sendPlayer($parameters, 'POST', $endpoint); } @@ -492,7 +512,7 @@ public function requestPlayersCSV($app_id = null, Array $parameters = null) { * @param $endpoint * @return mixed */ - private function sendPlayer(Array $parameters, $method, $endpoint) + private function sendPlayer(array $parameters, $method, $endpoint) { $this->requiresAuth(); $this->usesJSON(); @@ -504,28 +524,32 @@ private function sendPlayer(Array $parameters, $method, $endpoint) return $this->{$method}($endpoint); } - public function post($endPoint) { - if($this->requestAsync === true) { + public function post($endPoint) + { + if ($this->requestAsync === true) { $promise = $this->client->postAsync(self::API_URL . $endPoint, $this->headers); return (is_callable($this->requestCallback) ? $promise->then($this->requestCallback) : $promise); } return $this->client->post(self::API_URL . $endPoint, $this->headers); } - public function put($endPoint) { - if($this->requestAsync === true) { + public function put($endPoint) + { + if ($this->requestAsync === true) { $promise = $this->client->putAsync(self::API_URL . $endPoint, $this->headers); return (is_callable($this->requestCallback) ? $promise->then($this->requestCallback) : $promise); } return $this->client->put(self::API_URL . $endPoint, $this->headers); } - public function get($endPoint) { + public function get($endPoint) + { return $this->client->get(self::API_URL . $endPoint, $this->headers); } - public function delete($endPoint) { - if($this->requestAsync === true) { + public function delete($endPoint) + { + if ($this->requestAsync === true) { $promise = $this->client->deleteAsync(self::API_URL . $endPoint, $this->headers); return (is_callable($this->requestCallback) ? $promise->then($this->requestCallback) : $promise); } From 37d2c4dd4f8b736cfba82f5b44bb436884b88871 Mon Sep 17 00:00:00 2001 From: The Test Coder Date: Mon, 13 Jun 2022 17:17:02 +0530 Subject: [PATCH 2/2] fix: notification issue with guzzle id --- .phpunit.result.cache | 1 + composer.json | 5 +++-- phpunit.xml | 28 +++++++++++++++++++++++++ src/OneSignalClient.php | 6 +++++- tests/ExampleTest.php | 18 +++++++++++++++++ tests/Pest.php | 45 +++++++++++++++++++++++++++++++++++++++++ tests/test.php | 12 ----------- 7 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 .phpunit.result.cache create mode 100644 phpunit.xml create mode 100644 tests/ExampleTest.php create mode 100644 tests/Pest.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..19af9e1 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"C:\\laragon\\www\\laravel-onesignal\\tests\\ExampleTest.php::example":4,"C:\\laragon\\www\\laravel-onesignal\\tests\\ExampleTest.php::test":4},"times":{"C:\\laragon\\www\\laravel-onesignal\\tests\\ExampleTest.php::example":0.004,"C:\\laragon\\www\\laravel-onesignal\\tests\\ExampleTest.php::test":0.612}} \ No newline at end of file diff --git a/composer.json b/composer.json index bd3264d..4b70a70 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "symfony/psr-http-message-bridge": "1.*|2.*" }, "require-dev": { - "vlucas/phpdotenv": "^2.2" + "vlucas/phpdotenv": "^2.2", + "pestphp/pest": "^1.21" }, "autoload": { "psr-4": { @@ -40,5 +41,5 @@ } ], "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..62d56e0 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,28 @@ + + + + + ./tests + + + + + ./app + + + + + + + + + + + + + + diff --git a/src/OneSignalClient.php b/src/OneSignalClient.php index 752d431..d2de2d8 100644 --- a/src/OneSignalClient.php +++ b/src/OneSignalClient.php @@ -184,7 +184,11 @@ public function sendNotificationToUser($message, $userId, $url = null, $data = n ); } - return $this->sendNotificationCustom($params); + $response = $this->sendNotificationCustom($params); + + $body = $response->getBody(); + + return json_decode($body->getContents()); } /** diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php new file mode 100644 index 0000000..7bc1f6d --- /dev/null +++ b/tests/ExampleTest.php @@ -0,0 +1,18 @@ +load(); + + $client = new Berkayk\OneSignal\OneSignalClient( + getenv('APP_ID'), + getenv('REST_API_KEY'), + getenv('USER_AUTH_KEY')); + + echo $client->testCredentials(); + $response = $client->sendNotificationToUser("this is test message", "4bc5da02-1722-4fee-943d-c8b5ccd507a2"); +}); diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..5949c61 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,45 @@ +in('Feature'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation API at any time. +| +*/ + +expect()->extend('toBeOne', function () { + return $this->toBe(1); +}); + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can also expose helpers as +| global functions to help you to reduce the number of lines of code in your test files. +| +*/ + +function something() +{ + // .. +} diff --git a/tests/test.php b/tests/test.php index 2e7ea7a..a4abe2d 100644 --- a/tests/test.php +++ b/tests/test.php @@ -1,14 +1,2 @@ load(); - -$client = new Berkayk\OneSignal\OneSignalClient( - getenv('APP_ID'), - getenv('REST_API_KEY'), - getenv('USER_AUTH_KEY')); - -echo $client->testCredentials(); -$client->sendNotificationToUser(".","4bc5da02-1722-4fee-943d-c8b5ccd507a2"); \ No newline at end of file