diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..8943349 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +/tests export-ignore +/docs export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/.scrutinizer.yml export-ignore +/phpunit.xml export-ignore diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..0c32759 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,13 @@ +inherit: true + +tools: + external_code_coverage: + timeout: 1800 # 30 minute delay to allow for coverage reporting taking ages! + +checks: + php: + code_rating: true + duplication: true + +filter: + paths: [code/*, tests/*] diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9e83a65 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,35 @@ +language: php + +php: + - 5.3 + - 5.4 + - 5.5 + - 5.6 + - 7.0 + +sudo: false + +env: + - DB=MYSQL CORE_RELEASE=3.1 + +before_script: + - composer self-update || true + - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support + - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss + - cd ~/builds/ss + - composer install + +script: + - vendor/bin/phpunit --coverage-clover coverage.clover environmentcheck/tests + - wget https://scrutinizer-ci.com/ocular.phar + - git remote rm origin + - git remote add origin git@github.com:silverstripe-labs/silverstripe-environmentcheck.git + - php ocular.phar code-coverage:upload --format=php-clover coverage.clover + +branches: + only: + - master + +matrix: + allow_failures: + - php: 7.0 diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..ba47a2d --- /dev/null +++ b/changelog.md @@ -0,0 +1,9 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +This project adheres to [Semantic Versioning](http://semver.org/). + +## [1.1.1] + +Changelog added. diff --git a/code-of-conduct.md b/code-of-conduct.md new file mode 100644 index 0000000..9cad55d --- /dev/null +++ b/code-of-conduct.md @@ -0,0 +1,3 @@ +# Code of Conduct + +https://docs.silverstripe.org/en/3.1/contributing/code_of_conduct/ \ No newline at end of file diff --git a/code/DevCheckController.php b/code/DevCheckController.php index 1a111d3..1a5b17e 100644 --- a/code/DevCheckController.php +++ b/code/DevCheckController.php @@ -1,20 +1,37 @@ param('Suite') ? $request->param('Suite') : 'check'; - $e = new EnvironmentChecker($suiteName, 'Environment status'); - $e->init($this->config()->permission); //check for admin permissions before running this check - return $e; + $suite = 'check'; + + if ($name = $request->param('Suite')) { + $suite = $name; + } + + $checker = new EnvironmentChecker($suite, 'Environment status'); + $checker->init($this->config()->permission); + + return $checker; } } diff --git a/code/DevHealthController.php b/code/DevHealthController.php index 9f91c53..0130291 100644 --- a/code/DevHealthController.php +++ b/code/DevHealthController.php @@ -1,15 +1,25 @@ init(''); //empty permission check, the "health" check does not require a permission check to run - $e->setErrorCode(404); - return $e; + // health check does not require permission to run + + $checker = new EnvironmentChecker('health', 'Site health'); + $checker->init(''); + $checker->setErrorCode(404); + + return $checker; } } diff --git a/code/EnvironmentCheck.php b/code/EnvironmentCheck.php index 0b3af58..3677f29 100644 --- a/code/EnvironmentCheck.php +++ b/code/EnvironmentCheck.php @@ -3,8 +3,9 @@ /** * Interface for environment checks * - * An environment check is a test that can be performed on a live environment. They differ from unit - * tests in that they are designed to check the state of the evnironment / server, rather than the code. + * An environment check is a test that can be performed on a live environment. They differ from + * unit tests in that they are designed to check the state of the environment/server, rather than + * the code. * * Environment checks should *never* alter production data. * @@ -14,17 +15,25 @@ * - Are the file permissions correct? */ interface EnvironmentCheck { - + /** + * @var int + */ const ERROR = 3; + + /** + * @var int + */ const WARNING = 2; + + /** + * @var int + */ const OK = 1; /** - * Perform this check - * - * @return 2 element array( $status, $message ) - * $status is EnvironmentCheck::ERROR, EnvironmentCheck::WARNING, or EnvironmentCheck::OK + * @return array Result with 'status' and 'message' keys. + * + * Status is EnvironmentCheck::ERROR, EnvironmentCheck::WARNING, or EnvironmentCheck::OK. */ function check(); - } \ No newline at end of file diff --git a/code/EnvironmentCheckSuite.php b/code/EnvironmentCheckSuite.php index 1726139..a3e6968 100644 --- a/code/EnvironmentCheckSuite.php +++ b/code/EnvironmentCheckSuite.php @@ -1,4 +1,5 @@ run(); */ class EnvironmentCheckSuite extends Object { - /** * Name of this suite. + * + * @var string */ protected $name; + /** + * @var array + */ protected $checks = array(); /** @@ -32,14 +37,18 @@ class EnvironmentCheckSuite extends Object { * - definition (e.g. 'MyHealthCheck("my param")') * - title (e.g. 'Is my feature working?') * - state (setting this to 'disabled' will cause suites to skip this check entirely. + * + * @var array */ - private static $registered_checks; + private static $registered_checks = array(); /** * Associative array of named suites registered via the config system. Each suite should enumerate * named checks that have been configured in 'registered_checks'. + * + * @var array */ - private static $registered_suites; + private static $registered_suites = array(); /** * Load checks for this suite from the configuration system. This is an alternative to the @@ -48,6 +57,8 @@ class EnvironmentCheckSuite extends Object { * @param string $suiteName The name of this suite. */ public function __construct($suiteName) { + parent::__construct(); + if (empty($this->config()->registered_suites[$suiteName])) { // Not registered via config system, but it still may be configured later via self::register. return; @@ -72,13 +83,13 @@ class EnvironmentCheckSuite extends Object { } /** - * Run this test suite - * @return The result code of the worst result. + * Run this test suite and return the result code of the worst result. + * + * @return int */ public function run() { - $worstResult = 0; - $result = new EnvironmentCheckSuiteResult(); + foreach($this->checkInstances() as $check) { list($checkClass, $checkTitle) = $check; try { @@ -95,7 +106,9 @@ class EnvironmentCheckSuite extends Object { } /** - * Get instances of all the environment checks + * Get instances of all the environment checks. + * + * @return array */ protected function checkInstances() { $output = array(); @@ -119,6 +132,9 @@ class EnvironmentCheckSuite extends Object { /** * Add a check to this suite. + * + * @param mixed $check + * @param string $title */ public function push($check, $title = null) { if(!$title) { @@ -129,10 +145,17 @@ class EnvironmentCheckSuite extends Object { ///////////////////////////////////////////////////////////////////////////////////////////// + /** + * @var array + */ protected static $instances = array(); /** * Return a named instance of EnvironmentCheckSuite. + * + * @param string $name + * + * @return EnvironmentCheckSuite */ static function inst($name) { if(!isset(self::$instances[$name])) self::$instances[$name] = new EnvironmentCheckSuite($name); @@ -142,7 +165,9 @@ class EnvironmentCheckSuite extends Object { /** * Register a check against the named check suite. * - * @param String|Array + * @param string|array $names + * @param EnvironmentCheck $check + * @param string|array */ static function register($names, $check, $title = null) { if(!is_array($names)) $names = array($names); @@ -154,13 +179,26 @@ class EnvironmentCheckSuite extends Object { * A single set of results from running an EnvironmentCheckSuite */ class EnvironmentCheckSuiteResult extends ViewableData { - protected $details, $worst = 0; - + /** + * @var ArrayList + */ + protected $details; + + /** + * @var int + */ + protected $worst = 0; + function __construct() { parent::__construct(); $this->details = new ArrayList(); } + /** + * @param int $status + * @param string $message + * @param string $checkIdentifier + */ function addResult($status, $message, $checkIdentifier) { $this->details->push(new ArrayData(array( 'Check' => $checkIdentifier, @@ -172,21 +210,27 @@ class EnvironmentCheckSuiteResult extends ViewableData { } /** - * Returns true if there are no ERRORs, only WARNINGs or OK + * Returns true if there are no errors. + * + * @return bool */ - function ShouldPass() { + public function ShouldPass() { return $this->worst <= EnvironmentCheck::WARNING; } /** * Returns overall (i.e. worst) status as a string. + * + * @return string */ function Status() { return $this->statusText($this->worst); } /** - * Returns detailed status information about each check + * Returns detailed status information about each check. + * + * @return ArrayList */ function Details() { return $this->details; @@ -194,6 +238,7 @@ class EnvironmentCheckSuiteResult extends ViewableData { /** * Convert the final result status and details to JSON. + * * @return string */ function toJSON() { @@ -209,7 +254,9 @@ class EnvironmentCheckSuiteResult extends ViewableData { } /** - * Return a text version of a status code + * Return a text version of a status code. + * + * @return string */ protected function statusText($status) { switch($status) { diff --git a/code/EnvironmentChecker.php b/code/EnvironmentChecker.php index fc2145c..0f90108 100644 --- a/code/EnvironmentChecker.php +++ b/code/EnvironmentChecker.php @@ -4,30 +4,59 @@ * Provides an interface for checking the given EnvironmentCheckSuite. */ class EnvironmentChecker extends RequestHandler { - - static $url_handlers = array( + /** + * @var array + */ + private static $url_handlers = array( '' => 'index', ); + /** + * @var string + */ protected $checkSuiteName; - + + /** + * @var string + */ protected $title; - + + /** + * @var int + */ protected $errorCode = 500; + /** + * @var null|string + */ public static $to_email_address = null; - + + /** + * @var null|string + */ public static $from_email_address = null; - + + /** + * @var bool + */ public static $email_results = false; - + + /** + * @param string $checkSuiteName + * @param string $title + */ function __construct($checkSuiteName, $title) { parent::__construct(); $this->checkSuiteName = $checkSuiteName; $this->title = $title; } - + + /** + * @param string $permission + * + * @throws SS_HTTPResponse_Exception + */ function init($permission = 'ADMIN') { // if the environment supports it, provide a basic auth challenge and see if it matches configured credentials if(defined('ENVCHECK_BASICAUTH_USERNAME') && defined('ENVCHECK_BASICAUTH_PASSWORD')) { @@ -59,6 +88,14 @@ class EnvironmentChecker extends RequestHandler { } } + /** + * @param null|int|Member $member + * @param string $permission + * + * @return bool + * + * @throws SS_HTTPResponse_Exception + */ function canAccess($member = null, $permission = "ADMIN") { if(!$member) { $member = Member::currentUser(); @@ -90,7 +127,10 @@ class EnvironmentChecker extends RequestHandler { return false; } - + + /** + * @return SS_HTTPResponse + */ function index() { $response = new SS_HTTPResponse; $result = EnvironmentCheckSuite::inst($this->checkSuiteName)->run(); @@ -128,34 +168,52 @@ class EnvironmentChecker extends RequestHandler { /** * Set the HTTP status code that should be returned when there's an error. - * Defaults to 500 + * + * @param int $errorCode */ function setErrorCode($errorCode) { $this->errorCode = $errorCode; } + /** + * @param string $from + */ public static function set_from_email_address($from) { self::$from_email_address = $from; } + /** + * @return null|string + */ public static function get_from_email_address() { return self::$from_email_address; } + /** + * @param string $to + */ public static function set_to_email_address($to) { self::$to_email_address = $to; } + /** + * @return null|string + */ public static function get_to_email_address() { return self::$to_email_address; } + /** + * @param bool $results + */ public static function set_email_results($results) { self::$email_results = $results; } + /** + * @return bool + */ public static function get_email_results() { return self::$email_results; } - } diff --git a/code/checks/DatabaseCheck.php b/code/checks/DatabaseCheck.php index ee630b4..6b9200d 100644 --- a/code/checks/DatabaseCheck.php +++ b/code/checks/DatabaseCheck.php @@ -1,25 +1,27 @@ checkTable = $checkTable; } - - function check() { + /** + * @inheritdoc + * + * @return array + */ + function check() { if(!DB::getConn()->hasTable($this->checkTable)) { return array(EnvironmentCheck::ERROR, "$this->checkTable not present in the database"); } @@ -32,4 +34,4 @@ class DatabaseCheck implements EnvironmentCheck { return array(EnvironmentCheck::WARNING, "$this->checkTable queried ok but has no records"); } } -} \ No newline at end of file +} diff --git a/code/checks/ExternalURLCheck.php b/code/checks/ExternalURLCheck.php index fb1530b..9d61e04 100644 --- a/code/checks/ExternalURLCheck.php +++ b/code/checks/ExternalURLCheck.php @@ -1,4 +1,5 @@ EnvironmentCheckSuite::register('check', 'HasFunctionCheck("curl_init")', "Does PHP have CURL support?"); */ class ExternalURLCheck implements EnvironmentCheck { - /** * @var array */ @@ -20,14 +20,19 @@ class ExternalURLCheck implements EnvironmentCheck { protected $timeout; /** - * @param String Space separated list of absolute URLs - * (can't be an array as we're using Object::create() with strings for the constructor signature) + * @param string $urls Space-separated list of absolute URLs. + * @param int $timeout */ function __construct($urls, $timeout = 15) { if($urls) $this->urls = explode(' ', $urls); $this->timeout = $timeout; } + /** + * @inheritdoc + * + * @return array + */ function check() { $urls = $this->getURLs(); @@ -90,7 +95,7 @@ class ExternalURLCheck implements EnvironmentCheck { } /** - * @return Array + * @return array */ protected function getCurlOpts($url) { return array( @@ -103,9 +108,9 @@ class ExternalURLCheck implements EnvironmentCheck { } /** - * @return Array + * @return array */ protected function getURLs() { return $this->urls; } -} \ No newline at end of file +} diff --git a/code/checks/FileAccessibilityAndValidationCheck.php b/code/checks/FileAccessibilityAndValidationCheck.php index c7257a0..f7db038 100644 --- a/code/checks/FileAccessibilityAndValidationCheck.php +++ b/code/checks/FileAccessibilityAndValidationCheck.php @@ -1,50 +1,72 @@ path = $path; $this->fileTypeValidateFunc = ($fileTypeValidateFunc)? $fileTypeValidateFunc:'noVidation'; $this->checkType = ($checkType) ? $checkType : self::CHECK_SINGLE; } + /** + * @inheritdoc + * + * @return array + */ function check() { $origStage = Versioned::get_reading_mode(); Versioned::set_reading_mode('Live'); @@ -122,6 +144,11 @@ class FileAccessibilityAndValidationCheck implements EnvironmentCheck { return $checkReturn; } + /** + * @param string $file + * + * @return bool + */ private function jsonValidate($file){ $json = json_decode(file_get_contents($file)); if(!$json) { @@ -131,15 +158,21 @@ class FileAccessibilityAndValidationCheck implements EnvironmentCheck { } } + /** + * @param string $file + * + * @return bool + */ protected function noVidation($file) { return true; } /** - * @return Array Of absolute file paths + * Gets a list of absolute file paths. + * + * @return array */ protected function getFiles() { return glob($this->path); } - -} \ No newline at end of file +} diff --git a/code/checks/FileAgeCheck.php b/code/checks/FileAgeCheck.php index f5ee51e..6d5eb8d 100644 --- a/code/checks/FileAgeCheck.php +++ b/code/checks/FileAgeCheck.php @@ -1,4 +1,5 @@ ' or '<'. + * Type of comparison (either > or <). + * + * @var string */ protected $compareOperand; + /** + * @param string $path + * @param string $relativeAge + * @param string $compareOperand + * @param null|int $checkType + * @param string $checkFn + */ function __construct($path, $relativeAge, $compareOperand = '>', $checkType = null, $checkFn = 'filemtime') { $this->path = $path; $this->relativeAge = $relativeAge; @@ -58,6 +80,11 @@ class FileAgeCheck implements EnvironmentCheck { $this->compareOperand = $compareOperand; } + /** + * @inheritdoc + * + * @return array + */ function check() { $cutoffTime = strtotime($this->relativeAge, SS_Datetime::now()->Format('U')); $files = $this->getFiles(); @@ -98,10 +125,11 @@ class FileAgeCheck implements EnvironmentCheck { } /** - * @return Array Of absolute file paths + * Gets a list of absolute file paths. + * + * @return array */ protected function getFiles() { return glob($this->path); } - -} \ No newline at end of file +} diff --git a/code/checks/FileWriteableCheck.php b/code/checks/FileWriteableCheck.php index 366db02..4678bf9 100644 --- a/code/checks/FileWriteableCheck.php +++ b/code/checks/FileWriteableCheck.php @@ -1,16 +1,26 @@ path = $path; } - + + /** + * @inheritdoc + * + * @return array + */ function check() { if($this->path[0] == '/') $filename = $this->path; else $filename = BASE_PATH . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $this->path); @@ -55,4 +65,4 @@ class FileWriteableCheck implements EnvironmentCheck { return array(EnvironmentCheck::OK,''); } -} \ No newline at end of file +} diff --git a/code/checks/HasClassCheck.php b/code/checks/HasClassCheck.php index db372fa..a927674 100644 --- a/code/checks/HasClassCheck.php +++ b/code/checks/HasClassCheck.php @@ -1,18 +1,28 @@ className = $className; } - + + /** + * @inheritdoc + * + * @return array + */ function check() { if(class_exists($this->className)) return array(EnvironmentCheck::OK, 'Class ' . $this->className.' exists'); else return array(EnvironmentCheck::ERROR, 'Class ' . $this->className.' doesn\'t exist'); } -} \ No newline at end of file +} diff --git a/code/checks/HasFunctionCheck.php b/code/checks/HasFunctionCheck.php index 2f3d78b..7c1d6ce 100644 --- a/code/checks/HasFunctionCheck.php +++ b/code/checks/HasFunctionCheck.php @@ -1,18 +1,28 @@ functionName = $functionName; } - + + /** + * @inheritdoc + * + * @return array + */ function check() { if(function_exists($this->functionName)) return array(EnvironmentCheck::OK, $this->functionName.'() exists'); else return array(EnvironmentCheck::ERROR, $this->functionName.'() doesn\'t exist'); } -} \ No newline at end of file +} diff --git a/code/checks/SMTPConnectCheck.php b/code/checks/SMTPConnectCheck.php index d3dfb8c..f9fb560 100644 --- a/code/checks/SMTPConnectCheck.php +++ b/code/checks/SMTPConnectCheck.php @@ -1,28 +1,32 @@ host = ($host) ? $host : ini_get('SMTP'); @@ -34,6 +38,11 @@ class SMTPConnectCheck implements EnvironmentCheck { $this->timeout = $timeout; } + /** + * @inheritdoc + * + * @return array + */ function check() { $f = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); if(!$f) { @@ -53,6 +62,5 @@ class SMTPConnectCheck implements EnvironmentCheck { } return array(EnvironmentCheck::OK, ''); - } -} \ No newline at end of file +} diff --git a/code/checks/SolrIndexCheck.php b/code/checks/SolrIndexCheck.php index 145641f..799fac7 100644 --- a/code/checks/SolrIndexCheck.php +++ b/code/checks/SolrIndexCheck.php @@ -1,18 +1,28 @@ indexClass = $indexClass; } - + + /** + * @inheritdoc + * + * @return array + */ function check() { $brokenCores = array(); @@ -40,5 +50,4 @@ class SolrIndexCheck implements EnvironmentCheck { return array(EnvironmentCheck::OK, 'Expected indexes are available.'); } - } diff --git a/code/checks/URLCheck.php b/code/checks/URLCheck.php index be6767b..cc6b722 100644 --- a/code/checks/URLCheck.php +++ b/code/checks/URLCheck.php @@ -1,22 +1,37 @@ url = $url; $this->testString = $testString; } - + + /** + * @inheritdoc + * + * @return array + * + * @throws SS_HTTPResponse_Exception + */ function check() { $response = Director::test($this->url); @@ -39,4 +54,4 @@ class URLCheck implements EnvironmentCheck { ); } } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index 0edec7d..24c55f5 100644 --- a/composer.json +++ b/composer.json @@ -2,16 +2,19 @@ "name": "silverstripe/environmentcheck", "description": "Provides an API for building environment tests", "type": "silverstripe-module", - "keywords": ["silverstripe", "testing", "environment", "environmentcheck"], + "keywords": ["silverstripe", "testing", "environment", "check"], "authors": [ - { - "name": "Sam Minnee", - "email": "sam@silverstripe.com" - } + { + "name": "Will Rossiter", + "email": "will@fullscreen.io" + }, + { + "name": "Sam Minnee", + "email": "sam@silverstripe.com" + } ], - "require": { - "silverstripe/framework": "3.*" + "silverstripe/framework": "~3.1" } } diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..32334e2 --- /dev/null +++ b/contributing.md @@ -0,0 +1,3 @@ +# Contributing + +Contributions are welcome! Create an issue, explaining a bug or proposal. Submit pull requests if you feel brave. Speak to me on [Twitter](https://twitter.com/assertchris). diff --git a/license.md b/license.md new file mode 100644 index 0000000..f8030d4 --- /dev/null +++ b/license.md @@ -0,0 +1,16 @@ +Copyright (c) 2015, SilverStripe Limited +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + + * Neither the name of SilverStripe nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGE. diff --git a/README.md b/readme.md similarity index 80% rename from README.md rename to readme.md index e90e5fd..af1b8e6 100644 --- a/README.md +++ b/readme.md @@ -1,6 +1,10 @@ # SilverStripe Environment Checker Module -Initially developed by Sam Minnée, thanks to Will Rossiter. +[![Build Status](http://img.shields.io/travis/silverstripe-labs/silverstripe-environmentalcheck.svg?style=flat-square)](https://travis-ci.org/silverstripe-labs/silverstripe-environmentalcheck) +[![Code Quality](http://img.shields.io/scrutinizer/g/silverstripe-labs/silverstripe-environmentalcheck.svg?style=flat-square)](https://scrutinizer-ci.com/g/silverstripe-labs/silverstripe-environmentalcheck) +[![Code Coverage](http://img.shields.io/scrutinizer/coverage/g/silverstripe-labs/silverstripe-environmentalcheck.svg?style=flat-square)](https://scrutinizer-ci.com/g/silverstripe-labs/silverstripe-environmentalcheck) +[![Version](http://img.shields.io/packagist/v/silverstripe/environmentalcheck.svg?style=flat-square)](https://packagist.org/packages/silverstripe/silverstripe-environmentalcheck) +[![License](http://img.shields.io/packagist/l/silverstripe/environmentalcheck.svg?style=flat-square)](LICENSE.md) This module adds an API for running environment checks to your API. @@ -17,9 +21,13 @@ Almost, but not really. Environment checks differ from unit tests in two importa ## Installation -There are two ways to register your checks, both can be used at the same time. The checks will be appended to the suite. +```sh +$ composer require silverstripe/environmentalcheck +``` -### Direct method +You'll also need to run `/dev/build`. + +### Activating Directly Register checks in your own `_config.php` - see the `_config.php` in this module for some defaults. @@ -28,7 +36,7 @@ EnvironmentCheckSuite::register('health', 'DatabaseCheck', "Can we connect to th EnvironmentCheckSuite::register('check', 'URLCheck("")', "Is the homepage accessible?"); ``` -### Config system method +### Activating Via Config Register your checks on the `EnvironmentCheckSuite`. The same named check may be used multiple times. @@ -156,3 +164,13 @@ If you wish to embed an environment check suite in another, you can use the foll * `$result->Details()`: A `DataObjectSet` of details about the result of each check in the suite. See `EnvironmentChecker.ss` to see how these can be used to build a UI. + +## Versioning + +This library follows [Semver](http://semver.org). According to Semver, you will be able to upgrade to any minor or patch version of this library without any breaking changes to the public API. Semver also requires that we clearly define the public API for this library. + +All methods, with `public` visibility, are part of the public API. All other methods are not part of the public API. Where possible, we'll try to keep `protected` methods backwards-compatible in minor/patch versions, but if you're overriding methods then please test your work before upgrading. + +## Reporting Issues + +Please [create an issue](http://github.com/silverstripe-labs/silverstripe-environmentalcheck/issues) for any bugs you've found, or features you're missing. diff --git a/tests/DevCheckControllerTest.php b/tests/DevCheckControllerTest.php new file mode 100644 index 0000000..2485cd1 --- /dev/null +++ b/tests/DevCheckControllerTest.php @@ -0,0 +1,14 @@ +assertInstanceOf('EnvironmentChecker', $controller->index($request)); + } +} diff --git a/tests/DevHealthControllerTest.php b/tests/DevHealthControllerTest.php new file mode 100644 index 0000000..067bdf7 --- /dev/null +++ b/tests/DevHealthControllerTest.php @@ -0,0 +1,23 @@ +assertInstanceOf('EnvironmentChecker', $controller->index($request)); + } +} diff --git a/tests/checks/DatabaseCheckTest.php b/tests/checks/DatabaseCheckTest.php new file mode 100644 index 0000000..e1b9a1a --- /dev/null +++ b/tests/checks/DatabaseCheckTest.php @@ -0,0 +1,17 @@ +assertEquals($expected, $check->check()); + } +} diff --git a/tests/checks/ExternalURLCheckTest.php b/tests/checks/ExternalURLCheckTest.php new file mode 100644 index 0000000..5e4c3c1 --- /dev/null +++ b/tests/checks/ExternalURLCheckTest.php @@ -0,0 +1,19 @@ +markTestSkipped('ExternalURLCheck seems faulty on some systems'); + + $check = new ExternalURLCheck('http://missing-site/'); + + $expected = array( + EnvironmentCheck::ERROR, + 'Success retrieving "http://missing-site/" (Code: 404)', + ); + + $this->assertEquals($expected, $check->check()); + } +} diff --git a/tests/checks/FileWritableCheckTest.php b/tests/checks/FileWritableCheckTest.php new file mode 100644 index 0000000..bd1d44c --- /dev/null +++ b/tests/checks/FileWritableCheckTest.php @@ -0,0 +1,25 @@ +assertEquals($expected, $check->check()); + } + + public function testCheckReportsNonWritablePaths() { + $check = new FileWriteableCheck('/var'); + + $result = $check->check(); + + $this->assertEquals(EnvironmentCheck::ERROR, $result[0]); + } +} diff --git a/tests/checks/HasClassCheckTest.php b/tests/checks/HasClassCheckTest.php new file mode 100644 index 0000000..648d1fe --- /dev/null +++ b/tests/checks/HasClassCheckTest.php @@ -0,0 +1,28 @@ +assertEquals($expected, $check->check()); + } + + public function testCheckReportsFoundClasses() { + $check = new HasClassCheck('stdClass'); + + $expected = array( + EnvironmentCheck::OK, + 'Class stdClass exists', + ); + + $this->assertEquals($expected, $check->check()); + } +} diff --git a/tests/checks/HasFunctionCheckTest.php b/tests/checks/HasFunctionCheckTest.php new file mode 100644 index 0000000..312bd10 --- /dev/null +++ b/tests/checks/HasFunctionCheckTest.php @@ -0,0 +1,28 @@ +assertEquals($expected, $check->check()); + } + + public function testCheckReportsFoundFunctions() { + $check = new HasFunctionCheck('class_exists'); + + $expected = array( + EnvironmentCheck::OK, + 'class_exists() exists', + ); + + $this->assertEquals($expected, $check->check()); + } +} diff --git a/tests/checks/URLCheckTest.php b/tests/checks/URLCheckTest.php new file mode 100644 index 0000000..9f3d19c --- /dev/null +++ b/tests/checks/URLCheckTest.php @@ -0,0 +1,17 @@ +assertEquals($expected, $check->check()); + } +}