Skip to content

Commit

Permalink
Extract remaining collection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Jan 6, 2022
1 parent 45b115f commit baf0436
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 33 deletions.
70 changes: 69 additions & 1 deletion tests/Type/WebMozartAssert/data/collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,67 @@

use Webmozart\Assert\Assert;

class Collection
class CollectionTest
{

public function allString(array $a): void
{
Assert::allString($a);
\PHPStan\Testing\assertType('array<string>', $a);
}

public function allInteger(array $a, iterable $b, iterable $c): void
{
Assert::allInteger($a);
\PHPStan\Testing\assertType('array<int>', $a);

Assert::allInteger($b);
\PHPStan\Testing\assertType('iterable<int>', $b);
}

public function allInstanceOf(array $a): void
{
Assert::allIsInstanceOf($a, \stdClass::class);
\PHPStan\Testing\assertType('array<stdClass>', $a);
}

/**
* @param (CollectionFoo|CollectionBar)[] $a
*/
public function allNotInstanceOf(array $a): void
{
Assert::allNotInstanceOf($a, CollectionBar::class);
\PHPStan\Testing\assertType('array<PHPStan\Type\WebMozartAssert\CollectionFoo>', $a);
}

/**
* @param (int|null)[] $a
*/
public function allNotNull(array $a): void
{
Assert::allNotNull($a);
\PHPStan\Testing\assertType('array<int>', $a);
}

/**
* @param array{-1|1, -2|2, -3|3} $a
*/
public function allNotSame(array $a): void
{
Assert::allNotSame($a, -1);
\PHPStan\Testing\assertType('array{1, -2|2, -3|3}', $a);
}

public function allSubclassOf(array $a, $b): void
{
Assert::allSubclassOf($a, self::class);
// should array<PHPStan\Type\WebMozartAssert\CollectionTest>
\PHPStan\Testing\assertType('array<*NEVER*>', $a);

Assert::allSubclassOf($b, self::class);
\PHPStan\Testing\assertType('iterable<class-string<PHPStan\Type\WebMozartAssert\CollectionTest>|PHPStan\Type\WebMozartAssert\CollectionTest>', $b);
}

/**
* @param array<array{id?: int}> $a
* @param array<int, array<string, mixed>> $b
Expand All @@ -25,3 +83,13 @@ public function allKeyExists(array $a, array $b, array $c): void
}

}

class CollectionFoo
{

}

interface CollectionBar
{

}
32 changes: 0 additions & 32 deletions tests/Type/WebMozartAssert/data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,9 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
Assert::nullOrInteger($b);
\PHPStan\Testing\assertType('int|null', $b);

Assert::allInteger($c);
\PHPStan\Testing\assertType('array<int>', $c);

Assert::allInteger($d);
\PHPStan\Testing\assertType('iterable<int>', $d);

/** @var (Foo|Bar)[] $v */
$v = doFoo();
Assert::allNotInstanceOf($v, Bar::class);
\PHPStan\Testing\assertType('array<PHPStan\Type\WebMozartAssert\Foo>', $v);

/** @var (int|null)[] $w */
$w = doFoo();
Assert::allNotNull($w);
\PHPStan\Testing\assertType('array<int>', $w);

$z = [1, 2, 3];
if (doFoo()) {
$z = [-1, -2, -3];
}
Assert::allNotSame($z, -1);
\PHPStan\Testing\assertType('array{1, -2|2, -3|3}', $z);

Assert::subclassOf($aa, self::class);
\PHPStan\Testing\assertType('class-string<PHPStan\Type\WebMozartAssert\TypeInferenceTest>|PHPStan\Type\WebMozartAssert\TypeInferenceTest', $aa);

Assert::allSubclassOf($ab, self::class);
// should array<PHPStan\Type\WebMozartAssert\Foo>
\PHPStan\Testing\assertType('array<*NEVER*>', $ab);

Assert::implementsInterface($ae, Baz::class);
\PHPStan\Testing\assertType('PHPStan\Type\WebMozartAssert\Baz', $ae);

Expand All @@ -59,13 +32,8 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
$ah = false;
}

Assert::allIsInstanceOf($ah, \stdClass::class);
\PHPStan\Testing\assertType('array<stdClass>', $ah);

Assert::isList($ai);
\PHPStan\Testing\assertType('array', $ai);
Assert::allString($ai);
\PHPStan\Testing\assertType('array<string>', $ai);

/** @var int[] $aj */
$aj = doFoo();
Expand Down

0 comments on commit baf0436

Please sign in to comment.