Skip to content

Commit

Permalink
add support for inArray and oneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
ntzm authored Jul 2, 2021
1 parent 1ede841 commit e2155ab
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
20 changes: 20 additions & 0 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))),
]
);
},
];
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

Expand Down
11 changes: 10 additions & 1 deletion tests/Type/WebMozartAssert/data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

}
Expand Down

0 comments on commit e2155ab

Please sign in to comment.