Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions tests/src/integration/array.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

require('_utils.php');

// Tests sequential arrays
$array = test_array(['a', 'b', 'c', 'd']);
unset($array[2]);
Expand Down
27 changes: 27 additions & 0 deletions tests/src/integration/array/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::collections::HashMap;

use ext_php_rs::{php_function, prelude::ModuleBuilder, wrap_function};

#[php_function]
pub fn test_array(a: Vec<String>) -> Vec<String> {
a
}

#[php_function]
pub fn test_array_assoc(a: HashMap<String, String>) -> HashMap<String, String> {
a
}

pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
builder
.function(wrap_function!(test_array))
.function(wrap_function!(test_array_assoc))
}

#[cfg(test)]
mod tests {
#[test]
fn array_works() {
assert!(crate::integration::test::run_php("array/array.php"));
}
}
4 changes: 0 additions & 4 deletions tests/src/integration/binary.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

require('_utils.php');

$bin = test_binary(pack('L*', 1, 2, 3, 4, 5));
$result = unpack('L*', $bin);

Expand All @@ -10,4 +8,4 @@
assert(in_array(2, $result));
assert(in_array(3, $result));
assert(in_array(4, $result));
assert(in_array(5, $result));
assert(in_array(5, $result));
18 changes: 18 additions & 0 deletions tests/src/integration/binary/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use ext_php_rs::{binary::Binary, prelude::*};

#[php_function]
pub fn test_binary(a: Binary<u32>) -> Binary<u32> {
a
}

pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
builder.function(wrap_function!(test_binary))
}

#[cfg(test)]
mod tests {
#[test]
fn binary_works() {
assert!(crate::integration::test::run_php("binary/binary.php"));
}
}
4 changes: 0 additions & 4 deletions tests/src/integration/bool.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

require('_utils.php');

assert(test_bool(true) === true);
assert(test_bool(false) === false);
18 changes: 18 additions & 0 deletions tests/src/integration/bool/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use ext_php_rs::prelude::*;

#[php_function]
pub fn test_bool(a: bool) -> bool {
a
}

pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
builder.function(wrap_function!(test_bool))
}

#[cfg(test)]
mod tests {
#[test]
fn bool_works() {
assert!(crate::integration::test::run_php("bool/bool.php"));
}
}
4 changes: 0 additions & 4 deletions tests/src/integration/callable.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<?php

require('_utils.php');

assert(test_callable(fn (string $a) => $a, 'test') === 'test');
18 changes: 18 additions & 0 deletions tests/src/integration/callable/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use ext_php_rs::{prelude::*, types::Zval};

#[php_function]
pub fn test_callable(call: ZendCallable, a: String) -> Zval {
call.try_call(vec![&a]).expect("Failed to call function")
}

pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
builder.function(wrap_function!(test_callable))
}

#[cfg(test)]
mod tests {
#[test]
fn callable_works() {
assert!(crate::integration::test::run_php("callable/callable.php"));
}
}
4 changes: 0 additions & 4 deletions tests/src/integration/class.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

require('_utils.php');

// Tests constructor
$class = test_class('lorem ipsum', 2022);
assert($class instanceof TestClass);
Expand Down
59 changes: 59 additions & 0 deletions tests/src/integration/class/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use ext_php_rs::prelude::*;

#[php_class]
pub struct TestClass {
string: String,
number: i32,
#[php(prop)]
boolean: bool,
}

#[php_impl]
impl TestClass {
#[php(getter)]
pub fn get_string(&self) -> String {
self.string.to_string()
}

#[php(setter)]
pub fn set_string(&mut self, string: String) {
self.string = string;
}

#[php(getter)]
pub fn get_number(&self) -> i32 {
self.number
}

#[php(setter)]
pub fn set_number(&mut self, number: i32) {
self.number = number;
}

pub fn static_call(name: String) -> String {
format!("Hello {name}")
}
}

#[php_function]
pub fn test_class(string: String, number: i32) -> TestClass {
TestClass {
string,
number,
boolean: true,
}
}

pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
builder
.class::<TestClass>()
.function(wrap_function!(test_class))
}

#[cfg(test)]
mod tests {
#[test]
fn class_works() {
assert!(crate::integration::test::run_php("class/class.php"));
}
}
4 changes: 0 additions & 4 deletions tests/src/integration/closure.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require('_utils.php');
require(__DIR__ . '/../_utils.php');

$v = test_closure();

