silverstripe-environmentcheck/tests/EnvironmentCheckerTest.php

102 lines
3.4 KiB
PHP
Raw Permalink Normal View History

<?php
namespace SilverStripe\EnvironmentCheck\Tests;
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\EnvironmentCheck\EnvironmentChecker;
use SilverStripe\EnvironmentCheck\EnvironmentCheckSuite;
/**
* Class EnvironmentCheckerTest
*
* @package environmentcheck
*/
2015-11-21 19:18:35 +13:00
class EnvironmentCheckerTest extends SapphireTest
{
2016-07-12 14:08:29 +12:00
protected $usesDatabase = true;
2021-10-27 18:06:29 +13:00
protected function tearDown(): void
2015-11-21 19:18:35 +13:00
{
EnvironmentCheckSuite::reset();
2015-11-21 19:18:35 +13:00
parent::tearDown();
}
public function testOnlyLogsWithErrors()
{
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());
$logger = $this->getMockBuilder(Logger::class)
->disableOriginalConstructor()
2024-09-10 18:03:57 +12:00
->onlyMethods(['log'])
->getMock();
$logger->expects($this->never())->method('log');
Injector::inst()->registerService($logger, LoggerInterface::class);
(new EnvironmentChecker('test suite', 'test'))->index();
2015-11-21 19:18:35 +13:00
}
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());
$logger = $this->getMockBuilder(Logger::class)
->disableOriginalConstructor()
2024-09-10 18:03:57 +12:00
->onlyMethods(['log'])
->getMock();
2024-09-10 18:03:57 +12:00
$matcher = $this->once();
$logger->expects($matcher)
->method('log')
2024-09-10 18:03:57 +12:00
->willReturnCallback(function (mixed $level) use ($matcher) {
match ($matcher->numberOfInvocations()) {
1 => $this->assertSame(LogLevel::WARNING, $level),
};
});
Injector::inst()->registerService($logger, LoggerInterface::class);
(new EnvironmentChecker('test suite', 'test'))->index();
2015-11-21 19:18:35 +13:00
}
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());
$logger = $this->getMockBuilder(Logger::class)
->disableOriginalConstructor()
2024-09-10 18:03:57 +12:00
->onlyMethods(['log'])
->getMock();
2024-09-10 18:03:57 +12:00
$matcher = $this->once();
$logger->expects($matcher)
->method('log')
2024-09-10 18:03:57 +12:00
->willReturnCallback(function (mixed $level) use ($matcher) {
match ($matcher->numberOfInvocations()) {
1 => $this->assertSame(LogLevel::ALERT, $level),
};
});
Injector::inst()->registerService($logger, LoggerInterface::class);
(new EnvironmentChecker('test suite', 'test'))->index();
2015-11-21 19:18:35 +13:00
}
}