Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a4c61df
feat: integration tests
TorstenDittmann Jan 13, 2022
216918a
fix: some leftovers in tests
TorstenDittmann Jan 13, 2022
ad22d0b
chore: remove todos
TorstenDittmann Jan 13, 2022
09bf50d
fix: remove unnecessary lines
TorstenDittmann Jan 13, 2022
2b9ca2a
chore: fix some styling
TorstenDittmann Jan 14, 2022
9e15b1d
test: assert build status code
TorstenDittmann Jan 14, 2022
04d81a7
test: remove unnecessary assignment
TorstenDittmann Jan 14, 2022
b363c1a
lint: replace assert_eq with assert
TorstenDittmann Jan 14, 2022
ff14190
tests: fix condition
TorstenDittmann Jan 14, 2022
df838e9
ci: use os from matrix
TorstenDittmann Jan 15, 2022
6d80fb7
tests: restructure
TorstenDittmann Jan 16, 2022
b283f03
chore: run cargo fmt
TorstenDittmann Jan 16, 2022
aefed6e
cI: revert change for now
TorstenDittmann Jan 16, 2022
854f1d6
tests: show more informations
TorstenDittmann Jan 16, 2022
eb5a768
tests: show stdout on error
TorstenDittmann Jan 16, 2022
da50c82
tests: even more verbose informations
TorstenDittmann Jan 16, 2022
23a8df0
tests: revert to non-release extension for tests
TorstenDittmann Jan 16, 2022
73c00bf
tests: test closure
TorstenDittmann Jan 16, 2022
053e296
tests: enable closures again
TorstenDittmann Jan 16, 2022
eb4c9dd
tests: disable closure once test
TorstenDittmann Jan 16, 2022
c438b9c
Merge branch 'master' of https://github.com/davidcole1340/ext-php-rs …
TorstenDittmann Apr 3, 2022
411e9a0
fix: enable closure once test again
TorstenDittmann Apr 3, 2022
049f776
Merge remote-tracking branch 'origin/master' into feat-integration-tests
danog Nov 24, 2023
b8348fc
Fixup
danog Nov 24, 2023
4a51e5d
Fixup
danog Nov 24, 2023
e7f4f48
Fixup
danog Nov 24, 2023
af8e7d0
Fix argument type allocation
danog Nov 24, 2023
9547daf
Fix use after free
danog Nov 24, 2023
1749f07
Fixup
danog Nov 24, 2023
332b5a9
Fix tests
danog Nov 24, 2023
6ae0ac4
Postpone for later
danog Nov 24, 2023
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
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ closure = []
embed = []

