1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/TypeCombinationTest.php

150 lines
3.8 KiB
PHP
Raw Normal View History

2016-06-16 02:16:40 +02:00
<?php
namespace CodeInspector\Tests;
use CodeInspector\Type;
use PhpParser;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
class TypeCombinationTest extends PHPUnit_Framework_TestCase
{
protected static $_parser;
public static function setUpBeforeClass()
{
self::$_parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
}
public function testIntOrString()
{
$this->assertEquals(
'int|string',
(string) Type::combineTypes([
Type::parseString('int', false),
Type::parseString('string', false)
])
);
}
public function testArrayOfIntOrString()
{
$this->assertEquals(
'array<int|string>',
(string) Type::combineTypes([
Type::parseString('array<int>', false),
Type::parseString('array<string>', false)
])
);
}
public function testArrayOfIntOrAlsoString()
{
$this->assertEquals('array<int>|string',
(string) Type::combineTypes([
Type::parseString('array<int>', false),
Type::parseString('string', false)
])
);
}
public function testEmptyArrays()
{
$this->assertEquals('array<empty>',
(string) Type::combineTypes([
Type::parseString('array<empty>', false),
Type::parseString('array<empty>', false)
])
);
}
public function testArrayStringOrEmptyArray()
{
$this->assertEquals('array<string>',
(string) Type::combineTypes([
Type::parseString('array<empty>', false),
Type::parseString('array<string>', false)
])
);
}
public function testArrayMixedOrString()
{
$this->assertEquals(
'array<mixed>',
(string) Type::combineTypes([
Type::parseString('array<mixed>', false),
Type::parseString('array<string>', false)
])
);
}
public function testArrayMixedOrEmpty()
{
$this->assertEquals(
'array<mixed>',
(string) Type::combineTypes([
Type::parseString('array<empty>', false),
Type::parseString('array<mixed>', false)
])
);
}
public function testArrayBigCombination()
{
$this->assertEquals(
'array<int|float|string>',
(string) Type::combineTypes([
Type::parseString('array<int|float>', false),
Type::parseString('array<string>', false)
])
);
}
2016-06-16 07:19:52 +02:00
public function testFalseDestruction()
2016-06-16 02:16:40 +02:00
{
$this->assertEquals(
2016-06-16 07:19:52 +02:00
'bool',
2016-06-16 02:16:40 +02:00
(string) Type::combineTypes([
2016-06-16 07:19:52 +02:00
Type::parseString('false', false),
Type::parseString('bool', false)
2016-06-16 02:16:40 +02:00
])
);
}
2016-06-16 07:19:52 +02:00
public function testOnlyFalse()
2016-06-16 02:16:40 +02:00
{
$this->assertEquals(
'bool',
(string) Type::combineTypes([
2016-06-16 07:19:52 +02:00
Type::parseString('false', false)
2016-06-16 02:16:40 +02:00
])
);
}
2016-06-16 07:19:52 +02:00
public function testFalseFalseDestruction()
2016-06-16 02:16:40 +02:00
{
$this->assertEquals(
'bool',
(string) Type::combineTypes([
2016-06-16 07:19:52 +02:00
Type::parseString('false', false),
2016-06-16 02:16:40 +02:00
Type::parseString('false', false)
])
);
}
public function testMultipleValuedArray()
{
$stmts = self::$_parser->parse('<?php
class A {}
class B {}
$var = [];
$var[] = new A();
$var[] = new B();
');
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
}