Skip to content

Commit

Permalink
adding test for post hook type error (#119)
Browse files Browse the repository at this point in the history
* adding test for post hook type error
if an invalid typehint is provided for first param of post callback, this would previously cause a hang.
fixed by #118, this test confirms the fix works in this scenario.

* improve test, add to package.xml

* remove try/catch
  • Loading branch information
brettmc committed Jan 23, 2024
1 parent d4e11b6 commit 8c3f0b3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<file name="multiple_hooks_modify_returnvalue.phpt" role="test"/>
<file name="post_hook_return_ignored_without_type.phpt" role="test"/>
<file name="post_hook_throws_exception.phpt" role="test"/>
<file name="post_hook_type_error.phpt" role="test"/>
<file name="reimplemented_interface.phpt" role="test"/>
<file name="return_expanded_params.phpt" role="test"/>
<file name="return_expanded_params_internal.phpt" role="test"/>
Expand Down
28 changes: 28 additions & 0 deletions ext/tests/post_hook_type_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Check if type error in post hook is handled
--EXTENSIONS--
opentelemetry
--FILE--
<?php
class Foo
{
public function bar(): void
{
var_dump('bar');
}
}

\OpenTelemetry\Instrumentation\hook(
Foo::class,
'bar',
fn() => var_dump('pre'),
fn(string $scope, array $params, mixed $returnValue, ?Throwable $throwable) => var_dump('post')); //NB invalid type for $scope

(new Foo())->bar();
var_dump('baz');
--EXPECTF--
string(3) "pre"
string(3) "bar"

Warning: Foo::bar(): OpenTelemetry: post hook threw exception, class=Foo function=bar message=%sArgument #1 ($scope) must be of type string, Foo given%s
string(3) "baz"

0 comments on commit 8c3f0b3

Please sign in to comment.