Skip to content

Commit 5353153

Browse files
committed
test(macro): Add test for magic method call
1 parent 1f73d47 commit 5353153

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
// __call_static
36+
assert("Hello from static call 1 2 3" === MagicMethod::callStaticSomeMagic(1, 2, 3));
37+
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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,100 @@ pub fn test_class(string: String, number: i32) -> TestClass {
225225
}
226226
}
227227

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

0 commit comments

Comments
 (0)