[workspace]
members = ["crates/macros", "crates/cli"]
members = [
"crates/macros",
"crates/cli",
"tests"
]

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docs"]
4 changes: 2 additions & 2 deletions guide/src/types/binary.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn test_binary(input: Binary<u32>) -> Binary<u32> {
```php
<?php

$data = pack('*L', [1, 2, 3, 4, 5]);
$output = unpack('*L', test_binary($data));
$data = pack('L*', 1, 2, 3, 4, 5);
$output = unpack('L*', test_binary($data));
var_dump($output); // array(5) { [0] => 5, [1] => 4, [2] => 3, [3] => 2, [4] => 1 }
```
6 changes: 4 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'a> Arg<'a> {
{
self.zval
.as_mut()
.and_then(|zv| T::from_zval_mut(zv))
.and_then(|zv| T::from_zval_mut(zv.dereference_mut()))
.ok_or(self)
}

Expand All @@ -98,7 +98,9 @@ impl<'a> Arg<'a> {
where
T: FromZvalMut<'a>,
{
self.zval.as_mut().and_then(|zv| T::from_zval_mut(zv))
self.zval
.as_mut()
.and_then(|zv| T::from_zval_mut(zv.dereference_mut()))
}

/// Attempts to return a reference to the arguments internal Zval.
Expand Down
1 change: 1 addition & 0 deletions src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class_derives!(Closure);
///
/// This trait is automatically implemented on functions with up to 8
/// parameters.
#[allow(clippy::missing_safety_doc)]
pub unsafe trait PhpClosure {
/// Invokes the closure.
fn invoke<'a>(&'a mut self, parser: ArgParser<'a, '_>, ret: &mut Zval);
Expand Down
19 changes: 19 additions & 0 deletions src/types/zval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ impl Zval {
}
}

/// Dereference the zval, if it is a reference.
pub fn dereference(&self) -> &Self {
return self.reference().or_else(|| self.indirect()).unwrap_or(self);
}

/// Dereference the zval mutable, if it is a reference.
pub fn dereference_mut(&mut self) -> &mut Self {
// TODO: probably more ZTS work is needed here
if self.is_reference() {
#[allow(clippy::unwrap_used)]
return self.reference_mut().unwrap();
}
if self.is_indirect() {
#[allow(clippy::unwrap_used)]
return self.indirect_mut().unwrap();
}
self
}

/// Returns the value of the zval if it is a long.
pub fn long(&self) -> Option<ZendLong> {
if self.is_long() {
Expand Down
8 changes: 3 additions & 5 deletions src/zend/_type.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::{
ffi::{c_void, CString},
ptr,
};
use std::{ffi::c_void, ptr};

use crate::{
ffi::{
zend_type, IS_MIXED, MAY_BE_ANY, MAY_BE_BOOL, _IS_BOOL, _ZEND_IS_VARIADIC_BIT,
_ZEND_SEND_MODE_SHIFT, _ZEND_TYPE_NAME_BIT, _ZEND_TYPE_NULLABLE_BIT,
},
flags::DataType,
types::ZendStr,
};

/// Internal Zend type.
Expand Down Expand Up @@ -82,7 +80,7 @@ impl ZendType {
allow_null: bool,
) -> Option<Self> {
Some(Self {
ptr: CString::new(class_name).ok()?.into_raw() as *mut c_void,
ptr: ZendStr::new(class_name, true).into_raw().as_ptr() as *mut c_void,
type_mask: _ZEND_TYPE_NAME_BIT
| (if allow_null {
_ZEND_TYPE_NULLABLE_BIT
Expand Down
7 changes: 4 additions & 3 deletions src/zend/try_catch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ pub fn try_catch<R, F: FnMut() -> R + RefUnwindSafe>(func: F) -> Result<R, Catch

/// PHP propose a try catch mechanism in C using setjmp and longjmp (bailout)
/// It store the arg of setjmp into the bailout field of the global executor
/// If a bailout is triggered, the executor will jump to the setjmp and restore the previous setjmp
/// If a bailout is triggered, the executor will jump to the setjmp and restore
/// the previous setjmp
///
/// try_catch_first allow to use this mechanism
///
/// This functions differs from ['try_catch'] as it also initialize the bailout mechanism
/// for the first time
/// This functions differs from ['try_catch'] as it also initialize the bailout
/// mechanism for the first time
///
/// # Returns
///
Expand Down
12 changes: 12 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "tests"
version = "0.0.0"
edition = "2021"
publish = false
license = "MIT OR Apache-2.0"

[dependencies]
ext-php-rs = { path = "../", features = ["closure"] }

[lib]
crate-type = ["cdylib"]
11 changes: 11 additions & 0 deletions tests/src/integration/_utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

function assert_exception_thrown(callable $callback): void
{
try {
call_user_func($callback);
} catch (\Throwable $th) {
return;
}
throw new Exception("Excption was not thrown", 255);
}
26 changes: 26 additions & 0 deletions tests/src/integration/array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

require('_utils.php');

// Tests sequential arrays
$array = test_array(['a', 'b', 'c']);

assert(is_array($array));
assert(count($array) === 3);
assert(in_array('a', $array));
assert(in_array('b', $array));
assert(in_array('c', $array));

// Tests associative arrays
$assoc = test_array_assoc([
'a' => '1',
'b' => '2',
'c' => '3'
]);

assert(array_key_exists('a', $assoc));
assert(array_key_exists('b', $assoc));
assert(array_key_exists('c', $assoc));
assert(in_array('1', $assoc));
assert(in_array('2', $assoc));
assert(in_array('3', $assoc));
4 changes: 4 additions & 0 deletions tests/src/integration/array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn binary_works() {
assert!(crate::integration::run_php("array.php"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a 100% happy how the PHP code is executed using a hardcoded path to the compiled extension. Nontheless, I think that is a good start for the first iteration

And idea that might work is to put together a simple macro and embed the php snippet directly into this file:

#[test]
fn binary_works() {
    php_test!(r#"
// Tests sequential arrays
$array = test_array(['a', 'b', 'c']);

assert(is_array($array));
assert(count($array) === 3);
assert(in_array('a', $array));
assert(in_array('b', $array));
assert(in_array('c', $array));

// Tests associative arrays
$assoc = test_array_assoc([
    'a' => '1',
    'b' => '2',
    'c' => '3'
]);

assert(array_key_exists('a', $assoc));
assert(array_key_exists('b', $assoc));
assert(array_key_exists('c', $assoc));
assert(in_array('1', $assoc));
assert(in_array('2', $assoc));
assert(in_array('3', $assoc));
    "#);
}

For the initial version, we could either automatically write out a file an execute it or - I believe this should work - pipe the input into php without touching the filesystem.

I will resume working on my execution support, after which this could mature into something entirely in process.

}
13 changes: 13 additions & 0 deletions tests/src/integration/binary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require('_utils.php');

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

assert(count($result) === 5);
assert(in_array(1, $result));
assert(in_array(2, $result));
assert(in_array(3, $result));
assert(in_array(4, $result));
assert(in_array(5, $result));
4 changes: 4 additions & 0 deletions tests/src/integration/binary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn binary_works() {
assert!(crate::integration::run_php("binary.php"));
}
6 changes: 6 additions & 0 deletions tests/src/integration/bool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

require('_utils.php');

assert(test_bool(true) === true);
assert(test_bool(false) === false);
4 changes: 4 additions & 0 deletions tests/src/integration/bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn bool_works() {
assert!(crate::integration::run_php("bool.php"));
}
5 changes: 5 additions & 0 deletions tests/src/integration/callable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require('_utils.php');

assert(test_callable(fn (string $a) => $a, 'test') === 'test');
4 changes: 4 additions & 0 deletions tests/src/integration/callable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn callable_works() {
assert!(crate::integration::run_php("callable.php"));
}
21 changes: 21 additions & 0 deletions tests/src/integration/class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

require('_utils.php');

// Tests constructor
$class = test_class('lorem ipsum', 2022);
assert($class instanceof TestClass);

// Tests getter/setter
assert($class->getString() === 'lorem ipsum');
$class->setString('dolor et');
assert($class->getString() === 'dolor et');

assert($class->getNumber() === 2022);
$class->setNumber(2023);
assert($class->getNumber() === 2023);

// Tests #prop decorator
assert($class->boolean);
$class->boolean = false;
assert($class->boolean === false);
4 changes: 4 additions & 0 deletions tests/src/integration/class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn class_works() {
assert!(crate::integration::run_php("class.php"));
}
14 changes: 14 additions & 0 deletions tests/src/integration/closure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require('_utils.php');

$v = test_closure();

// Closure
assert($v('works') === 'works');

// Closure once
$closure = test_closure_once('test');

assert(call_user_func($closure) === 'test');
assert_exception_thrown($closure);
4 changes: 4 additions & 0 deletions tests/src/integration/closure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn closure_works() {
assert!(crate::integration::run_php("closure.php"));
}
6 changes: 6 additions & 0 deletions tests/src/integration/nullable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

require('_utils.php');

assert(is_null(test_nullable()));
assert(!is_null(test_nullable('value')));
4 changes: 4 additions & 0 deletions tests/src/integration/nullable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn nullable_works() {
assert!(crate::integration::run_php("nullable.php"));
}
18 changes: 18 additions & 0 deletions tests/src/integration/number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require('_utils.php');

// Signed
assert(test_number_signed(-12) === -12);
assert(test_number_signed(0) === 0);
assert(test_number_signed(12) === 12);

// Unsigned
assert(test_number_unsigned(0) === 0);
assert(test_number_unsigned(12) === 12);
assert_exception_thrown(fn () => test_number_unsigned(-12));

// Float
assert(round(test_number_float(-1.2), 2) === round(-1.2, 2));
assert(round(test_number_float(0.0), 2) === round(0.0, 2));
assert(round(test_number_float(1.2), 2) === round(1.2, 2));
4 changes: 4 additions & 0 deletions tests/src/integration/number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn number_works() {
assert!(crate::integration::run_php("number.php"));
}
16 changes: 16 additions & 0 deletions tests/src/integration/object.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$obj = new stdClass;
$obj->string = 'string';
$obj->bool = true;
$obj->number = 2022;
$obj->array = [
1, 2, 3
];

$test = test_object($obj);

assert($test->string === 'string');
assert($test->bool === true);
assert($test->number === 2022);
assert($test->array === [1, 2, 3]);
4 changes: 4 additions & 0 deletions tests/src/integration/object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn object_works() {
assert!(crate::integration::run_php("object.php"));
}
6 changes: 6 additions & 0 deletions tests/src/integration/string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

require('_utils.php');

assert(test_str('abc') === 'abc');
assert(test_string('abc') === 'abc');
4 changes: 4 additions & 0 deletions tests/src/integration/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn string_works() {
assert!(crate::integration::run_php("string.php"));
}
Loading