2019-06-04 17:14:49 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-06-04 17:14:49 +02:00
|
|
|
namespace Psalm\Tests\FileManipulation;
|
|
|
|
|
|
|
|
use Psalm\Context;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2021-07-02 01:10:21 +02:00
|
|
|
use Psalm\Internal\Provider\FakeFileProvider;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
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 20:11:20 +01:00
|
|
|
use Psalm\Tests\TestCase;
|
2019-06-04 17:14:49 +02:00
|
|
|
use Psalm\Tests\TestConfig;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strpos;
|
2019-06-04 17:14:49 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
class ClassConstantMoveTest extends TestCase
|
2019-06-04 17:14:49 +02:00
|
|
|
{
|
2022-12-16 19:58:47 +01:00
|
|
|
protected ProjectAnalyzer $project_analyzer;
|
2019-06-04 17:14:49 +02:00
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function setUp(): void
|
2019-06-04 17:14:49 +02:00
|
|
|
{
|
2020-08-23 16:32:07 +02:00
|
|
|
RuntimeCaches::clearAll();
|
2019-06-04 17:14:49 +02:00
|
|
|
|
2021-07-02 01:10:21 +02:00
|
|
|
$this->file_provider = new FakeFileProvider();
|
2019-06-04 17:14:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerValidCodeParse
|
|
|
|
* @param array<string, string> $constants_to_move
|
|
|
|
*/
|
|
|
|
public function testValidCode(
|
|
|
|
string $input_code,
|
|
|
|
string $output_code,
|
|
|
|
array $constants_to_move
|
2020-09-12 17:24:05 +02:00
|
|
|
): void {
|
2019-06-04 17:14:49 +02:00
|
|
|
$test_name = $this->getTestName();
|
|
|
|
if (strpos($test_name, 'SKIPPED-') !== false) {
|
|
|
|
$this->markTestSkipped('Skipped due to a bug.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = new TestConfig();
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2019-06-04 17:14:49 +02:00
|
|
|
$config,
|
2021-12-03 20:11:20 +01:00
|
|
|
new Providers(
|
2019-06-04 17:14:49 +02:00
|
|
|
$this->file_provider,
|
2022-12-18 17:15:15 +01:00
|
|
|
new FakeParserCacheProvider(),
|
|
|
|
),
|
2019-06-04 17:14:49 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$file_path = self::$src_dir_path . 'somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
2022-12-18 17:15:15 +01:00
|
|
|
$input_code,
|
2019-06-04 17:14:49 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
|
|
|
|
$this->project_analyzer->refactorCodeAfterCompletion($constants_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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array<string,array{input:string,output:string,migrations:array<string, string>}>
|
2019-06-04 17:14:49 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): array
|
2019-06-04 17:14:49 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'moveSimpleClassConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-06-04 17:14:49 +02:00
|
|
|
namespace Ns;
|
|
|
|
|
|
|
|
use ArrayObject;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
const FOO = 15;
|
|
|
|
const FAA = self::FOO;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public static function bar() : void {
|
|
|
|
echo A::FOO;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-06-04 17:14:49 +02:00
|
|
|
namespace Ns;
|
|
|
|
|
|
|
|
use ArrayObject;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
|
|
|
|
const FAA = B::FOO_BAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public static function bar() : void {
|
|
|
|
echo B::FOO_BAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FOO_BAR = 15;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'migrations' => [
|
2019-06-04 17:14:49 +02:00
|
|
|
'Ns\A::FOO' => 'Ns\B::FOO_BAR',
|
2019-07-05 22:24:00 +02:00
|
|
|
],
|
2019-06-04 17:14:49 +02:00
|
|
|
],
|
|
|
|
'renameSimpleClassConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-06-04 17:14:49 +02:00
|
|
|
namespace Ns;
|
|
|
|
|
|
|
|
use ArrayObject;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
const FOO = 15;
|
|
|
|
const FAA = self::FOO;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public static function bar() : void {
|
|
|
|
echo A::FOO;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-06-04 17:14:49 +02:00
|
|
|
namespace Ns;
|
|
|
|
|
|
|
|
use ArrayObject;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
const FOO_BAR = 15;
|
|
|
|
const FAA = self::FOO_BAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public static function bar() : void {
|
|
|
|
echo A::FOO_BAR;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'migrations' => [
|
2019-06-04 17:14:49 +02:00
|
|
|
'Ns\A::FOO' => 'Ns\A::FOO_BAR',
|
2019-07-05 22:24:00 +02:00
|
|
|
],
|
2019-06-04 17:14:49 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|