2019-06-14 00:28:46 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-06-14 00:28:46 +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-14 00:28:46 +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-14 00:28:46 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
class NamespaceMoveTest extends TestCase
|
2019-06-14 00:28:46 +02:00
|
|
|
{
|
2021-12-04 03:37:19 +01:00
|
|
|
/** @var ProjectAnalyzer */
|
2019-06-14 00:28:46 +02:00
|
|
|
protected $project_analyzer;
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function setUp(): void
|
2019-06-14 00:28:46 +02:00
|
|
|
{
|
2020-08-23 16:32:07 +02:00
|
|
|
RuntimeCaches::clearAll();
|
2019-06-14 00:28:46 +02:00
|
|
|
|
2021-07-02 01:10:21 +02:00
|
|
|
$this->file_provider = new FakeFileProvider();
|
2019-06-14 00:28:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerValidCodeParse
|
|
|
|
*
|
|
|
|
* @param array<string, string> $namespaces_to_move
|
|
|
|
*/
|
|
|
|
public function testValidCode(
|
|
|
|
string $input_code,
|
|
|
|
string $output_code,
|
|
|
|
array $namespaces_to_move
|
2020-09-12 17:24:05 +02:00
|
|
|
): void {
|
2019-06-14 00:28:46 +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-14 00:28:46 +02:00
|
|
|
$config,
|
2021-12-03 20:11:20 +01:00
|
|
|
new Providers(
|
2019-06-14 00:28:46 +02:00
|
|
|
$this->file_provider,
|
2021-12-04 21:55:53 +01:00
|
|
|
new FakeParserCacheProvider()
|
2019-06-14 00:28:46 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$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($namespaces_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>}>
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): array
|
2019-06-14 00:28:46 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'moveClassesIntoNamespace' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo {
|
|
|
|
class A {
|
|
|
|
/** @var ?B */
|
|
|
|
public $x = null;
|
|
|
|
/** @var ?A */
|
|
|
|
public $y = null;
|
|
|
|
/** @var A|B|C|null */
|
|
|
|
public $z = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Foo {
|
|
|
|
class B {
|
|
|
|
/** @var ?A */
|
|
|
|
public $x = null;
|
|
|
|
/** @var ?B */
|
|
|
|
public $y = null;
|
|
|
|
/** @var A|B|C|null */
|
|
|
|
public $z = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Bar {
|
|
|
|
use Foo\A;
|
|
|
|
use Foo\B;
|
|
|
|
|
|
|
|
class C {
|
|
|
|
/** @var ?A */
|
|
|
|
public $x = null;
|
|
|
|
/** @var ?B */
|
|
|
|
public $y = null;
|
|
|
|
/** @var null|A|B */
|
|
|
|
public $z = null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
namespace Bar\Baz {
|
|
|
|
class A {
|
2019-10-17 07:14:33 +02:00
|
|
|
/** @var B|null */
|
2019-06-14 00:28:46 +02:00
|
|
|
public $x = null;
|
|
|
|
/** @var null|self */
|
|
|
|
public $y = null;
|
2019-10-17 07:14:33 +02:00
|
|
|
/** @var B|\Foo\C|null|self */
|
2019-06-14 00:28:46 +02:00
|
|
|
public $z = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Bar\Baz {
|
|
|
|
class B {
|
2019-10-17 07:14:33 +02:00
|
|
|
/** @var A|null */
|
2019-06-14 00:28:46 +02:00
|
|
|
public $x = null;
|
|
|
|
/** @var null|self */
|
|
|
|
public $y = null;
|
2019-10-17 07:14:33 +02:00
|
|
|
/** @var A|\Foo\C|null|self */
|
2019-06-14 00:28:46 +02:00
|
|
|
public $z = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Bar {
|
|
|
|
use Bar\Baz\A;
|
|
|
|
use Bar\Baz\B;
|
|
|
|
|
|
|
|
class C {
|
2019-10-17 07:14:33 +02:00
|
|
|
/** @var A|null */
|
2019-06-14 00:28:46 +02:00
|
|
|
public $x = null;
|
2019-10-17 07:14:33 +02:00
|
|
|
/** @var B|null */
|
2019-06-14 00:28:46 +02:00
|
|
|
public $y = null;
|
2019-10-17 07:14:33 +02:00
|
|
|
/** @var A|B|null */
|
2019-06-14 00:28:46 +02:00
|
|
|
public $z = null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[
|
|
|
|
'Foo\*' => 'Bar\Baz\*',
|
2019-07-05 22:24:00 +02:00
|
|
|
],
|
2019-06-14 00:28:46 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|