From 2cf7c66e77f023d2f9f7e21491b88c4e20b97ca3 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Thu, 7 Mar 2024 20:59:10 +0100 Subject: [PATCH 1/2] Allow to initialise objects --- src/zend/handlers.rs | 5 +++-- tests/src/integration/_utils.php | 15 ++++++++++++++- tests/src/integration/exception.php | 14 ++++++++++++++ tests/src/integration/exception.rs | 4 ++++ tests/src/lib.rs | 18 +++++++++++++++++- 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tests/src/integration/exception.php create mode 100644 tests/src/integration/exception.rs diff --git a/src/zend/handlers.rs b/src/zend/handlers.rs index 7e88a7e053..4f667f8c2c 100644 --- a/src/zend/handlers.rs +++ b/src/zend/handlers.rs @@ -85,7 +85,7 @@ impl ZendObjectHandlers { let prop_name = member .as_ref() .ok_or("Invalid property name pointer given")?; - let self_ = &mut **obj; + let self_ = &mut *obj; let props = T::get_metadata().get_properties(); let prop = props.get(prop_name.as_str()?); @@ -132,7 +132,8 @@ impl ZendObjectHandlers { let prop_name = member .as_ref() .ok_or("Invalid property name pointer given")?; - let self_ = &mut **obj; + + let self_ = &mut *obj; let props = T::get_metadata().get_properties(); let prop = props.get(prop_name.as_str()?); let value_mut = value.as_mut().ok_or("Invalid return zval given")?; diff --git a/tests/src/integration/_utils.php b/tests/src/integration/_utils.php index 2201e1f4f0..2ba83bf17d 100644 --- a/tests/src/integration/_utils.php +++ b/tests/src/integration/_utils.php @@ -7,5 +7,18 @@ function assert_exception_thrown(callable $callback): void } catch (\Throwable $th) { return; } - throw new Exception("Excption was not thrown", 255); + throw new Exception("Exception was not thrown", 255); +} + + +function assert_specific_exception_thrown(callable $callback, string $expectedExpectionFqcn): void +{ + try { + call_user_func($callback); + } catch (\Throwable $e) { + if ($e instanceof $expectedExpectionFqcn) { + return; + } + } + throw new Exception("Exception was not thrown", 255); } diff --git a/tests/src/integration/exception.php b/tests/src/integration/exception.php new file mode 100644 index 0000000000..5860fd4ea7 --- /dev/null +++ b/tests/src/integration/exception.php @@ -0,0 +1,14 @@ + throw throw_default_exception(), \Exception::class); + +assert_specific_exception_thrown(fn() => throw throw_custom_exception(), \Test\TestException::class); + +try { + throw throw_custom_exception(); +} catch (\Throwable $e) { + // Check if object is initiated + assert("Not good custom!" === $e->getMessage()); +} \ No newline at end of file diff --git a/tests/src/integration/exception.rs b/tests/src/integration/exception.rs new file mode 100644 index 0000000000..6c8004ba5d --- /dev/null +++ b/tests/src/integration/exception.rs @@ -0,0 +1,4 @@ +#[test] +fn exception_works() { + assert!(crate::integration::run_php("exception.php")); +} diff --git a/tests/src/lib.rs b/tests/src/lib.rs index 237c08ef05..575ac10ec2 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -1,5 +1,5 @@ #![cfg_attr(windows, feature(abi_vectorcall))] -use ext_php_rs::{binary::Binary, prelude::*, types::ZendObject, types::Zval}; +use ext_php_rs::{binary::Binary, prelude::*, types::ZendObject, types::Zval, exception::PhpException, zend::ce}; use std::collections::HashMap; #[php_function] @@ -112,6 +112,21 @@ pub fn test_class(string: String, number: i32) -> TestClass { } } +#[php_class(name = "Test\\TestException")] +#[extends(ce::exception())] +#[derive(Debug)] +pub struct TestException; + +#[php_function] +pub fn throw_custom_exception() -> PhpResult { + Err(PhpException::from_class::("Not good custom!".into())) +} + +#[php_function] +pub fn throw_default_exception() -> PhpResult { + Err(PhpException::default("Not good!".into())) +} + #[php_module] pub fn get_module(module: ModuleBuilder) -> ModuleBuilder { module @@ -179,6 +194,7 @@ mod integration { mod callable; mod class; mod closure; + mod exception; mod nullable; mod number; mod object; From 19aebf3dc18219d6ce9fab14175f673a001e7bab Mon Sep 17 00:00:00 2001 From: Kajetan Date: Fri, 25 Oct 2024 18:05:19 +0200 Subject: [PATCH 2/2] Fixes --- tests/src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/src/lib.rs b/tests/src/lib.rs index 575ac10ec2..c5ffd6f495 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(windows, feature(abi_vectorcall))] -use ext_php_rs::{binary::Binary, prelude::*, types::ZendObject, types::Zval, exception::PhpException, zend::ce}; +use ext_php_rs::{ + binary::Binary, exception::PhpException, prelude::*, types::ZendObject, types::Zval, zend::ce, +}; use std::collections::HashMap; #[php_function] @@ -119,7 +121,9 @@ pub struct TestException; #[php_function] pub fn throw_custom_exception() -> PhpResult { - Err(PhpException::from_class::("Not good custom!".into())) + Err(PhpException::from_class::( + "Not good custom!".into(), + )) } #[php_function]