Skip to content

Commit

Permalink
Merge branch 'v2.0' of github.com:ans-group/laravel-health-check into…
Browse files Browse the repository at this point in the history
… v2.0
  • Loading branch information
phily245 committed Jul 23, 2024
2 parents 54d9793 + ad0b005 commit 8ca082b
Show file tree
Hide file tree
Showing 38 changed files with 501 additions and 427 deletions.
23 changes: 20 additions & 3 deletions .github/workflows/tests.yml → .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Pull Request
name: Checks

on:
push:
Expand All @@ -25,7 +25,7 @@ jobs:
- php_version: 8.3
laravel_version: 11

name: PHP ${{ matrix.environment.php_version }} & Laravel ${{ matrix.environment.laravel_version }} - ubuntu-latest
name: PHP ${{ matrix.environment.php_version }} & Laravel ${{ matrix.environment.laravel_version }}

steps:
- name: Checkout code
Expand All @@ -38,4 +38,21 @@ jobs:
laravel_version: ${{ matrix.environment.laravel_version }}

- name: Execute tests
run: vendor/bin/phpunit
run: composer test

php-codesniffer:
runs-on: ubuntu-latest
name: PHP Codesniffer

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup
with:
php_version: 8.3
laravel_version: 11

- name: Run codesniffer
run: composer phpcs
31 changes: 19 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
CONTRIBUTING
============
# CONTRIBUTING


Contributions are welcome and we look forward to seeing what you can bring to the project.

We do reserve the right to reject changes that we feel will not benefit users or take the project in a direction we do not wish to take.
We recommend discussing any large changes with us prior to making your changes, you can do this via an issue or email if you prefer.


Issues
------
## Issues

Issues should include a title and clear description with as much relevant information as possible.

If you'd like to discuss a potential change, you can open an issue or reach out to our open-source team via **[email protected]** who will get back to you as soon as possible.

If you think you have identified a security vulnerability, please contact our team via **[email protected]** instead of using the issue tracker.


Submitting Changes
------------------
## Submitting Changes

