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.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/crates/shirabe/src/event_dispatcher/event_dispatcher.rs b/crates/shirabe/src/event_dispatcher/event_dispatcher.rs
index 4aff50c9..5043610b 100644
--- a/crates/shirabe/src/event_dispatcher/event_dispatcher.rs
+++ b/crates/shirabe/src/event_dispatcher/event_dispatcher.rs
@@ -101,6 +101,10 @@ pub struct EventDispatcher {
/// 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. Mirrors PHPUnit's `getMockBuilder(EventDispatcher)->onlyMethods(['dispatchScript'])`:
+ /// when set, `dispatch_script` returns this closure's result instead of building and
+ /// dispatching a script event.
+ dispatch_script_override: Option<DispatchScriptOverride>,
}
/// For testing only. Holds a closure standing in for an overridden `getListeners` method.
@@ -112,6 +116,17 @@ impl std::fmt::Debug for GetListenersOverride {
}
}
+/// For testing only. Holds a closure standing in for an overridden `dispatchScript` method.
+pub struct DispatchScriptOverride(
+ pub Box<dyn Fn(&str, bool, &[String], &IndexMap<String, PhpMixed>) -> anyhow::Result<i64>>,
+);
+
+impl std::fmt::Debug for DispatchScriptOverride {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str("DispatchScriptOverride(..)")
+ }
+}
+
impl EventDispatcher {
pub fn new(
composer: PartialComposerWeakHandle,
@@ -142,6 +157,7 @@ impl EventDispatcher {
previous_hash: None,
previous_listeners: IndexMap::new(),
get_listeners_override: None,
+ dispatch_script_override: None,
}
}
@@ -155,6 +171,17 @@ impl EventDispatcher {
self.get_listeners_override = Some(GetListenersOverride(callback));
}
+ /// For testing only. Installs a closure that overrides `dispatch_script`, mirroring PHPUnit's
+ /// `onlyMethods(['dispatchScript'])->willReturnCallback(...)`.
+ pub fn __set_dispatch_script_override(
+ &mut self,
+ callback: Box<
+ dyn Fn(&str, bool, &[String], &IndexMap<String, PhpMixed>) -> anyhow::Result<i64>,
+ >,
+ ) {
+ self.dispatch_script_override = Some(DispatchScriptOverride(callback));
+ }
+
/// For testing only. Exposes the protected `getPhpExecCommand`, mirroring the PHP tests'
/// `new \ReflectionMethod($dispatcher, 'getPhpExecCommand')`.
pub fn __get_php_exec_command(&self) -> anyhow::Result<String> {
@@ -197,6 +224,10 @@ impl EventDispatcher {
additional_args: Vec<String>,
flags: IndexMap<String, PhpMixed>,
) -> anyhow::Result<i64> {
+ if let Some(over) = &self.dispatch_script_override {
+ return (over.0)(event_name, dev_mode, &additional_args, &flags);
+ }
+
let composer = self.composer();
assert!(
composer.is_full(),