feat(test): add an example on how to test a module (#276)

This commit is contained in:
Joel Wurtz 2023-10-23 11:42:01 +02:00 committed by GitHub
parent aa88f5af1e
commit dddc07f587
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -260,5 +260,6 @@ bind! {
zend_eval_string, zend_eval_string,
zend_file_handle, zend_file_handle,
zend_stream_init_filename, zend_stream_init_filename,
php_execute_script php_execute_script,
zend_register_module_ex
} }

44
tests/module.rs Normal file
View File

@ -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
}