Completed
Pull Request — master (#1)
by Marco
02:27
created

VerifyTest::makeDependencyGitRepository()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ComposerGpgVerify;
6
7
use Composer\Composer;
8
use Composer\Config;
9
use Composer\Installer\InstallationManager;
10
use Composer\Package\PackageInterface;
11
use Composer\Repository\RepositoryInterface;
12
use Composer\Repository\RepositoryManager;
13
use Composer\Script\Event;
14
use Composer\Script\ScriptEvents;
15
use PHPUnit\Framework\TestCase;
16
use Roave\ComposerGpgVerify\Verify;
17
use Symfony\Component\Process\Process;
18
19
/**
20
 * @covers \Roave\ComposerGpgVerify\Verify
21
 */
22
final class VerifyTest extends TestCase
23
{
24
    /**
25
     * @var Event|\PHPUnit_Framework_MockObject_MockObject
26
     */
27
    private $event;
28
29
    /**
30
     * @var Composer|\PHPUnit_Framework_MockObject_MockObject
31
     */
32
    private $composer;
33
34
    /**
35
     * @var Config|\PHPUnit_Framework_MockObject_MockObject
36
     */
37
    private $config;
38
39
    /**
40
     * @var RepositoryManager|\PHPUnit_Framework_MockObject_MockObject
41
     */
42
    private $repositoryManager;
43
44
    /**
45
     * @var InstallationManager|\PHPUnit_Framework_MockObject_MockObject
46
     */
47
    private $installationManager;
48
49
    /**
50
     * @var RepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
51
     */
52
    private $localRepository;
53
54
    /**
55
     * @var string
56
     */
57
    private $originalGpgHome;
58
59
    /**
60
     * @var string
61
     */
62
    private $originalLanguage;
63
64
    /**
65
     * @var PackageInterface[] indexed by installation path
66
     */
67
    private $installedPackages = [];
68
69
    protected function setUp() : void
70
    {
71
        parent::setUp();
72
73
        $this->installedPackages = [];
74
        $this->originalGpgHome   = (string) getenv('GNUPGHOME');
75
        $this->originalLanguage  = (string) getenv('LANGUAGE');
76
77
        $this->event               = $this->createMock(Event::class);
78
        $this->composer            = $this->createMock(Composer::class);
79
        $this->config              = $this->createMock(Config::class);
80
        $this->repositoryManager   = $this->createMock(RepositoryManager::class);
81
        $this->installationManager = $this->createMock(InstallationManager::class);
82
        $this->localRepository     = $this->createMock(RepositoryInterface::class);
83
84
        $this->event->expects(self::any())->method('getComposer')->willReturn($this->composer);
85
        $this->composer->expects(self::any())->method('getConfig')->willReturn($this->config);
86
        $this
87
            ->composer
88
            ->expects(self::any())
89
            ->method('getRepositoryManager')
90
            ->willReturn($this->repositoryManager);
91
        $this
92
            ->composer
93
            ->expects(self::any())
94
            ->method('getInstallationManager')
95
            ->willReturn($this->installationManager);
96
        $this
97
            ->repositoryManager
98
            ->expects(self::any())
99
            ->method('getLocalRepository')
100
            ->willReturn($this->localRepository);
101
        $this
102
            ->installationManager
103
            ->expects(self::any())
104
            ->method('getInstallPath')
105
            ->willReturnCallback(function (PackageInterface $package) : string {
106
                return array_search($package, $this->installedPackages, true);
107
            });
108
        $this
109
            ->localRepository
110
            ->expects(self::any())
111
            ->method('getPackages')
112
            ->willReturnCallback(function () {
113
                return array_values($this->installedPackages);
114
            });
115
    }
116
117
    protected function tearDown() : void
118
    {
119
        putenv(sprintf('GNUPGHOME=%s', $this->originalGpgHome));
120
        putenv(sprintf('LANGUAGE=%s', $this->originalLanguage));
121
122
        parent::tearDown();
123
    }
124
125
    public function testWillDisallowInstallationOnNonSourceInstall() : void
126
    {
127
        $this
128
            ->config
129
            ->expects(self::any())
130
            ->method('get')
131
            ->with('preferred-install')
132
            ->willReturn('foo');
133
134
        $this->expectException(\LogicException::class);
135
        $this->expectExceptionMessage('Expected installation "preferred-install" to be "source", found "foo" instead');
136
137
        Verify::verify($this->event);
138
    }
139
140
    public function testWillRetrieveSubscribedEvents() : void
141
    {
142
        $events = Verify::getSubscribedEvents();
143
144
        self::assertNotEmpty($events);
145
146
        $availableEvents = (new \ReflectionClass(ScriptEvents::class))->getConstants();
147
148
        foreach ($events as $eventName => $callback) {
149
            self::assertContains($eventName, $availableEvents);
150
            self::assertInternalType('string', $callback);
151
            self::assertInternalType('callable', [Verify::class, $callback]);
152
        }
153
    }
154
155 View Code Duplication
    public function testWillAcceptSignedAndTrustedPackages() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        $gpgHomeDirectory = $this->makeGpgHomeDirectory();
158
159
        $vendorName  = 'Mr. Magoo';
160
        $vendorEmail = '[email protected]';
161
        $vendorKey   = $this->makeKey($gpgHomeDirectory, $vendorEmail, $vendorName);
162
        $vendorDir   = $this->makeVendorDirectory();
163
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
164
165
        $this->signDependency($vendor1, $gpgHomeDirectory, $vendorKey);
166
167
        $this->configureCorrectComposerSetup();
168
169
        putenv('GNUPGHOME=' . $gpgHomeDirectory);
170
171
        Verify::verify($this->event);
172
    }
173
174 View Code Duplication
    public function testWillRejectPackageSignedWithImportedButUnTrustedKey() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
177
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
178
179
        $this->makeKey($personalGpgDirectory, '[email protected]', 'Just Me');
180
181
        $vendorName  = 'Mr. Magoo';
182
        $vendorEmail = '[email protected]';
183
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
184
        $vendorDir   = $this->makeVendorDirectory();
185
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
186
187
        $this->signDependency($vendor1, $foreignGpgDirectory, $vendorKey);
188
189
        $this->importForeignKeys($personalGpgDirectory, $foreignGpgDirectory, $vendorKey, false);
190
191
        $this->configureCorrectComposerSetup();
192
193
        putenv('GNUPGHOME=' . $personalGpgDirectory);
194
195
        $this->assertWillFailPackageVerification();
196
    }
