1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/TypeCombinationTest.php
2017-11-19 13:42:48 -05:00

199 lines
5.2 KiB
PHP

<?php
namespace Psalm\Tests;
use Psalm\Type;
class TypeCombinationTest extends TestCase
{
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @dataProvider providerTestValidTypeCombination
*
* @param string $expected
* @param array<int, string> $types
*
* @return void
*/
public function testValidTypeCombination($expected, $types)
{
foreach ($types as $k => $type) {
$types[$k] = self::getAtomic($type);
}
/** @psalm-suppress PossiblyInvalidArgument */
$this->assertSame(
$expected,
(string) Type::combineTypes($types)
);
}
/**
* @return array
*/
public function providerFileCheckerValidCodeParse()
{
return [
'multipleValuedArray' => [
'<?php
class A {}
class B {}
$var = [];
$var[] = new A();
$var[] = new B();',
],
];
}
/**
* @return array
*/
public function providerTestValidTypeCombination()
{
return [
'intOrString' => [
'int|string',
[
'int',
'string',
],
],
'arrayOfIntOrString' => [
'array<mixed, int|string>',
[
'array<int>',
'array<string>',
],
],
'arrayOfIntOrAlsoString' => [
'array<mixed, int>|string',
[
'array<int>',
'string',
],
],
'emptyArrays' => [
'array<empty, empty>',
[
'array<empty,empty>',
'array<empty,empty>',
],
],
'arrayStringOrEmptyArray' => [
'array<mixed, string>',
[
'array<empty>',
'array<string>',
],
],
'arrayMixedOrString' => [
'array<mixed, mixed>',
[
'array<mixed>',
'array<string>',
],
],
'arrayMixedOrStringKeys' => [
'array<mixed, string>',
[
'array<int|string,string>',
'array<mixed,string>',
],
],
'arrayMixedOrEmpty' => [
'array<mixed, mixed>',
[
'array<empty>',
'array<mixed>',
],
],
'arrayBigCombination' => [
'array<mixed, int|float|string>',
[
'array<int|float>',
'array<string>',
],
],
'falseDestruction' => [
'bool',
[
'false',
'bool',
],
],
'onlyFalse' => [
'bool',
[
'false',
],
],
'falseFalseDestruction' => [
'bool',
[
'false',
'false',
],
],
'aAndAOfB' => [
'A',
[
'A',
'A<B>',
],
],
'combineObjectType1' => [
'array{a:int, b:string}',
[
'array{a:int}',
'array{b:string}',
],
],
'combineObjectType2' => [
'array{a:int|string, b:string}',
[
'array{a:int}',
'array{a:string,b:string}',
],
],
'combineObjectTypeWithIntKeyedArray' => [
'array<int|string, string|int>',
[
'array{a:int}',
'array<int, string>',
],
],
'combineNestedObjectTypeWithObjectLikeIntKeyedArray' => [
'array{a:array<int|string, string|int>}',
[
'array{a:array{a:int}}',
'array{a:array<int, string>}',
],
],
'combineIntKeyedObjectTypeWithNestedIntKeyedArray' => [
'array<int, array<int|string, string|int>>',
[
'array<int, array{a:int}>',
'array<int, array<int, string>>',
],
],
'combineNestedObjectTypeWithNestedIntKeyedArray' => [
'array<int|string, array<int|string, string|int>>',
[
'array{a:array{a:int}}',
'array<int, array<int, string>>',
],
],
];
}
/**
* @param string $string
*
* @return Type\Atomic
*/
private static function getAtomic($string)
{
return array_values(Type::parseString($string)->types)[0];
}
}