-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectTest.php
More file actions
320 lines (274 loc) · 14.1 KB
/
Copy pathDetectTest.php
File metadata and controls
320 lines (274 loc) · 14.1 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
declare(strict_types=1);
namespace Composer\ApiSurfaceCheck\Tests;
use PHPUnit\Framework\TestCase;
/**
* Full-pipeline integration tests: drives scripts/detect.sh against synthetic
* two-commit git repos and asserts the rendered comment body. Covers the
* scenarios that previously caused false positives or regressions.
*/
final class DetectTest extends TestCase
{
private string $repoDir;
protected function setUp(): void
{
$this->repoDir = sys_get_temp_dir() . '/api-surface-detect-' . bin2hex(random_bytes(4));
mkdir($this->repoDir, 0o755, true);
$this->shell('git init -q');
$this->shell('git -c user.email=t@t -c user.name=t commit -q --allow-empty -m bootstrap');
}
protected function tearDown(): void
{
if (is_dir($this->repoDir)) {
$this->rrmdir($this->repoDir);
}
}
/**
* @param array<string,string> $files Map of relative path => contents.
*/
private function commit(array $files, string $message): void
{
foreach ($files as $relative => $contents) {
$full = $this->repoDir . '/' . $relative;
if (!is_dir(dirname($full))) {
mkdir(dirname($full), 0o755, true);
}
file_put_contents($full, $contents);
}
$this->shell('git add -A');
$this->shell(sprintf('git -c user.email=t@t -c user.name=t commit -q -m %s', escapeshellarg($message)));
}
/**
* @param array<string,string> $extraEnv
*/
private function runDetect(array $extraEnv = []): string
{
$scriptDir = realpath(__DIR__ . '/../scripts');
$env = array_merge([
'BASE_REF' => 'HEAD~1',
'PATHS_GLOB' => 'src/**/*.php',
'SOURCE_ROOTS' => 'src',
'OUTPUT_DIR' => 'result',
], $extraEnv);
$envString = '';
foreach ($env as $k => $v) {
$envString .= sprintf('%s=%s ', $k, escapeshellarg($v));
}
$this->shell($envString . 'bash ' . escapeshellarg($scriptDir . '/detect.sh'));
$body = $this->repoDir . '/result/comment-body.txt';
return is_file($body) ? file_get_contents($body) : '';
}
private function shell(string $cmd): string
{
$current = getcwd();
chdir($this->repoDir);
try {
$lines = [];
$status = 0;
exec($cmd . ' 2>&1', $lines, $status);
$output = implode("\n", $lines);
if ($status !== 0) {
$this->fail("Command failed (exit {$status}): {$cmd}\n{$output}");
}
return $output;
} finally {
chdir($current);
}
}
private function rrmdir(string $dir): void
{
foreach (scandir($dir) as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
$path = $dir . '/' . $entry;
is_dir($path) && !is_link($path) ? $this->rrmdir($path) : unlink($path);
}
rmdir($dir);
}
public function testNoSourceChangesProducesEmptyBody(): void
{
$this->commit(['src/Foo.php' => "<?php\nnamespace L;\nclass Foo {}\n"], 'base');
$this->commit(['docs/README.md' => "doc\n"], 'unrelated');
$this->assertSame('', $this->runDetect());
}
public function testNewClassWithPublicMethodIsReported(): void
{
$this->commit(['src/Existing.php' => "<?php\nnamespace L;\nclass Existing {}\n"], 'base');
$this->commit([
'src/Existing.php' => "<?php\nnamespace L;\nclass Existing {}\n",
'src/Fresh.php' => "<?php\nnamespace L;\nclass Fresh {\n public function go(int \$x): bool { return true; }\n}\n",
], 'add Fresh');
$body = $this->runDetect();
$this->assertStringContainsString('### New API Surface', $body);
$this->assertStringContainsString('class L\\Fresh', $body);
$this->assertStringContainsString('L\\Fresh::go', $body);
$this->assertStringContainsString('public function go(int $x): bool', $body);
}
public function testInterfaceImplementationIsNotReportedOnImplementer(): void
{
// Mirrors composer/composer PR #12803: a class newly implements an
// existing interface and adds the implementation methods. The interface
// is unchanged, so the methods are not "new API surface" — they're
// just an existing contract being satisfied on another class.
$this->commit([
'src/Iface.php' => "<?php\nnamespace L;\ninterface Iface { public function action(): bool; }\n",
'src/Repo.php' => "<?php\nnamespace L;\nclass Repo { public function existing(): void {} }\n",
], 'base');
$this->commit([
'src/Iface.php' => "<?php\nnamespace L;\ninterface Iface { public function action(): bool; }\n",
'src/Repo.php' => "<?php\nnamespace L;\nclass Repo implements Iface {\n public function existing(): void {}\n public function action(): bool { return true; }\n}\n",
], 'implement Iface on Repo');
$body = $this->runDetect();
$this->assertStringNotContainsString('### New API Surface', $body, 'Interface implementations should not be reported as new API surface.');
$this->assertStringNotContainsString('L\\Repo::action', $body);
}
public function testRemovedPublicMethodIsReported(): void
{
$this->commit([
'src/Foo.php' => "<?php\nnamespace L;\nclass Foo {\n public function keep(): void {}\n public function remove(): int { return 1; }\n}\n",
], 'base');
$this->commit([
'src/Foo.php' => "<?php\nnamespace L;\nclass Foo {\n public function keep(): void {}\n}\n",
], 'remove method');
$body = $this->runDetect();
$this->assertStringContainsString('### Removed API Surface', $body);
$this->assertStringContainsString('L\\Foo::remove', $body);
}
public function testNewPublicPropertyIsReportedUnderProperties(): void
{
$this->commit(['src/Foo.php' => "<?php\nnamespace L;\nclass Foo {}\n"], 'base');
$this->commit([
'src/Foo.php' => "<?php\nnamespace L;\nclass Foo {\n public readonly int \$count;\n}\n",
], 'add property');
$body = $this->runDetect();
$this->assertStringContainsString('#### Properties', $body);
$this->assertStringContainsString('L\\Foo::count', $body);
$this->assertStringContainsString('public readonly int $count', $body);
}
public function testParentMethodOverrideIsNotReportedOnChild(): void
{
$this->commit([
'src/Parent_.php' => "<?php\nnamespace L;\nclass Parent_ { public function shared(): void {} }\n",
'src/Child_.php' => "<?php\nnamespace L;\nclass Child_ extends Parent_ {}\n",
], 'base');
$this->commit([
'src/Parent_.php' => "<?php\nnamespace L;\nclass Parent_ { public function shared(): void {} }\n",
'src/Child_.php' => "<?php\nnamespace L;\nclass Child_ extends Parent_ { public function shared(): void {} }\n",
], 'override on child');
$body = $this->runDetect();
$this->assertSame('', $body, 'Method overrides on child classes should not appear as new API.');
}
public function testInternalAnnotationOnExistingMethodRemovesItFromSurface(): void
{
$this->commit([
'src/Foo.php' => "<?php\nnamespace L;\nclass Foo {\n public function exposed(): void {}\n}\n",
], 'base');
$this->commit([
'src/Foo.php' => "<?php\nnamespace L;\nclass Foo {\n /** @internal */\n public function exposed(): void {}\n}\n",
], 'mark internal');
$body = $this->runDetect();
$this->assertStringContainsString('### Removed API Surface', $body);
$this->assertStringContainsString('L\\Foo::exposed', $body);
}
public function testInternalSymbolStaysHiddenWithDefaultFilter(): void
{
$this->commit(['src/Foo.php' => "<?php\nnamespace L;\nclass Foo {}\n"], 'base');
$this->commit([
'src/Foo.php' => "<?php\nnamespace L;\nclass Foo {\n /** @internal */\n public function impl(): void {}\n}\n",
], 'add internal method');
$this->assertSame('', $this->runDetect(), '@internal additions should not surface by default.');
}
public function testIncludeInternalFlagShowsInternalAdditions(): void
{
$this->commit(['src/Foo.php' => "<?php\nnamespace L;\nclass Foo {}\n"], 'base');
$this->commit([
'src/Foo.php' => "<?php\nnamespace L;\nclass Foo {\n /** @internal */\n public function impl(): void {}\n}\n",
], 'add internal method');
$body = $this->runDetect(['INCLUDE_INTERNAL' => 'true']);
$this->assertStringContainsString('L\\Foo::impl', $body);
}
public function testModifiedSignatureFlag(): void
{
$this->commit([
'src/Foo.php' => "<?php\nnamespace L;\nclass Foo {\n public function mut(int \$x): void {}\n}\n",
], 'base');
$this->commit([
'src/Foo.php' => "<?php\nnamespace L;\nclass Foo {\n public function mut(int \$x, int \$y): void {}\n}\n",
], 'add param');
// Default → reported under Modified.
$body = $this->runDetect();
$this->assertStringContainsString('### Modified API Surface', $body);
$this->assertStringContainsString('L\\Foo::mut', $body);
$this->assertStringContainsString("```diff", $body);
$this->assertStringContainsString('- public function mut(int $x): void', $body);
$this->assertStringContainsString('+ public function mut(int $x, int $y): void', $body);
// Explicit opt-out → empty body.
$this->assertSame('', $this->runDetect(['SHOW_MODIFIED' => 'false']));
}
public function testVendorParentMethodIsNotReportedWhenVendorPathIsProvided(): void
{
// Pretend vendor/ is composer-installed: it lives outside SOURCE_ROOTS
// and outside PATHS_GLOB, so it's never analyzed — only used to
// resolve parent class declarations through better-reflection.
$this->commit([
'vendor/Acme/Lib/Base.php' => "<?php\nnamespace Acme\\Lib;\nclass Base {\n public function vendorMethod(): void {}\n}\n",
'src/Foo.php' => "<?php\nnamespace L;\nuse Acme\\Lib\\Base;\nclass Foo extends Base {}\n",
], 'base');
$this->commit([
'src/Foo.php' => "<?php\nnamespace L;\nuse Acme\\Lib\\Base;\nclass Foo extends Base {\n public function vendorMethod(): void {}\n}\n",
], 'override vendor method on Foo');
$vendorPath = $this->repoDir . '/vendor';
// Without VENDOR_PATH, better-reflection can't see Acme\Lib\Base and
// the override looks like a brand-new method on Foo.
$bodyWithout = $this->runDetect();
$this->assertStringContainsString('L\\Foo::vendorMethod', $bodyWithout, 'Sanity: without VENDOR_PATH the override should be (incorrectly) flagged.');
// With VENDOR_PATH, the introduction point of vendorMethod is Acme\Lib\Base
// (which lives outside the analyzed paths), so Foo::vendorMethod is skipped.
$bodyWith = $this->runDetect(['VENDOR_PATH' => $vendorPath]);
$this->assertSame('', $bodyWith, 'With VENDOR_PATH set, overriding a vendor method should not appear as new API.');
}
public function testTopLevelAndNestedFilesAreBothMatched(): void
{
$this->commit([
'src/TopLevel.php' => "<?php\nnamespace L;\nclass TopLevel {}\n",
'src/Sub/Nested.php' => "<?php\nnamespace L\\Sub;\nclass Nested {}\n",
], 'base');
$this->commit([
'src/TopLevel.php' => "<?php\nnamespace L;\nclass TopLevel { public function a(): void {} }\n",
'src/Sub/Nested.php' => "<?php\nnamespace L\\Sub;\nclass Nested { public function b(): void {} }\n",
], 'add methods');
$body = $this->runDetect();
$this->assertStringContainsString('L\\TopLevel::a', $body);
$this->assertStringContainsString('L\\Sub\\Nested::b', $body);
}
public function testRenamedClassIsReportedAsRemovedAndAdded(): void
{
// Mirrors composer/composer PR #12919: a file and its class are renamed
// (Version.php/class Version -> VersionRenamed.php/class VersionRenamed).
// The bodies are identical, so git classifies this as a single rename (R)
// entry. detect.sh must decompose it (via --no-renames) into a delete +
// add; otherwise --diff-filter=AMD drops the R entry and neither the old
// nor the new class reaches the snapshotter.
$classBody = "{\n public function major(): int { return 1; }\n public function minor(): int { return 2; }\n public function patch(): int { return 3; }\n}\n";
$this->commit([
'src/Platform/Version.php' => "<?php\nnamespace L\\Platform;\nclass Version " . $classBody,
], 'base');
// The commit() helper only writes files; remove the old one ourselves so
// `git add -A` stages the deletion alongside the new file.
unlink($this->repoDir . '/src/Platform/Version.php');
$this->commit([
'src/Platform/VersionRenamed.php' => "<?php\nnamespace L\\Platform;\nclass VersionRenamed " . $classBody,
], 'rename Version to VersionRenamed');
$rendered = $this->runDetect();
$this->assertStringContainsString('### New API Surface', $rendered);
$this->assertStringContainsString('### Removed API Surface', $rendered);
// New is rendered before Removed; split so we can assert membership.
$removedAt = strpos($rendered, '### Removed API Surface');
$newSection = substr($rendered, 0, $removedAt);
$removedSection = substr($rendered, $removedAt);
$this->assertStringContainsString('L\\Platform\\VersionRenamed', $newSection, 'The renamed-to class should be reported as new API surface.');
// Backtick-bounded so it does not also match `L\Platform\VersionRenamed`.
$this->assertStringContainsString('`L\\Platform\\Version`', $removedSection, 'The renamed-from class should be reported as removed API surface.');
}
}