Skip to content

Commit ffff35d

Browse files
authored
Implement assertGreaterThan() and assertLessThan() type narrowing (#351)
* Implement `assertGreaterThan()` and `assertLessThan()` type narrowing * simplify tests * Update assert-method.php
1 parent 13cc64d commit ffff35d

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ private static function getExpressionResolvers(): array
145145
new FuncCall(new Name('count'), [$actual]),
146146
),
147147
),
148+
'GreaterThan' => static fn (Scope $scope, Arg $expected, Arg $actual): Expr\BinaryOp\Greater => new Expr\BinaryOp\Greater(
149+
$actual->value,
150+
$expected->value,
151+
),
152+
'LessThan' => static fn (Scope $scope, Arg $expected, Arg $actual): Expr\BinaryOp\Smaller => new Expr\BinaryOp\Smaller(
153+
$actual->value,
154+
$expected->value,
155+
),
148156
'InstanceOf' => static fn (Scope $scope, Arg $class, Arg $object): Instanceof_ => new Instanceof_(
149157
$object->value,
150158
$class->value,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use function PHPUnit\Framework\assertNotCount;
1212
use function PHPUnit\Framework\assertEmpty;
1313
use function PHPUnit\Framework\assertInstanceOf;
14+
use function PHPUnit\Framework\assertGreaterThan;
15+
use function PHPUnit\Framework\assertLessThan;
1416

1517
class Foo
1618
{
@@ -110,4 +112,18 @@ public function notCount(array $a, array $b): void
110112
assertType('array', $b);
111113
}
112114

115+
/** @param string[] $arr */
116+
public function greaterThanFunction(array $arr): void
117+
{
118+
assertGreaterThan(2, count($arr));
119+
assertType('non-empty-array<string>', $arr);
120+
}
121+
122+
/** @param string[] $arr */
123+
public function lessThanFunction(array $arr): void
124+
{
125+
assertLessThan(1, count($arr));
126+
assertType('array{}', $arr);
127+
}
128+
113129
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AssertMethod;
44

5+
use PHPUnit\Framework\TestCase;
56
use function PHPStan\Testing\assertType;
67

78
class Foo
@@ -13,5 +14,44 @@ public function inheritedAssertMethodsNarrowType(?string $s): void
1314
$customAsserter->assertNotNull($s);
1415
assertType('string', $s);
1516
}
17+
}
18+
19+
class FooBar extends TestCase
20+
{
21+
/** @param string[] $arr */
22+
public function referenceAssertTrue(array $arr): void
23+
{
24+
self::assertTrue(count($arr) > 2);
25+
assertType('non-empty-array<string>', $arr);
26+
}
1627

28+
/** @param string[] $arr */
29+
public function greaterThanStatic(array $arr): void
30+
{
31+
self::assertGreaterThan(2, count($arr));
32+
assertType('non-empty-array<string>', $arr);
33+
}
34+
35+
/** @param string[] $arr */
36+
public function greaterThanMethod(array $arr): void
37+
{
38+
$this->assertGreaterThan(2, count($arr));
39+
assertType('non-empty-array<string>', $arr);
40+
}
41+
42+
/** @param string[] $arr */
43+
public function lessThanTest(array $arr): void
44+
{
45+
self::assertLessThan(1, count($arr));
46+
assertType('array{}', $arr);
47+
}
48+
49+
public function integers(int $a, int $c): void
50+
{
51+
self::assertGreaterThan(5, $a);
52+
assertType('int<6, max>', $a);
53+
54+
self::assertLessThan(5, $c);
55+
assertType('int<min, 4>', $c);
56+
}
1757
}

0 commit comments

Comments
 (0)