Skip to content

Commit 54ed35b

Browse files
Nicolas Reynisondrejmirtes
Nicolas Reynis
authored andcommitted
Add type specifying support for classes extending Assert
1 parent 5d50142 commit 54ed35b

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

Diff for: src/Type/PHPUnit/Assert/AssertMethodTypeSpecifyingExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
2424

2525
public function getClass(): string
2626
{
27-
return 'PHPUnit\Framework\TestCase';
27+
return 'PHPUnit\Framework\Assert';
2828
}
2929

3030
public function isMethodSupported(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\PHPUnit;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
7+
class AssertMethodTypeSpecifyingExtensionTest extends TypeInferenceTestCase
8+
{
9+
10+
/** @return mixed[] */
11+
public function dataFileAsserts(): iterable
12+
{
13+
yield from $this->gatherAssertTypes(__DIR__ . '/data/assert-method.php');
14+
}
15+
16+
/**
17+
* @dataProvider dataFileAsserts
18+
* @param string $assertType
19+
* @param string $file
20+
* @param mixed ...$args
21+
*/
22+
public function testFileAsserts(
23+
string $assertType,
24+
string $file,
25+
...$args
26+
): void
27+
{
28+
$this->assertFileAsserts($assertType, $file, ...$args);
29+
}
30+
31+
public static function getAdditionalConfigFiles(): array
32+
{
33+
return [__DIR__ . '/../../../extension.neon'];
34+
}
35+
36+
}

Diff for: tests/Type/PHPUnit/data/assert-method.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace AssertMethod;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
public function inheritedAssertMethodsNarrowType(?string $s): void
11+
{
12+
$customAsserter = new class () extends \PHPUnit\Framework\Assert {};
13+
$customAsserter->assertNotNull($s);
14+
assertType('string', $s);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)