diff options
Diffstat (limited to 'crates/shirabe/tests/common/php_worker.rs')
| -rw-r--r-- | crates/shirabe/tests/common/php_worker.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/crates/shirabe/tests/common/php_worker.rs b/crates/shirabe/tests/common/php_worker.rs new file mode 100644 index 00000000..4be064e4 --- /dev/null +++ b/crates/shirabe/tests/common/php_worker.rs @@ -0,0 +1,55 @@ +//! Shared access to the PHP worker for integration tests. +//! +//! Included into an integration-test binary via +//! `#[path = "../common/php_worker.rs"] mod php_worker;`. +#![allow(dead_code)] + +use shirabe_php_rpc::PluginValue; +use shirabe_symfony_process::PhpExecutableFinder; + +/// Whether a PHP binary is available. Without one the worker cannot start, so tests that need it +/// return early instead of failing. +pub fn php_runtime_available() -> bool { + PhpExecutableFinder::new().find(false).is_some() +} + +/// All tests in one binary share the single PHP worker, whose loaded-class table and class statics +/// persist across tests just like PHPUnit's single-process runs. Interleaving two tests would let +/// one test's state race the other's, so the worker-touching tests run serialized. +static PHP_WORKER_TESTS: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +pub fn lock_php_worker() -> std::sync::MutexGuard<'static, ()> { + PHP_WORKER_TESTS + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) +} + +/// Requires the Composer PHP runtime (`composer/vendor/autoload.php`) into the worker, which is +/// what makes the real `Composer\` classes autoloadable there. +pub fn load_composer_php_runtime() { + shirabe::event_dispatcher::EventDispatcher::__ensure_composer_php_runtime().unwrap(); +} + +/// Calls a static method in the worker, panicking on either failure lane. +pub fn php_call_static(class: &str, method: &str, args: Vec<PluginValue>) -> PluginValue { + shirabe_php_rpc::call_static_method(class, method, args, None) + .unwrap_or_else(|error| panic!("{class}::{method} request failed: {error}")) + .unwrap_or_else(|throw| { + panic!( + "{class}::{method} threw {}: {}", + throw.exception_class, throw.message + ) + }) +} + +/// Runs a PHP snippet in the worker and returns its `return` value. +pub fn php_eval(code: &str) -> PluginValue { + shirabe_php_rpc::call_function("__shirabe_eval", vec![PluginValue::string(code)]) + .expect("eval request failed") + .expect("eval threw") +} + +/// Quotes a string as a PHP single-quoted literal for a generated snippet. +pub fn php_single_quote(value: &str) -> String { + format!("'{}'", value.replace('\\', "\\\\").replace('\'', "\\'")) +} |
