Skip to content

Commit

Permalink
Narrow type on assertCount and assertNotCount
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra authored Feb 23, 2024
1 parent 70ecacc commit d5242a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ private static function getExpressionResolvers(): array
{
if (self::$resolvers === null) {
self::$resolvers = [
'Count' => static function (Scope $scope, Arg $expected, Arg $actual): Identical {
return new Identical(
$expected->value,
new FuncCall(new Name('count'), [$actual])
);
},
'NotCount' => static function (Scope $scope, Arg $expected, Arg $actual): BooleanNot {
return new BooleanNot(
new Identical(
$expected->value,
new FuncCall(new Name('count'), [$actual])
)
);
},
'InstanceOf' => static function (Scope $scope, Arg $class, Arg $object): Instanceof_ {
return new Instanceof_(
$object->value,
Expand Down
21 changes: 21 additions & 0 deletions tests/Type/PHPUnit/data/assert-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use function PHPUnit\Framework\assertContains;
use function PHPUnit\Framework\assertContainsEquals;
use function PHPUnit\Framework\assertContainsOnlyInstancesOf;
use function PHPUnit\Framework\assertCount;
use function PHPUnit\Framework\assertNotCount;
use function PHPUnit\Framework\assertEmpty;
use function PHPUnit\Framework\assertInstanceOf;
use function PHPUnit\Framework\assertObjectHasAttribute;
Expand Down Expand Up @@ -90,4 +92,23 @@ public function containsOnlyInstancesOf(array $a, \Traversable $b): void
assertType('Traversable', $b);
}

public function count(array $a, \Countable $b): void
{
assertCount(3, $a);
assertType('non-empty-array', $a);

assertCount(7, $b);
assertType('Countable', $b);
}

public function notCount(array $a, array $b): void
{
assertNotCount(0, $a);
assertType('non-empty-array', $a);

// still might be empty
assertNotCount(1, $b);
assertType('array', $b);
}

}

0 comments on commit d5242a5

Please sign in to comment.