Skip to content

Commit

Permalink
Merge pull request #20 from assertchris/make-supported
Browse files Browse the repository at this point in the history
Made supported
  • Loading branch information
Damian Mooyman committed Sep 14, 2015
2 parents 7e28448 + 53aafd9 commit 9b3d3be
Show file tree
Hide file tree
Showing 32 changed files with 682 additions and 133 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -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/*]
35 changes: 35 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions code-of-conduct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Code of Conduct

https://docs.silverstripe.org/en/3.1/contributing/code_of_conduct/
29 changes: 23 additions & 6 deletions code/DevCheckController.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
<?php

class DevCheckController extends Controller {

/**
* @var array
*/
public static $allowed_actions = array(
'index'
);

/**
* @var string Permission code to check for access to this controller.
* Permission code to check for access to this controller.
*
* @var string
*/
private static $permission = 'ADMIN';

/**
* @param SS_HTTPRequest $request
*
* @return EnvironmentChecker
*
* @throws SS_HTTPResponse_Exception
*/
function index($request) {
$suiteName = $request->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;
}
}
20 changes: 15 additions & 5 deletions code/DevHealthController.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
<?php

class DevHealthController extends Controller {

/**
* @var array
*/
public static $allowed_actions = array(
'index'
);

/**
* @return EnvironmentChecker
*
* @throws SS_HTTPResponse_Exception
*/
function index() {
$e = new EnvironmentChecker('health', 'Site health');
$e->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;
}
}
25 changes: 17 additions & 8 deletions code/EnvironmentCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();

}
77 changes: 62 additions & 15 deletions code/EnvironmentCheckSuite.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Represents a suite of environment checks.
* Specific checks can be registered against a named instance of EnvironmentCheckSuite.
Expand All @@ -19,27 +20,35 @@
* $result = EnvironmentCheckSuite::inst('health')->run();
*/
class EnvironmentCheckSuite extends Object {

/**
* Name of this suite.
*
* @var string
*/
protected $name;

/**
* @var array
*/
protected $checks = array();

/**
* Associative array of named checks registered via the config system. Each check should specify:
* - 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
Expand All @@ -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;
Expand All @@ -72,13 +83,13 @@ public function __construct($suiteName) {
}

/**
* 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 {
Expand All @@ -95,7 +106,9 @@ public function run() {
}

/**
* Get instances of all the environment checks
* Get instances of all the environment checks.
*
* @return array
*/
protected function checkInstances() {
$output = array();
Expand All @@ -119,6 +132,9 @@ protected function checkInstances() {

/**
* Add a check to this suite.
*
* @param mixed $check
* @param string $title
*/
public function push($check, $title = null) {
if(!$title) {
Expand All @@ -129,10 +145,17 @@ public function push($check, $title = null) {

/////////////////////////////////////////////////////////////////////////////////////////////

/**
* @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);
Expand All @@ -142,7 +165,9 @@ static function inst($name) {
/**
* 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);
Expand All @@ -154,13 +179,26 @@ static function register($names, $check, $title = null) {
* 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,
Expand All @@ -172,28 +210,35 @@ function addResult($status, $message, $checkIdentifier) {
}

/**
* 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;
}

/**
* Convert the final result status and details to JSON.
*
* @return string
*/
function toJSON() {
Expand All @@ -209,7 +254,9 @@ function toJSON() {
}

/**
* Return a text version of a status code
* Return a text version of a status code.
*
* @return string
*/
protected function statusText($status) {
switch($status) {
Expand Down

0 comments on commit 9b3d3be

Please sign in to comment.