MDL-80713 Unit Tests: Message sink improvement

- Added a new method called get_messages_by_component()
  This method will help to retreive the redirected messages
  of specific component only
- Added a new method called get_messages_by_component_and_type()
  This method will help to retreive the redirected messages
  of specific component and type only
This commit is contained in:
Huong Nguyen
2024-02-05 13:27:07 +07:00
parent 560f4d895b
commit cb075f6257
2 changed files with 128 additions and 1 deletions
+32 -1
View File
@@ -62,12 +62,43 @@ class phpunit_message_sink {
* The array indexes are numbered from 0 and the order is matching
* the creation of events.
*
* @param callable|null $filter Use to filter the messages.
* @return array
*/
public function get_messages() {
public function get_messages(?callable $filter = null): array {
if ($filter) {
return array_filter($this->messages, $filter);
}
return $this->messages;
}
/**
* Return all redirected messages for a given component.
*
* @param string $component Component name.
* @return array List of messages.
*/
public function get_messages_by_component(string $component): array {
$component = core_component::normalize_componentname($component);
return $this->get_messages(
fn ($message) => core_component::normalize_componentname($message->component) === $component,
);
}
/**
* Return all redirected messages for a given component and type.
*
* @param string $component Component name.
* @param string $type Message type.
* @return array List of messages.
*/
public function get_messages_by_component_and_type(string $component, string $type) {
return array_filter($this->get_messages_by_component($component), function($message) use ($type) {
return $message->eventtype == $type;
});
}
/**
* Return number of messages redirected to this sink.
* @return int