Skip to content

Commit 614acc1

Browse files
authored
Add support for assertObjectHasProperty
1 parent 6536e66 commit 614acc1

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php

+3
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ private static function getExpressionResolvers(): array
276276
'ObjectHasAttribute' => static function (Scope $scope, Arg $property, Arg $object): FuncCall {
277277
return new FuncCall(new Name('property_exists'), [$object, $property]);
278278
},
279+
'ObjectHasProperty' => static function (Scope $scope, Arg $property, Arg $object): FuncCall {
280+
return new FuncCall(new Name('property_exists'), [$object, $property]);
281+
},
279282
'Contains' => static function (Scope $scope, Arg $needle, Arg $haystack): Expr {
280283
return new Expr\BinaryOp\BooleanOr(
281284
new Expr\Instanceof_($haystack->value, new Name('Traversable')),

tests/Type/PHPUnit/AssertFunctionTypeSpecifyingExtensionTest.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ class AssertFunctionTypeSpecifyingExtensionTest extends TypeInferenceTestCase
1111
/** @return mixed[] */
1212
public function dataFileAsserts(): iterable
1313
{
14-
if (!function_exists('PHPUnit\\Framework\\assertInstanceOf')) {
15-
return [];
14+
if (function_exists('PHPUnit\\Framework\\assertInstanceOf')) {
15+
yield from $this->gatherAssertTypes(__DIR__ . '/data/assert-function.php');
1616
}
1717

18-
yield from $this->gatherAssertTypes(__DIR__ . '/data/assert-function.php');
18+
if (function_exists('PHPUnit\\Framework\\assertObjectHasProperty')) {
19+
yield from $this->gatherAssertTypes(__DIR__ . '/data/assert-function-9.6.11.php');
20+
}
21+
22+
return [];
1923
}
2024

2125
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace AssertFunction;
4+
5+
use function PHPStan\Testing\assertType;
6+
use function PHPUnit\Framework\assertObjectHasProperty;
7+
8+
class Foo
9+
{
10+
11+
public function objectHasProperty(object $a): void
12+
{
13+
assertObjectHasProperty('property', $a);
14+
assertType("object&hasProperty(property)", $a);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)