diff --git a/src/Constants.php b/src/Constants.php new file mode 100644 index 0000000..dad1abd --- /dev/null +++ b/src/Constants.php @@ -0,0 +1,12 @@ +headersSet) { return; } - $this->headers = ["User-Agent: Pancake/0.8 (+https://github.com/guibranco/pancake)", "Content-Type: application/json; charset=utf-8"]; + $this->headers = [Constants::USER_AGENT_HEADER, Constants::CONTENT_TYPE_JSON_HEADER]; } public function setHeaders($headers): void @@ -98,14 +98,14 @@ public function fail(): stdClass public function log($message): stdClass { $this->checkHeaders(); - return $this->request->post($this->logUrl, $message, $this->headers); + return $this->request->post($this->logUrl, $this->headers, $message); } public function error($errorMessage): stdClass { $this->checkHeaders(); $this->failed = true; - return $this->request->post($this->failUrl, $errorMessage, $this->headers); + return $this->request->post($this->failUrl, $this->headers, $errorMessage); } public function end(): stdClass diff --git a/src/Logger.php b/src/Logger.php index 783969d..f0c5bc7 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -13,11 +13,11 @@ class Logger implements ILogger public function __construct($loggerUrl, $loggerApiKey, $loggerApiToken, $customUserAgent = null) { $this->headers = array( - "User-Agent: " . ($customUserAgent ?? "Pancake/0.9 (+https://github.com/guibranco/pancake)"), - "Content-Type: application/json; charset=UTF-8", - "X-API-KEY: " . $loggerApiKey, - "X-API-TOKEN: " . $loggerApiToken, - "X-Correlation-Id: " . GUIDv4::random() + "User-Agent: ".($customUserAgent ?? Constants::USER_AGENT_VENDOR), + Constants::CONTENT_TYPE_JSON_HEADER, + "X-API-KEY: ".$loggerApiKey, + "X-API-TOKEN: ".$loggerApiToken, + "X-Correlation-Id: ".GUIDv4::random() ); $this->baseUrl = $loggerUrl; $this->request = new Request(); @@ -35,7 +35,7 @@ public function log($message, $details): bool $body = json_encode($caller); - $result = $this->request->post($this->baseUrl . "log-message", $body, $this->headers); + $result = $this->request->post($this->baseUrl . "log-message", $this->headers, $body); return $result->statusCode == 200; } diff --git a/src/OneSignal.php b/src/OneSignal.php index 2e3b92a..4235b1b 100644 --- a/src/OneSignal.php +++ b/src/OneSignal.php @@ -21,9 +21,9 @@ public function __construct($token, $logger = null, $customUserAgent = null, $cu $this->request = new Request(); $this->logger = $logger; $this->headers = array( - "User-Agent: " . ($customUserAgent ?? "Pancake/0.8 (+https://github.com/guibranco/pancake)"), - "Content-Type: application/json; charset=utf-8", - "Authorization: Basic " . $token + "User-Agent: ".($customUserAgent ?? Constants::USER_AGENT_VENDOR), + Constants::CONTENT_TYPE_JSON_HEADER, + "Authorization: Basic ".$token ); $this->endpoint = $customEndpoint ?? self::BASE_URL; } @@ -35,7 +35,7 @@ public function sendNotification($fields) private function sendInternal($content, $headers, $isRetry = false) { - $result = $this->request->post($this->endpoint . self::NOTIFICATIONS_ENDPOINT, $content, $headers); + $result = $this->request->post($this->endpoint . self::NOTIFICATIONS_ENDPOINT, $headers, $content); if ($result->statusCode == 200) { return true; diff --git a/src/Request.php b/src/Request.php index 2fa909b..0ce6e2c 100644 --- a/src/Request.php +++ b/src/Request.php @@ -93,7 +93,7 @@ public function get($url, $headers = array()): stdClass return $this->execute($fields); } - public function post($url, $data, $headers = array()): stdClass + public function post($url, $headers = array(), $data = null): stdClass { $fields = $this->getFields($url, $headers); $fields[CURLOPT_CUSTOMREQUEST] = "POST"; @@ -103,7 +103,7 @@ public function post($url, $data, $headers = array()): stdClass return $this->execute($fields); } - public function put($url, $data, $headers = array()): stdClass + public function put($url, $headers = array(), $data = null): stdClass { $fields = $this->getFields($url, $headers); $fields[CURLOPT_CUSTOMREQUEST] = "PUT"; @@ -123,7 +123,7 @@ public function delete($url, $headers = array(), $data = null): stdClass return $this->execute($fields); } - public function patch($url, $data, $headers = array()): stdClass + public function patch($url, $headers = array(), $data = null): stdClass { $fields = $this->getFields($url, $headers); $fields[CURLOPT_CUSTOMREQUEST] = "PATCH"; diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 0d21c1a..af26964 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -19,21 +19,42 @@ public function testCanGet(): void public function testCanPost(): void { $request = new Request(); - $response = $request->post('https://httpbin.org/post', ['name' => 'GuiBranco']); + $response = $request->post('https://httpbin.org/post'); + $this->assertEquals(200, $response->statusCode); + } + + public function testCanPostWithPayload(): void + { + $request = new Request(); + $response = $request->post('https://httpbin.org/post', array(), ['name' => 'GuiBranco']); $this->assertEquals(200, $response->statusCode); } public function testCanPut(): void { $request = new Request(); - $response = $request->put('https://httpbin.org/put', ['name' => 'GuiBranco']); + $response = $request->put('https://httpbin.org/put'); + $this->assertEquals(200, $response->statusCode); + } + + public function testCanPutWithPayload(): void + { + $request = new Request(); + $response = $request->put('https://httpbin.org/put', array(), ['name' => 'GuiBranco']); $this->assertEquals(200, $response->statusCode); } public function testCanPatch(): void { $request = new Request(); - $response = $request->patch('https://httpbin.org/patch', ['name' => 'GuiBranco']); + $response = $request->patch('https://httpbin.org/patch'); + $this->assertEquals(200, $response->statusCode); + } + + public function testCanPatchWithPayload(): void + { + $request = new Request(); + $response = $request->patch('https://httpbin.org/patch', array(), ['name' => 'GuiBranco']); $this->assertEquals(200, $response->statusCode); } @@ -75,7 +96,28 @@ public function testCanGetWithHeaders(): void public function testCanPostWithHeaders(): void { $request = new Request(); - $response = $request->post('https://httpbin.org/post', json_encode(['name' => 'GuiBranco']), ['X-Test: test']); + $response = $request->post('https://httpbin.org/post', ['X-Test: test'], json_encode(['name' => 'GuiBranco'])); + $this->assertEquals(200, $response->statusCode); + } + + public function testCanPutWithHeaders(): void + { + $request = new Request(); + $response = $request->put('https://httpbin.org/put', ['X-Test: test'], json_encode(['name' => 'GuiBranco'])); + $this->assertEquals(200, $response->statusCode); + } + + public function testCanPatchWithHeaders(): void + { + $request = new Request(); + $response = $request->patch('https://httpbin.org/patch', ['X-Test: test'], json_encode(['name' => 'GuiBranco'])); + $this->assertEquals(200, $response->statusCode); + } + + public function testCanDeleteWithHeaders(): void + { + $request = new Request(); + $response = $request->delete('https://httpbin.org/delete', ['X-Test: test'], json_encode(['name' => 'GuiBranco'])); $this->assertEquals(200, $response->statusCode); }