-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): add an example on how to test a module
- Loading branch information
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#![cfg_attr(windows, feature(abi_vectorcall))] | ||
extern crate ext_php_rs; | ||
|
||
#[cfg(feature = "embed")] | ||
use ext_php_rs::embed::{Embed}; | ||
use ext_php_rs::prelude::*; | ||
#[cfg(feature = "embed")] | ||
use ext_php_rs::ffi::zend_register_module_ex; | ||
|
||
|
||
#[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 | ||
} |