mirror of
https://github.com/VladPolskiy/dokuwiki.git
synced 2025-07-25 15:59:39 +00:00
Allow tests to expect log messages
This commit is contained in:
@ -3,6 +3,8 @@
|
|||||||
use dokuwiki\Extension\PluginController;
|
use dokuwiki\Extension\PluginController;
|
||||||
use dokuwiki\Extension\Event;
|
use dokuwiki\Extension\Event;
|
||||||
use dokuwiki\Extension\EventHandler;
|
use dokuwiki\Extension\EventHandler;
|
||||||
|
use dokuwiki\Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class to provide basic functionality for tests
|
* Helper class to provide basic functionality for tests
|
||||||
*
|
*
|
||||||
@ -266,4 +268,17 @@ abstract class DokuWikiTest extends PHPUnit\Framework\TestCase {
|
|||||||
$property->setAccessible(true);
|
$property->setAccessible(true);
|
||||||
$property->setValue($obj, $value);
|
$property->setValue($obj, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expect the next log message to contain $message
|
||||||
|
*
|
||||||
|
* @param string $facility
|
||||||
|
* @param string $message
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function expectLogMessage(string $message, string $facility = Logger::LOG_ERROR): void
|
||||||
|
{
|
||||||
|
$logger = Logger::getInstance($facility);
|
||||||
|
$logger->expect($message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,9 @@ class Logger
|
|||||||
|
|
||||||
protected $isLogging = true;
|
protected $isLogging = true;
|
||||||
|
|
||||||
|
/** @var string[] a list of expected log messages, only used during unit testing */
|
||||||
|
protected $expected = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logger constructor.
|
* Logger constructor.
|
||||||
*
|
*
|
||||||
@ -165,6 +168,17 @@ class Logger
|
|||||||
return $this->isLogging;
|
return $this->isLogging;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests may register log expectations
|
||||||
|
*
|
||||||
|
* @param string $log
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function expect($log)
|
||||||
|
{
|
||||||
|
$this->expected[] = $log;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats the given data as loglines
|
* Formats the given data as loglines
|
||||||
*
|
*
|
||||||
@ -228,10 +242,23 @@ class Logger
|
|||||||
protected function writeLogLines($lines, $logfile)
|
protected function writeLogLines($lines, $logfile)
|
||||||
{
|
{
|
||||||
if (defined('DOKU_UNITTEST')) {
|
if (defined('DOKU_UNITTEST')) {
|
||||||
|
// our tests may expect certain log messages
|
||||||
|
if($this->expected) {
|
||||||
|
$expected = array_shift($this->expected);
|
||||||
|
if(!str_contains($lines[0], $expected)) {
|
||||||
|
throw new \RuntimeException(
|
||||||
|
"Log expectation failed:\n".
|
||||||
|
"Expected: $expected\n".
|
||||||
|
"Actual: {$lines[0]}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
$stderr = fopen('php://stderr', 'w');
|
$stderr = fopen('php://stderr', 'w');
|
||||||
fwrite($stderr, "\n[" . $this->facility . '] ' . implode("\n", $lines) . "\n");
|
fwrite($stderr, "\n[" . $this->facility . '] ' . implode("\n", $lines) . "\n");
|
||||||
fclose($stderr);
|
fclose($stderr);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return io_saveFile($logfile, implode("\n", $lines) . "\n", true);
|
return io_saveFile($logfile, implode("\n", $lines) . "\n", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user