From e2155abb73c8d32b7cbd8b1e3ad73778d130b815 Mon Sep 17 00:00:00 2001 From: Nat Zimmermann Date: Fri, 2 Jul 2021 14:35:13 +0100 Subject: [PATCH] add support for inArray and oneOf --- README.md | 2 ++ .../AssertTypeSpecifyingExtension.php | 20 +++++++++++++++++++ .../AssertTypeSpecifyingExtensionTest.php | 12 +++++++++++ tests/Type/WebMozartAssert/data/data.php | 11 +++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b06f51a..3b7114f 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ This extension specifies types of values passed to: * `Assert::implementsInterface` * `Assert::classExists` * `Assert::minCount` +* `Assert::inArray` +* `Assert::oneOf` * `nullOr*` and `all*` variants of the above methods diff --git a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php index 2c98f4e..a200d41 100644 --- a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php +++ b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php @@ -386,6 +386,26 @@ private static function getExpressionResolvers(): array $number->value ); }, + 'inArray' => function (Scope $scope, Arg $needle, Arg $array): \PhpParser\Node\Expr { + return new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('in_array'), + [ + $needle, + $array, + new Arg(new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('true'))), + ] + ); + }, + 'oneOf' => function (Scope $scope, Arg $needle, Arg $array): \PhpParser\Node\Expr { + return new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('in_array'), + [ + $needle, + $array, + new Arg(new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('true'))), + ] + ); + }, ]; } diff --git a/tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php b/tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php index fd8a617..878f594 100644 --- a/tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php +++ b/tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php @@ -188,6 +188,18 @@ public function testExtension(): void 'Variable $ak is: int', 152, ], + [ + 'Variable $al is: \'bar\'|\'foo\'', + 155, + ], + [ + 'Variable $am is: \'bar\'|\'foo\'|null', + 158, + ], + [ + 'Variable $an is: 1|2', + 161, + ], ]); } diff --git a/tests/Type/WebMozartAssert/data/data.php b/tests/Type/WebMozartAssert/data/data.php index 2943149..bd79483 100644 --- a/tests/Type/WebMozartAssert/data/data.php +++ b/tests/Type/WebMozartAssert/data/data.php @@ -7,7 +7,7 @@ class Foo { - public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, $ac, $ad, $ae, $af, $ag, array $ah, $ai) + public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, $ac, $ad, $ae, $af, $ag, array $ah, $ai, $al, $am, $an) { $a; @@ -150,6 +150,15 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, Assert::minCount($aj, 1); $ak = array_pop($aj); $ak; + + Assert::inArray($al, ['foo', 'bar']); + $al; + + Assert::nullOrInArray($am, ['foo', 'bar']); + $am; + + Assert::oneOf($an, [1, 2]); + $an; } }