Skip to content

Commit 60eb6cb

Browse files
BackEndTeaondrejmirtes
authored andcommitted
Add a few more test cases
* Make sure `coerce` doesn't update original value * Check against union and intersection types * Differentiate between v1 and v2
1 parent 8741be8 commit 60eb6cb

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"require-dev": {
1616
"azjezz/psl": "^1.6||^2.0",
17-
"nikic/php-parser": "^4.14.0",
17+
"composer/semver": "^3.3",
18+
"nikic/php-parser": "^4.14.0",
1819
"php-parallel-lint/php-parallel-lint": "^1.2",
1920
"phpstan/phpstan-phpunit": "^1.0",
2021
"phpstan/phpstan-strict-rules": "^1.0",

tests/Type/PslTypeSpecifyingExtensionTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Psl\PHPStan\Type;
44

5+
use Composer\InstalledVersions;
6+
use Composer\Semver\VersionParser;
57
use PHPStan\Testing\TypeInferenceTestCase;
68

79
class PslTypeSpecifyingExtensionTest extends TypeInferenceTestCase
@@ -15,6 +17,11 @@ public function dataFileAsserts(): iterable
1517
yield from $this->gatherAssertTypes(__DIR__ . '/data/coerce.php');
1618
yield from $this->gatherAssertTypes(__DIR__ . '/data/assert.php');
1719
yield from $this->gatherAssertTypes(__DIR__ . '/data/matches.php');
20+
if (InstalledVersions::satisfies(new VersionParser(), 'azjezz/psl', '<2.0.0')) {
21+
yield from $this->gatherAssertTypes(__DIR__ . '/data/complexTypev1.php');
22+
} else {
23+
yield from $this->gatherAssertTypes(__DIR__ . '/data/complexTypev2.php');
24+
}
1825
}
1926

2027
/**

tests/Type/data/coerce.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ public function coerceShape(array $input): void
2323
])),
2424
]);
2525

26-
$input = $specification->coerce($input);
26+
$output = $specification->coerce($input);
2727

28-
assertType('array{name: string, age: int, location?: array{city: string, state: string, country: string}}', $input);
28+
assertType('array{name: string, age: int, location?: array{city: string, state: string, country: string}}', $output);
29+
assertType('array', $input);
2930
}
3031

3132
public function coerceInt($i): void
3233
{
3334
$spec = Type\int();
3435
$coerced = $spec->coerce($i);
3536
assertType('int', $coerced);
37+
assertType('mixed', $i);
3638
}
3739

3840
public function coerceWrongShape(): void

tests/Type/data/complexTypev1.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PslComplexV1Test;
4+
5+
use Psl\Type;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
interface Bike {}
10+
interface Plane {}
11+
12+
/**
13+
* For PSL < 2.0.0
14+
*/
15+
class ComplexTypesV1
16+
{
17+
18+
public function coerceShapeWithComplexTypes($input): void
19+
{
20+
$intNullOrString = Type\union(Type\int(), Type\nullable(Type\string()));
21+
$bikeAndPlane = Type\intersection(Type\object(Bike::class), Type\object(Plane::class));
22+
$shape = Type\shape([
23+
'name_or_length' => $intNullOrString,
24+
'transportation' => $bikeAndPlane,
25+
'something' => Type\union($intNullOrString, $bikeAndPlane)
26+
]);
27+
28+
$output = $shape->coerce($input);
29+
assertType('array{name_or_length: int|string|null, transportation: PslComplexV1Test\Bike&PslComplexV1Test\Plane, something: int|(PslComplexV1Test\Bike&PslComplexV1Test\Plane)|string|null}', $output);
30+
}
31+
32+
}

tests/Type/data/complexTypev2.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PslComplexV2Test;
4+
5+
use Psl\Type;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
10+
/**
11+
* For PSL >= 2.0.0
12+
*/
13+
class ComplexTypesV2
14+
{
15+
public function coerceShapeWithComplexTypes($input): void
16+
{
17+
$intNullOrString = Type\union(Type\int(), Type\nullable(Type\string()));
18+
$bikeAndPlane = Type\intersection(Type\instance_of(Bike::class), Type\instance_of(Plane::class));
19+
$shape = Type\shape([
20+
'name_or_length' => $intNullOrString,
21+
'transportation' => $bikeAndPlane,
22+
'something' => Type\union($intNullOrString, $bikeAndPlane)
23+
]);
24+
25+
$output = $shape->coerce($input);
26+
assertType('array{name_or_length: int|string|null, transportation: PslComplexV2Test\Bike&PslComplexV2Test\Plane, something: int|(PslComplexV2Test\Bike&PslComplexV2Test\Plane)|string|null}', $output);
27+
}
28+
}

0 commit comments

Comments
 (0)