Skip to content

Commit 3c65232

Browse files
committed
feat: Add tests
1 parent 87e19d9 commit 3c65232

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

crates/macros/src/helpers.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@ pub fn get_docs(attrs: &[Attribute]) -> Result<Vec<String>> {
2424
})
2525
.collect::<Result<Vec<_>>>()
2626
}
27+
28+
pub trait CleanPhpAttr {
29+
fn clean_php(&mut self);
30+
}
31+
32+
impl CleanPhpAttr for Vec<Attribute> {
33+
fn clean_php(&mut self) {
34+
self.retain(|attr| !attr.path().is_ident("php"));
35+
}
36+
}

crates/macros/src/interface.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use darling::util::Flag;
2-
use darling::{FromAttributes, FromMeta, ToTokens};
1+
use darling::{FromAttributes};
32
use proc_macro2::TokenStream;
43
use quote::{format_ident, quote};
5-
use syn::{Attribute, Expr, Fields, ItemStruct, ItemTrait};
4+
use syn::{ItemTrait, TraitItem, TraitItemFn};
5+
use crate::helpers::CleanPhpAttr;
66

7-
use crate::helpers::get_docs;
87
use crate::parsing::{PhpRename, RenameRule};
98
use crate::prelude::*;
109

@@ -21,6 +20,20 @@ pub fn parser(mut input: ItemTrait) -> Result<TokenStream> {
2120

2221
let interface_name = format_ident!("PhpInterface{ident}");
2322
let name = attr.rename.rename(ident.to_string(), RenameRule::Pascal);
23+
input.attrs.clean_php();
24+
25+
let mut interface_methods: Vec<TraitItemFn> = Vec::new();
26+
for i in input.items.clone().into_iter() {
27+
match i {
28+
TraitItem::Fn(f) => {
29+
if f.default.is_some() {
30+
bail!("Interface could not have default impl");
31+
}
32+
interface_methods.push(f);
33+
}
34+
_ => {}
35+
}
36+
};
2437

2538
Ok(quote! {
2639
#input
@@ -66,7 +79,7 @@ pub fn parser(mut input: ItemTrait) -> Result<TokenStream> {
6679
&[]
6780
}
6881

69-
fn get_properties<'a>() -> std::collections::HashMap<&'static str, PropertyInfo<'a, Self>> {
82+
fn get_properties<'a>() -> std::collections::HashMap<&'static str, ::ext_php_rs::internal::property::PropertyInfo<'a, Self>> {
7083
HashMap::new()
7184
}
7285

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
4+
/* assert(interface_exists('ExtPhpRs\Interface\EmptyObjectInterface'), 'Interface not exist'); */
5+
6+
final readonly class InterfaceExampleUsage implements ExtPhpRs\Interface\EmptyObjectInterface
7+
{
8+
9+
}
10+
11+
/* $example = new InterfaceExampleUsage; */
12+
/* assert( */
13+
/* is_a($example, ExtPhpRs\Interface\EmptyObjectInterface::class), */
14+
/* \sprintf( */
15+
/* 'Class should be implements of interface: %s', */
16+
/* Test\TestInterface::class */
17+
/* ) */
18+
/* ); */
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use ext_php_rs::prelude::*;
2+
use std::collections::HashMap;
3+
4+
#[php_interface]
5+
#[php(name = "ExtPhpRs\\Interface\\EmptyObjectInterface")]
6+
pub trait EmptyObjectInterface { }
7+
8+
pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
9+
builder.interface::<PhpInterfaceEmptyObjectInterface>()
10+
}
11+
12+
#[cfg(test)]
13+
mod tests {
14+
#[test]
15+
fn interface_work() {
16+
assert!(crate::integration::test::run_php("interface/interface.php"));
17+
}
18+
}

tests/src/integration/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod object;
1717
pub mod string;
1818
pub mod types;
1919
pub mod variadic_args;
20+
pub mod interface;
2021

2122
#[cfg(test)]
2223
mod test {

0 commit comments

Comments
 (0)