Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow to initialise objects #313

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/zend/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?);

Expand Down Expand Up @@ -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")?;
Expand Down
15 changes: 14 additions & 1 deletion tests/src/integration/_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
14 changes: 14 additions & 0 deletions tests/src/integration/exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require('_utils.php');

assert_specific_exception_thrown(fn() => 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());
}
4 changes: 4 additions & 0 deletions tests/src/integration/exception.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn exception_works() {
assert!(crate::integration::run_php("exception.php"));
}
22 changes: 21 additions & 1 deletion tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg_attr(windows, feature(abi_vectorcall))]
use ext_php_rs::{binary::Binary, prelude::*, types::ZendObject, types::Zval};
use ext_php_rs::{
binary::Binary, exception::PhpException, prelude::*, types::ZendObject, types::Zval, zend::ce,
};
use std::collections::HashMap;

#[php_function]
Expand Down Expand Up @@ -112,6 +114,23 @@ 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<i32> {
Err(PhpException::from_class::<TestException>(
"Not good custom!".into(),
))
}

#[php_function]
pub fn throw_default_exception() -> PhpResult<i32> {
Err(PhpException::default("Not good!".into()))
}

#[php_module]
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
module
Expand Down Expand Up @@ -179,6 +198,7 @@ mod integration {
mod callable;
mod class;
mod closure;
mod exception;
mod nullable;
mod number;
mod object;
Expand Down
Loading