From 5744ab671191d6b5c4f31179a936a1bffc380a28 Mon Sep 17 00:00:00 2001 From: markus Date: Sat, 31 Dec 2022 12:17:41 +0100 Subject: [PATCH] Use artisan 'about' command --- CHANGELOG.md | 2 ++ README.md | 38 ++++++++++++++---------------- src/Repository.php | 29 +++++------------------ src/ServiceProvider.php | 10 ++++++++ tests/Checks/DatabaseCheckTest.php | 4 ++-- tests/Feature/RouteTest.php | 7 +++++- tests/RepositoryTest.php | 29 ++++++----------------- tests/TestCheck.php | 2 +- 8 files changed, 52 insertions(+), 69 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfaab2d..5b41e7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 16dc039..796b49b 100644 --- a/README.md +++ b/README.md @@ -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": [ { @@ -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": [] } diff --git a/src/Repository.php b/src/Repository.php index a4d54df..0b2726b 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -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 diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index ecc8e44..21308cd 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -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; @@ -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'), + ]); } } diff --git a/tests/Checks/DatabaseCheckTest.php b/tests/Checks/DatabaseCheckTest.php index bb94215..bb56b62 100644 --- a/tests/Checks/DatabaseCheckTest.php +++ b/tests/Checks/DatabaseCheckTest.php @@ -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(); @@ -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:', diff --git a/tests/Feature/RouteTest.php b/tests/Feature/RouteTest.php index a5797a0..e78f7b9 100644 --- a/tests/Feature/RouteTest.php +++ b/tests/Feature/RouteTest.php @@ -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']); } } diff --git a/tests/RepositoryTest.php b/tests/RepositoryTest.php index a3c8e52..7f5b63b 100644 --- a/tests/RepositoryTest.php +++ b/tests/RepositoryTest.php @@ -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', @@ -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'); - } } diff --git a/tests/TestCheck.php b/tests/TestCheck.php index e750563..a28bca7 100644 --- a/tests/TestCheck.php +++ b/tests/TestCheck.php @@ -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.'); }