Before submitting your [pull request](https://help.github.com/en/articles/about-pull-requests),
please make sure that the coding standards are respected and that all tests are passing.
Expand All @@ -30,14 +26,25 @@ but larger changes should follow [The Seven Rules](https://chris.beams.io/posts/

We don't mind squashing small changes to tidy things up but please don't squash an entire branch as this can hinder code reviews.

## Coding Standards

### All Standards

All coding standards can be checked by running `composer standards:check` and any issues which can be automatically
resolved can be fixed by running `composer standards:fix`.

Coding Standards
---------------
### PSR12

This project adheres to the [PSR-12 Coding Standard](https://www.php-fig.org/psr/psr-12/).

#### PHP CodeSniffer && PHP Code Beautifier

This can be checked with `composer phpcs` and issues which can be fixed automatically can be fixed by running `composer phpcs:fix`.


## Testing

Testing
-------

Please ensure all new functionality is matched with appropriate tests, we will advise if we feel more is needed.

Tests can be run with `composer test`.
20 changes: 16 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@
"league/flysystem-ftp": "^3.28"
},
"require-dev": {
"phpunit/phpunit": "^10.0|^11.0",
"orchestra/testbench": "^8.0|^9.0",
"mockery/mockery": "^1.0",
"guzzlehttp/guzzle": "^7.5",
"illuminate/database": "^10.0|^11.0",
"illuminate/log": "^10.0|^11.0",
"illuminate/redis": "^10.0|^11.0",
"guzzlehttp/guzzle": "^7.5"
"mockery/mockery": "^1.0",
"orchestra/testbench": "^8.0|^9.0",
"phpunit/phpunit": "^10.0|^11.0",
"squizlabs/php_codesniffer": "^3.10"
},
"suggest": {
"illuminate/database": "Allows the database health check to function.",
Expand All @@ -79,5 +80,16 @@
"league/flysystem-ftp": "Allows the ftp health check to function.",
"guzzlehttp/guzzle": "Allows the http health check to function.",
"enlightn/security-checker": "Allows the package security health check to function."
},
"scripts": {
"standards:check": [
"@phpcs"
],
"standards:fix": [
"@phpcs:fix"
],
"test": "phpunit",
"phpcs": "./vendor/bin/phpcs --standard=PSR12 --ignore=vendor .",
"phpcs:fix": "./vendor/bin/phpcbf --standard=PSR12 --ignore=vendor ."
}
}
1 change: 0 additions & 1 deletion src/AppHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class AppHealth
{

public function __construct(
/**
* @var Collection<int, class-string>
Expand Down
2 changes: 1 addition & 1 deletion src/Checks/LogHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public function status(): Status

return $this->okay();
}
}
}
5 changes: 4 additions & 1 deletion src/Checks/PackageSecurityHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public function status(): Status
try {
if (! static::checkDependency(SecurityChecker::class)) {
if (static::checkDependency('SensioLabs\Security\SecurityChecker')) {
throw new Exception('The sensiolabs/security-checker package has been archived. Install enlightn/security-checker instead.');
throw new Exception(
'The sensiolabs/security-checker package has been archived.'
. ' Install enlightn/security-checker instead.'
);
}
throw new Exception('You need to install the enlightn/security-checker package to use this check.');
}
Expand Down
6 changes: 3 additions & 3 deletions src/HealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ public function name(): string

public function problem($message = '', $context = []): Status
{
return (new Status)
return (new Status())
->problem($message)
->withContext($context)
->withName($this->name());
}

public function degraded($message = '', $context = []): Status
{
return (new Status)
return (new Status())
->degraded($message)
->withContext($context)
->withName($this->name());
}

public function okay($context = []): Status
{
return (new Status)
return (new Status())
->okay()
->withContext($context)
->withName($this->name());
Expand Down
20 changes: 11 additions & 9 deletions src/HealthCheckServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ public function boot(): void
{
$this->configure();

$this->app->make('router')->get($this->withBasePath(config('healthcheck.route-paths.health', '/health')), [
'middleware' => config('healthcheck.middleware'),
'uses' => HealthCheckController::class,
'as' => config('healthcheck.route-name')
]);
$this->app->make('router')
->get($this->withBasePath(config('healthcheck.route-paths.health', '/health')), [
'middleware' => config('healthcheck.middleware'),
'uses' => HealthCheckController::class,
'as' => config('healthcheck.route-name')
]);

$this->app->bind('app-health', function ($app) {
$checks = collect();
Expand All @@ -38,16 +39,17 @@ public function boot(): void
]);
}

$this->app->make('router')->get($this->withBasePath(config('healthcheck.route-paths.ping', '/ping')), PingController::class);
$this->app->make('router')
->get($this->withBasePath(config('healthcheck.route-paths.ping', '/ping')), PingController::class);
}

protected function configure(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/healthcheck.php', 'healthcheck');
$configPath = $this->app->basePath().'/config/healthcheck.php';
$this->mergeConfigFrom(__DIR__ . '/../config/healthcheck.php', 'healthcheck');
$configPath = $this->app->basePath() . '/config/healthcheck.php';

$this->publishes([
__DIR__.'/../config/healthcheck.php' => $configPath,
__DIR__ . '/../config/healthcheck.php' => $configPath,
], 'config');

if ($this->app instanceof \Laravel\Lumen\Application) {
Expand Down
6 changes: 3 additions & 3 deletions src/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

class Status
{
const PROBLEM = 'PROBLEM';
public const PROBLEM = 'PROBLEM';

const DEGRADED = 'DEGRADED';
public const DEGRADED = 'DEGRADED';

const OKAY = 'OK';
public const OKAY = 'OK';

protected string|null $status = null;

Expand Down
50 changes: 7 additions & 43 deletions tests/AppHealthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,43 @@

namespace Tests;

use RuntimeException;
use Tests\Stubs\Checks\AlwaysDownCheck;
use Tests\Stubs\Checks\AlwaysUpCheck;
use Tests\Stubs\Checks\UnreliableCheck;
use UKFast\HealthCheck\AppHealth;
use UKFast\HealthCheck\Exceptions\CheckNotFoundException;
use UKFast\HealthCheck\HealthCheck;
use UKFast\HealthCheck\Status;

class AppHealthTest extends TestCase
{
public function testCanSeeIfACheckPassesByName(): void
{
$appHealth = new AppHealth(collect([new AlwaysUpCheck, new AlwaysDownCheck]));
$appHealth = new AppHealth(collect([new AlwaysUpCheck(), new AlwaysDownCheck()]));

$this->assertTrue($appHealth->passes('always-up'));
$this->assertFalse($appHealth->passes('always-down'));
}

public function testCanSeeIfACheckFailsByName(): void
{
$appHealth = new AppHealth(collect([new AlwaysUpCheck, new AlwaysDownCheck]));
$appHealth = new AppHealth(collect([new AlwaysUpCheck(), new AlwaysDownCheck()]));

$this->assertFalse($appHealth->fails('always-up'));
$this->assertTrue($appHealth->fails('always-down'));
}

public function testReturnsFalseIfCheckThrowsException(): void
{
$appHealth = new AppHealth(collect([new UnreliableCheck]));
$appHealth = new AppHealth(collect([new UnreliableCheck()]));

$this->assertFalse($appHealth->passes('unreliable'));
}

public function testThrowsExceptionIfCheckDoesNotExist(): void
{
$appHealth = new AppHealth(collect([new AlwaysUpCheck, new AlwaysDownCheck]));
$appHealth = new AppHealth(collect([new AlwaysUpCheck(), new AlwaysDownCheck()]));

$this->expectException(CheckNotFoundException::class);

$appHealth->passes('does-not-exist');
}
}

class AlwaysUpCheck extends HealthCheck
{
protected string $name = 'always-up';

public function status(): Status
{
return $this->okay();
}
}

class AlwaysDownCheck extends HealthCheck
{
protected string $name = 'always-down';

public function status(): Status
{
return $this->problem('Something went wrong', [
'debug' => 'info',
]);
}
}


class UnreliableCheck extends HealthCheck
{
protected string $name = 'unreliable';

/**
* @throws RuntimeException
*/
public function status(): never
{
throw new RuntimeException('Something went badly wrong');
}
}
13 changes: 1 addition & 12 deletions tests/Checks/CacheHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Tests\Checks;

use Exception;
use Illuminate\Foundation\Application;
use Tests\Stubs\Cache\BadStore;
use Tests\TestCase;
use Illuminate\Support\Facades\Cache;
use UKFast\HealthCheck\Checks\CacheHealthCheck;
Expand Down Expand Up @@ -65,14 +65,3 @@ public function testShowsOkayIfCanWriteToCache(): void
$this->assertTrue($status->isOkay());
}
}

class BadStore
{
/**
* @throws Exception
*/
public function __call($name, $arguments): never
{
throw new Exception();
}
}
Loading

0 comments on commit 8ca082b

Please sign in to comment.