From 1da5bc7462231bb56c00a5a1cbc8e145556b0254 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 14:20:57 +0100 Subject: [PATCH 01/13] Add PHPCS We can use this to ensure PSR12 compliance anfd automate fixes --- composer.json | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index d85ef8e..b60b5a0 100644 --- a/composer.json +++ b/composer.json @@ -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.", @@ -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 src/ tests/", + "phpcs:fix": "./vendor/bin/phpcbf --standard=PSR12 src/ tests/" } } From a2a67599b5e7a32d9919470bd5662047813c4a5c Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 14:21:56 +0100 Subject: [PATCH 02/13] Fix some PSR12 compliance issues I ran PHPCBF over the repo, these are all automated fixes --- src/AppHealth.php | 1 - src/Checks/LogHealthCheck.php | 2 +- src/HealthCheck.php | 6 ++--- src/HealthCheckServiceProvider.php | 6 ++--- tests/AppHealthTest.php | 10 ++++---- tests/Checks/CacheHealthCheckTest.php | 2 +- tests/Checks/DatabaseHealthCheckTest.php | 16 ++++++------- tests/Checks/EnvHealthCheckTest.php | 10 ++++---- tests/Checks/FtpHealthCheckTest.php | 3 ++- tests/Checks/LogHealthCheckTest.php | 5 ++-- .../MigrationUpToDateHealthCheckTest.php | 1 - .../Checks/PackageSecurityHealthCheckTest.php | 24 ++++++++----------- tests/Checks/RedisHealthCheckTest.php | 21 ++++++++-------- .../Controllers/HealthCheckControllerTest.php | 10 ++++---- tests/Controllers/PingControllerTest.php | 2 +- tests/Middleware/AddHeadersTest.php | 6 ++--- tests/Middleware/BasicAuthTest.php | 6 ++--- tests/StatusTest.php | 10 ++++---- 18 files changed, 66 insertions(+), 75 deletions(-) diff --git a/src/AppHealth.php b/src/AppHealth.php index ccc60c0..985bea2 100644 --- a/src/AppHealth.php +++ b/src/AppHealth.php @@ -8,7 +8,6 @@ class AppHealth { - public function __construct( /** * @var Collection diff --git a/src/Checks/LogHealthCheck.php b/src/Checks/LogHealthCheck.php index 9715e6d..3ab029a 100644 --- a/src/Checks/LogHealthCheck.php +++ b/src/Checks/LogHealthCheck.php @@ -35,4 +35,4 @@ public function status(): Status return $this->okay(); } -} \ No newline at end of file +} diff --git a/src/HealthCheck.php b/src/HealthCheck.php index 786c5c9..82e30e0 100644 --- a/src/HealthCheck.php +++ b/src/HealthCheck.php @@ -17,7 +17,7 @@ public function name(): string public function problem($message = '', $context = []): Status { - return (new Status) + return (new Status()) ->problem($message) ->withContext($context) ->withName($this->name()); @@ -25,7 +25,7 @@ public function problem($message = '', $context = []): Status public function degraded($message = '', $context = []): Status { - return (new Status) + return (new Status()) ->degraded($message) ->withContext($context) ->withName($this->name()); @@ -33,7 +33,7 @@ public function degraded($message = '', $context = []): Status public function okay($context = []): Status { - return (new Status) + return (new Status()) ->okay() ->withContext($context) ->withName($this->name()); diff --git a/src/HealthCheckServiceProvider.php b/src/HealthCheckServiceProvider.php index ddf6b0b..6e47d9b 100644 --- a/src/HealthCheckServiceProvider.php +++ b/src/HealthCheckServiceProvider.php @@ -43,11 +43,11 @@ public function boot(): void 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) { diff --git a/tests/AppHealthTest.php b/tests/AppHealthTest.php index c20bd23..3213453 100644 --- a/tests/AppHealthTest.php +++ b/tests/AppHealthTest.php @@ -12,7 +12,7 @@ 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')); @@ -20,7 +20,7 @@ public function testCanSeeIfACheckPassesByName(): void 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')); @@ -28,14 +28,14 @@ public function testCanSeeIfACheckFailsByName(): void 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); @@ -77,4 +77,4 @@ public function status(): never { throw new RuntimeException('Something went badly wrong'); } -} \ No newline at end of file +} diff --git a/tests/Checks/CacheHealthCheckTest.php b/tests/Checks/CacheHealthCheckTest.php index c0db0bf..b183df0 100644 --- a/tests/Checks/CacheHealthCheckTest.php +++ b/tests/Checks/CacheHealthCheckTest.php @@ -75,4 +75,4 @@ public function __call($name, $arguments): never { throw new Exception(); } -} \ No newline at end of file +} diff --git a/tests/Checks/DatabaseHealthCheckTest.php b/tests/Checks/DatabaseHealthCheckTest.php index 62bde15..3c4a98b 100644 --- a/tests/Checks/DatabaseHealthCheckTest.php +++ b/tests/Checks/DatabaseHealthCheckTest.php @@ -29,8 +29,8 @@ public function testShowsProblemWhenCantConnectToDb(): void 'healthcheck.database.connections' => ['default'], ]); - $db = new DatabaseManager; - $db->addConnection('default', new BadConnection); + $db = new DatabaseManager(); + $db->addConnection('default', new BadConnection()); $status = (new DatabaseHealthCheck($db))->status(); @@ -43,8 +43,8 @@ public function testShowsOkayWhenCanConnectToDb(): void 'healthcheck.database.connections' => ['default'], ]); - $db = new DatabaseManager; - $db->addConnection('default', new HealthyConnection); + $db = new DatabaseManager(); + $db->addConnection('default', new HealthyConnection()); $status = (new DatabaseHealthCheck($db))->status(); @@ -57,9 +57,9 @@ public function testShowsWhichConnectionFailed(): void 'healthcheck.database.connections' => ['healthy', 'bad'], ]); - $db = new DatabaseManager; - $db->addConnection('healthy', new HealthyConnection); - $db->addConnection('bad', new BadConnection); + $db = new DatabaseManager(); + $db->addConnection('healthy', new HealthyConnection()); + $db->addConnection('bad', new BadConnection()); $status = (new DatabaseHealthCheck($db))->status(); @@ -91,7 +91,7 @@ public function __construct() */ public function getPdo(): never { - throw new Exception; + throw new Exception(); } } diff --git a/tests/Checks/EnvHealthCheckTest.php b/tests/Checks/EnvHealthCheckTest.php index 4e14547..9e6e504 100644 --- a/tests/Checks/EnvHealthCheckTest.php +++ b/tests/Checks/EnvHealthCheckTest.php @@ -27,11 +27,11 @@ public function testShowsProblemIfMissingADotenvFile(): void 'REDIS_HOST', 'MYSQL_PASSWORD' ]]); - $status = (new EnvHealthCheck)->status(); + $status = (new EnvHealthCheck())->status(); $this->assertTrue($status->isProblem()); } - function testShowsOkayIfAllRequiredEnvParamsArePresent(): void + function testShowsOkayIfAllRequiredEnvParamsArePresent(): void { putenv('REDIS_HOST=here'); putenv('MYSQL_HOST=here'); @@ -41,9 +41,9 @@ function testShowsOkayIfAllRequiredEnvParamsArePresent(): void 'REDIS_HOST', 'MYSQL_PASSWORD' ]]); - $status = (new EnvHealthCheck)->status(); + $status = (new EnvHealthCheck())->status(); - $this->assertTrue($status->isOkay()); + $this->assertTrue($status->isOkay()); } public function testShowsOkayIfRequiredEnvParamIsPresentButNull(): void @@ -53,7 +53,7 @@ public function testShowsOkayIfRequiredEnvParamIsPresentButNull(): void config(['healthcheck.required-env' => [ 'REDIS_PASSWORD', ]]); - $status = (new EnvHealthCheck)->status(); + $status = (new EnvHealthCheck())->status(); $this->assertTrue($status->isOkay()); } diff --git a/tests/Checks/FtpHealthCheckTest.php b/tests/Checks/FtpHealthCheckTest.php index 0281080..4035381 100644 --- a/tests/Checks/FtpHealthCheckTest.php +++ b/tests/Checks/FtpHealthCheckTest.php @@ -26,7 +26,8 @@ public function testShowsProblemWhenCantConnectToFtpServer(): void public function testShowsOkayWhenCanConnectToFtpServer(): void { - function generator(): iterable { + function generator(): iterable + { yield 'foo'; yield 'bar'; yield 'baz'; diff --git a/tests/Checks/LogHealthCheckTest.php b/tests/Checks/LogHealthCheckTest.php index e4c4fde..41248fb 100644 --- a/tests/Checks/LogHealthCheckTest.php +++ b/tests/Checks/LogHealthCheckTest.php @@ -24,7 +24,7 @@ public function getPackageProviders($app): array public function testShowsProblemIfCannotWriteToLogs(): void { $this->app->bind('log', function () { - return new BadLogger; + return new BadLogger(); }); $status = (new LogHealthCheck($this->app))->status(); @@ -34,7 +34,7 @@ public function testShowsProblemIfCannotWriteToLogs(): void public function testShowsOkayIfCanWriteToLogs(): void { $this->app->bind('log', function () { - return new NullLogger; + return new NullLogger(); }); $status = (new LogHealthCheck($this->app))->status(); @@ -119,7 +119,6 @@ public function log($level, Stringable | string $message, array $context = []): class NullLogger implements LoggerInterface { - public function emergency(Stringable | string $message, array $context = []): void { } diff --git a/tests/Checks/MigrationUpToDateHealthCheckTest.php b/tests/Checks/MigrationUpToDateHealthCheckTest.php index beccedf..5c0c2ea 100644 --- a/tests/Checks/MigrationUpToDateHealthCheckTest.php +++ b/tests/Checks/MigrationUpToDateHealthCheckTest.php @@ -60,7 +60,6 @@ public function testCanReturnFalseWhenSchemaIsOutdated(): void $status = $this->healthCheck->status(); $this->assertFalse($status->isOkay()); $this->assertSame(['pending_migrations' => ['missing_migration.php']], $status->context()); - } public function testCanReturnFalseWhenRanMigrationCouldNotBeRetrieved(): void diff --git a/tests/Checks/PackageSecurityHealthCheckTest.php b/tests/Checks/PackageSecurityHealthCheckTest.php index dc2cebc..9111d35 100644 --- a/tests/Checks/PackageSecurityHealthCheckTest.php +++ b/tests/Checks/PackageSecurityHealthCheckTest.php @@ -36,7 +36,7 @@ protected function partialMock($abstract, Closure $mock = null): MockInterface public function testShowsProblemIfRequiredPackageNotLoaded() { - $status = (new MockedPackageSecurityHealthCheck)->status(); + $status = (new MockedPackageSecurityHealthCheck())->status(); $this->assertTrue($status->isProblem()); $this->assertSame('You need to install the enlightn/security-checker package to use this check.', $status->context()['exception']['error']); @@ -48,7 +48,7 @@ public function testShowsProblemIfIncorrectPackageLoaded(): void 'Enlightn\SecurityChecker\SecurityChecker' => false, 'SensioLabs\Security\SecurityChecker' => true, ]; - $status = (new MockedPackageSecurityHealthCheck)->status(); + $status = (new MockedPackageSecurityHealthCheck())->status(); $this->assertTrue($status->isProblem()); $this->assertSame('The sensiolabs/security-checker package has been archived. Install enlightn/security-checker instead.', $status->context()['exception']['error']); @@ -57,10 +57,9 @@ public function testShowsProblemIfIncorrectPackageLoaded(): void public function testShowsProblemIfCannotCheckPackages(): void { $this->partialMock('overload:Enlightn\SecurityChecker\SecurityChecker', fn (MockInterface $mock) => - $mock->shouldReceive('check')->andThrow(new Exception('File not found at [/tmp/composer.lock]')) - ); + $mock->shouldReceive('check')->andThrow(new Exception('File not found at [/tmp/composer.lock]'))); - $status = (new PackageSecurityHealthCheck)->status(); + $status = (new PackageSecurityHealthCheck())->status(); $this->assertTrue($status->isProblem()); } @@ -69,10 +68,9 @@ public function testShowsProblemIfPackageHasVulnerability(): void { $this->partialMock('overload:Enlightn\SecurityChecker\SecurityChecker', fn (MockInterface $mock) => $mock->shouldReceive('check') - ->andReturn(json_decode(file_get_contents('tests/json/securityCheckerPackageHasVulnerability.json'), true)) - ); + ->andReturn(json_decode(file_get_contents('tests/json/securityCheckerPackageHasVulnerability.json'), true))); - $status = (new PackageSecurityHealthCheck)->status(); + $status = (new PackageSecurityHealthCheck())->status(); $this->assertTrue($status->isProblem()); } @@ -87,10 +85,9 @@ public function testIgnoresPackageIfInConfig(): void $this->partialMock('overload:Enlightn\SecurityChecker\SecurityChecker', fn (MockInterface $mock) => $mock->shouldReceive('check') - ->andReturn(json_decode(file_get_contents('tests/json/securityCheckerPackageHasVulnerability.json'), true)) - ); + ->andReturn(json_decode(file_get_contents('tests/json/securityCheckerPackageHasVulnerability.json'), true))); - $status = (new PackageSecurityHealthCheck)->status(); + $status = (new PackageSecurityHealthCheck())->status(); $this->assertTrue($status->isOkay()); } @@ -99,10 +96,9 @@ public function testShowsOkayIfNoPackagesHaveVulnerabilities(): void { $this->partialMock('overload:Enlightn\SecurityChecker\SecurityChecker', fn(MockInterface $mock) => $mock->shouldReceive('check') - ->andReturn([]) - ); + ->andReturn([])); - $status = (new PackageSecurityHealthCheck)->status(); + $status = (new PackageSecurityHealthCheck())->status(); $this->assertTrue($status->isOkay()); } diff --git a/tests/Checks/RedisHealthCheckTest.php b/tests/Checks/RedisHealthCheckTest.php index 6e02ed8..63a4884 100644 --- a/tests/Checks/RedisHealthCheckTest.php +++ b/tests/Checks/RedisHealthCheckTest.php @@ -19,7 +19,6 @@ class RedisHealthCheckTest extends TestCase * @return array */ public function getPackageProviders($app): array - { return [HealthCheckServiceProvider::class]; } @@ -39,7 +38,7 @@ public function testShowsOkayIfCanPingPredis(): void Redis::swap($redis); - $status = (new RedisHealthCheck)->status(); + $status = (new RedisHealthCheck())->status(); $this->assertTrue($status->isOkay()); } @@ -58,7 +57,7 @@ public function testShowsProblemIfCannotPingPredis(): void Redis::swap($redis); - $status = (new RedisHealthCheck)->status(); + $status = (new RedisHealthCheck())->status(); $this->assertFalse($status->isOkay()); } @@ -77,7 +76,7 @@ public function testShowsOkayIfCannotPingPredisCluster(): void Redis::swap($redis); - $status = (new RedisHealthCheck)->status(); + $status = (new RedisHealthCheck())->status(); $this->assertTrue($status->isOkay()); } @@ -96,7 +95,7 @@ public function testShowsProblemIfCannotPingPredisCluster(): void Redis::swap($redis); - $status = (new RedisHealthCheck)->status(); + $status = (new RedisHealthCheck())->status(); $this->assertFalse($status->isOkay()); } @@ -124,7 +123,7 @@ public function testShowsOkayIfCanPingPhpredis(): void Redis::swap($redis); - $status = (new RedisHealthCheck)->status(); + $status = (new RedisHealthCheck())->status(); $this->assertTrue($status->isOkay()); } @@ -152,7 +151,7 @@ public function testShowsProblemIfCannotPingPhpredis(): void Redis::swap($redis); - $status = (new RedisHealthCheck)->status(); + $status = (new RedisHealthCheck())->status(); $this->assertFalse($status->isOkay()); } @@ -196,7 +195,7 @@ public function testShowsOkayIfCanPingPhpredisCluster(): void Redis::swap($redis); - $status = (new RedisHealthCheck)->status(); + $status = (new RedisHealthCheck())->status(); $this->assertTrue($status->isOkay()); } @@ -223,7 +222,7 @@ public function testShowsProblemIfCannotPingFirstPhpredisClusterMaster(): void $matcher = $this->exactly(1); $redisConn->expects($matcher) ->method('ping') - ->willReturnCallback(function() use ($matcher) { + ->willReturnCallback(function () use ($matcher) { return match ($matcher->numberOfInvocations()) { 1 => [['master1', '6379']], }; @@ -238,7 +237,7 @@ public function testShowsProblemIfCannotPingFirstPhpredisClusterMaster(): void Redis::swap($redis); - $status = (new RedisHealthCheck)->status(); + $status = (new RedisHealthCheck())->status(); $this->assertFalse($status->isOkay()); } @@ -282,7 +281,7 @@ public function testShowsProblemIfCannotPingThirdPhpredisClusterMaster(): void Redis::swap($redis); - $status = (new RedisHealthCheck)->status(); + $status = (new RedisHealthCheck())->status(); $this->assertFalse($status->isOkay()); } } diff --git a/tests/Controllers/HealthCheckControllerTest.php b/tests/Controllers/HealthCheckControllerTest.php index b225b7c..627b3f6 100644 --- a/tests/Controllers/HealthCheckControllerTest.php +++ b/tests/Controllers/HealthCheckControllerTest.php @@ -25,7 +25,7 @@ public function getPackageProviders($app): array public function testReturnsOverallStatusOfOkayWhenEverythingIsUp(): void { $this->setChecks([AlwaysUpCheck::class]); - $response = (new HealthCheckController)->__invoke($this->app); + $response = (new HealthCheckController())->__invoke($this->app); $this->assertSame([ 'status' => 'OK', @@ -106,7 +106,6 @@ public function testDefaultsThePingPathIfConfigIsNotSet(): void $response = $this->get('/ping'); $this->assertSame('pong', $response->getContent()); - } public function testDefaultsTheHealthPathIfConfigIsNotSet(): void @@ -152,7 +151,7 @@ public function testReturnsDegradedStatusWithResponseCode200WhenServiceIsDegrade public function testReturnsStatusOfProblemWhenAProblemOccurs(): void { $this->setChecks([AlwaysUpCheck::class, AlwaysDownCheck::class]); - $response = (new HealthCheckController)->__invoke($this->app); + $response = (new HealthCheckController())->__invoke($this->app); $this->assertSame([ 'status' => 'PROBLEM', @@ -168,7 +167,7 @@ public function testReturnsStatusOfProblemWhenAProblemOccurs(): void public function testReturnsStatusOfProblemWhenBothDegradedAndProblemStatusesOccur(): void { $this->setChecks([AlwaysUpCheck::class, AlwaysDegradedCheck::class, AlwaysDownCheck::class]); - $response = (new HealthCheckController)->__invoke($this->app); + $response = (new HealthCheckController())->__invoke($this->app); $this->assertSame([ 'status' => 'PROBLEM', @@ -186,7 +185,7 @@ public function testReturnsStatusOfProblemWhenBothDegradedAndProblemStatusesOccu ], json_decode($response->getContent(), true)); $this->setChecks([AlwaysUpCheck::class, AlwaysDownCheck::class, AlwaysDegradedCheck::class,]); - $response = (new HealthCheckController)->__invoke($this->app); + $response = (new HealthCheckController())->__invoke($this->app); $this->assertSame([ 'status' => 'PROBLEM', @@ -202,7 +201,6 @@ public function testReturnsStatusOfProblemWhenBothDegradedAndProblemStatusesOccu 'context' => ['debug' => 'info'], ], ], json_decode($response->getContent(), true)); - } protected function setChecks($checks): void diff --git a/tests/Controllers/PingControllerTest.php b/tests/Controllers/PingControllerTest.php index b1c03c5..cf319ae 100644 --- a/tests/Controllers/PingControllerTest.php +++ b/tests/Controllers/PingControllerTest.php @@ -20,7 +20,7 @@ public function getPackageProviders($app): array public function testReturnsPong(): void { - $this->assertSame('pong', (new PingController)->__invoke()); + $this->assertSame('pong', (new PingController())->__invoke()); } public function testOverridesDefaultPath(): void diff --git a/tests/Middleware/AddHeadersTest.php b/tests/Middleware/AddHeadersTest.php index aa0ea16..0bc62ca 100644 --- a/tests/Middleware/AddHeadersTest.php +++ b/tests/Middleware/AddHeadersTest.php @@ -15,13 +15,13 @@ public function testAddsHeadersForEachCheck(): void { $this->app->bind('app-health', function () { return new AppHealth(collect([ - new AlwaysUpCheck, - new AlwaysDownCheck, + new AlwaysUpCheck(), + new AlwaysDownCheck(), ])); }); $request = Request::create('/health', 'GET'); - $response = (new AddHeaders)->handle($request, function ($request) { + $response = (new AddHeaders())->handle($request, function ($request) { return response()->json(null, 500); }); diff --git a/tests/Middleware/BasicAuthTest.php b/tests/Middleware/BasicAuthTest.php index 3044871..b619b36 100644 --- a/tests/Middleware/BasicAuthTest.php +++ b/tests/Middleware/BasicAuthTest.php @@ -20,7 +20,7 @@ public function testOnlyShowsStatusCodeIfFailsBasicAuth(): void 'PHP_AUTH_PW' => 'wrong-password', ]); - $response = (new BasicAuth)->handle($request, function () { + $response = (new BasicAuth())->handle($request, function () { return response('body', 500); }); @@ -37,7 +37,7 @@ public function testOnlyShowsStatusCodeIfNoAuthCredentialsArePassed(): void $request = Request::create('/health', 'GET'); - $response = (new BasicAuth)->handle($request, function () { + $response = (new BasicAuth())->handle($request, function () { return response('body', 500); }); @@ -57,7 +57,7 @@ public function testShowsFullResponseIfPassesBasicAuth(): void 'PHP_AUTH_PW' => 'correct-password', ]); - $response = (new BasicAuth)->handle($request, function () { + $response = (new BasicAuth())->handle($request, function () { return response('body', 500); }); diff --git a/tests/StatusTest.php b/tests/StatusTest.php index 297ca85..18cef23 100644 --- a/tests/StatusTest.php +++ b/tests/StatusTest.php @@ -8,7 +8,7 @@ class StatusTest extends TestCase { public function testCanCreateAnOkayStatus(): void { - $status = (new Status)->okay(); + $status = (new Status())->okay(); $this->assertTrue($status->isOkay()); $this->assertFalse($status->isProblem()); @@ -16,7 +16,7 @@ public function testCanCreateAnOkayStatus(): void public function testCanCreateAProblemStatus(): void { - $status = (new Status)->problem(); + $status = (new Status())->problem(); $this->assertTrue($status->isProblem()); $this->assertFalse($status->isOkay()); @@ -24,7 +24,7 @@ public function testCanCreateAProblemStatus(): void public function testCanInjectAContext(): void { - $status = (new Status)->withContext([ + $status = (new Status())->withContext([ 'context' => 'arbitrary context', ]); @@ -38,14 +38,14 @@ public function testCanInjectAContext(): void public function testCanSetANameForAStatus(): void { - $status = (new Status)->withName('redis'); + $status = (new Status())->withName('redis'); $this->assertSame('redis', $status->name()); } public function testWhenCreatingAProblemCanAttachAMessage(): void { - $status = (new Status)->problem('My thing doesnt work'); + $status = (new Status())->problem('My thing doesnt work'); $this->assertSame('My thing doesnt work', $status->message()); } From 3c41169c7c2ac8bc95aa98628a0d8b2e1ac0eae4 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 14:26:59 +0100 Subject: [PATCH 03/13] Change PSR12 checks to include all by default This way, if a new folder is added it is automatically checked --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b60b5a0..a438f40 100644 --- a/composer.json +++ b/composer.json @@ -89,7 +89,7 @@ "@phpcs:fix" ], "test": "phpunit", - "phpcs": "./vendor/bin/phpcs --standard=PSR12 src/ tests/", - "phpcs:fix": "./vendor/bin/phpcbf --standard=PSR12 src/ tests/" + "phpcs": "./vendor/bin/phpcs --standard=PSR12 --ignore=vendor .", + "phpcs:fix": "./vendor/bin/phpcbf --standard=PSR12 --ignore=vendor ." } } From b08a355052def432a2ec85538a37357c4f666e6e Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 15:17:11 +0100 Subject: [PATCH 04/13] Move test stuub classes into their own files This brings us into PSR12 compliance --- tests/AppHealthTest.php | 42 +----- tests/Checks/CacheHealthCheckTest.php | 13 +- tests/Checks/DatabaseHealthCheckTest.php | 65 +--------- tests/Checks/LogHealthCheckTest.php | 120 +----------------- .../Checks/PackageSecurityHealthCheckTest.php | 23 +--- tests/Checks/StorageHealthCheckTest.php | 13 +- .../Controllers/HealthCheckControllerTest.php | 39 +----- tests/Middleware/AddHeadersTest.php | 26 +--- tests/Stubs/Cache/BadStore.php | 16 +++ tests/Stubs/Checks/AlwaysDegradedCheck.php | 18 +++ tests/Stubs/Checks/AlwaysDownCheck.php | 18 +++ tests/Stubs/Checks/AlwaysUpCheck.php | 16 +++ .../Checks/PackageSecurityHealthCheck.php | 21 +++ tests/Stubs/Checks/UnreliableCheck.php | 19 +++ tests/Stubs/Database/BadConnection.php | 21 +++ tests/Stubs/Database/DatabaseManager.php | 36 ++++++ tests/Stubs/Database/HealthyConnection.php | 17 +++ tests/Stubs/Log/BadLogger.php | 82 ++++++++++++ tests/Stubs/Log/NullLogger.php | 46 +++++++ tests/Stubs/Storage/BadDisk.php | 16 +++ 20 files changed, 345 insertions(+), 322 deletions(-) create mode 100644 tests/Stubs/Cache/BadStore.php create mode 100644 tests/Stubs/Checks/AlwaysDegradedCheck.php create mode 100644 tests/Stubs/Checks/AlwaysDownCheck.php create mode 100644 tests/Stubs/Checks/AlwaysUpCheck.php create mode 100644 tests/Stubs/Checks/PackageSecurityHealthCheck.php create mode 100644 tests/Stubs/Checks/UnreliableCheck.php create mode 100644 tests/Stubs/Database/BadConnection.php create mode 100644 tests/Stubs/Database/DatabaseManager.php create mode 100644 tests/Stubs/Database/HealthyConnection.php create mode 100644 tests/Stubs/Log/BadLogger.php create mode 100644 tests/Stubs/Log/NullLogger.php create mode 100644 tests/Stubs/Storage/BadDisk.php diff --git a/tests/AppHealthTest.php b/tests/AppHealthTest.php index 3213453..1e171c9 100644 --- a/tests/AppHealthTest.php +++ b/tests/AppHealthTest.php @@ -2,11 +2,11 @@ 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 { @@ -42,39 +42,3 @@ public function testThrowsExceptionIfCheckDoesNotExist(): void $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'); - } -} diff --git a/tests/Checks/CacheHealthCheckTest.php b/tests/Checks/CacheHealthCheckTest.php index b183df0..cc8bfe9 100644 --- a/tests/Checks/CacheHealthCheckTest.php +++ b/tests/Checks/CacheHealthCheckTest.php @@ -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; @@ -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(); - } -} diff --git a/tests/Checks/DatabaseHealthCheckTest.php b/tests/Checks/DatabaseHealthCheckTest.php index 3c4a98b..733cc88 100644 --- a/tests/Checks/DatabaseHealthCheckTest.php +++ b/tests/Checks/DatabaseHealthCheckTest.php @@ -2,12 +2,10 @@ namespace Tests\Checks; -use Exception; -use Illuminate\Database\Connection; -use Illuminate\Database\ConnectionResolver; -use Illuminate\Database\DatabaseManager as IlluminateDatabaseManager; use Illuminate\Foundation\Application; -use InvalidArgumentException; +use Tests\Stubs\Database\BadConnection; +use Tests\Stubs\Database\DatabaseManager; +use Tests\Stubs\Database\HealthyConnection; use Tests\TestCase; use UKFast\HealthCheck\Checks\DatabaseHealthCheck; use UKFast\HealthCheck\HealthCheckServiceProvider; @@ -67,60 +65,3 @@ public function testShowsWhichConnectionFailed(): void $this->assertSame('bad', $status->context()['connection']); } } - -class HealthyConnection extends Connection -{ - public function __construct() - { - } - - public function getPdo(): bool - { - return true; - } -} - -class BadConnection extends Connection -{ - public function __construct() - { - } - - /** - * @throws Exception - */ - public function getPdo(): never - { - throw new Exception(); - } -} - -class DatabaseManager extends IlluminateDatabaseManager -{ - protected $connections = []; - - public function __construct() - { - } - - /** - * @throws InvalidArgumentException - */ - public function connection($name = null) - { - if (!$name) { - return $this->connection('default'); - } - - if (!isset($this->connections[$name])) { - throw new InvalidArgumentException("Database [$name] not configured."); - } - - return $this->connections[$name]; - } - - public function addConnection($name, $connection) - { - $this->connections[$name] = $connection; - } -} diff --git a/tests/Checks/LogHealthCheckTest.php b/tests/Checks/LogHealthCheckTest.php index 41248fb..816f702 100644 --- a/tests/Checks/LogHealthCheckTest.php +++ b/tests/Checks/LogHealthCheckTest.php @@ -3,11 +3,10 @@ namespace Tests\Checks; use Illuminate\Foundation\Application; -use Psr\Log\LoggerInterface; -use Stringable; +use Tests\Stubs\Log\BadLogger; +use Tests\Stubs\Log\NullLogger; use Tests\TestCase; use UKFast\HealthCheck\Checks\LogHealthCheck; -use Exception; use UKFast\HealthCheck\HealthCheckServiceProvider; class LogHealthCheckTest extends TestCase @@ -41,118 +40,3 @@ public function testShowsOkayIfCanWriteToLogs(): void $this->assertTrue($status->isOkay()); } } - -class BadLogger implements LoggerInterface -{ - /** - * @throws Exception - */ - public function emergency(Stringable | string $message, array $context = []): void - { - throw new Exception('Failed to log'); - } - - /** - * @throws Exception - */ - public function alert(Stringable | string $message, array $context = []): void - { - throw new Exception('Failed to log'); - } - - /** - * @throws Exception - */ - public function critical(Stringable | string $message, array $context = []): void - { - throw new Exception('Failed to log'); - } - - /** - * @throws Exception - */ - public function error(Stringable | string $message, array $context = []): void - { - throw new Exception('Failed to log'); - } - - /** - * @throws Exception - */ - public function warning(Stringable | string $message, array $context = []): void - { - throw new Exception('Failed to log'); - } - - /** - * @throws Exception - */ - public function notice(Stringable | string $message, array $context = []): void - { - throw new Exception('Failed to log'); - } - - /** - * @throws Exception - */ - public function info(Stringable | string $message, array $context = []): void - { - throw new Exception('Failed to log'); - } - - /** - * @throws Exception - */ - public function debug(Stringable | string $message, array $context = []): void - { - throw new Exception('Failed to log'); - } - - /** - * @throws Exception - */ - public function log($level, Stringable | string $message, array $context = []): void - { - throw new Exception('Failed to log'); - } -} - -class NullLogger implements LoggerInterface -{ - public function emergency(Stringable | string $message, array $context = []): void - { - } - - public function alert(Stringable | string $message, array $context = []): void - { - } - - public function critical(Stringable | string $message, array $context = []): void - { - } - - public function error(Stringable | string $message, array $context = []): void - { - } - - public function warning(Stringable | string $message, array $context = []): void - { - } - - public function notice(Stringable | string $message, array $context = []): void - { - // TODO: Implement notice() method. - } - - public function info(Stringable | string $message, array $context = []): void - { - } - - public function debug(Stringable | string $message, array $context = []): void - { - } - - public function log($level, Stringable | string $message, array $context = []): void - { - } -} diff --git a/tests/Checks/PackageSecurityHealthCheckTest.php b/tests/Checks/PackageSecurityHealthCheckTest.php index 9111d35..102933b 100644 --- a/tests/Checks/PackageSecurityHealthCheckTest.php +++ b/tests/Checks/PackageSecurityHealthCheckTest.php @@ -7,6 +7,7 @@ use Illuminate\Foundation\Application; use Mockery; use Mockery\MockInterface; +use Tests\Stubs\Checks\PackageSecurityHealthCheck as StubPackageSecurityHealthCheck; use Tests\TestCase; use UKFast\HealthCheck\Checks\PackageSecurityHealthCheck; use UKFast\HealthCheck\HealthCheckServiceProvider; @@ -36,7 +37,7 @@ protected function partialMock($abstract, Closure $mock = null): MockInterface public function testShowsProblemIfRequiredPackageNotLoaded() { - $status = (new MockedPackageSecurityHealthCheck())->status(); + $status = (new StubPackageSecurityHealthCheck())->status(); $this->assertTrue($status->isProblem()); $this->assertSame('You need to install the enlightn/security-checker package to use this check.', $status->context()['exception']['error']); @@ -44,11 +45,11 @@ public function testShowsProblemIfRequiredPackageNotLoaded() public function testShowsProblemIfIncorrectPackageLoaded(): void { - MockedPackageSecurityHealthCheck::$classResults = [ + StubPackageSecurityHealthCheck::$classResults = [ 'Enlightn\SecurityChecker\SecurityChecker' => false, 'SensioLabs\Security\SecurityChecker' => true, ]; - $status = (new MockedPackageSecurityHealthCheck())->status(); + $status = (new StubPackageSecurityHealthCheck())->status(); $this->assertTrue($status->isProblem()); $this->assertSame('The sensiolabs/security-checker package has been archived. Install enlightn/security-checker instead.', $status->context()['exception']['error']); @@ -103,19 +104,3 @@ public function testShowsOkayIfNoPackagesHaveVulnerabilities(): void $this->assertTrue($status->isOkay()); } } - -class MockedPackageSecurityHealthCheck extends PackageSecurityHealthCheck -{ - /** - * @var array - */ - public static array $classResults = [ - 'Enlightn\SecurityChecker\SecurityChecker' => false, - 'SensioLabs\Security\SecurityChecker' => false, - ]; - - public static function checkDependency(string $class): bool - { - return static::$classResults[$class]; - } -} diff --git a/tests/Checks/StorageHealthCheckTest.php b/tests/Checks/StorageHealthCheckTest.php index ddb1628..dc6a150 100644 --- a/tests/Checks/StorageHealthCheckTest.php +++ b/tests/Checks/StorageHealthCheckTest.php @@ -2,8 +2,8 @@ namespace Tests\Checks; -use Exception; use Illuminate\Foundation\Application; +use Tests\Stubs\Storage\BadDisk; use Tests\TestCase; use Illuminate\Support\Facades\Storage; use UKFast\HealthCheck\Checks\StorageHealthCheck; @@ -66,14 +66,3 @@ public function test_shows_okay_if_can_write_to_storage(): void $this->assertTrue($status->isOkay()); } } - -class BadDisk -{ - /** - * @throws Exception - */ - public function __call($name, $arguments): never - { - throw new Exception(); - } -} diff --git a/tests/Controllers/HealthCheckControllerTest.php b/tests/Controllers/HealthCheckControllerTest.php index 627b3f6..bb1b9bb 100644 --- a/tests/Controllers/HealthCheckControllerTest.php +++ b/tests/Controllers/HealthCheckControllerTest.php @@ -5,11 +5,12 @@ use Illuminate\Foundation\Application; use Illuminate\Http\Response; use Illuminate\Routing\RouteCollection; +use Tests\Stubs\Checks\AlwaysDegradedCheck; +use Tests\Stubs\Checks\AlwaysDownCheck; +use Tests\Stubs\Checks\AlwaysUpCheck; use Tests\TestCase; use UKFast\HealthCheck\Controllers\HealthCheckController; -use UKFast\HealthCheck\HealthCheck; use UKFast\HealthCheck\HealthCheckServiceProvider; -use UKFast\HealthCheck\Status; class HealthCheckControllerTest extends TestCase { @@ -208,37 +209,3 @@ protected function setChecks($checks): void config(['healthcheck.checks' => $checks]); } } - -class AlwaysUpCheck extends HealthCheck -{ - protected string $name = 'always-up'; - - public function status(): Status - { - return $this->okay(); - } -} - -class AlwaysDegradedCheck extends HealthCheck -{ - protected string $name = 'always-degraded'; - - public function status(): Status - { - return $this->degraded('Something went wrong', [ - 'debug' => 'info', - ]); - } -} - -class AlwaysDownCheck extends HealthCheck -{ - protected string $name = 'always-down'; - - public function status(): Status - { - return $this->problem('Something went wrong', [ - 'debug' => 'info', - ]); - } -} diff --git a/tests/Middleware/AddHeadersTest.php b/tests/Middleware/AddHeadersTest.php index 0bc62ca..c5b8225 100644 --- a/tests/Middleware/AddHeadersTest.php +++ b/tests/Middleware/AddHeadersTest.php @@ -3,11 +3,11 @@ namespace Tests\Middleware; use Illuminate\Http\Request; +use Tests\Stubs\Checks\AlwaysDownCheck; +use Tests\Stubs\Checks\AlwaysUpCheck; use Tests\TestCase; use UKFast\HealthCheck\AppHealth; -use UKFast\HealthCheck\HealthCheck; use UKFast\HealthCheck\Middleware\AddHeaders; -use UKFast\HealthCheck\Status; class AddHeadersTest extends TestCase { @@ -29,25 +29,3 @@ public function testAddsHeadersForEachCheck(): void $this->assertSame('0', $response->headers->get('X-always-down-status')); } } - -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', - ]); - } -} diff --git a/tests/Stubs/Cache/BadStore.php b/tests/Stubs/Cache/BadStore.php new file mode 100644 index 0000000..f1ed8aa --- /dev/null +++ b/tests/Stubs/Cache/BadStore.php @@ -0,0 +1,16 @@ +degraded('Something went wrong', [ + 'debug' => 'info', + ]); + } +} diff --git a/tests/Stubs/Checks/AlwaysDownCheck.php b/tests/Stubs/Checks/AlwaysDownCheck.php new file mode 100644 index 0000000..e0f4c25 --- /dev/null +++ b/tests/Stubs/Checks/AlwaysDownCheck.php @@ -0,0 +1,18 @@ +problem('Something went wrong', [ + 'debug' => 'info', + ]); + } +} diff --git a/tests/Stubs/Checks/AlwaysUpCheck.php b/tests/Stubs/Checks/AlwaysUpCheck.php new file mode 100644 index 0000000..86cbcc4 --- /dev/null +++ b/tests/Stubs/Checks/AlwaysUpCheck.php @@ -0,0 +1,16 @@ +okay(); + } +} diff --git a/tests/Stubs/Checks/PackageSecurityHealthCheck.php b/tests/Stubs/Checks/PackageSecurityHealthCheck.php new file mode 100644 index 0000000..193c41b --- /dev/null +++ b/tests/Stubs/Checks/PackageSecurityHealthCheck.php @@ -0,0 +1,21 @@ + + */ + public static array $classResults = [ + 'Enlightn\SecurityChecker\SecurityChecker' => false, + 'SensioLabs\Security\SecurityChecker' => false, + ]; + + public static function checkDependency(string $class): bool + { + return static::$classResults[$class]; + } +} diff --git a/tests/Stubs/Checks/UnreliableCheck.php b/tests/Stubs/Checks/UnreliableCheck.php new file mode 100644 index 0000000..097640b --- /dev/null +++ b/tests/Stubs/Checks/UnreliableCheck.php @@ -0,0 +1,19 @@ +connection('default'); + } + + if (!isset($this->connections[$name])) { + throw new InvalidArgumentException("Database [$name] not configured."); + } + + return $this->connections[$name]; + } + + public function addConnection($name, $connection) + { + $this->connections[$name] = $connection; + } +} diff --git a/tests/Stubs/Database/HealthyConnection.php b/tests/Stubs/Database/HealthyConnection.php new file mode 100644 index 0000000..1c76a1a --- /dev/null +++ b/tests/Stubs/Database/HealthyConnection.php @@ -0,0 +1,17 @@ + Date: Tue, 23 Jul 2024 15:18:37 +0100 Subject: [PATCH 05/13] Add a PSR 12 exception Unfortunately, this is deep in vendor dependencies so we cannot change this for PSR12 compliance --- tests/Stubs/Redis/Connections/PhpRedisClusterConnection.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Stubs/Redis/Connections/PhpRedisClusterConnection.php b/tests/Stubs/Redis/Connections/PhpRedisClusterConnection.php index accf2d6..1f01de0 100644 --- a/tests/Stubs/Redis/Connections/PhpRedisClusterConnection.php +++ b/tests/Stubs/Redis/Connections/PhpRedisClusterConnection.php @@ -16,10 +16,13 @@ public function ping(string|array $message = null): string|bool } /** + * This cannot be changed for PSR 12 compliance as it is stubbing a dependency * @return array */ + // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore public function _masters(): array { return []; } + // phpcs:enable PSR2.Methods.MethodDeclaration.Underscore } From 23cc5674fe3cf6ce5a9930c45e820bb689b25b49 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 15:21:12 +0100 Subject: [PATCH 06/13] Specify visibility for PSR12 This makes visibility more obvious --- src/Status.php | 6 +++--- tests/Checks/EnvHealthCheckTest.php | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Status.php b/src/Status.php index 978822f..ba3e269 100644 --- a/src/Status.php +++ b/src/Status.php @@ -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; diff --git a/tests/Checks/EnvHealthCheckTest.php b/tests/Checks/EnvHealthCheckTest.php index 9e6e504..a44e113 100644 --- a/tests/Checks/EnvHealthCheckTest.php +++ b/tests/Checks/EnvHealthCheckTest.php @@ -31,7 +31,8 @@ public function testShowsProblemIfMissingADotenvFile(): void $this->assertTrue($status->isProblem()); } - function testShowsOkayIfAllRequiredEnvParamsArePresent(): void + + public function testShowsOkayIfAllRequiredEnvParamsArePresent(): void { putenv('REDIS_HOST=here'); putenv('MYSQL_HOST=here'); From 478e22aceb8423b2a71e67cf6bf26d5ddb21b564 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 15:22:51 +0100 Subject: [PATCH 07/13] Change snake case methods to camel case THis is now inline with PSR12 --- tests/Checks/StorageHealthCheckTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Checks/StorageHealthCheckTest.php b/tests/Checks/StorageHealthCheckTest.php index dc6a150..7562cd7 100644 --- a/tests/Checks/StorageHealthCheckTest.php +++ b/tests/Checks/StorageHealthCheckTest.php @@ -53,7 +53,7 @@ public function testShowsProblemIfIncorrectReadFromStorage(): void $this->assertTrue($status->isProblem()); } - public function test_shows_okay_if_can_write_to_storage(): void + public function testShowsOkayIfCanWriteToStorage(): void { config([ 'healthcheck.storage.disks' => [ From 425beeebabfdc9fdd252cce60cdcdc946f2f623a Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 15:23:15 +0100 Subject: [PATCH 08/13] Remove an errant trailing semi colon This isn't needed and breaches PSR 12 --- tests/Checks/FtpHealthCheckTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Checks/FtpHealthCheckTest.php b/tests/Checks/FtpHealthCheckTest.php index 4035381..3aad6db 100644 --- a/tests/Checks/FtpHealthCheckTest.php +++ b/tests/Checks/FtpHealthCheckTest.php @@ -31,7 +31,7 @@ function generator(): iterable yield 'foo'; yield 'bar'; yield 'baz'; - }; + } $ftp = Mockery::mock(FtpAdapter::class) ->expects('listContents') From 1108eeb6bdadb9cde0bbcbae8dc5bef9e96d9dc3 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 15:27:22 +0100 Subject: [PATCH 09/13] Reduce line lengths This is now PSR12 compliant --- src/Checks/PackageSecurityHealthCheck.php | 5 ++++- src/HealthCheckServiceProvider.php | 14 +++++++------ tests/Checks/HttpHealthCheckTest.php | 7 +++++-- .../Checks/PackageSecurityHealthCheckTest.php | 20 +++++++++++++++---- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Checks/PackageSecurityHealthCheck.php b/src/Checks/PackageSecurityHealthCheck.php index a695e8c..979faf9 100644 --- a/src/Checks/PackageSecurityHealthCheck.php +++ b/src/Checks/PackageSecurityHealthCheck.php @@ -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.'); } diff --git a/src/HealthCheckServiceProvider.php b/src/HealthCheckServiceProvider.php index 6e47d9b..91d0320 100644 --- a/src/HealthCheckServiceProvider.php +++ b/src/HealthCheckServiceProvider.php @@ -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(); @@ -38,7 +39,8 @@ 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 diff --git a/tests/Checks/HttpHealthCheckTest.php b/tests/Checks/HttpHealthCheckTest.php index 10cc35b..d0656a3 100644 --- a/tests/Checks/HttpHealthCheckTest.php +++ b/tests/Checks/HttpHealthCheckTest.php @@ -3,7 +3,10 @@ namespace Tests\Checks; use GuzzleHttp\Client; +use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Exception\TooManyRedirectsException; use GuzzleHttp\Handler\MockHandler; +use GuzzleHttp\Psr7\Request; use Illuminate\Foundation\Application; use Tests\TestCase; use UKFast\HealthCheck\Checks\HttpHealthCheck; @@ -80,7 +83,7 @@ public function testShowsProblemOnConnectException(): void $this->app->bind(Client::class, function ($app, $args) { $exceptions = [ - (new \GuzzleHttp\Exception\ConnectException('Connection refused', new \GuzzleHttp\Psr7\Request('GET', 'test'))), + (new ConnectException('Connection refused', new Request('GET', 'test'))), ]; $mockHandler = new MockHandler($exceptions); @@ -104,7 +107,7 @@ public function testShowsProblemOnGeneralException(): void $this->app->bind(Client::class, function ($app, $args) { $exceptions = [ - (new \GuzzleHttp\Exception\TooManyRedirectsException('Will not follow more than 5 redirects', new \GuzzleHttp\Psr7\Request('GET', 'test'))), + (new TooManyRedirectsException('Will not follow more than 5 redirects', new Request('GET', 'test'))), ]; $mockHandler = new MockHandler($exceptions); diff --git a/tests/Checks/PackageSecurityHealthCheckTest.php b/tests/Checks/PackageSecurityHealthCheckTest.php index 102933b..4f8e2ed 100644 --- a/tests/Checks/PackageSecurityHealthCheckTest.php +++ b/tests/Checks/PackageSecurityHealthCheckTest.php @@ -40,7 +40,10 @@ public function testShowsProblemIfRequiredPackageNotLoaded() $status = (new StubPackageSecurityHealthCheck())->status(); $this->assertTrue($status->isProblem()); - $this->assertSame('You need to install the enlightn/security-checker package to use this check.', $status->context()['exception']['error']); + $this->assertSame( + 'You need to install the enlightn/security-checker package to use this check.', + $status->context()['exception']['error'] + ); } public function testShowsProblemIfIncorrectPackageLoaded(): void @@ -52,7 +55,10 @@ public function testShowsProblemIfIncorrectPackageLoaded(): void $status = (new StubPackageSecurityHealthCheck())->status(); $this->assertTrue($status->isProblem()); - $this->assertSame('The sensiolabs/security-checker package has been archived. Install enlightn/security-checker instead.', $status->context()['exception']['error']); + $this->assertSame( + 'The sensiolabs/security-checker package has been archived. Install enlightn/security-checker instead.', + $status->context()['exception']['error'] + ); } public function testShowsProblemIfCannotCheckPackages(): void @@ -69,7 +75,10 @@ public function testShowsProblemIfPackageHasVulnerability(): void { $this->partialMock('overload:Enlightn\SecurityChecker\SecurityChecker', fn (MockInterface $mock) => $mock->shouldReceive('check') - ->andReturn(json_decode(file_get_contents('tests/json/securityCheckerPackageHasVulnerability.json'), true))); + ->andReturn(json_decode( + file_get_contents('tests/json/securityCheckerPackageHasVulnerability.json'), + true + ))); $status = (new PackageSecurityHealthCheck())->status(); @@ -86,7 +95,10 @@ public function testIgnoresPackageIfInConfig(): void $this->partialMock('overload:Enlightn\SecurityChecker\SecurityChecker', fn (MockInterface $mock) => $mock->shouldReceive('check') - ->andReturn(json_decode(file_get_contents('tests/json/securityCheckerPackageHasVulnerability.json'), true))); + ->andReturn(json_decode( + file_get_contents('tests/json/securityCheckerPackageHasVulnerability.json'), + true + ))); $status = (new PackageSecurityHealthCheck())->status(); From 21e3067afe0f829db5c63c6bacbf8b82f230deb8 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 15:28:28 +0100 Subject: [PATCH 10/13] Rename the workflow to checks THis will align with multiple jobs --- .github/workflows/{tests.yml => checks.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{tests.yml => checks.yml} (97%) diff --git a/.github/workflows/tests.yml b/.github/workflows/checks.yml similarity index 97% rename from .github/workflows/tests.yml rename to .github/workflows/checks.yml index bfcb8d5..9e4df27 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/checks.yml @@ -1,4 +1,4 @@ -name: Pull Request +name: Checks on: push: From da0b228a86cdb3148221035ac4d65d7ae374fac2 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 15:31:42 +0100 Subject: [PATCH 11/13] Add PHPCodesniffer to CI This will enforce PSR12 now --- .github/workflows/checks.yml | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 9e4df27..3e2eb87 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -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 @@ -39,3 +39,20 @@ jobs: - name: Execute tests run: vendor/bin/phpunit + + 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 From 549624cc7907d1af41ca647cab7e8e57747641de Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 15:41:00 +0100 Subject: [PATCH 12/13] Update the contributing guide This shows the coding standards and tools now in use in CI --- CONTRIBUTING.md | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0172869..48a3efe 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,14 +1,12 @@ -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. @@ -16,9 +14,7 @@ If you'd like to discuss a potential change, you can open an issue or reach out If you think you have identified a security vulnerability, please contact our team via **security@ukfast.co.uk** 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. @@ -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`. From bef33df16207cc5a985cb7294804fd788059734f Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 23 Jul 2024 15:42:28 +0100 Subject: [PATCH 13/13] Move to a single sorce of truth for running tests Should we update the test runner, we only have to update it in one place --- .github/workflows/checks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 3e2eb87..8d290d6 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -38,7 +38,7 @@ jobs: laravel_version: ${{ matrix.environment.laravel_version }} - name: Execute tests - run: vendor/bin/phpunit + run: composer test php-codesniffer: runs-on: ubuntu-latest