From dddc07f587cd5d40f3b3ff64b9a59fba8299d953 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Mon, 23 Oct 2023 11:42:01 +0200 Subject: [PATCH] feat(test): add an example on how to test a module (#276) --- allowed_bindings.rs | 3 ++- tests/module.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/module.rs diff --git a/allowed_bindings.rs b/allowed_bindings.rs index 49b15856d8..4a5dbfc4d7 100644 --- a/allowed_bindings.rs +++ b/allowed_bindings.rs @@ -260,5 +260,6 @@ bind! { zend_eval_string, zend_file_handle, zend_stream_init_filename, - php_execute_script + php_execute_script, + zend_register_module_ex } diff --git a/tests/module.rs b/tests/module.rs new file mode 100644 index 0000000000..f1ef22b852 --- /dev/null +++ b/tests/module.rs @@ -0,0 +1,44 @@ +#![cfg_attr(windows, feature(abi_vectorcall))] +extern crate ext_php_rs; + +#[cfg(feature = "embed")] +use ext_php_rs::embed::Embed; +#[cfg(feature = "embed")] +use ext_php_rs::ffi::zend_register_module_ex; +use ext_php_rs::prelude::*; + +#[test] +#[cfg(feature = "embed")] +fn test_module() { + Embed::run(|| { + // Allow to load the module + unsafe { zend_register_module_ex(get_module()) }; + + let result = Embed::eval("$foo = hello_world('foo');"); + + assert!(result.is_ok()); + + let zval = result.unwrap(); + + assert!(zval.is_string()); + + let string = zval.string().unwrap(); + + assert_eq!(string.to_string(), "Hello, foo!"); + }); +} + +/// Gives you a nice greeting! +/// +/// @param string $name Your name. +/// +/// @return string Nice greeting! +#[php_function] +pub fn hello_world(name: String) -> String { + format!("Hello, {}!", name) +} + +#[php_module] +pub fn module(module: ModuleBuilder) -> ModuleBuilder { + module +}