197
198
    public function testWillRejectPackageSignedWithImportedButUnTrustedKeyWithDifferentLocaleSettings() : void
199
    {
200
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
201
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
202
203
        $this->makeKey($personalGpgDirectory, '[email protected]', 'Just Me');
204
205
        $vendorName  = 'Mr. Magoo';
206
        $vendorEmail = '[email protected]';
207
208
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
209
        $vendorDir   = $this->makeVendorDirectory();
210
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
211
212
        $this->signDependency($vendor1, $foreignGpgDirectory, $vendorKey);
213
214
        $this->importForeignKeys($personalGpgDirectory, $foreignGpgDirectory, $vendorKey, false);
215
216
        $this->configureCorrectComposerSetup();
217
218
        putenv('GNUPGHOME=' . $personalGpgDirectory);
219
        putenv('LANGUAGE=de_DE');
220
221
        try {
222
            Verify::verify($this->event);
223
        } catch (\RuntimeException $failure) {
224
            self::assertSame('de_DE', getenv('LANGUAGE'));
225
226
            return;
227
        }
228
229
        self::fail('Exception was not thrown');
230
    }
231
232 View Code Duplication
    public function testWillAcceptPackageSignedWithImportedAndTrustedKey() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
    {
234
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
235
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
236
237
        $this->makeKey($personalGpgDirectory, '[email protected]', 'Just Me');
238
239
        $vendorName  = 'Mr. Magoo';
240
        $vendorEmail = '[email protected]';
241
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
242
        $vendorDir   = $this->makeVendorDirectory();
243
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
244
245
        $this->signDependency($vendor1, $foreignGpgDirectory, $vendorKey);
246
247
        $this->importForeignKeys($personalGpgDirectory, $foreignGpgDirectory, $vendorKey, true);
248
249
        $this->configureCorrectComposerSetup();
250
251
        putenv('GNUPGHOME=' . $personalGpgDirectory);
252
253
        Verify::verify($this->event);
254
    }