Expand All @@ -19,4 +19,4 @@ function take(\stdClass $rs): void { }
take($closure);
} catch (\TypeError $e) {
assert(str_starts_with($e->getMessage(), 'take(): Argument #1 ($rs) must be of type stdClass, RustClosure given, called in '));
}
}
25 changes: 25 additions & 0 deletions tests/src/integration/closure/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use ext_php_rs::prelude::*;

#[php_function]
pub fn test_closure() -> Closure {
Closure::wrap(Box::new(|a| a) as Box<dyn Fn(String) -> String>)
}

#[php_function]
pub fn test_closure_once(a: String) -> Closure {
Closure::wrap_once(Box::new(move || a) as Box<dyn FnOnce() -> String>)
}

pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
builder
.function(wrap_function!(test_closure))
.function(wrap_function!(test_closure_once))
}

#[cfg(test)]
mod tests {
#[test]
fn closure_works() {
assert!(crate::integration::test::run_php("closure/closure.php"));
}
}
4 changes: 0 additions & 4 deletions tests/src/integration/globals.rs

This file was deleted.

52 changes: 52 additions & 0 deletions tests/src/integration/globals/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use ext_php_rs::{boxed::ZBox, prelude::*, types::ZendHashTable, zend::ProcessGlobals};

#[php_function]
pub fn test_globals_http_get() -> ZBox<ZendHashTable> {
ProcessGlobals::get().http_get_vars().to_owned()
}

#[php_function]
pub fn test_globals_http_post() -> ZBox<ZendHashTable> {
ProcessGlobals::get().http_post_vars().to_owned()
}

#[php_function]
pub fn test_globals_http_cookie() -> ZBox<ZendHashTable> {
ProcessGlobals::get().http_cookie_vars().to_owned()
}

#[php_function]
pub fn test_globals_http_server() -> ZBox<ZendHashTable> {
ProcessGlobals::get().http_server_vars().unwrap().to_owned()
}

#[php_function]
pub fn test_globals_http_request() -> ZBox<ZendHashTable> {
ProcessGlobals::get()
.http_request_vars()
.unwrap()
.to_owned()
}

#[php_function]
pub fn test_globals_http_files() -> ZBox<ZendHashTable> {
ProcessGlobals::get().http_files_vars().to_owned()
}

pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
builder
.function(wrap_function!(test_globals_http_get))
.function(wrap_function!(test_globals_http_post))
.function(wrap_function!(test_globals_http_cookie))
.function(wrap_function!(test_globals_http_server))
.function(wrap_function!(test_globals_http_request))
.function(wrap_function!(test_globals_http_files))
}

#[cfg(test)]
mod tests {
#[test]
fn globals_works() {
assert!(crate::integration::test::run_php("globals/globals.php"));
}
}
4 changes: 0 additions & 4 deletions tests/src/integration/iterator.rs

This file was deleted.

71 changes: 71 additions & 0 deletions tests/src/integration/iterator/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use ext_php_rs::{
prelude::*,
types::{ArrayKey, ZendHashTable, Zval},
};

#[php_function]
pub fn iter_next(ht: &ZendHashTable) -> Vec<Zval> {
ht.iter()
.flat_map(|(k, v)| [key_to_zval(k), v.shallow_clone()])
.collect()
}

#[php_function]
pub fn iter_back(ht: &ZendHashTable) -> Vec<Zval> {
ht.iter()
.rev()
.flat_map(|(k, v)| [key_to_zval(k), v.shallow_clone()])
.collect()
}

#[php_function]
pub fn iter_next_back(ht: &ZendHashTable, modulus: usize) -> Vec<Option<Zval>> {
let mut result = Vec::with_capacity(ht.len());
let mut iter = ht.iter();

for i in 0..ht.len() + modulus {
let entry = if i % modulus == 0 {
iter.next_back()
} else {
iter.next()
};

if let Some((k, v)) = entry {
result.push(Some(key_to_zval(k)));
result.push(Some(v.shallow_clone()));
} else {
result.push(None);
}
}

result
}

fn key_to_zval(key: ArrayKey) -> Zval {
match key {
ArrayKey::String(s) => {
let mut zval = Zval::new();
let _ = zval.set_string(s.as_str(), false);
zval
}
ArrayKey::Long(l) => {
let mut zval = Zval::new();
zval.set_long(l);
zval
}
}
}
pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
builder
.function(wrap_function!(iter_next))
.function(wrap_function!(iter_back))
.function(wrap_function!(iter_next_back))
}

#[cfg(test)]
mod tests {
#[test]
fn iterator_works() {
assert!(crate::integration::test::run_php("iterator/iterator.php"));
}
}
4 changes: 0 additions & 4 deletions tests/src/integration/magic_method.rs

This file was deleted.

Loading