1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 16:59:45 +01:00
psalm/tests/ProjectCheckerTest.php

381 lines
13 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
2021-06-08 04:55:21 +02:00
use Psalm\Config;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
2021-06-08 04:55:21 +02:00
use Psalm\Internal\IncludeCollector;
use Psalm\Internal\Provider\FakeFileProvider;
2021-12-03 20:11:20 +01:00
use Psalm\Internal\Provider\Providers;
2021-06-08 04:55:21 +02:00
use Psalm\Internal\RuntimeCaches;
2021-12-03 20:11:20 +01:00
use Psalm\IssueBuffer;
2021-06-08 04:55:21 +02:00
use Psalm\Plugin\EventHandler\AfterCodebasePopulatedInterface;
use Psalm\Plugin\EventHandler\Event\AfterCodebasePopulatedEvent;
2021-12-03 20:11:20 +01:00
use Psalm\Report;
use Psalm\Report\ReportOptions;
2021-12-04 21:55:53 +01:00
use Psalm\Tests\Internal\Provider\ClassLikeStorageInstanceCacheProvider;
use Psalm\Tests\Internal\Provider\FakeFileReferenceCacheProvider;
use Psalm\Tests\Internal\Provider\FileStorageInstanceCacheProvider;
use Psalm\Tests\Internal\Provider\ParserInstanceCacheProvider;
use Psalm\Tests\Internal\Provider\ProjectCacheProvider;
2021-06-08 04:55:21 +02:00
use Psalm\Tests\Progress\EchoProgress;
2019-07-05 22:24:00 +02:00
use function define;
use function defined;
use function get_class;
use function getcwd;
use function microtime;
use function ob_end_clean;
use function ob_get_clean;
use function ob_start;
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 function realpath;
2021-06-08 04:55:21 +02:00
use const DIRECTORY_SEPARATOR;
2019-03-23 19:27:54 +01:00
class ProjectCheckerTest extends TestCase
{
2022-12-16 19:58:47 +01:00
protected static TestConfig $config;
2022-12-16 19:58:47 +01:00
protected ProjectAnalyzer $project_analyzer;
public static function setUpBeforeClass(): void
{
self::$config = new TestConfig();
if (!defined('PSALM_VERSION')) {
define('PSALM_VERSION', '4.0.0');
}
if (!defined('PHP_PARSER_VERSION')) {
define('PHP_PARSER_VERSION', '4.0.0');
}
}
public function setUp(): void
{
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();
$this->file_provider = new FakeFileProvider();
}
private function getProjectAnalyzerWithConfig(Config $config): ProjectAnalyzer
{
$config->setIncludeCollector(new IncludeCollector());
2021-12-03 20:11:20 +01:00
return new ProjectAnalyzer(
$config,
2021-12-03 20:11:20 +01:00
new Providers(
$this->file_provider,
2021-12-04 21:55:53 +01:00
new ParserInstanceCacheProvider(),
new FileStorageInstanceCacheProvider(),
new ClassLikeStorageInstanceCacheProvider(),
new FakeFileReferenceCacheProvider(),
2022-12-18 17:15:15 +01:00
new ProjectCacheProvider(),
),
2022-12-18 17:15:15 +01:00
new ReportOptions(),
);
}
public function testCheck(): void
{
2018-11-11 18:01:14 +01:00
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
(string)getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="tests/fixtures/DummyProject" />
</projectFiles>
2022-12-18 17:15:15 +01:00
</psalm>',
),
);
$this->project_analyzer->setPhpVersion('8.1', 'tests');
$this->project_analyzer->progress = new EchoProgress();
ob_start();
$this->project_analyzer->check('tests/fixtures/DummyProject');
$output = ob_get_clean();
$this->assertStringContainsString('Target PHP version: 8.1 (set by tests)', $output);
$this->assertStringContainsString('Scanning files...', $output);
$this->assertStringContainsString('Analyzing files...', $output);
2021-12-03 20:11:20 +01:00
$this->assertSame(0, IssueBuffer::getErrorCount());
$codebase = $this->project_analyzer->getCodebase();
$this->assertSame([0, 5], $codebase->analyzer->getTotalTypeCoverage($codebase));
$this->assertSame(
'Psalm was able to infer types for 100% of the codebase',
$codebase->analyzer->getTypeInferenceSummary(
2022-12-18 17:15:15 +01:00
$codebase,
),
);
}
public function testAfterCodebasePopulatedIsInvoked(): void
2019-02-24 19:48:57 +01:00
{
2019-03-23 19:27:54 +01:00
$hook = new class implements AfterCodebasePopulatedInterface {
2022-12-16 19:58:47 +01:00
public static bool $called = false;
2019-03-23 19:27:54 +01:00
/**
* @return void
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint
*/
public static function afterCodebasePopulated(AfterCodebasePopulatedEvent $event)
2019-02-24 19:48:57 +01:00
{
self::$called = true;
}
};
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
(string)getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="tests/fixtures/DummyProject" />
2019-02-24 19:48:57 +01:00
</projectFiles>
2022-12-18 17:15:15 +01:00
</psalm>',
),
2019-02-24 19:48:57 +01:00
);
2019-03-01 14:45:18 +01:00
$hook_class = get_class($hook);
$this->project_analyzer->getCodebase()->config->eventDispatcher->after_codebase_populated[] = $hook_class;
2019-02-24 19:48:57 +01:00
ob_start();
$this->project_analyzer->check('tests/fixtures/DummyProject');
2019-02-24 20:14:14 +01:00
ob_end_clean();
2019-02-24 19:48:57 +01:00
$this->assertTrue($hook::$called);
}
public function testCheckAfterNoChange(): void
{
2018-11-11 18:01:14 +01:00
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
(string)getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="tests/fixtures/DummyProject" />
</projectFiles>
2022-12-18 17:15:15 +01:00
</psalm>',
),
);
$this->assertNotNull($this->project_analyzer->stdout_report_options);
2021-12-03 20:11:20 +01:00
$this->project_analyzer->stdout_report_options->format = Report::TYPE_JSON;
$this->project_analyzer->check('tests/fixtures/DummyProject', true);
ob_start();
2021-12-03 20:11:20 +01:00
IssueBuffer::finish($this->project_analyzer, true, microtime(true));
ob_end_clean();
$this->assertSame(
'Psalm was able to infer types for 100% of the codebase',
$this->project_analyzer->getCodebase()->analyzer->getTypeInferenceSummary(
2022-12-18 17:15:15 +01:00
$this->project_analyzer->getCodebase(),
),
);
2018-11-11 18:01:14 +01:00
$this->project_analyzer->getCodebase()->reloadFiles($this->project_analyzer, []);
$this->project_analyzer->check('tests/fixtures/DummyProject', true);
2021-12-03 20:11:20 +01:00
$this->assertSame(0, IssueBuffer::getErrorCount());
$this->assertSame(
2020-06-06 22:57:25 +02:00
'Psalm was able to infer types for 100% of the codebase',
$this->project_analyzer->getCodebase()->analyzer->getTypeInferenceSummary(
2022-12-18 17:15:15 +01:00
$this->project_analyzer->getCodebase(),
),
);
}
public function testCheckAfterFileChange(): void
{
2018-11-11 18:01:14 +01:00
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
(string)getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="tests/fixtures/DummyProject" />
</projectFiles>
2022-12-18 17:15:15 +01:00
</psalm>',
),
);
$this->assertNotNull($this->project_analyzer->stdout_report_options);
2021-12-03 20:11:20 +01:00
$this->project_analyzer->stdout_report_options->format = Report::TYPE_JSON;
$this->project_analyzer->check('tests/fixtures/DummyProject', true);
ob_start();
2021-12-03 20:11:20 +01:00
IssueBuffer::finish($this->project_analyzer, true, microtime(true));
ob_end_clean();
$this->assertSame(
'Psalm was able to infer types for 100% of the codebase',
$this->project_analyzer->getCodebase()->analyzer->getTypeInferenceSummary(
2022-12-18 17:15:15 +01:00
$this->project_analyzer->getCodebase(),
),
);
$bat_file_path = getcwd()
. DIRECTORY_SEPARATOR . 'tests'
. DIRECTORY_SEPARATOR . 'fixtures'
. DIRECTORY_SEPARATOR . 'DummyProject'
. DIRECTORY_SEPARATOR . 'Bat.php';
$bat_replacement_contents = '<?php
namespace Vimeo\Test\DummyProject;
class Bat
{
public function __construct()
{
$a = new Bar();
}
}
';
$this->file_provider->registerFile($bat_file_path, $bat_replacement_contents);
2018-11-11 18:01:14 +01:00
$this->project_analyzer->getCodebase()->reloadFiles($this->project_analyzer, []);
$this->project_analyzer->check('tests/fixtures/DummyProject', true);
2021-12-03 20:11:20 +01:00
$this->assertSame(0, IssueBuffer::getErrorCount());
$this->assertSame(
'Psalm was able to infer types for 100% of the codebase',
$this->project_analyzer->getCodebase()->analyzer->getTypeInferenceSummary(
2022-12-18 17:15:15 +01:00
$this->project_analyzer->getCodebase(),
),
);
}
public function testCheckDir(): void
{
2018-11-11 18:01:14 +01:00
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
(string)getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="tests/fixtures/DummyProject" />
</projectFiles>
2022-12-18 17:15:15 +01:00
</psalm>',
),
);
$this->project_analyzer->setPhpVersion('8.1', 'tests');
$this->project_analyzer->progress = new EchoProgress();
ob_start();
$this->project_analyzer->checkDir('tests/fixtures/DummyProject');
$output = ob_get_clean();
$this->assertStringContainsString('Target PHP version: 8.1 (set by tests)', $output);
$this->assertStringContainsString('Scanning files...', $output);
$this->assertStringContainsString('Analyzing files...', $output);
2021-12-03 20:11:20 +01:00
$this->assertSame(0, IssueBuffer::getErrorCount());
$this->assertSame(
'Psalm was able to infer types for 100% of the codebase',
$this->project_analyzer->getCodebase()->analyzer->getTypeInferenceSummary(
2022-12-18 17:15:15 +01:00
$this->project_analyzer->getCodebase(),
),
);
}
public function testCheckPaths(): void
{
2018-11-11 18:01:14 +01:00
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
(string)getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="tests/fixtures/DummyProject" />
</projectFiles>
2022-12-18 17:15:15 +01:00
</psalm>',
),
);
$this->project_analyzer->setPhpVersion('8.1', 'tests');
$this->project_analyzer->progress = new EchoProgress();
ob_start();
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
// checkPaths expects absolute paths,
// otherwise it's unable to match them against configured folders
$this->project_analyzer->checkPaths([
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
realpath(getcwd() . '/tests/fixtures/DummyProject/Bar.php'),
realpath(getcwd() . '/tests/fixtures/DummyProject/SomeTrait.php'),
]);
$output = ob_get_clean();
$this->assertStringContainsString('Target PHP version: 8.1 (set by tests)', $output);
$this->assertStringContainsString('Scanning files...', $output);
$this->assertStringContainsString('Analyzing files...', $output);
2021-12-03 20:11:20 +01:00
$this->assertSame(0, IssueBuffer::getErrorCount());
$this->assertSame(
'Psalm was able to infer types for 100% of the codebase',
$this->project_analyzer->getCodebase()->analyzer->getTypeInferenceSummary(
2022-12-18 17:15:15 +01:00
$this->project_analyzer->getCodebase(),
),
);
}
public function testCheckFile(): void
{
2018-11-11 18:01:14 +01:00
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
(string)getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="tests/fixtures/DummyProject" />
</projectFiles>
2022-12-18 17:15:15 +01:00
</psalm>',
),
);
$this->project_analyzer->setPhpVersion('8.1', 'tests');
$this->project_analyzer->progress = new EchoProgress();
ob_start();
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
// checkPaths expects absolute paths,
// otherwise it's unable to match them against configured folders
$this->project_analyzer->checkPaths([
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
realpath(getcwd() . '/tests/fixtures/DummyProject/Bar.php'),
realpath(getcwd() . '/tests/fixtures/DummyProject/SomeTrait.php'),
]);
$output = ob_get_clean();
$this->assertStringContainsString('Target PHP version: 8.1 (set by tests)', $output);
$this->assertStringContainsString('Scanning files...', $output);
$this->assertStringContainsString('Analyzing files...', $output);
2021-12-03 20:11:20 +01:00
$this->assertSame(0, IssueBuffer::getErrorCount());
$this->assertSame(
'Psalm was able to infer types for 100% of the codebase',
$this->project_analyzer->getCodebase()->analyzer->getTypeInferenceSummary(
2022-12-18 17:15:15 +01:00
$this->project_analyzer->getCodebase(),
),
);
}
}