255
256 View Code Duplication
    public function testWillRejectPackageTaggedAndSignedWithImportedButUnTrustedKey() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
    {
258
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
259
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
260
261
        $this->makeKey($personalGpgDirectory, '[email protected]', 'Just Me');
262
263
        $vendorName  = 'Mr. Magoo';
264
        $vendorEmail = '[email protected]';
265
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
266
        $vendorDir   = $this->makeVendorDirectory();
267
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
268
269
        $this->createDependencySignedTag($vendor1, $foreignGpgDirectory, $vendorKey);
270
271
        $this->importForeignKeys($personalGpgDirectory, $foreignGpgDirectory, $vendorKey, false);
272
273
        $this->configureCorrectComposerSetup();
274
275
        putenv('GNUPGHOME=' . $personalGpgDirectory);
276
277
        $this->assertWillFailPackageVerification();
278
    }
279
280 View Code Duplication
    public function testWillAcceptPackageTaggedAndSignedWithImportedAndTrustedKey() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
    {
282
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
283
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
284
285
        $this->makeKey($personalGpgDirectory, '[email protected]', 'Just Me');
286
287
        $vendorName  = 'Mr. Magoo';
288
        $vendorEmail = '[email protected]';
289
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
290
        $vendorDir   = $this->makeVendorDirectory();
291
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
292
293
        $this->createDependencySignedTag($vendor1, $foreignGpgDirectory, $vendorKey);
294
295
        $this->importForeignKeys($personalGpgDirectory, $foreignGpgDirectory, $vendorKey, true);
296
297
        $this->configureCorrectComposerSetup();
298
299
        putenv('GNUPGHOME=' . $personalGpgDirectory);
300
301
        Verify::verify($this->event);
302
    }
303
304 View Code Duplication
    public function testWillAcceptSignedAndTrustedTaggedPackages() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
305
    {
306
        $gpgHomeDirectory = $this->makeGpgHomeDirectory();
307
308
        $vendorName  = 'Mr. Magoo';
309
        $vendorEmail = '[email protected]';
310
        $vendorKey   = $this->makeKey($gpgHomeDirectory, $vendorEmail, $vendorName);
311
        $vendorDir   = $this->makeVendorDirectory();
312
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
313
314
        $this->createDependencySignedTag($vendor1, $gpgHomeDirectory, $vendorKey);
315
316
        $this->configureCorrectComposerSetup();
317
318
        putenv('GNUPGHOME=' . $gpgHomeDirectory);
319
320
        Verify::verify($this->event);
321
    }
322
323
    public function testWillRejectUnSignedCommits() : void
324
    {
325
        $vendorName  = 'Mr. Magoo';
326
        $vendorEmail = '[email protected]';
327
        $vendorDir   = $this->makeVendorDirectory();
328
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
329
330
        (new Process('git commit --allow-empty -m "unsigned commit"', $vendor1))
331
            ->setTimeout(30)
332
            ->mustRun();
333
334
        $this->configureCorrectComposerSetup();
335
336
        putenv('GNUPGHOME=' . $this->makeGpgHomeDirectory());
337
338
        $this->assertWillFailPackageVerification();
339
    }
340
341
    public function testWillRejectUnSignedTags() : void
342
    {
343
        $vendorName  = 'Mr. Magoo';
344
        $vendorEmail = '[email protected]';
345
        $vendorDir   = $this->makeVendorDirectory();
346
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
347
348
        (new Process('git commit --allow-empty -m "unsigned commit"', $vendor1))
349
            ->setTimeout(30)
350
            ->mustRun();
351
352
        (new Process('git tag unsigned-tag -m "unsigned tag"', $vendor1))
353
            ->setTimeout(30)
354
            ->mustRun();
355
356
        $this->configureCorrectComposerSetup();
357
358
        putenv('GNUPGHOME=' . $this->makeGpgHomeDirectory());
359
360
        $this->assertWillFailPackageVerification();
361
    }
362
363 View Code Duplication
    public function testWillRejectSignedTagsFromUnknownKey() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
364
    {
365
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
366
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
367
        $vendorName  = 'Mr. Magoo';
368
        $vendorEmail = '[email protected]';
369
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
370
        $vendorDir   = $this->makeVendorDirectory();
371
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
372
373
        $this->createDependencySignedTag($vendor1, $foreignGpgDirectory, $vendorKey);
374
375
        $this->configureCorrectComposerSetup();
376
377
        putenv('GNUPGHOME=' . $personalGpgDirectory);
378
379
        $this->assertWillFailPackageVerification();
380
    }
381
382 View Code Duplication
    public function testWillRejectSignedTagsFromNonHeadCommit() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
