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

216 lines
5.6 KiB
PHP
Raw Normal View History

2019-06-04 06:32:19 +02:00
<?php
namespace Psalm\Tests\FileManipulation;
use Psalm\Context;
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;
2019-06-04 06:32:19 +02:00
use Psalm\Tests\Internal\Provider;
use Psalm\Tests\TestConfig;
use function strpos;
2019-06-04 06:32:19 +02:00
class PropertyMoveTest extends \Psalm\Tests\TestCase
{
/** @var \Psalm\Internal\Analyzer\ProjectAnalyzer */
protected $project_analyzer;
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();
2019-06-04 06:32:19 +02:00
$this->file_provider = new Provider\FakeFileProvider();
}
/**
* @dataProvider providerValidCodeParse
*
* @param array<string, string> $properties_to_move
* @param array<string, string> $call_transforms
*
*/
public function testValidCode(
string $input_code,
string $output_code,
array $properties_to_move
): void {
2019-06-04 06:32:19 +02:00
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
$config = new TestConfig();
$this->project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer(
$config,
new \Psalm\Internal\Provider\Providers(
$this->file_provider,
new Provider\FakeParserCacheProvider()
)
);
$context = new Context();
$file_path = self::$src_dir_path . 'somefile.php';
$this->addFile(
$file_path,
$input_code
);
$codebase = $this->project_analyzer->getCodebase();
$this->project_analyzer->refactorCodeAfterCompletion($properties_to_move);
$this->analyzeFile($file_path, $context);
$this->project_analyzer->prepareMigration();
$codebase->analyzer->updateFile($file_path, false);
$this->project_analyzer->migrateCode();
$this->assertSame($output_code, $codebase->getFileContents($file_path));
}
/**
* @return array<string,array{string,string,array<string, string>}>
*/
public function providerValidCodeParse(): array
2019-06-04 06:32:19 +02:00
{
return [
'moveSimpleStaticProperty' => [
'<?php
namespace Ns;
use ArrayObject;
class A {
/** @var ArrayObject<int, int> */
public static $foo;
}
class B {
public static function bar() : void {
foreach (A::$foo as $f) {}
}
}',
'<?php
namespace Ns;
use ArrayObject;
class A {
}
class B {
public static function bar() : void {
foreach (B::$fooBar as $f) {}
}
/** @var ArrayObject<int, int> */
public static $fooBar;
}',
[
'Ns\A::$foo' => 'Ns\B::$fooBar',
2019-07-05 22:24:00 +02:00
],
2019-06-04 06:32:19 +02:00
],
'renameInstanceProperty' => [
'<?php
namespace Ns;
class A {
/** @var ?int */
public $foo;
}
function foo(A $a) {
echo $a->foo;
$a->foo = 10;
}',
'<?php
namespace Ns;
class A {
/** @var ?int */
public $fooBar;
}
function foo(A $a) {
echo $a->fooBar;
$a->fooBar = 10;
}',
[
'Ns\A::$foo' => 'Ns\A::$fooBar',
2019-07-05 22:24:00 +02:00
],
2019-06-04 06:32:19 +02:00
],
'renameStaticProperty' => [
'<?php
namespace Ns;
class A {
/** @var ?int */
public static $foo;
}
function foo() {
echo A::$foo;
A::$foo = 10;
}',
'<?php
namespace Ns;
class A {
/** @var ?int */
public static $fooBar;
}
function foo() {
echo A::$fooBar;
A::$fooBar = 10;
}',
[
'Ns\A::$foo' => 'Ns\A::$fooBar',
2019-07-05 22:24:00 +02:00
],
2019-06-04 06:32:19 +02:00
],
'moveStaticProperty' => [
'<?php
namespace Ns;
class A {
/** @var ?int */
public static $foo;
}
class B {
}
function foo() {
echo A::$foo;
A::$foo = 10;
}',
'<?php
namespace Ns;
class A {
}
class B {
/** @var int|null */
2019-06-04 06:32:19 +02:00
public static $fooBar;
}
function foo() {
echo B::$fooBar;
B::$fooBar = 10;
}',
[
'Ns\A::$foo' => 'Ns\B::$fooBar',
2019-07-05 22:24:00 +02:00
],
2019-06-04 06:32:19 +02:00
],
];
}
}