diff --git a/composer.json b/composer.json index 21d532a..a2508ce 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/composer.lock b/composer.lock index e1be23f..16bfaf7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8316d12673c9a5bd24343f05c8b95c9c", + "content-hash": "77facb02ffb60de0ffe3fa381c23e58d", "packages": [ { "name": "doctrine/inflector", @@ -292,16 +292,16 @@ }, { "name": "laravel/framework", - "version": "v5.8.28", + "version": "v5.8.29", "source": { "type": "git", "url": "https://github.com/laravel/framework.git", - "reference": "341fb54bea9693cada2a5b8d398621a57f82862d" + "reference": "489ae2218c7eb138caac780de584d8df9fe8160b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/framework/zipball/341fb54bea9693cada2a5b8d398621a57f82862d", - "reference": "341fb54bea9693cada2a5b8d398621a57f82862d", + "url": "https://api.github.com/repos/laravel/framework/zipball/489ae2218c7eb138caac780de584d8df9fe8160b", + "reference": "489ae2218c7eb138caac780de584d8df9fe8160b", "shasum": "" }, "require": { @@ -435,7 +435,7 @@ "framework", "laravel" ], - "time": "2019-07-09T14:00:02+00:00" + "time": "2019-07-16T14:05:28+00:00" }, { "name": "league/flysystem", @@ -601,16 +601,16 @@ }, { "name": "nesbot/carbon", - "version": "2.21.0", + "version": "2.21.2", "source": { "type": "git", "url": "https://github.com/briannesbitt/Carbon.git", - "reference": "da6410b77f74eecd98d24ffb6dfecefccf3ca17f" + "reference": "a8bfb6631e3bc982427406d13ad817fc45d47b61" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/da6410b77f74eecd98d24ffb6dfecefccf3ca17f", - "reference": "da6410b77f74eecd98d24ffb6dfecefccf3ca17f", + "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/a8bfb6631e3bc982427406d13ad817fc45d47b61", + "reference": "a8bfb6631e3bc982427406d13ad817fc45d47b61", "shasum": "" }, "require": { @@ -664,7 +664,7 @@ "datetime", "time" ], - "time": "2019-07-14T17:06:46+00:00" + "time": "2019-07-17T07:49:05+00:00" }, { "name": "opis/closure", @@ -4758,6 +4758,65 @@ "homepage": "https://github.com/sebastianbergmann/version", "time": "2016-10-03T07:35:21+00:00" }, + { + "name": "symfony/yaml", + "version": "v4.3.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "c60ecf5ba842324433b46f58dc7afc4487dbab99" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/c60ecf5ba842324433b46f58dc7afc4487dbab99", + "reference": "c60ecf5ba842324433b46f58dc7afc4487dbab99", + "shasum": "" + }, + "require": { + "php": "^7.1.3", + "symfony/polyfill-ctype": "~1.8" + }, + "conflict": { + "symfony/console": "<3.4" + }, + "require-dev": { + "symfony/console": "~3.4|~4.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2019-04-06T14:04:46+00:00" + }, { "name": "theseer/tokenizer", "version": "1.1.3", diff --git a/src/Checks/File/Base.php b/src/Checks/File/Base.php new file mode 100644 index 0000000..b593b3d --- /dev/null +++ b/src/Checks/File/Base.php @@ -0,0 +1,57 @@ +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); +} \ No newline at end of file diff --git a/src/Checks/File/Ini.php b/src/Checks/File/Ini.php new file mode 100644 index 0000000..caf2624 --- /dev/null +++ b/src/Checks/File/Ini.php @@ -0,0 +1,18 @@ +parse(file_get_contents($file)); + } catch (ParseException $e) { + return false; + } + + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/tests/Checks/CheckTestCase.php b/tests/Checks/CheckTestCase.php index baa8452..595d786 100755 --- a/tests/Checks/CheckTestCase.php +++ b/tests/Checks/CheckTestCase.php @@ -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()); diff --git a/tests/Checks/FileIniTest.php b/tests/Checks/FileIniTest.php new file mode 100644 index 0000000..1308bfe --- /dev/null +++ b/tests/Checks/FileIniTest.php @@ -0,0 +1,31 @@ +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'); + } +} \ No newline at end of file diff --git a/tests/Checks/FileJsonTest.php b/tests/Checks/FileJsonTest.php new file mode 100644 index 0000000..512ec8a --- /dev/null +++ b/tests/Checks/FileJsonTest.php @@ -0,0 +1,31 @@ +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'); + } +} \ No newline at end of file diff --git a/tests/Checks/FileYamlTest.php b/tests/Checks/FileYamlTest.php new file mode 100644 index 0000000..5611b33 --- /dev/null +++ b/tests/Checks/FileYamlTest.php @@ -0,0 +1,31 @@ +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'); + } +} \ No newline at end of file diff --git a/tests/files/invalid.ini b/tests/files/invalid.ini new file mode 100644 index 0000000..af75269 --- /dev/null +++ b/tests/files/invalid.ini @@ -0,0 +1,3 @@ +; comment text + +#@!#@!# \ No newline at end of file diff --git a/tests/files/invalid.json.txt b/tests/files/invalid.json.txt new file mode 100644 index 0000000..4a49f80 --- /dev/null +++ b/tests/files/invalid.json.txt @@ -0,0 +1,3 @@ +{ + "test +} \ No newline at end of file diff --git a/tests/files/invalid.yaml.txt b/tests/files/invalid.yaml.txt new file mode 100644 index 0000000..5637933 --- /dev/null +++ b/tests/files/invalid.yaml.txt @@ -0,0 +1,3 @@ + + "test +} \ No newline at end of file diff --git a/tests/files/valid.ini b/tests/files/valid.ini new file mode 100644 index 0000000..4cdd3e4 --- /dev/null +++ b/tests/files/valid.ini @@ -0,0 +1,5 @@ +; comment text +foo=bar +[section] +a=a +b=b \ No newline at end of file diff --git a/tests/files/valid.json b/tests/files/valid.json new file mode 100644 index 0000000..54084e6 --- /dev/null +++ b/tests/files/valid.json @@ -0,0 +1,3 @@ +{ + "test": 1 +} \ No newline at end of file diff --git a/tests/files/valid.yaml b/tests/files/valid.yaml new file mode 100644 index 0000000..70496a1 --- /dev/null +++ b/tests/files/valid.yaml @@ -0,0 +1,13 @@ +language: php + +php: + - 7.1 + - 7.2 + - 7.3 + +before_script: + - composer install --no-interaction --prefer-source + +after_success: +# Submit coverage report to https://codecov.io +- bash <(curl -s https://codecov.io/bash) \ No newline at end of file