silverstripe-fulltextsearch/tests/SolrReindexTest/SolrReindexTest_RecordingLogger.php

75 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\FullTextSearch\Tests\SolrReindexTest;
use Monolog\Logger;
use SilverStripe\Dev\TestOnly;
use SilverStripe\FullTextSearch\Tests\SolrReindexTest\SolrReindexTest_Handler;
/**
* Test logger for recording messages
*/
class SolrReindexTest_RecordingLogger extends Logger implements TestOnly
{
/**
* @var SolrReindexTest_Handler
*/
protected $testHandler = null;
public function __construct($name = 'testlogger', array $handlers = array(), array $processors = array())
{
parent::__construct($name, $handlers, $processors);
$this->testHandler = new SolrReindexTest_Handler();
$this->pushHandler($this->testHandler);
}
/**
* @return array
*/
public function getMessages()
{
return $this->testHandler->getMessages();
}
/**
* Clear all messages
*/
public function clear()
{
$this->testHandler->clear();
}
/**
* Get messages with the given filter
*
* @param string $containing
* @return array Filtered array
*/
public function filterMessages($containing)
{
return array_values(array_filter(
2022-04-13 01:24:03 +02:00
$this->getMessages() ?? [],
function ($content) use ($containing) {
2022-04-13 01:24:03 +02:00
return stripos($content ?? '', $containing ?? '') !== false;
}
));
}
/**
* Count all messages containing the given substring
*
* @param string $containing Message to filter by
* @return int
*/
public function countMessages($containing = null)
{
if ($containing) {
$messages = $this->filterMessages($containing);
} else {
$messages = $this->getMessages();
}
2022-04-13 01:24:03 +02:00
return count($messages ?? []);
}
}