Skip to content

Commit

Permalink
Add enum ResultState
Browse files Browse the repository at this point in the history
  • Loading branch information
ttrig committed Mar 22, 2024
1 parent 6fe4848 commit 566d4e4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- **BREAKING**: Require Laravel 11.
- **BREAKING**: Add enum `ResultState`.

## [0.5.2] - 2023-12-29
### Added
Expand Down
22 changes: 22 additions & 0 deletions src/Enums/ResultState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Butler\Health\Enums;

enum ResultState: string
{
case OK = 'ok';
case WARNING = 'warning';
case CRITICAL = 'critical';
case UNKNOWN = 'unknown';

public function order(): int
{
return match ($this) {
self::CRITICAL => 3,
self::WARNING => 2,
self::OK => 1,
self::UNKNOWN => 0,
default => 0,
};
}
}
32 changes: 9 additions & 23 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,34 @@

namespace Butler\Health;

use Butler\Health\Enums\ResultState;

class Result
{
public const OK = 'ok';
public const WARNING = 'warning';
public const CRITICAL = 'critical';
public const UNKNOWN = 'unknown';

public $value = null;

private function __construct(public string $message, public string $state)
private function __construct(public string $message, public ResultState $state)
{
}

public static function ok(string $message): static
{
return new static($message, static::OK);
return new static($message, ResultState::OK);
}

public static function warning(string $message): static
{
return new static($message, static::WARNING);
return new static($message, ResultState::WARNING);
}

public static function critical(string $message): static
{
return new static($message, static::CRITICAL);
return new static($message, ResultState::CRITICAL);
}

public static function unknown(string $message): static
{
return new static($message, static::UNKNOWN);
return new static($message, ResultState::UNKNOWN);
}

public function value()
Expand All @@ -44,24 +41,13 @@ public function value()
return $this->value;
}

public function order(): int
{
return match ($this->state) {
static::CRITICAL => 3,
static::WARNING => 2,
static::OK => 1,
static::UNKNOWN => 0,
default => 0,
};
}

public function toArray(): array
{
return [
'value' => $this->value,
'message' => $this->message,
'state' => $this->state,
'order' => $this->order(),
'state' => $this->state->value,
'order' => $this->state->order(),
];
}
}
14 changes: 7 additions & 7 deletions tests/Checks/DatabaseCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Butler\Tests\Health;

use Butler\Health\Checks\Database;
use Butler\Health\Result;
use Butler\Health\Enums\ResultState;
use Butler\Health\Tests\AbstractTestCase;
use Illuminate\Support\Facades\DB;

Expand All @@ -16,7 +16,7 @@ public function test_unknown_when_no_database_connections_exist()
$result = (new Database())->run();

$this->assertEquals('No database connections found.', $result->message);
$this->assertEquals(Result::UNKNOWN, $result->state);
$this->assertEquals(ResultState::UNKNOWN, $result->state);
$this->assertNull($result->value());
}

Expand All @@ -34,7 +34,7 @@ public function test_ok_when_single_database_connection_succeeds()
$result = (new Database())->run();

$this->assertEquals('Connected to the database.', $result->message);
$this->assertEquals(Result::OK, $result->state);
$this->assertEquals(ResultState::OK, $result->state);
$this->assertEquals(1, $result->value());
}

Expand All @@ -56,7 +56,7 @@ public function test_ok_when_multiple_database_connections_succeeds()
$result = (new Database())->run();

$this->assertEquals('Connected to all 2 databases.', $result->message);
$this->assertEquals(Result::OK, $result->state);
$this->assertEquals(ResultState::OK, $result->state);
$this->assertEquals(1, $result->value());
}

Expand All @@ -76,7 +76,7 @@ public function test_ok_when_connection_was_disconnected()
$result = (new Database())->run();

$this->assertEquals('Connected to the database.', $result->message);
$this->assertEquals(Result::OK, $result->state);
$this->assertEquals(ResultState::OK, $result->state);
$this->assertEquals(1, $result->value());
}

