Skip to content

Commit

Permalink
Add a few more test cases
Browse files Browse the repository at this point in the history
* Make sure `coerce` doesn't update original value
* Check against union and intersection types
* Differentiate between v1 and v2
  • Loading branch information
BackEndTea authored and ondrejmirtes committed Jun 11, 2022
1 parent 8741be8 commit 60eb6cb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"require-dev": {
"azjezz/psl": "^1.6||^2.0",
"nikic/php-parser": "^4.14.0",
"composer/semver": "^3.3",
"nikic/php-parser": "^4.14.0",
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
Expand Down
7 changes: 7 additions & 0 deletions tests/Type/PslTypeSpecifyingExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Psl\PHPStan\Type;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use PHPStan\Testing\TypeInferenceTestCase;

class PslTypeSpecifyingExtensionTest extends TypeInferenceTestCase
Expand All @@ -15,6 +17,11 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/coerce.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/assert.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/matches.php');
if (InstalledVersions::satisfies(new VersionParser(), 'azjezz/psl', '<2.0.0')) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/complexTypev1.php');
} else {
yield from $this->gatherAssertTypes(__DIR__ . '/data/complexTypev2.php');
}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/Type/data/coerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ public function coerceShape(array $input): void
])),
]);

$input = $specification->coerce($input);
$output = $specification->coerce($input);

assertType('array{name: string, age: int, location?: array{city: string, state: string, country: string}}', $input);
assertType('array{name: string, age: int, location?: array{city: string, state: string, country: string}}', $output);
assertType('array', $input);
}

public function coerceInt($i): void
{
$spec = Type\int();
$coerced = $spec->coerce($i);
assertType('int', $coerced);
assertType('mixed', $i);
}

public function coerceWrongShape(): void
Expand Down
32 changes: 32 additions & 0 deletions tests/Type/data/complexTypev1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace PslComplexV1Test;

use Psl\Type;

use function PHPStan\Testing\assertType;

interface Bike {}
interface Plane {}

/**
* For PSL < 2.0.0
*/
class ComplexTypesV1
{

public function coerceShapeWithComplexTypes($input): void
{
$intNullOrString = Type\union(Type\int(), Type\nullable(Type\string()));
$bikeAndPlane = Type\intersection(Type\object(Bike::class), Type\object(Plane::class));
$shape = Type\shape([
'name_or_length' => $intNullOrString,
'transportation' => $bikeAndPlane,
'something' => Type\union($intNullOrString, $bikeAndPlane)
]);

$output = $shape->coerce($input);
assertType('array{name_or_length: int|string|null, transportation: PslComplexV1Test\Bike&PslComplexV1Test\Plane, something: int|(PslComplexV1Test\Bike&PslComplexV1Test\Plane)|string|null}', $output);
}

}
28 changes: 28 additions & 0 deletions tests/Type/data/complexTypev2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace PslComplexV2Test;

use Psl\Type;

use function PHPStan\Testing\assertType;


/**
* For PSL >= 2.0.0
*/
class ComplexTypesV2
{
public function coerceShapeWithComplexTypes($input): void
{
$intNullOrString = Type\union(Type\int(), Type\nullable(Type\string()));
$bikeAndPlane = Type\intersection(Type\instance_of(Bike::class), Type\instance_of(Plane::class));
$shape = Type\shape([
'name_or_length' => $intNullOrString,
'transportation' => $bikeAndPlane,
'something' => Type\union($intNullOrString, $bikeAndPlane)
]);

$output = $shape->coerce($input);
assertType('array{name_or_length: int|string|null, transportation: PslComplexV2Test\Bike&PslComplexV2Test\Plane, something: int|(PslComplexV2Test\Bike&PslComplexV2Test\Plane)|string|null}', $output);
}
}

0 comments on commit 60eb6cb

Please sign in to comment.