mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Added tests for exposed methods
This commit is contained in:
parent
0e5ee1a811
commit
991fd90a12
@ -1162,8 +1162,6 @@ class Codebase
|
||||
*
|
||||
* Useful for emitting issues like InvalidArgument, where argument at the call site
|
||||
* should be a subset of the function parameter type.
|
||||
*
|
||||
* @psalm-suppress PossiblyUnusedMethod
|
||||
*/
|
||||
public function isTypeContainedByType(
|
||||
Type\Union $input_type,
|
||||
@ -1184,8 +1182,6 @@ class Codebase
|
||||
* Useful for emitting issues like PossiblyInvalidArgument, where argument at the call
|
||||
* site should be a subtype of the function parameter type, but it's has some types that are
|
||||
* not a subtype of the required type.
|
||||
*
|
||||
* @psalm-suppress PossiblyUnusedMethod
|
||||
*/
|
||||
public function canTypeBeContainedByType(
|
||||
Type\Union $input_type,
|
||||
@ -1207,7 +1203,6 @@ class Codebase
|
||||
* ```
|
||||
*
|
||||
* @return array{Type\Union,Type\Union}
|
||||
* @psalm-suppress PossiblyUnusedMethod
|
||||
*/
|
||||
public function getKeyValueParamsForTraversableObject(Type\Atomic $type): array
|
||||
{
|
||||
|
114
tests/CodebaseTest.php
Normal file
114
tests/CodebaseTest.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
namespace Psalm\Tests;
|
||||
|
||||
use Generator;
|
||||
use Psalm\Codebase;
|
||||
use Psalm\Context;
|
||||
use Psalm\Type;
|
||||
|
||||
class CodebaseTest extends TestCase
|
||||
{
|
||||
/** @var Codebase */
|
||||
private $codebase;
|
||||
|
||||
/** @return void */
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->codebase = $this->project_analyzer->getCodebase();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider typeContainments
|
||||
* @return void
|
||||
*/
|
||||
public function isTypeContainedByType(string $input, string $container, bool $expected)
|
||||
{
|
||||
$input = Type::parseString($input);
|
||||
$container = Type::parseString($container);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->codebase->isTypeContainedByType($input, $container),
|
||||
'Expected ' . $input->getId() . ($expected ? ' ' : ' not ')
|
||||
. 'to be contained in ' . $container->getId()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/** @return iterable<int,array{string,string,bool} */
|
||||
public function typeContainments()
|
||||
{
|
||||
yield ['int', 'int|string', true];
|
||||
yield ['int|string', 'int', false];
|
||||
|
||||
// This fails with 'could not get class storage' :(
|
||||
|
||||
// yield ['RuntimeException', 'Exception', true];
|
||||
// yield ['Exception', 'RuntimeException', false];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider typeIntersections
|
||||
* @return void
|
||||
*/
|
||||
public function canTypeBeContainedByType(string $input, string $container, bool $expected)
|
||||
{
|
||||
$input = Type::parseString($input);
|
||||
$container = Type::parseString($container);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->codebase->canTypeBeContainedByType($input, $container),
|
||||
'Expected ' . $input->getId() . ($expected ? ' ' : ' not ')
|
||||
. 'to be contained in ' . $container->getId()
|
||||
);
|
||||
}
|
||||
|
||||
/** @return iterable<int,array{string,string,bool} */
|
||||
public function typeIntersections()
|
||||
{
|
||||
yield ['int', 'int|string', true];
|
||||
yield ['int|string', 'int', true];
|
||||
yield ['int|string', 'string|float', true];
|
||||
yield ['int', 'string', false];
|
||||
yield ['int|string', 'array|float', false];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider iterableParams
|
||||
* @param array{string,string} $expected
|
||||
* @return void
|
||||
*/
|
||||
public function getKeyValueParamsForTraversableObject(string $input, array $expected)
|
||||
{
|
||||
list($input) = array_values(Type::parseString($input)->getTypes());
|
||||
|
||||
$expected_key_type = Type::parseString($expected[0]);
|
||||
$expected_value_type = Type::parseString($expected[1]);
|
||||
|
||||
$actual = $this->codebase->getKeyValueParamsForTraversableObject($input);
|
||||
|
||||
$this->assertTrue(
|
||||
$expected_key_type->equals($actual[0]),
|
||||
'Expected ' . $input->getId() . ' to have ' . $expected_key_type
|
||||
. ' but got ' . $actual[0]->getId()
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$expected_value_type->equals($actual[1]),
|
||||
'Expected ' . $input->getId() . ' to have ' . $expected_value_type
|
||||
. ' but got ' . $actual[1]->getId()
|
||||
);
|
||||
}
|
||||
|
||||
/** @return iterable<int,array{string,array{string,string}} */
|
||||
public function iterableParams()
|
||||
{
|
||||
yield ['iterable<int,string>', ['int', 'string']];
|
||||
yield ['iterable<int|string,bool|float', ['int|string', 'bool|float']];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user