Skip to content

Commit e468b76

Browse files
villfaondrejmirtes
authored andcommitted
Add support for assertContains and assertContainsEquals
1 parent 2742e1c commit e468b76

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,21 @@ private static function getExpressionResolvers(): array
276276
'ObjectHasAttribute' => staticfunction (Scope$scope, Arg$property, Arg$object): FuncCall {
277277
returnnewFuncCall(newName('property_exists'), [$object, $property]);
278278
},
279+
'Contains' => staticfunction (Scope$scope, Arg$needle, Arg$haystack): Expr {
280+
returnnewExpr\BinaryOp\BooleanOr(
281+
newExpr\Instanceof_($haystack->value, newName('Traversable')),
282+
newFuncCall(newName('in_array'), [$needle, $haystack, newArg(newConstFetch(newName('true')))])
283+
);
284+
},
285+
'ContainsEquals' => staticfunction (Scope$scope, Arg$needle, Arg$haystack): Expr {
286+
returnnewExpr\BinaryOp\BooleanOr(
287+
newExpr\Instanceof_($haystack->value, newName('Traversable')),
288+
newExpr\BinaryOp\BooleanAnd(
289+
newExpr\BooleanNot(newExpr\Empty_($haystack->value)),
290+
newFuncCall(newName('in_array'), [$needle, $haystack, newArg(newConstFetch(newName('false')))])
291+
)
292+
);
293+
},
279294
'ContainsOnlyInstancesOf' => staticfunction (Scope$scope, Arg$className, Arg$haystack): Expr {
280295
returnnewExpr\BinaryOp\BooleanOr(
281296
newExpr\Instanceof_($haystack->value, newName('Traversable')),

‎tests/Type/PHPUnit/data/assert-function.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
usefunctionPHPStan\Testing\assertType;
66
usefunctionPHPUnit\Framework\assertArrayHasKey;
7+
usefunctionPHPUnit\Framework\assertContains;
8+
usefunctionPHPUnit\Framework\assertContainsEquals;
79
usefunctionPHPUnit\Framework\assertContainsOnlyInstancesOf;
810
usefunctionPHPUnit\Framework\assertEmpty;
911
usefunctionPHPUnit\Framework\assertInstanceOf;
@@ -61,6 +63,24 @@ public function testEmpty($a): void
6163
assertType("0|0.0|''|'0'|array{}|Countable|EmptyIterator|false|null", $a);
6264
}
6365

66+
publicfunctioncontains(array$a, \Traversable$b): void
67+
{
68+
assertContains('foo', $a);
69+
assertType('non-empty-array', $a);
70+
71+
assertContains('foo', $b);
72+
assertType('Traversable', $b);
73+
}
74+
75+
publicfunctioncontainsEquals(array$a, \Traversable$b): void
76+
{
77+
assertContainsEquals('foo', $a);
78+
assertType('non-empty-array', $a);
79+
80+
assertContainsEquals('foo', $b);
81+
assertType('Traversable', $b);
82+
}
83+
6484
publicfunctioncontainsOnlyInstancesOf(array$a, \Traversable$b): void
6585
{
6686
assertContainsOnlyInstancesOf(\stdClass::class, $a);

0 commit comments

Comments
 (0)