Skip to content

Commit

Permalink
Merge pull request #23 from cronitorio/unit-tests
Browse files Browse the repository at this point in the history
Fixes and unit tests
  • Loading branch information
aflanagan authored Mar 2, 2021
2 parents 830eb2d + 8186013 commit f0dddc7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.DS_Store
/coverage
*.code-workspace
.phpunit.result.cache
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.3
1.0.4
1 change: 1 addition & 0 deletions lib/Client.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Cronitor;

class Client
Expand Down
24 changes: 13 additions & 11 deletions lib/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Monitor
{
private const BASE_MONITOR_API_URL = 'https://cronitor.io/api/monitors';
private const BASE_PING_API_URL = "https://cronitor.link/p";
private const BASE_FALLBACK_PING_URL = "https://cronitor.link/p";
private const BASE_FALLBACK_PING_API_URL = "https://cronitor.io/p";
private const PING_RETRY_THRESHOLD = 5;

public $apiKey;
Expand Down Expand Up @@ -76,7 +76,7 @@ public static function delete($apiKey, $apiVersion, $key)

public function ping($params = array())
{
$retryCount = $params['retryCount'] ?: 0;
$retryCount = isset($params['retryCount']) ? $params['retryCount'] : 0;

if (!$this->apiKey) {
\error_log('No API key detected. Set Cronitor.api_key or initialize Monitor with an api_key:', 0);
Expand All @@ -86,24 +86,24 @@ public function ping($params = array())
try {
$queryString = $this->buildPingQuery($params);
$client = $this->getPingClient($retryCount);
$response = $client->get("/?$queryString");
$response = $client->get("?$queryString");
$responseCode = $response['code'];

if ($responseCode < 200 || $responseCode > 299) {
if ($responseCode !== 200) {
\error_log("Cronitor Telemetry Error: $responseCode", 0);
return false;
}

return true;
} catch (Exception $e) {
} catch (\Exception $e) {
// rescue instances of StandardError i.e. Timeout::Error, SocketError, etc
\error_log("Cronitor Telemetry Error: $e", 0);
if ($retryCount >= self::PING_RETRY_THRESHOLD) {
return false;
}

// apply a backoff before sending the next ping
sleep(calculateSleep($retryCount));
sleep($this->calculateSleep($retryCount));
$this->ping(array_merge($params, ['retryCount' => $retryCount + 1]));
}
}
Expand Down Expand Up @@ -163,9 +163,11 @@ private function cleanParams($params)
'env' => isset($params['env']) ? $params['env'] : null,
];

return array_filter($cleanedParams, function ($v) {
$filteredParams = array_filter($cleanedParams, function ($v) {
return !is_null($v);
});

return $filteredParams;
}

private function cleanMetrics($metrics)
Expand All @@ -183,21 +185,21 @@ private function getPingClient($retryCount)
}
private function getPingApiUrl()
{
return self::BASE_PING_API_URL . "/$apiKey/$key";
return self::BASE_PING_API_URL . "/$this->apiKey/$this->key";
}

private function getFallbackPingApiUrl()
{
return self::BASE_FALLBACK_PING_API_URL . "/$apiKey/$key";
return self::BASE_FALLBACK_PING_API_URL . "/$this->apiKey/$this->key";
}

private function buildPingQuery($params)
{
$cleanParams = $this->cleanParams($params);
$metrics = $cleanParams['metric'];
unset($cleanParams['metric']);

$queryParams = array_map(function ($key) {
$queryParams = array_map(function ($key) use ($cleanParams) {
$value = $cleanParams[$key];
return "$key=$value";
}, array_keys($cleanParams));
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
Expand Down
32 changes: 32 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use PHPUnit\Framework\TestCase;

final class ClientTest extends TestCase
{
private $client;

protected function setUp(): void
{
$this->client = new Cronitor\Client($apiKey, $apiVersion, $environment);
}

public function testIsInitializable()
{
$apiKey = '1234';
$apiVersion = '2020-01-01';
$environment = 'staging';

$this->client = new Cronitor\Client($apiKey, $apiVersion, $environment);
$this->assertEquals($this->client->apiKey, $apiKey);
$this->assertEquals($this->client->apiVersion, $apiVersion);
$this->assertEquals($this->client->environment, $environment);
}

public function testMonitor()
{
$monitorKey = '1234';
$monitor = $this->client->monitor($monitorKey);
$this->assertInstanceOf(Cronitor\Monitor::class, $monitor);
}
}

0 comments on commit f0dddc7

Please sign in to comment.