Skip to content

Commit 96c97be

Browse files
committed
test(macro): Add test for magic method call
1 parent 360ae34 commit 96c97be

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
$magicMethod = new MagicMethod();
4+
5+
// __set
6+
$magicMethod->count = 10;
7+
// __get
8+
assert(10 === $magicMethod->count);
9+
assert(null === $magicMethod->test);
10+
11+
//__isset
12+
assert(true === isset($magicMethod->count));
13+
assert(false === isset($magicMethod->noCount));
14+
15+
// __unset
16+
unset($magicMethod->count);
17+
assert(0 === $magicMethod->count);
18+
19+
// __toString
20+
assert("0" === $magicMethod->__toString());
21+
assert("0" === (string) $magicMethod);
22+
23+
// __invoke
24+
assert(34 === $magicMethod(34));
25+
26+
// __debugInfo
27+
$debug = print_r($magicMethod, true);
28+
$expectedDebug = "MagicMethod Object\n(\n [count] => 0\n)\n";
29+
assert($expectedDebug === $debug);
30+
31+
// __call
32+
assert("Hello" === $magicMethod->callMagicMethod(1, 2, 3));
33+
assert(null === $magicMethod->callUndefinedMagicMethod());
34+
35+
file_put_contents('/tmp/test.txt', MagicMethod::callStaticSomeMagic(1, 2, 3));
36+
// __call_static
37+
assert("Hello from static call 1 2 3" === MagicMethod::callStaticSomeMagic(1, 2, 3));
38+
assert(null === MagicMethod::callUndefinedStaticSomeMagic());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn magic_method() {
3+
assert!(crate::integration::run_php("magic_method.php"));
4+
}

tests/src/lib.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use ext_php_rs::{
33
binary::Binary,
44
boxed::ZBox,
5+
convert::IntoZval,
56
prelude::*,
67
types::{ArrayKey, ZendHashTable, ZendObject, Zval},
78
zend::ProcessGlobals,
@@ -210,6 +211,10 @@ impl TestClass {
210211
pub fn set_number(&mut self, number: i32) {
211212
self.number = number;
212213
}
214+
215+
pub fn static_call(name: String) -> String {
216+
format!("Hello {name}")
217+
}
213218
}
214219

215220
#[php_function]
@@ -221,10 +226,100 @@ pub fn test_class(string: String, number: i32) -> TestClass {
221226
}
222227
}
223228

229+
#[php_class]
230+
pub struct MagicMethod(i64);
231+
232+
#[php_impl]
233+
impl MagicMethod {
234+
pub fn __construct() -> Self {
235+
Self(0)
236+
}
237+
238+
pub fn __destruct(&self) {}
239+
240+
pub fn __call(&self, name: String, _arguments: HashMap<String, &Zval>) -> Zval {
241+
let mut z = Zval::new();
242+
if name == "callMagicMethod" {
243+
let s = "Hello".to_string();
244+
245+
let _ = z.set_string(s.as_str(), false);
246+
z
247+
} else {
248+
z.set_null();
249+
z
250+
}
251+
}
252+
253+
pub fn __call_static(name: String, arguments: HashMap<String, &Zval>) -> Zval {
254+
let mut zval = Zval::new();
255+
if name == "callStaticSomeMagic" {
256+
let concat_args = format!(
257+
"Hello from static call {}",
258+
arguments
259+
.iter()
260+
.filter(|(_, v)| v.is_long())
261+
.map(|(_, s)| s.long().unwrap().to_string())
262+
.collect::<Vec<_>>()
263+
.join(" ")
264+
);
265+
266+
let _ = zval.set_string(&concat_args, false);
267+
zval
268+
} else {
269+
zval.set_null();
270+
zval
271+
}
272+
}
273+
274+
pub fn __get(&self, name: String) -> Zval {
275+
let mut v = Zval::new();
276+
v.set_null();
277+
if name == "count" {
278+
v.set_long(self.0);
279+
}
280+
281+
v
282+
}
283+
284+
pub fn __set(&mut self, prop_name: String, val: &Zval) {
285+
if val.is_long() && prop_name == "count" {
286+
self.0 = val.long().unwrap()
287+
}
288+
}
289+
290+
pub fn __isset(&self, prop_name: String) -> bool {
291+
"count" == prop_name
292+
}
293+
294+
pub fn __unset(&mut self, prop_name: String) {
295+
if prop_name == "count" {
296+
self.0 = 0;
297+
}
298+
}
299+
300+
pub fn __to_string(&self) -> String {
301+
self.0.to_string()
302+
}
303+
304+
pub fn __invoke(&self, n: i64) -> i64 {
305+
self.0 + n
306+
}
307+
308+
pub fn __debug_info(&self) -> HashMap<String, Zval> {
309+
let mut h: HashMap<String, Zval> = HashMap::new();
310+
let mut z = Zval::new();
311+
z.set_long(self.0);
312+
h.insert("count".to_string(), z);
313+
314+
h
315+
}
316+
}
317+
224318
#[php_module]
225319
pub fn build_module(module: ModuleBuilder) -> ModuleBuilder {
226320
module
227321
.class::<TestClass>()
322+
.class::<MagicMethod>()
228323
.function(wrap_function!(test_str))
229324
.function(wrap_function!(test_string))
230325
.function(wrap_function!(test_bool))
@@ -317,6 +412,7 @@ mod integration {
317412
mod closure;
318413
mod globals;
319414
mod iterator;
415+
mod magic_method;
320416
mod nullable;
321417
mod number;
322418
mod object;

0 commit comments

Comments
 (0)