Expand All @@ -87,7 +87,7 @@ public function test_critical_when_one_database_connection_fails()
$result = (new Database())->run();

$this->assertEquals('Not connected to the database.', $result->message);
$this->assertEquals(Result::CRITICAL, $result->state);
$this->assertEquals(ResultState::CRITICAL, $result->state);
$this->assertEquals(0, $result->value());
}

Expand All @@ -106,7 +106,7 @@ public function test_critical_when_one_of_multiple_database_connections_fails()
$result = (new Database())->run();

$this->assertEquals('Connected to 1 of 2 databases.', $result->message);
$this->assertEquals(Result::CRITICAL, $result->state);
$this->assertEquals(ResultState::CRITICAL, $result->state);
$this->assertEquals(0.5, $result->value());
}
}
10 changes: 5 additions & 5 deletions tests/Checks/FailedJobsCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Butler\Health\Tests;

use Butler\Health\Checks\FailedJobs;
use Butler\Health\Result;
use Butler\Health\Enums\ResultState;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

Expand All @@ -23,7 +23,7 @@ public function test_ok_when_no_failed_jobs_exist()
$result = (new FailedJobs())->run();

$this->assertEquals('No failed jobs.', $result->message);
$this->assertEquals(Result::OK, $result->state);
$this->assertEquals(ResultState::OK, $result->state);
$this->assertEquals(0, $result->value());
}

Expand All @@ -37,7 +37,7 @@ public function test_critical_when_multiple_failed_jobs_exist()
$result = (new FailedJobs())->run();

$this->assertEquals('2 failed jobs.', $result->message);
$this->assertEquals(Result::CRITICAL, $result->state);
$this->assertEquals(ResultState::CRITICAL, $result->state);
$this->assertEquals(2, $result->value());
}

Expand All @@ -48,7 +48,7 @@ public function test_critical_when_one_failed_job_exist()
$result = (new FailedJobs())->run();

$this->assertEquals('One failed job.', $result->message);
$this->assertEquals(Result::CRITICAL, $result->state);
$this->assertEquals(ResultState::CRITICAL, $result->state);
$this->assertEquals(1, $result->value());
}

Expand All @@ -59,7 +59,7 @@ public function test_unknown_when_table_dont_exist()
$result = (new FailedJobs())->run();

$this->assertEquals('Table foobar not found.', $result->message);
$this->assertEquals(Result::UNKNOWN, $result->state);
$this->assertEquals(ResultState::UNKNOWN, $result->state);
$this->assertNull($result->value());
}
}
8 changes: 4 additions & 4 deletions tests/Checks/RedisCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Butler\Tests\Health;

use Butler\Health\Checks\Redis;
use Butler\Health\Result;
use Butler\Health\Enums\ResultState;
use Butler\Health\Tests\AbstractTestCase;
use Illuminate\Support\Facades\Redis as RedisClient;

Expand All @@ -18,7 +18,7 @@ public function test_unknown_when_redis_extension_is_not_loaded()
$result = (new Redis())->run();

$this->assertEquals('Redis extension not enabled.', $result->message);
$this->assertEquals(Result::UNKNOWN, $result->state);
$this->assertEquals(ResultState::UNKNOWN, $result->state);
$this->assertNull($result->value());
}

Expand All @@ -33,7 +33,7 @@ public function test_unknown_when_redis_host_is_undefined()
$result = (new Redis())->run();

$this->assertEquals('Redis host undefined.', $result->message);
$this->assertEquals(Result::UNKNOWN, $result->state);
$this->assertEquals(ResultState::UNKNOWN, $result->state);
$this->assertNull($result->value());
}

Expand All @@ -50,7 +50,7 @@ public function test_ok()
$result = (new Redis())->run();

$this->assertEquals('Connected to redis on localhost.', $result->message);
$this->assertEquals(Result::OK, $result->state);
$this->assertEquals(ResultState::OK, $result->state);
$this->assertNull($result->value());
}
}

0 comments on commit 566d4e4

Please sign in to comment.