2015-11-20 00:01:21 +01:00
|
|
|
<?php
|
2017-01-04 23:16:12 +01:00
|
|
|
|
|
|
|
namespace SilverStripe\EnvironmentCheck\Tests;
|
|
|
|
|
|
|
|
use Phockito;
|
|
|
|
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\EnvironmentCheckSuite;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class EnvironmentCheckerTest
|
|
|
|
*
|
|
|
|
* @package environmentcheck
|
|
|
|
*/
|
2015-11-21 07:18:35 +01:00
|
|
|
class EnvironmentCheckerTest extends SapphireTest
|
|
|
|
{
|
2017-01-04 23:16:12 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* @var bool
|
|
|
|
*/
|
2016-07-12 04:08:29 +02:00
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
2017-01-04 23:16:12 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2015-11-21 07:18:35 +01:00
|
|
|
public function setUpOnce()
|
|
|
|
{
|
|
|
|
parent::setUpOnce();
|
|
|
|
|
|
|
|
Phockito::include_hamcrest();
|
2017-01-04 23:16:12 +01:00
|
|
|
|
|
|
|
$logger = Injector::inst()->get('Logger');
|
|
|
|
if ($logger instanceof \Monolog\Logger) {
|
|
|
|
// It logs to stderr by default - disable
|
|
|
|
$logger->pushHandler(new \Monolog\Handler\NullHandler);
|
|
|
|
}
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 23:16:12 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2015-11-21 07:18:35 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Config::nest();
|
|
|
|
}
|
|
|
|
|
2017-01-04 23:16:12 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2015-11-21 07:18:35 +01:00
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
Config::unnest();
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnlyLogsWithErrors()
|
|
|
|
{
|
2017-01-04 23:16:12 +01:00
|
|
|
Config::inst()->update('SilverStripe\\EnvironmentCheck\\EnvironmentChecker', 'log_results_warning', true);
|
|
|
|
Config::inst()->update('SilverStripe\\EnvironmentCheck\\EnvironmentChecker', 'log_results_error', true);
|
2015-11-21 07:18:35 +01:00
|
|
|
EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckNoErrors());
|
|
|
|
$checker = Phockito::spy(
|
2017-01-04 23:16:12 +01:00
|
|
|
'SilverStripe\\EnvironmentCheck\\EnvironmentChecker',
|
2015-11-21 07:18:35 +01:00
|
|
|
'test suite',
|
|
|
|
'test'
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $checker->index();
|
|
|
|
Phockito::verify($checker, 0)->log(anything(), anything());
|
|
|
|
EnvironmentCheckSuite::reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogsWithWarnings()
|
|
|
|
{
|
2017-01-04 23:16:12 +01:00
|
|
|
Config::inst()->update('SilverStripe\\EnvironmentCheck\\EnvironmentChecker', 'log_results_warning', true);
|
|
|
|
Config::inst()->update('SilverStripe\\EnvironmentCheck\\EnvironmentChecker', 'log_results_error', false);
|
2015-11-21 07:18:35 +01:00
|
|
|
EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckWarnings());
|
|
|
|
EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckErrors());
|
|
|
|
$checker = Phockito::spy(
|
2017-01-04 23:16:12 +01:00
|
|
|
'SilverStripe\\EnvironmentCheck\\EnvironmentChecker',
|
2015-11-21 07:18:35 +01:00
|
|
|
'test suite',
|
|
|
|
'test'
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $checker->index();
|
|
|
|
Phockito::verify($checker, 1)->log(containsString('warning'), anything());
|
|
|
|
Phockito::verify($checker, 0)->log(containsString('error'), anything());
|
|
|
|
EnvironmentCheckSuite::reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogsWithErrors()
|
|
|
|
{
|
2017-01-04 23:16:12 +01:00
|
|
|
Config::inst()->update('SilverStripe\\EnvironmentCheck\\EnvironmentChecker', 'log_results_error', false);
|
|
|
|
Config::inst()->update('SilverStripe\\EnvironmentCheck\\EnvironmentChecker', 'log_results_error', true);
|
2015-11-21 07:18:35 +01:00
|
|
|
EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckWarnings());
|
|
|
|
EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckErrors());
|
|
|
|
$checker = Phockito::spy(
|
2017-01-04 23:16:12 +01:00
|
|
|
'SilverStripe\\EnvironmentCheck\\EnvironmentChecker',
|
2015-11-21 07:18:35 +01:00
|
|
|
'test suite',
|
|
|
|
'test'
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $checker->index();
|
|
|
|
Phockito::verify($checker, 0)->log(containsString('warning'), anything());
|
|
|
|
Phockito::verify($checker, 1)->log(containsString('error'), anything());
|
|
|
|
EnvironmentCheckSuite::reset();
|
|
|
|
}
|
2015-11-20 00:01:21 +01:00
|
|
|
}
|
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
class EnvironmentCheckerTest_CheckNoErrors implements EnvironmentCheck, TestOnly
|
|
|
|
{
|
|
|
|
public function check()
|
|
|
|
{
|
|
|
|
return array(EnvironmentCheck::OK, '');
|
|
|
|
}
|
2015-11-20 00:01:21 +01:00
|
|
|
}
|
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
class EnvironmentCheckerTest_CheckWarnings implements EnvironmentCheck, TestOnly
|
|
|
|
{
|
|
|
|
public function check()
|
|
|
|
{
|
2017-01-04 23:16:12 +01:00
|
|
|
return array(EnvironmentCheck::WARNING, 'test warning');
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2015-11-20 00:01:21 +01:00
|
|
|
}
|
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
class EnvironmentCheckerTest_CheckErrors implements EnvironmentCheck, TestOnly
|
|
|
|
{
|
|
|
|
public function check()
|
|
|
|
{
|
2017-01-04 23:16:12 +01:00
|
|
|
return array(EnvironmentCheck::ERROR, 'test error');
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
|
|
|
}
|