Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\UnionType;
use ReflectionObject;
use function array_key_exists;
use function count;
Expand Down Expand Up @@ -149,10 +152,30 @@ private static function getExpressionResolvers(): array
$actual->value,
$expected->value,
),
'GreaterThanOrEqual' => static function (Scope $scope, Arg $expected, Arg $actual): ?Expr\BinaryOp\GreaterOrEqual {
if (!self::isIntOrFloat($scope, $expected) || !self::isIntOrFloat($scope, $actual)) {
return null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a test with values different from int|float

}

return new Expr\BinaryOp\GreaterOrEqual(
$actual->value,
$expected->value,
);
},
'LessThan' => static fn (Scope $scope, Arg $expected, Arg $actual): Expr\BinaryOp\Smaller => new Expr\BinaryOp\Smaller(
$actual->value,
$expected->value,
),
'LessThanOrEqual' => static function (Scope $scope, Arg $expected, Arg $actual): ?Expr\BinaryOp\SmallerOrEqual {
if (!self::isIntOrFloat($scope, $expected) || !self::isIntOrFloat($scope, $actual)) {
return null;
}

return new Expr\BinaryOp\SmallerOrEqual(
$actual->value,
$expected->value,
);
},
'InstanceOf' => static fn (Scope $scope, Arg $class, Arg $object): Instanceof_ => new Instanceof_(
$object->value,
$class->value,
Expand Down Expand Up @@ -304,4 +327,9 @@ private static function getExpressionResolvers(): array
return self::$resolvers;
}

private static function isIntOrFloat(Scope $scope, Arg $arg): bool
{
return (new UnionType([new IntegerType(), new FloatType()]))->isSuperTypeOf($scope->getType($arg->value))->yes();
}

}
16 changes: 16 additions & 0 deletions tests/Type/PHPUnit/data/assert-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use function PHPUnit\Framework\assertEmpty;
use function PHPUnit\Framework\assertInstanceOf;
use function PHPUnit\Framework\assertGreaterThan;
use function PHPUnit\Framework\assertGreaterThanOrEqual;
use function PHPUnit\Framework\assertLessThan;
use function PHPUnit\Framework\assertLessThanOrEqual;

class Foo
{
Expand Down Expand Up @@ -126,4 +128,18 @@ public function lessThanFunction(array $arr): void
assertType('array{}', $arr);
}

/** @param string[] $arr */
public function greaterThanOrEqualFunction(array $arr): void
{
assertGreaterThanOrEqual(1, count($arr));
assertType('non-empty-array<string>', $arr);
}

/** @param string[] $arr */
public function lessThanOrEqualFunction(array $arr): void
{
assertLessThanOrEqual(0, count($arr));
assertType('array{}', $arr);
}

}
54 changes: 54 additions & 0 deletions tests/Type/PHPUnit/data/assert-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public function lessThanTest(array $arr): void
assertType('array{}', $arr);
}

/** @param string[] $arr */
public function greaterThanOrEqualStatic(array $arr): void
{
self::assertGreaterThanOrEqual(1, count($arr));
assertType('non-empty-array<string>', $arr);
}

/** @param string[] $arr */
public function lessThanOrEqualTest(array $arr): void
{
self::assertLessThanOrEqual(0, count($arr));
assertType('array{}', $arr);
}

public function integers(int $a, int $c): void
{
self::assertGreaterThan(5, $a);
Expand All @@ -54,4 +68,44 @@ public function integers(int $a, int $c): void
self::assertLessThan(5, $c);
assertType('int<min, 4>', $c);
}

public function integersOrEqual(int $a, int $c): void
{
self::assertGreaterThanOrEqual(5, $a);
assertType('int<5, max>', $a);

self::assertLessThanOrEqual(5, $c);
assertType('int<min, 5>', $c);
}

/**
* @param 1|5|9.5 $a
* @param 1.5|5|9 $c
*/
public function integersAndFloatsOrEqual($a, $c): void
{
self::assertGreaterThanOrEqual(5.0, $a);
assertType('5|9.5', $a);

self::assertLessThanOrEqual(5.0, $c);
assertType('1.5|5', $c);
}
Comment on lines +81 to +92

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we support float we should also test with INF and NAN


public function notNarrowedActualOrEqual(?int $a, ?int $c): void
{
self::assertGreaterThanOrEqual(1, $a);
assertType('int|null', $a);

self::assertLessThanOrEqual(1, $c);
assertType('int|null', $c);
}

public function notNarrowedExpectedOrEqual(int $a, int $c): void
{
self::assertGreaterThanOrEqual('5', $a);
assertType('int', $a);

self::assertLessThanOrEqual('5', $c);
assertType('int', $c);
}
}