Skip to content

Commit

Permalink
Add types to tests
Browse files Browse the repository at this point in the history
This helps to make the cvode stricter and let the IDE know what is going
on
  • Loading branch information
phily245 committed Jul 19, 2024
1 parent d559a1e commit 250188b
Show file tree
Hide file tree
Showing 23 changed files with 234 additions and 150 deletions.
21 changes: 13 additions & 8 deletions tests/AppHealthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,38 @@

namespace Tests;

use RuntimeException;
use UKFast\HealthCheck\AppHealth;
use UKFast\HealthCheck\Exceptions\CheckNotFoundException;
use UKFast\HealthCheck\HealthCheck;
use UKFast\HealthCheck\Status;

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

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

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

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

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

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

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

Expand All @@ -45,7 +47,7 @@ class AlwaysUpCheck extends HealthCheck
{
protected $name = 'always-up';

public function status()
public function status(): Status
{
return $this->okay();
}
Expand All @@ -55,7 +57,7 @@ class AlwaysDownCheck extends HealthCheck
{
protected $name = 'always-down';

public function status()
public function status(): Status
{
return $this->problem('Something went wrong', [
'debug' => 'info',
Expand All @@ -68,8 +70,11 @@ class UnreliableCheck extends HealthCheck
{
protected $name = 'unreliable';

public function status()
/**
* @throws RuntimeException
*/
public function status(): never
{
throw new \RuntimeException('Something went badly wrong');
throw new RuntimeException('Something went badly wrong');
}
}
21 changes: 15 additions & 6 deletions tests/Checks/CacheHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
namespace Tests\Checks;

use Exception;
use Illuminate\Foundation\Application;
use Tests\TestCase;
use Illuminate\Support\Facades\Cache;
use UKFast\HealthCheck\Checks\CacheHealthCheck;
use UKFast\HealthCheck\HealthCheckServiceProvider;

class CacheHealthCheckTest extends TestCase
{
public function getPackageProviders($app)
/**
* @param Application $app
* @return array<int, class-string>
*/
public function getPackageProviders($app): array
{
return ['UKFast\HealthCheck\HealthCheckServiceProvider'];
return [HealthCheckServiceProvider::class];
}

public function testShowsProblemIfCannotWriteToCache()
public function testShowsProblemIfCannotWriteToCache(): void
{
config([
'healthcheck.cache.stores' => [
Expand All @@ -29,7 +35,7 @@ public function testShowsProblemIfCannotWriteToCache()
$this->assertTrue($status->isProblem());
}

public function testShowsProblemIfIncorrectReadFromCache()
public function testShowsProblemIfIncorrectReadFromCache(): void
{
config([
'healthcheck.cache.stores' => [
Expand All @@ -46,7 +52,7 @@ public function testShowsProblemIfIncorrectReadFromCache()
$this->assertTrue($status->isProblem());
}

public function showsOkayIfCanWriteToCache()
public function testShowsOkayIfCanWriteToCache(): void
{
config([
'healthcheck.cache.stores' => [
Expand All @@ -62,7 +68,10 @@ public function showsOkayIfCanWriteToCache()

class BadStore
{
public function __call($name, $arguments)
/**
* @throws Exception
*/
public function __call($name, $arguments): never
{
throw new Exception();
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Checks/CrossServiceHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class CrossServiceHealthCheckTest extends TestCase
{
public function testReturnsOkayIfAllRelatedServicesAreUp()
public function testReturnsOkayIfAllRelatedServicesAreUp(): void
{
config(['healthcheck.x-service-checks' => ['http://api.example.com/health']]);

Expand All @@ -30,7 +30,7 @@ public function testReturnsOkayIfAllRelatedServicesAreUp()
$this->assertTrue(isset($container[0]['request']->getHeaders()['X-Service-Check']));
}

public function testReturnsProblemIfAtLeastOneServiceIsDown()
public function testReturnsProblemIfAtLeastOneServiceIsDown(): void
{
config(['healthcheck.x-service-checks' => ['http://api.example.com/health']]);

Expand All @@ -46,7 +46,7 @@ public function testReturnsProblemIfAtLeastOneServiceIsDown()
$this->assertTrue(isset($container[0]['request']->getHeaders()['X-Service-Check']));
}

public function testSkipsCheckIfXServiceCheckHeaderIsPresent()
public function testSkipsCheckIfXServiceCheckHeaderIsPresent(): void
{
config(['healthcheck.x-service-checks' => ['http://api.example.com/health']]);

Expand All @@ -62,7 +62,7 @@ public function testSkipsCheckIfXServiceCheckHeaderIsPresent()
$this->assertSame(0, count($container));
}

private function mockClient($responses, &$container)
private function mockClient($responses, &$container): Client
{
$container = [];
$history = Middleware::history($container);
Expand Down
32 changes: 23 additions & 9 deletions tests/Checks/DatabaseHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
use Exception;
use Illuminate\Database\Connection;
use Illuminate\Database\ConnectionResolver;
use Illuminate\Database\DatabaseManager as IlluminateDatabaseManager;
use Illuminate\Foundation\Application;
use InvalidArgumentException;
use Tests\TestCase;
use UKFast\HealthCheck\Checks\DatabaseHealthCheck;
use UKFast\HealthCheck\HealthCheckServiceProvider;

class DatabaseHealthCheckTest extends TestCase
{
public function getPackageProviders($app)
/**
* @param Application $app
* @return array<int, class-string>
*/
public function getPackageProviders($app): array
{
return ['UKFast\HealthCheck\HealthCheckServiceProvider'];
return [HealthCheckServiceProvider::class];
}

public function testShowsProblemWhenCantConnectToDb()
public function testShowsProblemWhenCantConnectToDb(): void
{
config([
'healthcheck.database.connections' => ['default'],
Expand All @@ -29,7 +37,7 @@ public function testShowsProblemWhenCantConnectToDb()
$this->assertTrue($status->isProblem());
}

public function testShowsOkayWhenCanConnectToDb()
public function testShowsOkayWhenCanConnectToDb(): void
{
config([
'healthcheck.database.connections' => ['default'],
Expand All @@ -43,7 +51,7 @@ public function testShowsOkayWhenCanConnectToDb()
$this->assertTrue($status->isOkay());
}

public function testShowsWhichConnectionFailed()
public function testShowsWhichConnectionFailed(): void
{
config([
'healthcheck.database.connections' => ['healthy', 'bad'],
Expand All @@ -66,7 +74,7 @@ public function __construct()
{
}

public function getPdo()
public function getPdo(): bool
{
return true;
}
Expand All @@ -78,28 +86,34 @@ public function __construct()
{
}

public function getPdo()
/**
* @throws Exception
*/
public function getPdo(): never
{
throw new Exception;
}
}

class DatabaseManager extends \Illuminate\Database\DatabaseManager
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.");
throw new InvalidArgumentException("Database [$name] not configured.");
}

return $this->connections[$name];
Expand Down
16 changes: 11 additions & 5 deletions tests/Checks/EnvHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

namespace Tests\Checks;

use Illuminate\Foundation\Application;
use Tests\TestCase;
use UKFast\HealthCheck\Checks\EnvHealthCheck;
use UKFast\HealthCheck\HealthCheckServiceProvider;

class EnvHealthCheckTest extends TestCase
{
public function getPackageProviders($app)
/**
* @param Application $app
* @return array<int, class-string>
*/
public function getPackageProviders($app): array
{
return ['UKFast\HealthCheck\HealthCheckServiceProvider'];
return [HealthCheckServiceProvider::class];
}

public function testShowsProblemIfMissingADotenvFile()
public function testShowsProblemIfMissingADotenvFile(): void
{
putenv('REDIS_HOST=here');
putenv('MYSQL_HOST=here');
Expand All @@ -25,7 +31,7 @@ public function testShowsProblemIfMissingADotenvFile()

$this->assertTrue($status->isProblem());
}
function testShowsOkayIfAllRequiredEnvParamsArePresent()
function testShowsOkayIfAllRequiredEnvParamsArePresent(): void
{
putenv('REDIS_HOST=here');
putenv('MYSQL_HOST=here');
Expand All @@ -40,7 +46,7 @@ function testShowsOkayIfAllRequiredEnvParamsArePresent()
$this->assertTrue($status->isOkay());
}

public function testShowsOkayIfRequiredEnvParamIsPresentButNull()
public function testShowsOkayIfRequiredEnvParamIsPresentButNull(): void
{
putenv('REDIS_PASSWORD=null');

Expand Down
4 changes: 2 additions & 2 deletions tests/Checks/FtpHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class FtpHealthCheckTest extends TestCase
{
public function testShowsProblemWhenCantConnectToFtpServer()
public function testShowsProblemWhenCantConnectToFtpServer(): void
{
$ftp = m::mock(Ftp::class)
->expects('getConnection')
Expand All @@ -24,7 +24,7 @@ public function testShowsProblemWhenCantConnectToFtpServer()
m::close();
}

public function testShowsOkayWhenCanConnectToFtpServer()
public function testShowsOkayWhenCanConnectToFtpServer(): void
{
$ftp = m::mock(Ftp::class)
->expects('getConnection')
Expand Down
20 changes: 13 additions & 7 deletions tests/Checks/HttpHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\Foundation\Application;
use Tests\TestCase;
use UKFast\HealthCheck\Checks\HttpHealthCheck;
use UKFast\HealthCheck\HealthCheckServiceProvider;

class HttpHealthCheckTest extends TestCase
{
public function getPackageProviders($app)
/**
* @param Application $app
* @return array<int, class-string>
*/
public function getPackageProviders($app): array
{
return ['UKFast\HealthCheck\HealthCheckServiceProvider'];
return [HealthCheckServiceProvider::class];
}

public function testShowsProblemIfResponseCodeIsIncorrect()
public function testShowsProblemIfResponseCodeIsIncorrect(): void
{
config([
'healthcheck.addresses' => [
Expand All @@ -38,7 +44,7 @@ public function testShowsProblemIfResponseCodeIsIncorrect()
$this->assertTrue($status->isProblem());
}

public function testShowsProblemIfConnectionIsUnreachable()
public function testShowsProblemIfConnectionIsUnreachable(): void
{
config([
'healthcheck.addresses' => [
Expand All @@ -62,7 +68,7 @@ public function testShowsProblemIfConnectionIsUnreachable()
$this->assertTrue($status->isProblem());
}

public function testShowsProblemOnConnectException()
public function testShowsProblemOnConnectException(): void
{
config([
'healthcheck.addresses' => [
Expand All @@ -86,7 +92,7 @@ public function testShowsProblemOnConnectException()
$this->assertTrue($status->isProblem());
}

public function testShowsProblemOnGeneralException()
public function testShowsProblemOnGeneralException(): void
{
config([
'healthcheck.addresses' => [
Expand All @@ -110,7 +116,7 @@ public function testShowsProblemOnGeneralException()
$this->assertTrue($status->isProblem());
}

public function testShowsOkayIfAllConnectionsAreReachable()
public function testShowsOkayIfAllConnectionsAreReachable(): void
{
config([
'healthcheck.addresses' => [
Expand Down
Loading

0 comments on commit 250188b

Please sign in to comment.