Skip to content

Commit 07dbe47

Browse files
committed
feat(test): add an example on how to test a module
1 parent aa88f5a commit 07dbe47

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

allowed_bindings.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,6 @@ bind! {
260260
zend_eval_string,
261261
zend_file_handle,
262262
zend_stream_init_filename,
263-
php_execute_script
263+
php_execute_script,
264+
zend_register_module_ex
264265
}

tests/module.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#![cfg_attr(windows, feature(abi_vectorcall))]
2+
extern crate ext_php_rs;
3+
4+
#[cfg(feature = "embed")]
5+
use ext_php_rs::embed::{Embed};
6+
use ext_php_rs::prelude::*;
7+
#[cfg(feature = "embed")]
8+
use ext_php_rs::ffi::zend_register_module_ex;
9+
10+
11+
#[test]
12+
#[cfg(feature = "embed")]
13+
fn test_module() {
14+
Embed::run(|| {
15+
// Allow to load the module
16+
unsafe { zend_register_module_ex(get_module()) };
17+
18+
let result = Embed::eval("$foo = hello_world('foo');");
19+
20+
assert!(result.is_ok());
21+
22+
let zval = result.unwrap();
23+
24+
assert!(zval.is_string());
25+
26+
let string = zval.string().unwrap();
27+
28+
assert_eq!(string.to_string(), "Hello, foo!");
29+
});
30+
}
31+
32+
/// Gives you a nice greeting!
33+
///
34+
/// @param string $name Your name.
35+
///
36+
/// @return string Nice greeting!
37+
#[php_function]
38+
pub fn hello_world(name: String) -> String {
39+
format!("Hello, {}!", name)
40+
}
41+
42+
#[php_module]
43+
pub fn module(module: ModuleBuilder) -> ModuleBuilder {
44+
45+
module
46+
}

0 commit comments

Comments
 (0)