1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Message sink.
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @category phpunit
|
|
|
22 |
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Message sink.
|
|
|
29 |
*
|
|
|
30 |
* @package core
|
|
|
31 |
* @category phpunit
|
|
|
32 |
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class phpunit_message_sink {
|
|
|
36 |
/** @var array of records from messages table */
|
|
|
37 |
protected $messages = array();
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Stop message redirection.
|
|
|
41 |
*
|
|
|
42 |
* Use if you do not want message redirected any more.
|
|
|
43 |
*/
|
|
|
44 |
public function close() {
|
|
|
45 |
phpunit_util::stop_message_redirection();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* To be called from phpunit_util only!
|
|
|
50 |
*
|
|
|
51 |
* @param stdClass $message record from messages table
|
|
|
52 |
*/
|
|
|
53 |
public function add_message($message) {
|
|
|
54 |
/* Number messages from 0. */
|
|
|
55 |
$this->messages[] = $message;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Returns all redirected messages.
|
|
|
60 |
*
|
|
|
61 |
* The instances are records from the messages table.
|
|
|
62 |
* The array indexes are numbered from 0 and the order is matching
|
|
|
63 |
* the creation of events.
|
|
|
64 |
*
|
|
|
65 |
* @param callable|null $filter Use to filter the messages.
|
|
|
66 |
* @return array
|
|
|
67 |
*/
|
|
|
68 |
public function get_messages(?callable $filter = null): array {
|
|
|
69 |
if ($filter) {
|
|
|
70 |
return array_filter($this->messages, $filter);
|
|
|
71 |
}
|
|
|
72 |
return $this->messages;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Return all redirected messages for a given component.
|
|
|
77 |
*
|
|
|
78 |
* @param string $component Component name.
|
|
|
79 |
* @return array List of messages.
|
|
|
80 |
*/
|
|
|
81 |
public function get_messages_by_component(string $component): array {
|
|
|
82 |
$component = core_component::normalize_componentname($component);
|
|
|
83 |
|
|
|
84 |
return $this->get_messages(
|
|
|
85 |
fn ($message) => core_component::normalize_componentname($message->component) === $component,
|
|
|
86 |
);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Return all redirected messages for a given component and type.
|
|
|
91 |
*
|
|
|
92 |
* @param string $component Component name.
|
|
|
93 |
* @param string $type Message type.
|
|
|
94 |
* @return array List of messages.
|
|
|
95 |
*/
|
|
|
96 |
public function get_messages_by_component_and_type(
|
|
|
97 |
string $component,
|
|
|
98 |
string $type,
|
|
|
99 |
): array {
|
|
|
100 |
return array_filter($this->get_messages_by_component($component), function($message) use ($type) {
|
|
|
101 |
return $message->eventtype == $type;
|
|
|
102 |
});
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Return number of messages redirected to this sink.
|
|
|
107 |
* @return int
|
|
|
108 |
*/
|
|
|
109 |
public function count() {
|
|
|
110 |
return count($this->messages);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Removes all previously stored messages.
|
|
|
115 |
*/
|
|
|
116 |
public function clear() {
|
|
|
117 |
$this->messages = array();
|
|
|
118 |
}
|
|
|
119 |
}
|