Skip to content

Commit

Permalink
separated enum tests
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed May 30, 2024
1 parent b4e0192 commit 57167fd
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 111 deletions.
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,9 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10080.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/http-response-header.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9662.php');
if (PHP_VERSION_ID >= 80100) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9662-enums.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/impure-error-log.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/falsy-isset.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/falsey-coalesce.php');
Expand Down
105 changes: 105 additions & 0 deletions tests/PHPStan/Analyser/data/bug-9662-enums.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php // lint >= 8.1

namespace Bug9662Enums;

use function PHPStan\Testing\assertType;

enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}

/**
* @param array<Suit> $suite
*/
function doEnum(array $suite, array $arr) {
if (in_array('NotAnEnumCase', $suite) === false) {
assertType('array<Bug9662Enums\Suit>', $suite);
} else {
assertType("non-empty-array<Bug9662Enums\Suit>", $suite);
}
assertType('array<Bug9662Enums\Suit>', $suite);

if (in_array(Suit::Hearts, $suite) === false) {
assertType('array<Bug9662Enums\Suit~Bug9662Enums\Suit::Hearts>', $suite);
} else {
assertType("non-empty-array<Bug9662Enums\Suit>", $suite);
}
assertType('array<Bug9662Enums\Suit>', $suite);


if (in_array(Suit::Hearts, $arr) === false) {
assertType('array<mixed~Bug9662Enums\Suit::Hearts>', $arr);
} else {
assertType("non-empty-array", $arr);
}
assertType('array', $arr);


if (in_array('NotAnEnumCase', $arr) === false) {
assertType('array', $arr);
} else {
assertType("non-empty-array", $arr);
}
assertType('array', $arr);
}

enum StringBackedSuit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}

/**
* @param array<StringBackedSuit> $suite
*/
function doBackedEnum(array $suite, array $arr, string $s, int $i, $mixed) {
if (in_array('NotAnEnumCase', $suite) === false) {
assertType('array<Bug9662Enums\StringBackedSuit>', $suite);
} else {
assertType("non-empty-array<Bug9662Enums\StringBackedSuit>", $suite);
}
assertType('array<Bug9662Enums\StringBackedSuit>', $suite);

if (in_array(StringBackedSuit::Hearts, $suite) === false) {
assertType('array<Bug9662Enums\StringBackedSuit~Bug9662Enums\StringBackedSuit::Hearts>', $suite);
} else {
assertType("non-empty-array<Bug9662Enums\StringBackedSuit>", $suite);
}
assertType('array<Bug9662Enums\StringBackedSuit>', $suite);


if (in_array($s, $suite) === false) {
assertType('array<Bug9662Enums\StringBackedSuit>', $suite);
} else {
assertType("non-empty-array<Bug9662Enums\StringBackedSuit>", $suite);
}
assertType('array<Bug9662Enums\StringBackedSuit>', $suite);

if (in_array($i, $suite) === false) {
assertType('array<Bug9662Enums\StringBackedSuit>', $suite);
} else {
assertType("non-empty-array<Bug9662Enums\StringBackedSuit>", $suite);
}
assertType('array<Bug9662Enums\StringBackedSuit>', $suite);

if (in_array($mixed, $suite) === false) {
assertType('array<Bug9662Enums\StringBackedSuit>', $suite);
} else {
assertType("non-empty-array<Bug9662Enums\StringBackedSuit>", $suite);
}
assertType('array<Bug9662Enums\StringBackedSuit>', $suite);


if (in_array(StringBackedSuit::Hearts, $arr) === false) {
assertType('array<mixed~Bug9662Enums\StringBackedSuit::Hearts>', $arr);
} else {
assertType("non-empty-array", $arr);
}
assertType('array', $arr);
}
103 changes: 1 addition & 102 deletions tests/PHPStan/Analyser/data/bug-9662.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php // lint >= 8.1
<?php

namespace Bug9662;

Expand Down Expand Up @@ -187,104 +187,3 @@ function addDeliveryPrice($price_list, $delete = false): void

var_dump($keys);
}


enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}

