Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add health check make command #48

Merged
merged 6 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"php": ">=5.6",
"ext-json": "*",
"illuminate/support": "^5.4|^6.0|^7.0|^8.0",
"illuminate/http": "^5.4|^6.0|^7.0|^8.0"
"illuminate/http": "^5.4|^6.0|^7.0|^8.0",
"illuminate/console": "^5.4|^6.0|^7.0|^8.0"
},
"require-dev": {
"phpunit/phpunit": ">=5.0",
Expand Down
50 changes: 50 additions & 0 deletions src/Commands/HealthCheckMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace UKFast\HealthCheck\Commands;

use Illuminate\Console\GeneratorCommand;

class HealthCheckMakeCommand extends GeneratorCommand
{
/**
* The health check name.
*
* @var string
*/
protected $signature = 'make:check {name : The name of the health check class}';

/**
* The check command description.
*
* @var string
*/
protected $description = 'Create a new health check class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Check';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__ . '/Stubs/ExampleHealthCheck.php.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Checks';
}
}
15 changes: 15 additions & 0 deletions src/Commands/Stubs/ExampleHealthCheck.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace DummyNamespace;

use UKFast\HealthCheck\HealthCheck;

class DummyClass extends HealthCheck
{
protected $name = 'example';

public function status()
{
return $this->okay();
}
}
2 changes: 2 additions & 0 deletions src/HealthCheckServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\ServiceProvider;
use UKFast\HealthCheck\Commands\CacheSchedulerRunning;
use UKFast\HealthCheck\Commands\HealthCheckMakeCommand;
use UKFast\HealthCheck\Controllers\HealthCheckController;
use UKFast\HealthCheck\Controllers\PingController;

Expand Down Expand Up @@ -31,6 +32,7 @@ public function boot()
if ($this->app->runningInConsole()) {
$this->commands([
CacheSchedulerRunning::class,
HealthCheckMakeCommand::class,
]);
}

Expand Down
55 changes: 55 additions & 0 deletions tests/Commands/HealthCheckMakeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Tests\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Facades\File;
use UKFast\HealthCheck\HealthCheckServiceProvider;
use Tests\TestCase;

class HealthCheckMakeCommandTest extends TestCase
{
public function getPackageProviders($app)
{
return [HealthCheckServiceProvider::class];
}

/**
* @test
*/
public function creates_a_new_check()
{
$checkName = "TestCheck";
$checkClassFile = $this->app->basePath("app/Checks/{$checkName}.php");

$this->assertFalse(File::exists($checkClassFile));

$this->artisan("make:check", ["name" => $checkName]);

$this->assertTrue(File::exists($checkClassFile));

// Checking right file is created.
$this->assertTrue(is_int(strpos(File::get($checkClassFile), "class {$checkName}")));

// Cleaning the file.
unlink($checkClassFile);
}

/**
* @test
*/
public function php_reserved_name_check_does_not_get_created()
{
if (!property_exists(GeneratorCommand::class, 'reservedNames')) {
$this->markTestSkipped('GeneratorCommand does not support reservedNames.');
}

$checkName = "array";
$checkClassFile = $this->app->basePath("app/Checks/{$checkName}.php");
$this->assertFalse(File::exists($checkClassFile));

$this->artisan("make:check", ["name" => $checkName]);

$this->assertFalse(File::exists($checkClassFile));
}
}
12 changes: 10 additions & 2 deletions tests/HealthCheckServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Artisan;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use UKFast\HealthCheck\Checks\LogHealthCheck;
use UKFast\HealthCheck\Commands\UpdateSchedulerTimestamp;
use UKFast\HealthCheck\HealthCheckServiceProvider;
use URL;

Expand Down Expand Up @@ -58,6 +56,16 @@ public function registers_scheduler_health_check_command()
$this->assertArrayHasKey('health-check:cache-scheduler-running', Artisan::all());
}

/**
* @test
*/
public function registers_health_check_make_command()
{
$this->app->register(HealthCheckServiceProvider::class);

$this->assertArrayHasKey('make:check', Artisan::all());
}

/**
* @test
*/
Expand Down