Skip to content

Commit

Permalink
Use artisan 'about' command
Browse files Browse the repository at this point in the history
  • Loading branch information
ttrig committed Jan 19, 2023
1 parent 1f80316 commit 5744ab6
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 69 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- **BREAKING**: Use `artisan about` instead of `customApplicationData`.

## [0.2.2] - 2022-12-27
### Added
Expand Down
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ The default route is `/health` and is configured at `butler.health.route`.
The endpoint will return data in JSON.

```json
// example response
{
"application": {
"name": "application name",
"timezone": "Europe/Stockholm",
"php": "8.0.10",
"laravel": "8.60.0",
"butlerHealth": "0.1"
"about": {
"environment": {},
"cache": {},
"drivers": {},
"butler_health": {
"version": "0.1"
},
},
"checks": [
{
Expand Down Expand Up @@ -76,27 +76,25 @@ return [

Extend `Butler\Health\Check` and add it to `butler.health.checks`, done.

## Custom application data
## Custom about information

If you want custom "application" data in the response you can register a callback like in the example below.
You can push additional "about" information via [laravels `AboutCommand`](https://laravel.com/docs/9.x/packages#about-artisan-command) class.

```php
Repository::customApplicationData(fn () => [
'name' => 'custom name',
'operatingSystem' => php_uname('s'),'v'),
AboutCommand::add('Environment', fn () => [
'Operating System' => php_uname('s'),
]);
```

```json
// example response with custom application data
{
"application": {
"name": "custom name",
"timezone": "Europe/Stockholm",
"php": "8.0.10",
"laravel": "8.60.0",
"butlerHealth": "0.1",
"operatingSystem": "Linux"
"about": {
"environment": {
"operating_system": "Linux"
},
"cache": {},
"drivers": {},
"butler_health": {},
},
"checks": []
}
Expand Down
29 changes: 6 additions & 23 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,25 @@

namespace Butler\Health;

use Closure;
use Composer\InstalledVersions;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Artisan;

class Repository
{
protected static $customApplicationData;

public function __invoke()
{
return [
'application' => $this->applicationData(),
'about' => $this->about(),
'checks' => $this->checks(),
];
}

public static function customApplicationData(Closure $callback): void
private function about(): array
{
static::$customApplicationData = $callback;
}

private function applicationData(): array
{
$data = [
'name' => config('app.name'),
'timezone' => config('app.timezone'),
'php' => PHP_VERSION,
'laravel' => Application::VERSION,
'butlerHealth' => ltrim(InstalledVersions::getPrettyVersion('glesys/butler-health'), 'v'),
];
Artisan::call('about --json');

if (static::$customApplicationData) {
return array_merge($data, (static::$customApplicationData)());
}
$output = Artisan::output();

return $data;
return json_decode(trim($output), true);
}

private function checks(): array
Expand Down
10 changes: 10 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Butler\Health;

use Composer\InstalledVersions;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

Expand All @@ -20,5 +22,13 @@ public function boot()
if (! app()->routesAreCached() && config('butler.health.route')) {
Route::get(config('butler.health.route'), Controller::class)->name('butler-health');
}

AboutCommand::add('Environment', fn () => [
'Timezone' => config('app.timezone'),
]);

AboutCommand::add('Butler Health', fn () => [
'Version' => ltrim(InstalledVersions::getPrettyVersion('glesys/butler-health'), 'v'),
]);
}
}
4 changes: 2 additions & 2 deletions tests/Checks/DatabaseCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function test_ok_when_connection_was_disconnected()

public function test_critical_when_one_database_connection_fails()
{
config(['database.connections' => ['foobar']]);
config(['database.connections' => ['foobar' => []]]);

$result = (new Database())->run();

Expand All @@ -95,7 +95,7 @@ public function test_critical_when_one_of_multiple_database_connections_fails()
{
config([
'database.connections' => [
'foobar',
'foobar' => [],
'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
Expand Down
7 changes: 6 additions & 1 deletion tests/Feature/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

namespace Butler\Health\Tests\Feature;

use Butler\Health\Repository;
use Butler\Health\Tests\AbstractTestCase;

class RouteTest extends AbstractTestCase
{
public function test_happy_path()
{
$this->mock(Repository::class, function ($mock) {
$mock->expects('__invoke')->andReturns(['data']);
});

$this->getJson(route('butler-health'))
->assertOk()
->assertJsonStructure();
->assertExactJson(['data']);
}
}
29 changes: 7 additions & 22 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class RepositoryTest extends AbstractTestCase
{
public function test_returns_correct_data()
{
$this->travelTo(now());

$result = (new Repository())();
$result = $this->travelTo(now(), function () {
return (new Repository())();
});

AssertableJson::fromArray($result)
->has('application', fn (AssertableJson $json) => $json
->where('name', config('app.name'))
->where('timezone', config('app.timezone'))
->hasAll('php', 'laravel', 'butlerHealth'))
->has('about', fn (AssertableJson $json) => $json
->where('environment.timezone', config('app.timezone'))
->has('butler_health.version')
->etc())
->where('checks', [
[
'name' => 'Test Check',
Expand All @@ -34,19 +34,4 @@ public function test_returns_correct_data()
],
]);
}

public function test_customApplicationData()
{
Repository::customApplicationData(fn () => [
'name' => 'custom name',
'foo' => 'bar',
]);

$result = (new Repository())();

AssertableJson::fromArray($result['application'])
->where('name', 'custom name')
->where('foo', 'bar')
->hasAll('timezone', 'php', 'laravel', 'butlerHealth');
}
}
2 changes: 1 addition & 1 deletion tests/TestCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestCheck extends Check

public function run(): Result
{
Carbon::setTestNow(Carbon::now()->addMilliseconds(100));
Carbon::setTestNow(now()->addMilliseconds(100));

return Result::ok('Looking good.');
}
Expand Down

0 comments on commit 5744ab6

Please sign in to comment.