From 2131840da4089e6b88a6ea05e83818ffc2a9bea4 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Fri, 25 Aug 2017 15:19:45 +1200 Subject: [PATCH 1/2] NEW Remove Phockito test dependency, update config API use --- composer.json | 7 +- src/EnvironmentChecker.php | 26 ++-- tests/EnvironmentCheckerTest.php | 144 +++++++----------- tests/EnvironmentCheckerTest/CheckErrors.php | 14 ++ .../EnvironmentCheckerTest/CheckNoErrors.php | 14 ++ .../EnvironmentCheckerTest/CheckWarnings.php | 14 ++ 6 files changed, 108 insertions(+), 111 deletions(-) create mode 100644 tests/EnvironmentCheckerTest/CheckErrors.php create mode 100644 tests/EnvironmentCheckerTest/CheckNoErrors.php create mode 100644 tests/EnvironmentCheckerTest/CheckWarnings.php diff --git a/composer.json b/composer.json index 541702f..fbd5c02 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,6 @@ "silverstripe/framework": "^4.0@dev" }, "require-dev": { - "hafriedlander/phockito": "dev-master", "silverstripe/versioned": "^1.0@dev" }, "extra": { @@ -28,11 +27,7 @@ }, "autoload": { "psr-4": { - "SilverStripe\\EnvironmentCheck\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { + "SilverStripe\\EnvironmentCheck\\": "src/", "SilverStripe\\EnvironmentCheck\\Tests\\": "tests/" } } diff --git a/src/EnvironmentChecker.php b/src/EnvironmentChecker.php index 38bd042..204dd32 100644 --- a/src/EnvironmentChecker.php +++ b/src/EnvironmentChecker.php @@ -193,10 +193,10 @@ class EnvironmentChecker extends RequestHandler 'ErrorCode' => $this->errorCode, ])->renderWith(__CLASS__); - if ($this->config()->email_results && !$result->ShouldPass()) { + if ($this->config()->get('email_results') && !$result->ShouldPass()) { $email = new Email( - $this->config()->from_email_address, - $this->config()->to_email_address, + $this->config()->get('from_email_address'), + $this->config()->get('to_email_address'), $this->title, $resultText ); @@ -205,15 +205,15 @@ class EnvironmentChecker extends RequestHandler // Optionally log errors and warnings individually foreach ($result->Details() as $detail) { - if ($this->config()->log_results_warning && $detail->StatusCode == EnvironmentCheck::WARNING) { + if ($this->config()->get('log_results_warning') && $detail->StatusCode == EnvironmentCheck::WARNING) { $this->log( sprintf('EnvironmentChecker warning at "%s" check. Message: %s', $detail->Check, $detail->Message), - $this->config()->log_results_warning_level + $this->config()->get('log_results_warning_level') ); - } elseif ($this->config()->log_results_error && $detail->StatusCode == EnvironmentCheck::ERROR) { + } elseif ($this->config()->get('log_results_error') && $detail->StatusCode == EnvironmentCheck::ERROR) { $this->log( sprintf('EnvironmentChecker error at "%s" check. Message: %s', $detail->Check, $detail->Message), - $this->config()->log_results_error_level + $this->config()->get('log_results_error_level') ); } } @@ -260,7 +260,7 @@ class EnvironmentChecker extends RequestHandler public static function set_from_email_address($from) { Deprecation::notice('2.0', 'Use config API instead'); - Config::modify()->set(__CLASS__, 'from_email_address', $from); + static::config()->set('from_email_address', $from); } /** @@ -270,7 +270,7 @@ class EnvironmentChecker extends RequestHandler public static function get_from_email_address() { Deprecation::notice('2.0', 'Use config API instead'); - return Config::inst()->get(__CLASS__, 'from_email_address'); + return static::config()->get('from_email_address'); } /** @@ -280,7 +280,7 @@ class EnvironmentChecker extends RequestHandler public static function set_to_email_address($to) { Deprecation::notice('2.0', 'Use config API instead'); - Config::modify()->set(__CLASS__, 'to_email_address', $to); + static::config()->set('to_email_address', $to); } /** @@ -290,7 +290,7 @@ class EnvironmentChecker extends RequestHandler public static function get_to_email_address() { Deprecation::notice('2.0', 'Use config API instead'); - return Config::inst()->get(__CLASS__, 'to_email_address'); + return static::config()->get('to_email_address'); } /** @@ -300,7 +300,7 @@ class EnvironmentChecker extends RequestHandler public static function set_email_results($results) { Deprecation::notice('2.0', 'Use config API instead'); - Config::modify()->set(__CLASS__, 'email_results', $results); + static::config()->set('email_results', $results); } /** @@ -310,6 +310,6 @@ class EnvironmentChecker extends RequestHandler public static function get_email_results() { Deprecation::notice('2.0', 'Use config API instead'); - return Config::inst()->get(__CLASS__, 'email_results'); + return static::config()->get('email_results'); } } diff --git a/tests/EnvironmentCheckerTest.php b/tests/EnvironmentCheckerTest.php index f03110e..77b21db 100644 --- a/tests/EnvironmentCheckerTest.php +++ b/tests/EnvironmentCheckerTest.php @@ -2,13 +2,12 @@ namespace SilverStripe\EnvironmentCheck\Tests; -use Phockito; +use Monolog\Logger; use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\SapphireTest; -use SilverStripe\Dev\TestOnly; -use SilverStripe\EnvironmentCheck\EnvironmentCheck; use SilverStripe\EnvironmentCheck\EnvironmentChecker; use SilverStripe\EnvironmentCheck\EnvironmentCheckSuite; @@ -19,43 +18,11 @@ use SilverStripe\EnvironmentCheck\EnvironmentCheckSuite; */ class EnvironmentCheckerTest extends SapphireTest { - /** - * {@inheritDoc} - * @var bool - */ protected $usesDatabase = true; - /** - * {@inheritDoc} - */ - public static function setUpBeforeClass() + protected function tearDown() { - parent::setUpBeforeClass(); - - Phockito::include_hamcrest(); - - $logger = Injector::inst()->get(LoggerInterface::class); - if ($logger instanceof \Monolog\Logger) { - // It logs to stderr by default - disable - $logger->pushHandler(new \Monolog\Handler\NullHandler); - } - } - - /** - * {@inheritDoc} - */ - public function setUp() - { - parent::setUp(); - Config::nest(); - } - - /** - * {@inheritDoc} - */ - public function tearDown() - { - Config::unnest(); + EnvironmentCheckSuite::reset(); parent::tearDown(); } @@ -63,75 +30,68 @@ class EnvironmentCheckerTest extends SapphireTest { Config::modify()->set(EnvironmentChecker::class, 'log_results_warning', true); Config::modify()->set(EnvironmentChecker::class, 'log_results_error', true); - EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckNoErrors()); - $checker = Phockito::spy( - EnvironmentChecker::class, - 'test suite', - 'test' - ); - $response = $checker->index(); - Phockito::verify($checker, 0)->log(\anything(), \anything()); - EnvironmentCheckSuite::reset(); + EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckNoErrors()); + + $logger = $this->getMockBuilder(Logger::class) + ->disableOriginalConstructor() + ->setMethods(['log']) + ->getMock(); + + $logger->expects($this->never())->method('log'); + + Injector::inst()->registerService($logger, LoggerInterface::class); + + (new EnvironmentChecker('test suite', 'test'))->index(); } public function testLogsWithWarnings() { Config::modify()->set(EnvironmentChecker::class, 'log_results_warning', true); Config::modify()->set(EnvironmentChecker::class, 'log_results_error', false); - EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckWarnings()); - EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckErrors()); - $checker = Phockito::spy( - EnvironmentChecker::class, - 'test suite', - 'test' - ); - $response = $checker->index(); - Phockito::verify($checker, 1)->log(containsString('warning'), \anything()); - Phockito::verify($checker, 0)->log(containsString('error'), \anything()); - EnvironmentCheckSuite::reset(); + EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckWarnings()); + EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckErrors()); + + $logger = $this->getMockBuilder(Logger::class) + ->disableOriginalConstructor() + ->setMethods(['log']) + ->getMock(); + + $logger->expects($this->once()) + ->method('log') + ->withConsecutive( + $this->equalTo(LogLevel::WARNING), + $this->anything() + ); + + Injector::inst()->registerService($logger, LoggerInterface::class); + + (new EnvironmentChecker('test suite', 'test'))->index(); } public function testLogsWithErrors() { Config::modify()->set(EnvironmentChecker::class, 'log_results_error', false); Config::modify()->set(EnvironmentChecker::class, 'log_results_error', true); - EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckWarnings()); - EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckErrors()); - $checker = Phockito::spy( - EnvironmentChecker::class, - 'test suite', - 'test' - ); - $response = $checker->index(); - Phockito::verify($checker, 0)->log(containsString('warning'), \anything()); - Phockito::verify($checker, 1)->log(containsString('error'), \anything()); - EnvironmentCheckSuite::reset(); - } -} - -class EnvironmentCheckerTest_CheckNoErrors implements EnvironmentCheck, TestOnly -{ - public function check() - { - return [EnvironmentCheck::OK, '']; - } -} - -class EnvironmentCheckerTest_CheckWarnings implements EnvironmentCheck, TestOnly -{ - public function check() - { - return [EnvironmentCheck::WARNING, 'test warning']; - } -} - -class EnvironmentCheckerTest_CheckErrors implements EnvironmentCheck, TestOnly -{ - public function check() - { - return [EnvironmentCheck::ERROR, 'test error']; + EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckWarnings()); + EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckErrors()); + + $logger = $this->getMockBuilder(Logger::class) + ->disableOriginalConstructor() + ->setMethods(['log']) + ->getMock(); + + $logger->expects($this->once()) + ->method('log') + ->withConsecutive( + [$this->equalTo(LogLevel::ALERT), $this->anything()], + [$this->equalTo(LogLevel::WARNING), $this->anything()] + ); + + Injector::inst()->registerService($logger, LoggerInterface::class); + + (new EnvironmentChecker('test suite', 'test'))->index(); } } diff --git a/tests/EnvironmentCheckerTest/CheckErrors.php b/tests/EnvironmentCheckerTest/CheckErrors.php new file mode 100644 index 0000000..a646110 --- /dev/null +++ b/tests/EnvironmentCheckerTest/CheckErrors.php @@ -0,0 +1,14 @@ + Date: Fri, 25 Aug 2017 15:31:18 +1200 Subject: [PATCH 2/2] Replace Travis configuration with standalone, add phpcs dependency and phpunit config --- .travis.yml | 33 ++++++++++++++++++++------------- codecov.yml | 1 + composer.json | 8 ++++++-- phpunit.xml.dist | 16 ++++++++++++++++ readme.md | 6 +++--- src/EnvironmentChecker.php | 2 +- 6 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 codecov.yml create mode 100644 phpunit.xml.dist diff --git a/.travis.yml b/.travis.yml index 47dbad0..c912129 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,24 +1,31 @@ -# See https://github.com/silverstripe/silverstripe-travis-support for setup details - -sudo: false - language: php +env: + global: + - COMPOSER_ROOT_VERSION="4.0.x-dev" + matrix: include: - php: 5.6 - env: DB=MYSQL CORE_RELEASE=4 + env: DB=MYSQL PHPCS_TEST=1 PHPUNIT_TEST=1 - php: 7.0 - env: DB=PGSQL CORE_RELEASE=4 + env: DB=PGSQL PHPUNIT_TEST=1 - php: 7.1 - env: DB=MYSQL CORE_RELEASE=4 + env: DB=MYSQL PHPUNIT_COVERAGE_TEST=1 before_script: - - composer self-update || true - - git clone git://github.com/silverstripe/silverstripe-travis-support.git ~/travis-support - - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss - - cd ~/builds/ss - - composer install + - phpenv rehash + - phpenv config-rm xdebug.ini + + - composer install --prefer-dist + - composer require --prefer-dist --no-update silverstripe/framework:4.0.x-dev + - if [[ $DB == PGSQL ]]; then composer require --no-update --prefer-dist silverstripe/postgresql:2.0.x-dev; fi + - composer update script: - - vendor/bin/phpunit environmentcheck/tests + - if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit tests/; fi + - if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml; fi + - if [[ $PHPCS_TEST ]]; then vendor/bin/phpcs --standard=framework/phpcs.xml.dist src/ tests/ ; fi + +after_success: + - if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml; fi diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..69cb760 --- /dev/null +++ b/codecov.yml @@ -0,0 +1 @@ +comment: false diff --git a/composer.json b/composer.json index fbd5c02..4d5cf7b 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,9 @@ "silverstripe/framework": "^4.0@dev" }, "require-dev": { - "silverstripe/versioned": "^1.0@dev" + "silverstripe/versioned": "^1.0@dev", + "phpunit/phpunit": "^5.7", + "squizlabs/php_codesniffer": "^3.0" }, "extra": { "branch-alias": { @@ -30,5 +32,7 @@ "SilverStripe\\EnvironmentCheck\\": "src/", "SilverStripe\\EnvironmentCheck\\Tests\\": "tests/" } - } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..eca2a93 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,16 @@ + + + + tests + + + + + src/. + + tests/ + + + + + diff --git a/readme.md b/readme.md index 7df0200..1fcb7ea 100644 --- a/readme.md +++ b/readme.md @@ -1,8 +1,8 @@ # SilverStripe Environment Checker Module -[![Build Status](http://img.shields.io/travis/silverstripe/silverstripe-environmentcheck.svg?style=flat-square)](https://travis-ci.org/silverstripe/silverstripe-environmentcheck) -[![Code Quality](http://img.shields.io/scrutinizer/g/silverstripe-labs/silverstripe-environmentcheck.svg?style=flat-square)](https://scrutinizer-ci.com/g/silverstripe-labs/silverstripe-environmentcheck) -[![Code Coverage](http://img.shields.io/scrutinizer/coverage/g/silverstripe-labs/silverstripe-environmentcheck.svg?style=flat-square)](https://scrutinizer-ci.com/g/silverstripe-labs/silverstripe-environmentcheck) +[![Build Status](https://travis-ci.org/silverstripe/silverstripe-environmentcheck.svg?branch=master)](https://travis-ci.org/silverstripe/silverstripe-environmentcheck) +[![Code Quality](https://scrutinizer-ci.com/g/silverstripe/silverstripe-environmentcheck/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/silverstripe/silverstripe-environmentcheck/?branch=master) +[![Code Coverage](https://codecov.io/gh/silverstripe/silverstripe-environmentcheck/branch/master/graph/badge.svg)](https://codecov.io/gh/silverstripe/silverstripe-environmentcheck) [![Version](http://img.shields.io/packagist/v/silverstripe/environmentcheck.svg?style=flat-square)](https://packagist.org/packages/silverstripe/environmentcheck) [![License](http://img.shields.io/packagist/l/silverstripe/environmentcheck.svg?style=flat-square)](LICENSE.md) diff --git a/src/EnvironmentChecker.php b/src/EnvironmentChecker.php index 204dd32..96177c3 100644 --- a/src/EnvironmentChecker.php +++ b/src/EnvironmentChecker.php @@ -280,7 +280,7 @@ class EnvironmentChecker extends RequestHandler public static function set_to_email_address($to) { Deprecation::notice('2.0', 'Use config API instead'); - static::config()->set('to_email_address', $to); + static::config()->set('to_email_address', $to); } /**