1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/FileReferenceTest.php

421 lines
13 KiB
PHP
Raw Normal View History

2018-01-22 05:42:57 +01:00
<?php
2018-01-22 05:42:57 +01:00
namespace Psalm\Tests;
use Psalm\Context;
2021-12-03 20:11:20 +01:00
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\FakeFileProvider;
2021-12-03 20:11:20 +01:00
use Psalm\Internal\Provider\Providers;
Test parallelization (#4045) * Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
2020-08-23 16:32:07 +02:00
use Psalm\Internal\RuntimeCaches;
2021-12-04 21:55:53 +01:00
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
2021-12-03 21:40:18 +01:00
use UnexpectedValueException;
2021-06-08 04:55:21 +02:00
use function count;
use function strpos;
2018-01-22 05:42:57 +01:00
class FileReferenceTest extends TestCase
{
2021-12-04 03:37:19 +01:00
/** @var ProjectAnalyzer */
2018-11-11 18:01:14 +01:00
protected $project_analyzer;
2018-01-22 05:42:57 +01:00
public function setUp(): void
2018-01-22 05:42:57 +01:00
{
Test parallelization (#4045) * Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
2020-08-23 16:32:07 +02:00
RuntimeCaches::clearAll();
2018-01-22 05:42:57 +01:00
$this->file_provider = new FakeFileProvider();
2018-01-22 05:42:57 +01:00
2021-12-03 20:11:20 +01:00
$this->project_analyzer = new ProjectAnalyzer(
2018-01-22 05:42:57 +01:00
new TestConfig(),
2021-12-03 20:11:20 +01:00
new Providers(
$this->file_provider,
2021-12-04 21:55:53 +01:00
new FakeParserCacheProvider()
)
2018-01-22 05:42:57 +01:00
);
$this->project_analyzer->getCodebase()->collectLocations();
$this->project_analyzer->setPhpVersion('7.3', 'tests');
2018-01-22 05:42:57 +01:00
}
/**
* @dataProvider providerReferenceLocations
2018-01-22 05:42:57 +01:00
*
* @param string $input_code
* @param string $symbol
* @param array<int, string> $expected_locations
*
*/
public function testReferenceLocations($input_code, $symbol, $expected_locations): void
2018-01-22 05:42:57 +01:00
{
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
2018-01-22 05:42:57 +01:00
$this->markTestSkipped('Skipped due to a bug.');
}
$context = new Context();
$file_path = self::$src_dir_path . 'somefile.php';
$this->addFile($file_path, $input_code);
$this->analyzeFile($file_path, $context);
2018-11-11 18:01:14 +01:00
$found_references = $this->project_analyzer->getCodebase()->findReferencesToSymbol($symbol);
2018-01-22 05:42:57 +01:00
if (!$found_references) {
2021-12-03 21:40:18 +01:00
throw new UnexpectedValueException('No file references found in this file');
2018-01-22 05:42:57 +01:00
}
$this->assertSame(count($found_references), count($expected_locations));
2018-01-22 05:42:57 +01:00
foreach ($expected_locations as $i => $expected_location) {
$actual_location = $found_references[$i];
2018-01-22 05:42:57 +01:00
$this->assertSame(
$expected_location,
$actual_location->getLineNumber() . ':' . $actual_location->getColumn()
. ':' . $actual_location->getSelectedText()
);
}
}
/**
* @dataProvider providerReferencedMethods
*
* @param array<string,array<string,bool>> $expected_method_references_to_members
* @param array<string,array<string,bool>> $expected_file_references_to_members
* @param array<string,array<string,bool>> $expected_method_references_to_missing_members
* @param array<string,array<string,bool>> $expected_file_references_to_missing_members
*
*/
public function testReferencedMethods(
string $input_code,
array $expected_method_references_to_members,
array $expected_method_references_to_missing_members,
array $expected_file_references_to_members,
array $expected_file_references_to_missing_members
): void {
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
$context = new Context();
2018-11-02 03:03:47 +01:00
$file_path = '/var/www/somefile.php';
$this->addFile($file_path, $input_code);
$this->analyzeFile($file_path, $context);
$referenced_members = $this->project_analyzer->getCodebase()->file_reference_provider->getAllMethodReferencesToClassMembers();
$this->assertSame($expected_method_references_to_members, $referenced_members);
$referenced_missing_members = $this->project_analyzer->getCodebase()->file_reference_provider->getAllMethodReferencesToMissingClassMembers();
$this->assertSame($expected_method_references_to_missing_members, $referenced_missing_members);
$referenced_files = $this->project_analyzer->getCodebase()->file_reference_provider->getAllFileReferencesToClassMembers();
$this->assertSame($expected_file_references_to_members, $referenced_files);
$referenced_missing_files = $this->project_analyzer->getCodebase()->file_reference_provider->getAllFileReferencesToMissingClassMembers();
$this->assertSame($expected_file_references_to_missing_members, $referenced_missing_files);
}
2018-01-22 05:42:57 +01:00
/**
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395) * Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389) * Default to sealed arrays * Fix array_merge bug * Fixes * Fix * Sealed type checks * Properly infer properties-of and get_object_vars on final classes * Fix array_map zipping * Fix tests * Fixes * Fixes * Fix more stuff * Recursively resolve type aliases * Fix typo * Fixes * Fix array_is_list assertion on keyed array * Add BC docs * Fixes * fix * Update * Update * Update * Update * Seal arrays with count assertions * Fix #8528 * Fix * Update * Improve sealed array foreach logic * get_object_vars on template properties * Fix sealed array assertion reconciler logic * Improved reconciler * Add tests * Single source of truth for test types * Fix tests * Fixup tests * Fixup tests * Fixup tests * Update * Fix tests * Fix tests * Final fixes * Fixes * Use list syntax only when needed * Fix tests * Cs-fix * Update docs * Update docs * Update docs * Update docs * Update docs * Document missing types * Update docs * Improve class-string-map docs * Update * Update * I love working on psalm :) * Keep arrays unsealed by default * Fixup tests * Fix syntax mistake * cs-fix * Fix typo * Re-import missing types * Keep strict types only in return types * argc/argv fixes * argc/argv fixes * Fix test * Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
2022-11-05 22:34:42 +01:00
* @return array<string,strict-array{string,string,array<int,string>}>
2018-01-22 05:42:57 +01:00
*/
public function providerReferenceLocations(): array
2018-01-22 05:42:57 +01:00
{
return [
'getClassLocation' => [
'<?php
class A {}
new A();',
'A',
2019-03-23 19:27:54 +01:00
['4:25:A'],
2018-01-22 05:42:57 +01:00
],
'getMethodLocation' => [
'<?php
class A {
public function foo(): void {}
}
(new A())->foo();',
'A::foo',
['6:32:foo'],
2018-01-22 05:42:57 +01:00
],
];
}
/**
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395) * Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389) * Default to sealed arrays * Fix array_merge bug * Fixes * Fix * Sealed type checks * Properly infer properties-of and get_object_vars on final classes * Fix array_map zipping * Fix tests * Fixes * Fixes * Fix more stuff * Recursively resolve type aliases * Fix typo * Fixes * Fix array_is_list assertion on keyed array * Add BC docs * Fixes * fix * Update * Update * Update * Update * Seal arrays with count assertions * Fix #8528 * Fix * Update * Improve sealed array foreach logic * get_object_vars on template properties * Fix sealed array assertion reconciler logic * Improved reconciler * Add tests * Single source of truth for test types * Fix tests * Fixup tests * Fixup tests * Fixup tests * Update * Fix tests * Fix tests * Final fixes * Fixes * Use list syntax only when needed * Fix tests * Cs-fix * Update docs * Update docs * Update docs * Update docs * Update docs * Document missing types * Update docs * Improve class-string-map docs * Update * Update * I love working on psalm :) * Keep arrays unsealed by default * Fixup tests * Fix syntax mistake * cs-fix * Fix typo * Re-import missing types * Keep strict types only in return types * argc/argv fixes * argc/argv fixes * Fix test * Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
2022-11-05 22:34:42 +01:00
* @return array<string, strict-array{
* 0: string,
* 1: array<string,array<string,bool>>,
* 2: array<string,array<string,bool>>,
* 3: array<string,array<string,bool>>,
* 4: array<string,array<string,bool>>
* }>
*/
public function providerReferencedMethods(): array
{
return [
'getClassReferences' => [
'<?php
namespace Foo;
class A {
public static function bat() : void {
}
}
class B {
public function __construct() {
new A();
A::bat();
}
public function bar() : void {
(new C)->foo();
}
}
class C {
public function foo() : void {
new A();
}
}
class D {
/** @var ?string */
public $foo;
public function __construct() {}
}
$d = new D();
$d->foo = "bar";
$a = new A();',
[
2018-11-02 03:03:47 +01:00
'use:A:d7863b8594fe57f85cb8183fe55a6c15' => [
2018-11-02 02:52:39 +01:00
'foo\b::__construct' => true,
'foo\c::foo' => true,
],
'foo\a::bat' => [
'foo\b::__construct' => true,
],
2018-11-02 03:03:47 +01:00
'use:C:d7863b8594fe57f85cb8183fe55a6c15' => [
2018-11-02 02:52:39 +01:00
'foo\b::bar' => true,
],
'foo\c::foo' => [
'foo\b::bar' => true,
],
],
[
'foo\a::__construct' => [
'foo\b::__construct' => true,
'foo\c::foo' => true,
],
'foo\c::__construct' => [
'foo\b::bar' => true,
2019-03-23 19:27:54 +01:00
],
],
[
'foo\d::__construct' => [
'/var/www/somefile.php' => true,
],
'foo\d::$foo' => [
'/var/www/somefile.php' => true,
],
],
[
'foo\a::__construct' => [
'/var/www/somefile.php' => true,
],
],
],
'interpolateClassCalls' => [
'<?php
namespace Foo;
class A {
public function __construct() {}
public static function bar() : void {}
}
class B extends A { }
class C extends B { }
class D {
public function bat() : void {
$c = new C();
$c->bar();
}
}',
[
2018-11-02 03:03:47 +01:00
'use:C:d7863b8594fe57f85cb8183fe55a6c15' => [
2018-11-02 02:52:39 +01:00
'foo\d::bat' => true,
],
'foo\b::__construct' => [
'foo\d::bat' => true,
],
'foo\a::__construct' => [
'foo\d::bat' => true,
],
'foo\c::__construct' => [
'foo\d::bat' => true,
],
'foo\b::bar' => [
'foo\d::bat' => true,
],
'foo\a::bar' => [
'foo\d::bat' => true,
],
'foo\c::bar' => [
'foo\d::bat' => true,
],
],
[],
[],
2019-07-05 22:24:00 +02:00
[],
],
'constantRefs' => [
'<?php
namespace Foo;
class A {
const C = "bar";
}
class B {
public function __construct() {
echo A::C;
}
}
class C {
public function foo() : void {
echo A::C;
}
}',
[
'foo\a::C' => [
'foo\b::__construct' => true,
'foo\c::foo' => true,
],
],
[],
[],
[],
],
'staticPropertyRefs' => [
'<?php
namespace Foo;
class A {
/** @var int */
public static $fooBar = 5;
}
class B {
public function __construct() {
echo A::$fooBar;
}
}
class C {
public function foo() : void {
echo A::$fooBar;
}
}',
[
2018-11-02 03:03:47 +01:00
'use:A:d7863b8594fe57f85cb8183fe55a6c15' => [
2018-11-02 02:52:39 +01:00
'foo\b::__construct' => true,
'foo\c::foo' => true,
],
'foo\a::$fooBar' => [
'foo\b::__construct' => true,
'foo\c::foo' => true,
],
],
[],
[],
2019-07-05 22:24:00 +02:00
[],
],
'instancePropertyRefs' => [
'<?php
namespace Foo;
class A {
/** @var int */
public $fooBar = 5;
}
class B {
public function __construct() {
echo (new A)->fooBar;
}
}
class C {
public function foo() : void {
echo (new A)->fooBar;
}
}',
[
'use:A:d7863b8594fe57f85cb8183fe55a6c15' => [
'foo\b::__construct' => true,
'foo\c::foo' => true,
],
'foo\a::$fooBar' => [
2018-11-02 02:52:39 +01:00
'foo\b::__construct' => true,
'foo\c::foo' => true,
],
],
[
'foo\a::__construct' => [
'foo\b::__construct' => true,
'foo\c::foo' => true,
],
],
[],
2019-07-05 22:24:00 +02:00
[],
],
2019-05-23 19:10:23 +02:00
'traitAbstractRefs' => [
'<?php
namespace Ns;
abstract class A {
public function foo() : void {}
}
trait T {
public function bar(A $a) : void {
$a->foo();
}
}
class C {
use T;
}',
[
2021-02-15 17:31:34 +01:00
'use:A:d7863b8594fe57f85cb8183fe55a6c15' => [
'ns\c::bar' => true
],
'ns\a::foo' => [
'ns\c::bar' => true,
],
],
[],
[],
[],
],
];
}
2018-01-22 05:42:57 +01:00
}