Skip to content

Commit 0834e8f

Browse files
author
Nicolas Reynis
committed
Add type specifying support for classes extending Assert
1 parent 5d50142 commit 0834e8f

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/Type/PHPUnit/Assert/AssertMethodTypeSpecifyingExtension.php

Lines changed: 1 addition & 1 deletion
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(
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
if (!class_exists('PHPUnit\\Framework\\Assert')) {
14+
return [];
15+
}
16+
17+
yield from $this->gatherAssertTypes(__DIR__ . '/data/assert-method.php');
18+
}
19+
20+
/**
21+
* @dataProvider dataFileAsserts
22+
* @param string $assertType
23+
* @param string $file
24+
* @param mixed ...$args
25+
*/
26+
public function testFileAsserts(
27+
string $assertType,
28+
string $file,
29+
...$args
30+
): void
31+
{
32+
$this->assertFileAsserts($assertType, $file, ...$args);
33+
}
34+
35+
public static function getAdditionalConfigFiles(): array
36+
{
37+
return [__DIR__ . '/../../../extension.neon'];
38+
}
39+
40+
}
Lines changed: 17 additions & 0 deletions
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)