Skip to content

Add a few more test cases #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}