-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
TolerantImportNameTest.php
105 lines (88 loc) · 3.13 KB
/
TolerantImportNameTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Phpactor\CodeTransform\Tests\Adapter\TolerantParser\Refactor;
use Generator;
use Phpactor\CodeTransform\Adapter\TolerantParser\Refactor\TolerantImportName;
use Phpactor\CodeTransform\Domain\Refactor\ImportClass\NameImport;
use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\TextDocument\ByteOffset;
use Phpactor\TextDocument\TextEdits;
class TolerantImportNameTest extends AbstractTolerantImportNameCase
{
public function provideImportClass(): Generator
{
yield 'with existing class imports' => [
'importClass1.test',
'Barfoo\Foobar',
];
yield 'with namespace' => [
'importClass2.test',
'Barfoo\Foobar',
];
yield 'with no namespace declaration or use statements' => [
'importClass3.test',
'Barfoo\Foobar',
];
yield 'with alias' => [
'importClass4.test',
'Barfoo\Foobar',
'Barfoo',
];
yield 'with static alias' => [
'importClass5.test',
'Barfoo\Foobar',
'Barfoo',
];
yield 'with multiple aliases' => [
'importClass6.test',
'Barfoo\Foobar',
'Barfoo',
];
yield 'with alias and existing name' => [
'importClass7.test',
'Barfoo\Foobar',
'Barfoo',
];
yield 'with class in root namespace' => [
'importClass8.test',
'Foobar',
];
yield 'from phpdoc' => [
'importClass9.test',
'Barfoo\Foobar',
];
yield 'from phpdoc (resolved in a SourceFileNode)' => [
'importClass10.test',
'Barfoo\Foobar',
];
}
public function provideImportFunction(): Generator
{
yield 'import function' => [
'importFunction1.test',
'Acme\foobar',
];
}
public function testImportsGlobal(): void
{
$source = '<?php namespace Foobar;';
$edits = $this->importName($source, 10, NameImport::forFunction('array_map', null), true);
self::assertStringContainsString('array_map', $edits->apply($source));
}
public function testNotImportGlobalWhenDisabled(): void
{
$source = '<?php namespace Foobar;';
$edits = $this->importName($source, 10, NameImport::forFunction('array_map', null), false);
self::assertStringNotContainsString('array_map', $edits->apply($source));
}
public function testImportNotGlobalWhenDisabled(): void
{
$source = '<?php namespace Foobar;';
$edits = $this->importName($source, 10, NameImport::forFunction('Bar\array_map', null), false);
self::assertStringContainsString('Bar\array_map', $edits->apply($source));
}
protected function importName(string $source, int $offset, NameImport $nameImport, bool $importGlobals = true): TextEdits
{
$importClass = (new TolerantImportName($this->updater(), $this->parser(), $importGlobals));
return $importClass->importName(SourceCode::fromString($source), ByteOffset::fromInt($offset), $nameImport);
}
}