mirror of
https://github.com/pestphp/pest.git
synced 2026-12-05 23:32:17 +01:00
fix: avoid passing an object when invoking static methods via reflection (#1988)
* fix: avoid passing an object when invoking static methods via reflection
PHP 8.6 deprecates passing an object to `ReflectionMethod::invoke()` for
static methods, which breaks higher-order assertions such as
`it('...')->assertTrue(true)` since `Assert::assertTrue()` is static.
* test: update visual snapshots for new reflection tests
This commit is contained in:
@@ -33,7 +33,7 @@ final class Reflection
|
||||
try {
|
||||
$reflectionMethod = $reflectionClass->getMethod($method);
|
||||
|
||||
return $reflectionMethod->invoke($object, ...$args);
|
||||
return $reflectionMethod->invoke($reflectionMethod->isStatic() ? null : $object, ...$args);
|
||||
} catch (ReflectionException $exception) {
|
||||
if (method_exists($object, '__call')) {
|
||||
return $object->__call($method, $args);
|
||||
|
||||
@@ -2146,6 +2146,8 @@
|
||||
✓ it gets property values
|
||||
✓ it gets properties from classes
|
||||
✓ it gets methods from classes
|
||||
✓ it calls instance methods
|
||||
✓ it calls static methods without passing an object
|
||||
|
||||
PASS Tests\Unit\Support\Str
|
||||
✓ it evaluates the code with ('version()', '__pest_evaluable_version__')
|
||||
@@ -2271,4 +2273,4 @@
|
||||
✓ pass with dataset with ('my-datas-set-value')
|
||||
✓ within describe → pass with dataset with ('my-datas-set-value')
|
||||
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1610 passed (3516 assertions)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1612 passed (3518 assertions)
|
||||
@@ -74,3 +74,29 @@ it('gets methods from classes', function (): void {
|
||||
'getBar',
|
||||
]);
|
||||
});
|
||||
|
||||
it('calls instance methods', function (): void {
|
||||
$class = new class
|
||||
{
|
||||
private string $foo = 'bar';
|
||||
|
||||
private function getFoo(): string
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
};
|
||||
|
||||
expect(Reflection::call($class, 'getFoo'))->toBe('bar');
|
||||
});
|
||||
|
||||
it('calls static methods without passing an object', function (): void {
|
||||
$class = new class
|
||||
{
|
||||
private static function concat(string $a, string $b): string
|
||||
{
|
||||
return $a.$b;
|
||||
}
|
||||
};
|
||||
|
||||
expect(Reflection::call($class, 'concat', ['foo', 'bar']))->toBe('foobar');
|
||||
});
|
||||
|
||||
@@ -20,7 +20,7 @@ test('parallel', function () use ($run): void {
|
||||
$output = $run('--exclude-group=integration');
|
||||
$output = implode("\n", array_slice(explode("\n", (string) $output), -10));
|
||||
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1591 passed (3458 assertions)';
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1593 passed (3460 assertions)';
|
||||
|
||||
expect($output)
|
||||
->toContain("Tests: {$expected}")
|
||||
|
||||
Reference in New Issue
Block a user