aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe/src')
-rw-r--r--crates/shirabe/src/event_dispatcher/event_dispatcher.rs31
-rw-r--r--crates/shirabe/src/util/auth_helper.rs33
2 files changed, 64 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(),
diff --git a/crates/shirabe/src/util/auth_helper.rs b/crates/shirabe/src/util/auth_helper.rs
index c779a1bb..a323c37e 100644
--- a/crates/shirabe/src/util/auth_helper.rs
+++ b/crates/shirabe/src/util/auth_helper.rs
@@ -436,6 +436,39 @@ impl AuthHelper {
})
}
+ /// @param string[] $headers
+ ///
+ /// @return string[] updated headers array
+ pub fn add_authentication_header(
+ &mut self,
+ headers: Vec<String>,
+ origin: &str,
+ url: &str,
+ ) -> anyhow::Result<Vec<String>> {
+ shirabe_php_shim::trigger_error(
+ "AuthHelper::addAuthenticationHeader is deprecated since Composer 2.9 use addAuthenticationOptions instead.",
+ shirabe_php_shim::E_USER_DEPRECATED,
+ );
+
+ let mut http: IndexMap<String, PhpMixed> = IndexMap::new();
+ http.insert(
+ "header".to_string(),
+ PhpMixed::List(headers.into_iter().map(PhpMixed::String).collect()),
+ );
+ let mut options: IndexMap<String, PhpMixed> = IndexMap::new();
+ options.insert("http".to_string(), PhpMixed::Array(http));
+ let options = self.add_authentication_options(options, origin, url)?;
+
+ Ok(options["http"]
+ .as_array()
+ .and_then(|http| http.get("header"))
+ .and_then(|header| header.as_list())
+ .expect("addAuthenticationOptions always leaves http.header a list")
+ .iter()
+ .map(|v| v.as_string().unwrap_or("").to_string())
+ .collect())
+ }
+
/// @return array<string, mixed> updated options
pub fn add_authentication_options(
&mut self,