-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathAddPrefixCommandIntegrationTest.php
365 lines (286 loc) · 10.3 KB
/
AddPrefixCommandIntegrationTest.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
declare(strict_types=1);
/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Humbug\PhpScoper\Console\Command;
use Fidry\Console\Test\AppTester;
use Humbug\PhpScoper\Console\Application;
use Humbug\PhpScoper\Console\AppTesterAbilities;
use Humbug\PhpScoper\Console\AppTesterTestCase;
use Humbug\PhpScoper\Container;
use Humbug\PhpScoper\FileSystemTestCase;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use function array_reduce;
use function iterator_to_array;
use function Safe\file_get_contents;
use function Safe\file_put_contents;
use function Safe\fileperms;
use function Safe\preg_replace;
use function Safe\realpath;
use function sprintf;
use function str_replace;
use const DIRECTORY_SEPARATOR;
/**
* @internal
*/
#[Group('integration')]
#[CoversNothing]
class AddPrefixCommandIntegrationTest extends FileSystemTestCase implements AppTesterTestCase
{
use AppTesterAbilities;
private const FIXTURE_PATH = __DIR__.'/../../../fixtures/set002/original';
protected function setUp(): void
{
parent::setUp();
$application = new Application(
new Container(),
'TestVersion',
'28/01/2020',
false,
false,
);
$this->appTester = AppTester::fromConsoleApp($application);
file_put_contents('scoper.inc.php', '<?php return [];');
}
public function test_scope_the_given_paths(): void
{
$outputDir = $this->tmp.DIRECTORY_SEPARATOR.'build';
$input = [
'add-prefix',
'--prefix' => 'MyPrefix',
'paths' => [
self::FIXTURE_PATH,
],
'--output-dir' => $outputDir,
'--no-interaction' => null,
'--no-config' => null,
];
$this->appTester->run($input);
self::assertSame(0, $this->appTester->getStatusCode());
self::assertFilesAreSame(
self::FIXTURE_PATH.'/../scoped',
$outputDir,
);
}
public function test_scope_in_quiet_mode(): void
{
$outputDir = $this->tmp.DIRECTORY_SEPARATOR.'build';
$input = [
'add-prefix',
'--prefix' => 'MyPrefix',
'paths' => [
self::FIXTURE_PATH,
],
'--output-dir' => $outputDir,
'--quiet' => null,
];
$this->appTester->run($input);
$this->assertExpectedOutput('', 0);
}
public function test_scope_in_normal_mode(): void
{
$outputDir = $this->tmp.DIRECTORY_SEPARATOR.'build';
$input = [
'add-prefix',
'--prefix' => 'MyPrefix',
'paths' => [
self::FIXTURE_PATH,
],
'--output-dir' => $outputDir,
'--no-interaction' => null,
];
$this->appTester->run($input);
$expected = <<<'EOF'
____ __ ______ _____
/ __ \/ / / / __ \ / ___/_________ ____ ___ _____
/ /_/ / /_/ / /_/ / \__ \/ ___/ __ \/ __ \/ _ \/ ___/
/ ____/ __ / ____/ ___/ / /__/ /_/ / /_/ / __/ /
/_/ /_/ /_/_/ /____/\___/\____/ .___/\___/_/
/_/
PhpScoper version TestVersion 28/01/2020
0/5 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0%
5/5 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
[OK] Successfully prefixed 5 files.
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
EOF;
$this->assertExpectedOutput(
$expected,
0,
$this->createDisplayNormalizer(),
self::replaceIntermediateProgressBarSteps(...),
);
}
public function test_scope_in_verbose_mode(): void
{
$outputDir = $this->tmp.DIRECTORY_SEPARATOR.'build';
$input = [
'add-prefix',
'--prefix' => 'MyPrefix',
'paths' => [
self::FIXTURE_PATH,
],
'--output-dir' => $outputDir,
'-v' => null,
'--no-interaction' => null,
];
$this->appTester->run($input);
$expected = <<<'EOF'
____ __ ______ _____
/ __ \/ / / / __ \ / ___/_________ ____ ___ _____
/ /_/ / /_/ / /_/ / \__ \/ ___/ __ \/ __ \/ _ \/ ___/
/ ____/ __ / ____/ ___/ / /__/ /_/ / /_/ / __/ /
/_/ /_/ /_/_/ /____/\___/\____/ .___/\___/_/
/_/
PhpScoper version TestVersion 28/01/2020
* [OK] /path/to/composer/installed.json
* [OK] /path/to/executable-file.php
* [OK] /path/to/file.php
* [NO] /path/to/invalid-file.php
* [OK] /path/to/scoper.inc.php
[OK] Successfully prefixed 5 files.
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
EOF;
$this->assertExpectedOutput(
$expected,
0,
$this->createDisplayNormalizer(),
);
}
public function test_scope_in_very_verbose_mode(): void
{
$outputDir = $this->tmp.DIRECTORY_SEPARATOR.'build';
$input = [
'add-prefix',
'--prefix' => 'MyPrefix',
'paths' => [
self::FIXTURE_PATH,
],
'--output-dir' => $outputDir,
'-vv' => null,
'--no-interaction' => null,
];
$this->appTester->run($input);
$expected = <<<'EOF'
____ __ ______ _____
/ __ \/ / / / __ \ / ___/_________ ____ ___ _____
/ /_/ / /_/ / /_/ / \__ \/ ___/ __ \/ __ \/ _ \/ ___/
/ ____/ __ / ____/ ___/ / /__/ /_/ / /_/ / __/ /
/_/ /_/ /_/_/ /____/\___/\____/ .___/\___/_/
/_/
PhpScoper version TestVersion 28/01/2020
* [OK] /path/to/composer/installed.json
* [OK] /path/to/executable-file.php
* [OK] /path/to/file.php
* [NO] /path/to/invalid-file.php
Could not parse the file "/path/to/invalid-file.php".: PhpParser
Stack trace:
#0
#1
#2
#3
#4
#5
#6
#7
#8
#9
* [OK] /path/to/scoper.inc.php
[OK] Successfully prefixed 5 files.
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
EOF;
$extraDisplayNormalization = static function (string $display): string {
$display = preg_replace('/(Could not parse the file ".+?"\.: \w+).*(\n)/', '$1$2', $display);
$display = preg_replace('/(#\d+).*(\n)/', '$1$2', $display);
// Remove overly lengthy stack-trace
return preg_replace('/(Stack trace:(?:\n\#\d)+)\n?((?:\n\#\d{2,})+)/', '$1', $display);
};
$this->assertExpectedOutput(
$expected,
0,
$this->createDisplayNormalizer(),
$extraDisplayNormalization,
);
}
/**
* @return callable(string):string
*/
private function createDisplayNormalizer(): callable
{
return fn ($display) => $this->getNormalizeDisplay($display);
}
private function getNormalizeDisplay(string $display): string
{
$display = str_replace(
[realpath(self::FIXTURE_PATH), $this->tmp],
'/path/to',
$display,
);
return preg_replace(
'/\/\/ Memory usage: \d+\.\d{2}MB \(peak: \d+\.\d{2}MB\), time: \d+\.\d{2}s/',
'// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s',
$display,
);
}
private static function assertFilesAreSame(string $expectedDir, string $actualDir): void
{
$expected = self::collectFiles($expectedDir);
$actual = self::collectFiles($actualDir);
self::assertSame($expected, $actual);
}
private static function collectFiles(string $dir): array
{
$dir = realpath($dir);
$finder = new Finder();
$files = $finder->files()
->in($dir)
->sortByName();
return array_reduce(
iterator_to_array($files),
static function (array $collectedFiles, SplFileInfo $file) use ($dir): array {
$realPath = $file->getRealPath();
self::assertIsString(
$realPath,
sprintf(
'Expected file "%s" to have a real path.',
$file->getPathname(),
),
);
$path = str_replace($dir, '', $realPath);
$collectedFiles[$path] = [file_get_contents($realPath), fileperms($realPath)];
return $collectedFiles;
},
[],
);
}
private static function replaceIntermediateProgressBarSteps(string $output): string
{
return str_replace(
[
<<<'EOF'
1/5 [▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░] 20%
EOF,
<<<'EOF'
2/5 [▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░] 40%
EOF,
<<<'EOF'
3/5 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░] 60%
EOF,
<<<'EOF'
4/5 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░] 80%
EOF,
],
['', '', ''],
$output,
);
}
}