Skip to content

Commit 30eab80

Browse files
committed
Pass exmaples hello integation test.
1 parent a74a744 commit 30eab80

File tree

6 files changed

+53
-29
lines changed

6 files changed

+53
-29
lines changed

examples/hello/tests/integration.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@ use std::{env, path::Path};
33

44
#[test]
55
fn test_php() {
6-
dbg!(env!("CARGO_BIN_EXE_hello"));
7-
dbg!(env::current_exe());
8-
// test_php_scripts(
9-
// Path::new(env!("CARGO_MANIFEST_DIR"))
10-
// .join("..")
11-
// .join("..")
12-
// .join("target"),
13-
// env!("CARGO_PKG_NAME"),
14-
// &[Path::new(env!("CARGO_MANIFEST_DIR"))
15-
// .join("tests")
16-
// .join("php")
17-
// .join("test.php")],
18-
// );
6+
test_php_scripts(
7+
env!("CARGO_BIN_EXE_hello"),
8+
&[Path::new(env!("CARGO_MANIFEST_DIR"))
9+
.join("tests")
10+
.join("php")
11+
.join("test.php")],
12+
);
1913
}

examples/hello/tests/php/test.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44
ini_set("display_startup_errors", "On");
55
error_reporting(E_ALL);
66

7-
assert_eq(get_extension_funcs('hello'), ["say_hello"]);
8-
assert_eq(say_hello("world"), "Hello, world!");
7+
assert_eq(hello_say_hello("world"), "Hello, world!\n");
8+
9+
assert_eq(class_exists("PHPerException"), true);
10+
11+
try {
12+
hello_throw_exception();
13+
} catch (PHPerException $e) {
14+
assert_eq($e->getMessage(), "I am sorry");
15+
}
16+
17+
assert_eq(hello_get_all_ini(), [
18+
"hello.enable" => true,
19+
"hello.description" => "hello world.",
20+
]);
21+
22+
$foo = new FooClass();
23+
assert_eq($foo->getFoo(), 100);
24+
25+
$foo->setFoo("Hello");
26+
assert_eq($foo->getFoo(), "Hello");
927

1028
function assert_eq($left, $right) {
1129
if ($left !== $right) {

phper-test/src/lib.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@ use std::{
33
env, ffi::OsStr, fmt::Debug, fs::read_to_string, io::Write, path::Path, process::Command,
44
};
55
use tempfile::NamedTempFile;
6+
use std::ffi::OsString;
7+
use std::path::PathBuf;
8+
use std::error::Error;
69

710
pub fn test_php_scripts(
8-
target_dir: impl AsRef<Path>,
9-
lib_name: &str,
11+
exe_path: impl AsRef<Path>,
1012
scripts: &[impl AsRef<Path>],
1113
) {
1214
let context = php_context();
1315

14-
let lib_path = target_dir
15-
.as_ref()
16-
.join(if cfg!(debug_assertions) {
17-
"debug"
18-
} else {
19-
"release"
20-
})
21-
.join(format!("lib{}.so", lib_name));
16+
let lib_path = get_lib_path(exe_path);
2217

2318
let mut out_ini_temp_file = NamedTempFile::new().unwrap();
2419
let out_ini_file = out_ini_temp_file.as_file_mut();
@@ -117,3 +112,16 @@ fn execute_command<S: AsRef<OsStr> + Debug>(argv: &[S]) -> String {
117112
.stdout;
118113
String::from_utf8(output).unwrap().trim().to_owned()
119114
}
115+
116+
fn get_lib_path(exe_path: impl AsRef<Path>) -> PathBuf {
117+
let exe_path = exe_path.as_ref();
118+
let exe_stem = exe_path.file_stem().expect("failed to get current exe stem");
119+
let target_dir = exe_path.parent().expect("failed to get current exe directory");
120+
121+
let mut ext_name = OsString::new();
122+
ext_name.push("lib");
123+
ext_name.push(exe_stem);
124+
ext_name.push(".so");
125+
126+
target_dir.join(ext_name)
127+
}

phper/src/arrays.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ impl Array {
1818
Self { inner }
1919
}
2020

21+
pub fn as_ptr(&self) -> *const zend_array {
22+
self.inner.deref()
23+
}
24+
2125
pub fn insert(&mut self, key: impl AsRef<str>, value: &mut Val) {
2226
let key = key.as_ref();
2327
unsafe {
2428
zend_hash_str_update(
25-
&mut *self.inner,
29+
self.inner.deref_mut(),
2630
key.as_ptr().cast(),
2731
key.len(),
2832
value.as_mut(),

phper/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub trait Throwable: error::Error {
3939
fn code(&self) -> u64;
4040
}
4141

42-
pub(crate) const EXCEPTION_CLASS_NAME: &'static str = "\\Phper\\Exception\\ErrorException";
42+
pub(crate) const EXCEPTION_CLASS_NAME: &'static str = "PHPerException";
4343

4444
impl Throwable for Error {
4545
fn class_entity(&self) -> *const ClassEntity {

phper/src/values.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl ExecuteData {
3434
&mut *(ptr as *mut Self)
3535
}
3636

37-
pub fn as_mut(&mut self) -> *mut zend_execute_data {
37+
pub fn as_mut_ptr(&mut self) -> *mut zend_execute_data {
3838
&mut self.inner
3939
}
4040

@@ -213,7 +213,7 @@ impl SetVal for String {
213213
impl SetVal for Array {
214214
fn set_val(&self, val: &mut Val) {
215215
unsafe {
216-
phper_zval_arr(val.as_mut(), &self as *const _ as *mut _);
216+
phper_zval_arr(val.as_mut(), self.as_ptr() as *mut _);
217217
}
218218
}
219219
}

0 commit comments

Comments
 (0)