383
    {
384
        $gpgHome     = $this->makeGpgHomeDirectory();
385
        $vendorName  = 'Mr. Magoo';
386
        $vendorEmail = '[email protected]';
387
        $vendorKey   = $this->makeKey($gpgHome, $vendorEmail, $vendorName);
388
        $vendorDir   = $this->makeVendorDirectory();
389
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
390
391
        $this->createDependencySignedTag($vendor1, $gpgHome, $vendorKey);
392
393
        (new Process('git commit --allow-empty -m "unsigned commit"', $vendor1))
394
            ->setTimeout(30)
395
            ->mustRun();
396
397
        $this->configureCorrectComposerSetup();
398
399
        putenv('GNUPGHOME=' . $gpgHome);
400
401
        $this->assertWillFailPackageVerification();
402
    }
403
404 View Code Duplication
    public function testWillOnlyConsiderTheHeadCommitForValidation() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
405
    {
406
        $gpgHome     = $this->makeGpgHomeDirectory();
407
        $vendorName  = 'Mr. Magoo';
408
        $vendorEmail = '[email protected]';
409
        $vendorKey   = $this->makeKey($gpgHome, $vendorEmail, $vendorName);
410
        $vendorDir   = $this->makeVendorDirectory();
411
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
412
413
        $this->signDependency($vendor1, $gpgHome, $vendorKey);
414
415
        (new Process('git commit --allow-empty -m "unsigned commit"', $vendor1))
416
            ->setTimeout(30)
417
            ->mustRun();
418
419
        $this->configureCorrectComposerSetup();
420
421
        putenv('GNUPGHOME=' . $gpgHome);
422
423
        $this->assertWillFailPackageVerification();
424
    }
425
426 View Code Duplication
    public function testWillRejectSignedCommitsFromUnknownKeys() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
427
    {
428
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
429
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
430
431
        $vendorName  = 'Mr. Magoo';
432
        $vendorEmail = '[email protected]';
433
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
434
        $vendorDir   = $this->makeVendorDirectory();
435
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
436
437
        $this->signDependency($vendor1, $foreignGpgDirectory, $vendorKey);
438
439
        $this->configureCorrectComposerSetup();
440
441
        putenv('GNUPGHOME=' . $personalGpgDirectory);
442
443
        $this->assertWillFailPackageVerification();
444
    }
445
446 View Code Duplication
    private function makeVendorDirectory() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
447
    {
448
        $vendorDirectory = sys_get_temp_dir() . '/' . uniqid('vendor', true);
449
450
        self::assertTrue(mkdir($vendorDirectory));
451
452
        return $vendorDirectory;
453
    }
454
455
    private function signDependency(
456
        string $dependencyDirectory,
457
        string $gpgHomeDirectory,
458
        string $signingKey
459
    ) : void {
460
        (new Process(sprintf('git config --local --add user.signingkey %s', escapeshellarg($signingKey)), $dependencyDirectory))
461
            ->setTimeout(30)
462
            ->mustRun();
463
464
        (new Process(
465
            'git commit --allow-empty -m "signed commit" -S',
466
            $dependencyDirectory,
467
            ['GNUPGHOME' => $gpgHomeDirectory, 'GIT_TRACE' => '2']
468
        ))
469
            ->setTimeout(30)
470
            ->mustRun();
471
    }
472
473
    private function createDependencySignedTag(
474
        string $dependencyDirectory,
475
        string $gpgHomeDirectory,
476
        string $signingKey
477
    ) : void {
478
        (new Process(sprintf('git config --local --add user.signingkey %s', escapeshellarg($signingKey)), $dependencyDirectory))
479
            ->setTimeout(30)
480
            ->mustRun();
481
482
        (new Process('git commit --allow-empty -m "unsigned commit"', $dependencyDirectory))
483
            ->setTimeout(30)
484
            ->mustRun();
485
486
        (new Process(
487
            'git tag -s "tag-name" -m "signed tag"',
488
            $dependencyDirectory,
489
            ['GNUPGHOME' => $gpgHomeDirectory, 'GIT_TRACE' => '2']
490
        ))
491
            ->setTimeout(30)
492
            ->mustRun();
493
    }