/**
* @param array<Suit> $suite
*/
function doEnum(array $suite, array $arr) {
if (in_array('NotAnEnumCase', $suite) === false) {
assertType('array<Bug9662\Suit>', $suite);
} else {
assertType("non-empty-array<Bug9662\Suit>", $suite);
}
assertType('array<Bug9662\Suit>', $suite);

if (in_array(Suit::Hearts, $suite) === false) {
assertType('array<Bug9662\Suit~Bug9662\Suit::Hearts>', $suite);
} else {
assertType("non-empty-array<Bug9662\Suit>", $suite);
}
assertType('array<Bug9662\Suit>', $suite);


if (in_array(Suit::Hearts, $arr) === false) {
assertType('array<mixed~Bug9662\Suit::Hearts>', $arr);
} else {
assertType("non-empty-array", $arr);
}
assertType('array', $arr);


if (in_array('NotAnEnumCase', $arr) === false) {
assertType('array', $arr);
} else {
assertType("non-empty-array", $arr);
}
assertType('array', $arr);
}

enum StringBackedSuit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}

/**
* @param array<StringBackedSuit> $suite
*/
function doBackedEnum(array $suite, array $arr, string $s, int $i, $mixed) {
if (in_array('NotAnEnumCase', $suite) === false) {
assertType('array<Bug9662\StringBackedSuit>', $suite);
} else {
assertType("non-empty-array<Bug9662\StringBackedSuit>", $suite);
}
assertType('array<Bug9662\StringBackedSuit>', $suite);

if (in_array(StringBackedSuit::Hearts, $suite) === false) {
assertType('array<Bug9662\StringBackedSuit~Bug9662\StringBackedSuit::Hearts>', $suite);
} else {
assertType("non-empty-array<Bug9662\StringBackedSuit>", $suite);
}
assertType('array<Bug9662\StringBackedSuit>', $suite);


if (in_array($s, $suite) === false) {
assertType('array<Bug9662\StringBackedSuit>', $suite);
} else {
assertType("non-empty-array<Bug9662\StringBackedSuit>", $suite);
}
assertType('array<Bug9662\StringBackedSuit>', $suite);

if (in_array($i, $suite) === false) {
assertType('array<Bug9662\StringBackedSuit>', $suite);
} else {
assertType("non-empty-array<Bug9662\StringBackedSuit>", $suite);
}
assertType('array<Bug9662\StringBackedSuit>', $suite);

if (in_array($mixed, $suite) === false) {
assertType('array<Bug9662\StringBackedSuit>', $suite);
} else {
assertType("non-empty-array<Bug9662\StringBackedSuit>", $suite);
}
assertType('array<Bug9662\StringBackedSuit>', $suite);


if (in_array(StringBackedSuit::Hearts, $arr) === false) {
assertType('array<mixed~Bug9662\StringBackedSuit::Hearts>', $arr);
} else {
assertType("non-empty-array", $arr);
}
assertType('array', $arr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1025,28 +1025,39 @@ static function (array $i): array {

public function testNonStrictInArray(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/../../Analyser/data/bug-9662.php'], []);
}

public function testNonStrictInArrayEnums(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';

$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/../../Analyser/data/bug-9662.php'], [
$this->analyse([__DIR__ . '/../../Analyser/data/bug-9662-enums.php'], [
[
"Call to function in_array() with 'NotAnEnumCase' and array<Bug9662\Suit> will always evaluate to false.",
204,
"Call to function in_array() with 'NotAnEnumCase' and array<Bug9662Enums\Suit> will always evaluate to false.",
19,
$tipText,
],
[
"Call to function in_array() with 'NotAnEnumCase' and array<Bug9662\StringBackedSuit> will always evaluate to false.",
247,
"Call to function in_array() with 'NotAnEnumCase' and array<Bug9662Enums\StringBackedSuit> will always evaluate to false.",
62,
$tipText,
],
[
'Call to function in_array() with string and array<Bug9662\StringBackedSuit> will always evaluate to false.',
262,
'Call to function in_array() with string and array<Bug9662Enums\StringBackedSuit> will always evaluate to false.',
77,
],
[
'Call to function in_array() with int and array<Bug9662\StringBackedSuit> will always evaluate to false.',
269,
'Call to function in_array() with int and array<Bug9662Enums\StringBackedSuit> will always evaluate to false.',
84,
],
]);
}
Expand Down

0 comments on commit 57167fd

Please sign in to comment.