-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
AbstractTolerantImportNameCase.php
225 lines (197 loc) · 8.86 KB
/
AbstractTolerantImportNameCase.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
declare(strict_types=1);
namespace Phpactor\CodeTransform\Tests\Adapter\TolerantParser\Refactor;
use Generator;
use Phpactor\CodeTransform\Domain\Refactor\ImportClass\AliasAlreadyUsedException;
use Phpactor\CodeTransform\Domain\Refactor\ImportClass\ClassIsCurrentClassException;
use Phpactor\CodeTransform\Domain\Refactor\ImportClass\NameAlreadyImportedException;
use Phpactor\CodeTransform\Domain\Refactor\ImportClass\NameAlreadyInNamespaceException;
use Phpactor\CodeTransform\Domain\Refactor\ImportClass\NameImport;
use Phpactor\CodeTransform\Tests\Adapter\TolerantParser\TolerantTestCase;
use Phpactor\TextDocument\TextEdits;
abstract class AbstractTolerantImportNameCase extends TolerantTestCase
{
/**
* @dataProvider provideImportClass
*/
public function testImportClass(string $test, string $name, string $alias = null): void
{
[$expected, $transformed] = $this->importNameFromTestFile('class', $test, $name, $alias);
$this->assertEquals(trim($expected), trim($transformed));
}
abstract public function provideImportClass(): Generator;
public function testThrowsExceptionIfClassAlreadyImported(): void
{
$this->expectException(NameAlreadyImportedException::class);
$this->expectExceptionMessage('Class "DateTime" is already imported');
$this->importNameFromTestFile('class', 'importClass1.test', 'DateTime');
}
public function testThrowsExceptionIfImportedClassIsTheCurrentClass1(): void
{
$this->expectException(ClassIsCurrentClassException::class);
$this->expectExceptionMessage('Class "Foobar" is the current class');
$this->importName('<?php class Foobar {}', 14, NameImport::forClass('Foobar'));
}
public function testThrowsExceptionClassIsCurrentClassExceptionTrait(): void
{
$this->expectException(ClassIsCurrentClassException::class);
$this->expectExceptionMessage('Class "Foobar" is the current class');
$this->importName('<?php trait Foobar {}', 14, NameImport::forClass('Foobar'));
}
public function testThrowsExceptionClassIsCurrentClassExceptionInterface(): void
{
$this->expectException(ClassIsCurrentClassException::class);
$this->expectExceptionMessage('Class "Foobar" is the current class');
$this->importName('<?php interface Foobar {}', 14, NameImport::forClass('Foobar'));
}
public function testThrowsExceptionIfAliasAlreadyUsed(): void
{
$this->expectException(AliasAlreadyUsedException::class);
$this->expectExceptionMessage('Class alias "DateTime" is already used');
$this->importNameFromTestFile('class', 'importClass1.test', 'Foobar', 'DateTime');
}
public function testThrowsNameAlreadyImportedExistingAliasName(): void
{
try {
$this->importName(
'<?php namespace Foo; use Foo1\Bar; use Foo2\Bar as Foo2Bar;',
55,
NameImport::forClass('Foo2\Bar')
);
self::fail('Expected NameAlreadyImportedException has not been raised');
} catch (NameAlreadyImportedException $error) {
self::assertSame('Class "Bar" is already imported', $error->getMessage());
self::assertSame('Bar', $error->name());
self::assertSame('Foo2Bar', $error->existingName());
self::assertSame('Foo2\Bar', $error->existingFQN());
}
}
public function testThrowsNameAlreadyImportedNameInUse(): void
{
try {
$this->importName(
'<?php namespace Foo; use Foo1\Bar;',
34,
NameImport::forClass('Foo2\Bar')
);
self::fail('Expected NameAlreadyImportedException has not been raised');
} catch (NameAlreadyImportedException $error) {
self::assertSame('Class "Bar" is already imported', $error->getMessage());
self::assertSame('Bar', $error->name());
self::assertSame('Bar', $error->existingName());
self::assertSame('Foo1\Bar', $error->existingFQN());
}
}
public function testThrowsNameAlreadyImportedOnlyAliasName(): void
{
try {
$this->importName(
'<?php namespace Foo; use Foo2\Bar as Foo2Bar;',
45,
NameImport::forClass('Foo2\Bar')
);
self::fail('Expected NameAlreadyImportedException has not been raised');
} catch (NameAlreadyImportedException $error) {
self::assertSame('Class "Bar" is already imported', $error->getMessage());
self::assertSame('Bar', $error->name());
self::assertSame('Foo2Bar', $error->existingName());
self::assertSame('Foo2\Bar', $error->existingFQN());
}
}
public function testThrowsNameAlreadyImportedFunction(): void
{
try {
$this->importName(
'<?php use function in_array;',
55,
NameImport::forFunction('in_array')
);
self::fail('Expected NameAlreadyImportedException has not been raised');
} catch (NameAlreadyImportedException $error) {
self::assertSame('Function "in_array" is already imported', $error->getMessage());
self::assertSame('in_array', $error->name());
self::assertSame('in_array', $error->existingName());
self::assertSame('in_array', $error->existingFQN());
}
}
public function testThrowsNameAlreadyImportedFunctionAlias(): void
{
try {
$this->importName(
'<?php use function in_array as foo_in_array;',
55,
NameImport::forFunction('in_array')
);
self::fail('Expected NameAlreadyImportedException has not been raised');
} catch (NameAlreadyImportedException $error) {
self::assertSame('Function "in_array" is already imported', $error->getMessage());
self::assertSame('in_array', $error->name());
self::assertSame('foo_in_array', $error->existingName());
self::assertSame('in_array', $error->existingFQN());
}
}
public function testThrowsExceptionIfImportedClassHasSameNameAsCurrentClassName(): void
{
try {
$this->importName(
'<?php namespace Barfoo; class Foobar extends Foobar',
47,
NameImport::forClass('BazBar\Foobar')
);
self::fail('Expected NameAlreadyImportedException has not been raised');
} catch (NameAlreadyImportedException $error) {
self::assertSame('Class "Foobar" is already imported', $error->getMessage());
self::assertSame('Foobar', $error->name());
self::assertSame('Foobar', $error->existingName());
self::assertSame('Barfoo\Foobar', $error->existingFQN());
}
}
public function testThrowsExceptionIfImportedClassHasSameNameAsCurrentInterfaceName(): void
{
$this->expectException(NameAlreadyImportedException::class);
$this->importName(
'<?php namespace Barfoo; interface Foobar extends Foobar',
50,
NameImport::forClass('BazBar\Foobar')
);
}
public function testThrowsExceptionIfImportedClassInSameNamespace(): void
{
$this->expectException(NameAlreadyInNamespaceException::class);
$this->expectExceptionMessage('Class "Barfoo" is in the same namespace as current class');
$source = <<<'EOT'
<?php
namespace Barfoo;
class Foobar {
public function use(Barfoo $barfoo) {}
}
}
EOT;
$this->importName($source, 64, NameImport::forClass('Barfoo\Barfoo'));
}
/**
* @dataProvider provideImportFunction
*/
public function testImportFunction(string $test, string $name, string $alias = null): void
{
[$expected, $transformed] = $this->importNameFromTestFile('function', $test, $name, $alias);
$this->assertEquals(trim($expected), trim($transformed));
}
abstract public function provideImportFunction(): Generator;
abstract protected function importName(string $source, int $offset, NameImport $nameImport, bool $importGlobals = true): TextEdits;
/**
* @return array{string,string}
*/
private function importNameFromTestFile(string $type, string $test, string $name, string $alias = null): array
{
[$source, $expected, $offset] = $this->sourceExpectedAndOffset(__DIR__ . '/fixtures/' . $test);
$edits = TextEdits::none();
if ($type === 'class') {
$edits = $this->importName($source, $offset, NameImport::forClass($name, $alias));
}
if ($type === 'function') {
$edits = $this->importName($source, $offset, NameImport::forFunction($name, $alias));
}
return [$expected, $edits->apply($source)];
}
}