494
495
    private function makeDependencyGitRepository(
496
        string $vendorDirectory,
497
        string $packageName,
498
        string $email,
499
        string $name
500
    ) : string {
501
        $dependencyRepository = $vendorDirectory . '/' . $packageName;
502
503
        self::assertTrue(mkdir($dependencyRepository, 0777, true));
504
505
        (new Process('git init', $dependencyRepository))
506
            ->setTimeout(30)
507
            ->mustRun();
508
509
        (new Process(sprintf('git config --local --add user.email %s', escapeshellarg($email)), $dependencyRepository))
510
            ->setTimeout(30)
511
            ->mustRun();
512
513
        (new Process(sprintf('git config --local --add user.name %s', escapeshellarg($name)), $dependencyRepository))
514
            ->setTimeout(30)
515
            ->mustRun();
516
517
        /* @var $package PackageInterface|\PHPUnit_Framework_MockObject_MockObject */
518
        $package = $this->createMock(PackageInterface::class);
519
520
        $package->expects(self::any())->method('getName')->willReturn($packageName);
521
522
        $this->installedPackages[$dependencyRepository] = $package;
523
524
        return $dependencyRepository;
525
    }
526
527 View Code Duplication
    private function makeGpgHomeDirectory() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
528
    {
529
        $homeDirectory = sys_get_temp_dir() . '/' . uniqid('gpg-verification-test', true);
530
531
        self::assertTrue(mkdir($homeDirectory, 0700));
532
533
        return $homeDirectory;
534
    }
535
536
    private function makeKey(string $gpgHomeDirectory, string $emailAddress, string $name) : string
537
    {
538
        $input = <<<'KEY'
539
%echo Generating a standard key
540
Key-Type: RSA
541
Key-Length: 128
542
Name-Real: <<<NAME>>>
543
Name-Email: <<<EMAIL>>>
544
Expire-Date: 0
545
%no-protection
546
%no-ask-passphrase
547
%commit
548
%echo done
549
550
KEY;
551
        self::assertGreaterThan(
552
            0,
553
            file_put_contents(
554
                $gpgHomeDirectory . '/key-info.txt',
555
                str_replace(['<<<NAME>>>', '<<<EMAIL>>>'], [$name, $emailAddress], $input)
556
            )
557
        );
558
559
        $keyOutput = (new Process(
560
            'gpg --batch --gen-key -a key-info.txt',
561
            $gpgHomeDirectory,
562
            ['GNUPGHOME' => $gpgHomeDirectory]
563
        ))
564
            ->setTimeout(30)
565
            ->mustRun()
566
            ->getErrorOutput();
567
568
        self::assertRegExp('/key [0-9A-F]+ marked as ultimately trusted/i', $keyOutput);
569
570
        preg_match('/key ([0-9A-F]+) marked as ultimately trusted/i', $keyOutput, $matches);
571
572
        return $matches[1];
573
    }
574
575
    private function configureCorrectComposerSetup() : void
576
    {
577
        $this
578
            ->config
579
            ->expects(self::any())
580
            ->method('get')
581
            ->with('preferred-install')
582
            ->willReturn('source');
583
    }
584
585
    private function assertWillFailPackageVerification(string ...$packages) : void
586
    {
587
        $this->expectException(\RuntimeException::class);
588
        $this->expectExceptionMessage(
589
            'The following packages need to be signed and verified, or added to exclusions: '
590
            . "\n"
591
            . implode("\n", $packages)
592
        );
593
594
        Verify::verify($this->event);
595
    }
596
597
    private function importForeignKeys(
598
        string $localGpgHome,
599
        string $foreignGpgHome,
600
        string $foreignKey,
601
        bool $sign
602
    ) : void {
603
        $exportPath = sys_get_temp_dir() . '/' . uniqid('exportedKey', true);
604
605
        (new Process(
606
            sprintf('gpg --export --armor > %s', escapeshellarg($exportPath)),
607
            null,
608
            ['GNUPGHOME' => $foreignGpgHome]
609
        ))
610
            ->setTimeout(30)
611
            ->mustRun();
612
613
        self::assertFileExists($exportPath);
614
615
        (new Process(
616
            sprintf('gpg --import < %s', escapeshellarg($exportPath)),
617
            null,
618
            ['GNUPGHOME' => $localGpgHome]
619
        ))
620
            ->setTimeout(30)
621
            ->mustRun();
622
623
        if (! $sign) {
624
            return;
625
        }
626
627
        (new Process(
628
            sprintf('gpg --batch --yes --sign-key %s', escapeshellarg($foreignKey)),
629
            null,
630
            ['GNUPGHOME' => $localGpgHome]
631
        ))
632
            ->setTimeout(30)
633
            ->mustRun();
634
    }
635
}
636