Skip to content

Commit

Permalink
Ini/Yaml/Json File Checks
Browse files Browse the repository at this point in the history
  • Loading branch information
svilborg committed Jul 17, 2019
1 parent 58252e7 commit 1ac497f
Show file tree
Hide file tree
Showing 16 changed files with 323 additions and 13 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
},
"require-dev": {
"guzzlehttp/guzzle": "^6.3",
"symfony/yaml" : "~3.4|~4.0",
"phpunit/phpunit": "^7.0",
"orchestra/testbench": "~3.0",
"phan/phan": "^2.2",
"mockery/mockery": "^1.2"
},
"suggest": {
"guzzlehttp/guzzle": "^6.3"
"guzzlehttp/guzzle": "^6.3",
"symfony/yaml" : "~3.4|~4.0"
},
"autoload": {
"psr-4": {
Expand Down
81 changes: 70 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions src/Checks/File/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Health\Checks\File;

use Health\Checks\BaseCheck;

abstract class Base extends BaseCheck
{

/**
* List of files
*
* @var array
*/
protected $files = [];

/**
*
* @param array $params
*/
public function __construct(array $params = [])
{
parent::__construct($params);

$this->files = $this->getParam('files', []);
}

/**
*
* {@inheritdoc}
* @see \Health\Checks\HealthCheckInterface::call()
*/
public function call()
{
$builder = $this->getBuilder();

$errors = [];

foreach ($this->files as $file) {
if (! is_file($file) || ! $this->isValid($file)) {
$errors[] = $file;
}
}

if ($errors) {
$builder->down()->withData('errors', $errors);
}

return $builder->withData('files', $this->files)->build();
}

/**
*
* @param string $file
* @return boolean
*/
abstract protected function isValid($file);
}
18 changes: 18 additions & 0 deletions src/Checks/File/Ini.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Health\Checks\File;

use Health\Checks\HealthCheckInterface;

class Ini extends Base implements HealthCheckInterface
{

/**
*
* @param string $file
* @return boolean
*/
protected function isValid($file)
{
return @parse_ini_file($file) !== false;
}
}
18 changes: 18 additions & 0 deletions src/Checks/File/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Health\Checks\File;

use Health\Checks\HealthCheckInterface;

class Json extends Base implements HealthCheckInterface
{

/**
*
* @param string $file
* @return boolean
*/
protected function isValid($file)
{
return ! is_null(json_decode(@file_get_contents($file)));
}
}
34 changes: 34 additions & 0 deletions src/Checks/File/Yaml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Health\Checks\File;

use Health\Checks\HealthCheckInterface;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;

class Yaml extends Base implements HealthCheckInterface
{

/**
*
* @param string $file
* @return boolean
*/
protected function isValid($file)
{
if (function_exists('yaml_parse_file')) {
return (@yaml_parse_file($file) === false) ? false : true;
}

if (class_exists('Symfony\Component\Yaml\Parser')) {
try {
(new Parser())->parse(file_get_contents($file));
} catch (ParseException $e) {
return false;
}

return true;
}

return false;
}
}
1 change: 0 additions & 1 deletion tests/Checks/CheckTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class CheckTestCase extends \Orchestra\Testbench\TestCase
protected function assertCheck($health, string $state = 'UP')
{
// dump($health);

$this->assertInstanceOf(HealthCheck::class, $health);
$this->assertNotEmpty($health->getName());
$this->assertEquals($state, $health->getState());
Expand Down
31 changes: 31 additions & 0 deletions tests/Checks/FileIniTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Tests\Checks;

use Health\Checks\File\Ini;

class FileIniTest extends CheckTestCase
{

public function testCheckFile()
{
$this->assertCheck($this->runCheck(Ini::class, []), 'UP');

$this->assertCheck($this->runCheck(Ini::class, [
'files' => [
'./tests/files/valid.ini'
]
]), 'UP');

$this->assertCheck($this->runCheck(Ini::class, [
'files' => [
'./tests/files/invalid.ini'
]
]), 'DOWN');

$this->assertCheck($this->runCheck(Ini::class, [
'files' => [
'./tests/files/notfound.ini'
]
]), 'DOWN');
}
}
31 changes: 31 additions & 0 deletions tests/Checks/FileJsonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Tests\Checks;

use Health\Checks\File\Json;

class FileJsonTest extends CheckTestCase
{

public function testCheckFile()
{
$this->assertCheck($this->runCheck(Json::class, []), 'UP');

$this->assertCheck($this->runCheck(Json::class, [
'files' => [
'./tests/files/valid.json'
]
]), 'UP');

$this->assertCheck($this->runCheck(Json::class, [
'files' => [
'./tests/files/invalid.json.txt'
]
]), 'DOWN');

$this->assertCheck($this->runCheck(Json::class, [
'files' => [
'./tests/files/notfound.json'
]
]), 'DOWN');
}
}
31 changes: 31 additions & 0 deletions tests/Checks/FileYamlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Tests\Checks;

use Health\Checks\File\Yaml;

class FileYamlTest extends CheckTestCase
{

public function testCheckFile()
{
$this->assertCheck($this->runCheck(Yaml::class, []), 'UP');

$this->assertCheck($this->runCheck(Yaml::class, [
'files' => [
'./tests/files/valid.yaml'
]
]), 'UP');

$this->assertCheck($this->runCheck(Yaml::class, [
'files' => [
'./tests/files/invalid.yaml.txt'
]
]), 'DOWN');

$this->assertCheck($this->runCheck(Yaml::class, [
'files' => [
'./tests/files/notfound.yaml'
]
]), 'DOWN');
}
}
3 changes: 3 additions & 0 deletions tests/files/invalid.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; comment text

#@!#@!#
Loading

0 comments on commit 1ac497f

Please sign in to comment.