aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/event_dispatcher/event_dispatcher.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe/src/event_dispatcher/event_dispatcher.rs')
-rw-r--r--crates/shirabe/src/event_dispatcher/event_dispatcher.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/crates/shirabe/src/event_dispatcher/event_dispatcher.rs b/crates/shirabe/src/event_dispatcher/event_dispatcher.rs
index 3ef27f8..2441be6 100644
--- a/crates/shirabe/src/event_dispatcher/event_dispatcher.rs
+++ b/crates/shirabe/src/event_dispatcher/event_dispatcher.rs
@@ -70,6 +70,19 @@ pub struct EventDispatcher {
skip_scripts: Vec<String>,
previous_hash: Option<String>,
previous_listeners: IndexMap<String, bool>,
+ /// For testing only. Mirrors PHPUnit's `getMockBuilder(EventDispatcher)->onlyMethods(['getListeners'])`:
+ /// when set, `get_listeners` returns this closure's result verbatim instead of resolving
+ /// registered listeners and package scripts.
+ get_listeners_override: Option<GetListenersOverride>,
+}
+
+/// For testing only. Holds a closure standing in for an overridden `getListeners` method.
+pub struct GetListenersOverride(pub Box<dyn Fn(&dyn EventInterface) -> Vec<Callable>>);
+
+impl std::fmt::Debug for GetListenersOverride {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str("GetListenersOverride(..)")
+ }
}
impl EventDispatcher {
@@ -101,9 +114,20 @@ impl EventDispatcher {
skip_scripts,
previous_hash: None,
previous_listeners: IndexMap::new(),
+ get_listeners_override: None,
}
}
+ /// For testing only. Installs a closure that overrides `get_listeners`, mirroring
+ /// PHPUnit's `onlyMethods(['getListeners'])->will($this->returnValue(...))` /
+ /// `->will($this->returnCallback(...))`.
+ pub fn __set_get_listeners_override(
+ &mut self,
+ callback: Box<dyn Fn(&dyn EventInterface) -> Vec<Callable>>,
+ ) {
+ self.get_listeners_override = Some(GetListenersOverride(callback));
+ }
+
/// Set whether script handlers are active or not
pub fn set_run_scripts(&mut self, run_scripts: bool) -> &mut Self {
self.run_scripts = run_scripts;
@@ -923,6 +947,12 @@ impl EventDispatcher {
/// Retrieves all listeners for a given event
fn get_listeners(&mut self, event: &dyn EventInterface) -> Vec<Callable> {
+ // For testing only: a test may override this method, mirroring PHPUnit's
+ // `onlyMethods(['getListeners'])`.
+ if let Some(override_cb) = &self.get_listeners_override {
+ return override_cb.0(event);
+ }
+
let script_listeners: Vec<Callable> = if self.run_scripts {
self.get_script_listeners(event)
} else {