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

VerifyTest::testWillRejectUnSignedCommits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
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\Exception\PackagesTrustCheckFailed;
17
use Roave\ComposerGpgVerify\Verify;
18
use Symfony\Component\Process\Process;
19
20
/**
21
 * @covers \Roave\ComposerGpgVerify\Verify
22
 */
23
final class VerifyTest extends TestCase
24
{
25
    /**
26
     * @var Event|\PHPUnit_Framework_MockObject_MockObject
27
     */
28
    private $event;
29
30
    /**
31
     * @var Composer|\PHPUnit_Framework_MockObject_MockObject
32
     */
33
    private $composer;
34
35
    /**
36
     * @var Config|\PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private $config;
39
40
    /**
41
     * @var RepositoryManager|\PHPUnit_Framework_MockObject_MockObject
42
     */
43
    private $repositoryManager;
44
45
    /**
46
     * @var InstallationManager|\PHPUnit_Framework_MockObject_MockObject
47
     */
48
    private $installationManager;
49
50
    /**
51
     * @var RepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
52
     */
53
    private $localRepository;
54
55
    /**
56
     * @var string
57
     */
58
    private $originalGpgHome;
59
60
    /**
61
     * @var string
62
     */
63
    private $originalLanguage;
64
65
    /**
66
     * @var PackageInterface[] indexed by installation path
67
     */
68
    private $installedPackages = [];
69
70
    protected function setUp() : void
71
    {
72
        parent::setUp();
73
74
        $this->installedPackages = [];
75
        $this->originalGpgHome   = (string) getenv('GNUPGHOME');
76
        $this->originalLanguage  = (string) getenv('LANGUAGE');
77
78
        $this->event               = $this->createMock(Event::class);
79
        $this->composer            = $this->createMock(Composer::class);
80
        $this->config              = $this->createMock(Config::class);
81
        $this->repositoryManager   = $this->createMock(RepositoryManager::class);
82
        $this->installationManager = $this->createMock(InstallationManager::class);
83
        $this->localRepository     = $this->createMock(RepositoryInterface::class);
84
85
        $this->event->expects(self::any())->method('getComposer')->willReturn($this->composer);
86
        $this->composer->expects(self::any())->method('getConfig')->willReturn($this->config);
87
        $this
88
            ->composer
89
            ->expects(self::any())
90
            ->method('getRepositoryManager')
91
            ->willReturn($this->repositoryManager);
92
        $this
93
            ->composer
94
            ->expects(self::any())
95
            ->method('getInstallationManager')
96
            ->willReturn($this->installationManager);
97
        $this
98
            ->repositoryManager
99
            ->expects(self::any())
100
            ->method('getLocalRepository')
101
            ->willReturn($this->localRepository);
102
        $this
103
            ->installationManager
104
            ->expects(self::any())
105
            ->method('getInstallPath')
106
            ->willReturnCallback(function (PackageInterface $package) : string {
107
                return array_search($package, $this->installedPackages, true);
108
            });
109
        $this
110
            ->localRepository
111
            ->expects(self::any())
112
            ->method('getPackages')
113
            ->willReturnCallback(function () {
114
                return array_values($this->installedPackages);
115
            });
116
    }
117
118
    protected function tearDown() : void
119
    {
120
        putenv(sprintf('GNUPGHOME=%s', $this->originalGpgHome));
121
        putenv(sprintf('LANGUAGE=%s', $this->originalLanguage));
122
123
        parent::tearDown();
124
    }
125
126
    public function testWillDisallowInstallationOnNonSourceInstall() : void
127
    {
128
        $this
129
            ->config
130
            ->expects(self::any())
131
            ->method('get')
132
            ->with('preferred-install')
133
            ->willReturn('foo');
134
135
        $this->expectException(\LogicException::class);
136
        $this->expectExceptionMessage('Expected installation "preferred-install" to be "source", found "foo" instead');
137
138
        Verify::verify($this->event);
139
    }
140
141
    public function testWillRetrieveSubscribedEvents() : void
142
    {
143
        $events = Verify::getSubscribedEvents();
144
145
        self::assertNotEmpty($events);
146
147
        $availableEvents = (new \ReflectionClass(ScriptEvents::class))->getConstants();
148
149
        foreach ($events as $eventName => $callback) {
150
            self::assertContains($eventName, $availableEvents);
151
            self::assertInternalType('string', $callback);
152
            self::assertInternalType('callable', [Verify::class, $callback]);
153
        }
154
    }
155
156 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...
157
    {
158
        $gpgHomeDirectory = $this->makeGpgHomeDirectory();
159
160
        $vendorName  = 'Mr. Magoo';
161
        $vendorEmail = '[email protected]';
162
        $vendorKey   = $this->makeKey($gpgHomeDirectory, $vendorEmail, $vendorName);
163
        $vendorDir   = $this->makeVendorDirectory();
164
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
165
166
        $this->signDependency($vendor1, $gpgHomeDirectory, $vendorKey);
167
168
        $this->configureCorrectComposerSetup();
169
170
        putenv('GNUPGHOME=' . $gpgHomeDirectory);
171
172
        Verify::verify($this->event);
173
    }
174
175 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...
176
    {
177
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
178
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
179
180
        $this->makeKey($personalGpgDirectory, '[email protected]', 'Just Me');
181
182
        $vendorName  = 'Mr. Magoo';
183
        $vendorEmail = '[email protected]';
184
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
185
        $vendorDir   = $this->makeVendorDirectory();
186
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
187
188
        $this->signDependency($vendor1, $foreignGpgDirectory, $vendorKey);
189
190
        $this->importForeignKeys($personalGpgDirectory, $foreignGpgDirectory, $vendorKey, false);
191
192
        $this->configureCorrectComposerSetup();
193
194
        putenv('GNUPGHOME=' . $personalGpgDirectory);
195
196
        $this->assertWillFailPackageVerification();
197
    }
198
199
    public function testWillRejectPackageSignedWithImportedButUnTrustedKeyWithDifferentLocaleSettings() : void
200
    {
201
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
202
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
203
204
        $this->makeKey($personalGpgDirectory, '[email protected]', 'Just Me');
205
206
        $vendorName  = 'Mr. Magoo';
207
        $vendorEmail = '[email protected]';
208
209
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
210
        $vendorDir   = $this->makeVendorDirectory();
211
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
212
213
        $this->signDependency($vendor1, $foreignGpgDirectory, $vendorKey);
214
215
        $this->importForeignKeys($personalGpgDirectory, $foreignGpgDirectory, $vendorKey, false);
216
217
        $this->configureCorrectComposerSetup();
218
219
        putenv('GNUPGHOME=' . $personalGpgDirectory);
220
        putenv('LANGUAGE=de_DE');
221
222
        try {
223
            Verify::verify($this->event);
224
        } catch (PackagesTrustCheckFailed $failure) {
225
            self::assertSame('de_DE', getenv('LANGUAGE'));
226
227
            return;
228
        }
229
230
        self::fail('Exception was not thrown');
231
    }
232
233 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...
234
    {
235
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
236
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
237
238
        $this->makeKey($personalGpgDirectory, '[email protected]', 'Just Me');
239
240
        $vendorName  = 'Mr. Magoo';
241
        $vendorEmail = '[email protected]';
242
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
243
        $vendorDir   = $this->makeVendorDirectory();
244
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
245
246
        $this->signDependency($vendor1, $foreignGpgDirectory, $vendorKey);
247
248
        $this->importForeignKeys($personalGpgDirectory, $foreignGpgDirectory, $vendorKey, true);
249
250
        $this->configureCorrectComposerSetup();
251
252
        putenv('GNUPGHOME=' . $personalGpgDirectory);
253
254
        Verify::verify($this->event);
255
    }
256
257 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...
258
    {
259
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
260
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
261
262
        $this->makeKey($personalGpgDirectory, '[email protected]', 'Just Me');
263
264
        $vendorName  = 'Mr. Magoo';
265
        $vendorEmail = '[email protected]';
266
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
267
        $vendorDir   = $this->makeVendorDirectory();
268
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
269
270
        $this->createDependencySignedTag($vendor1, $foreignGpgDirectory, $vendorKey);
271
272
        $this->importForeignKeys($personalGpgDirectory, $foreignGpgDirectory, $vendorKey, false);
273
274
        $this->configureCorrectComposerSetup();
275
276
        putenv('GNUPGHOME=' . $personalGpgDirectory);
277
278
        $this->assertWillFailPackageVerification();
279
    }
280
281 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...
282
    {
283
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
284
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
285
286
        $this->makeKey($personalGpgDirectory, '[email protected]', 'Just Me');
287
288
        $vendorName  = 'Mr. Magoo';
289
        $vendorEmail = '[email protected]';
290
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
291
        $vendorDir   = $this->makeVendorDirectory();
292
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
293
294
        $this->createDependencySignedTag($vendor1, $foreignGpgDirectory, $vendorKey);
295
296
        $this->importForeignKeys($personalGpgDirectory, $foreignGpgDirectory, $vendorKey, true);
297
298
        $this->configureCorrectComposerSetup();
299
300
        putenv('GNUPGHOME=' . $personalGpgDirectory);
301
302
        Verify::verify($this->event);
303
    }
304
305 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...
306
    {
307
        $gpgHomeDirectory = $this->makeGpgHomeDirectory();
308
309
        $vendorName  = 'Mr. Magoo';
310
        $vendorEmail = '[email protected]';
311
        $vendorKey   = $this->makeKey($gpgHomeDirectory, $vendorEmail, $vendorName);
312
        $vendorDir   = $this->makeVendorDirectory();
313
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
314
315
        $this->createDependencySignedTag($vendor1, $gpgHomeDirectory, $vendorKey);
316
317
        $this->configureCorrectComposerSetup();
318
319
        putenv('GNUPGHOME=' . $gpgHomeDirectory);
320
321
        Verify::verify($this->event);
322
    }
323
324
    public function testWillRejectUnSignedCommits() : void
325
    {
326
        $vendorName  = 'Mr. Magoo';
327
        $vendorEmail = '[email protected]';
328
        $vendorDir   = $this->makeVendorDirectory();
329
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
330
331
        (new Process('git commit --allow-empty -m "unsigned commit"', $vendor1))
332
            ->setTimeout(30)
333
            ->mustRun();
334
335
        $this->configureCorrectComposerSetup();
336
337
        putenv('GNUPGHOME=' . $this->makeGpgHomeDirectory());
338
339
        $this->assertWillFailPackageVerification();
340
    }
341
342
    public function testWillRejectUnSignedTags() : void
343
    {
344
        $vendorName  = 'Mr. Magoo';
345
        $vendorEmail = '[email protected]';
346
        $vendorDir   = $this->makeVendorDirectory();
347
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
348
349
        (new Process('git commit --allow-empty -m "unsigned commit"', $vendor1))
350
            ->setTimeout(30)
351
            ->mustRun();
352
353
        (new Process('git tag unsigned-tag -m "unsigned tag"', $vendor1))
354
            ->setTimeout(30)
355
            ->mustRun();
356
357
        $this->configureCorrectComposerSetup();
358
359
        putenv('GNUPGHOME=' . $this->makeGpgHomeDirectory());
360
361
        $this->assertWillFailPackageVerification();
362
    }
363
364 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...
365
    {
366
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
367
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
368
        $vendorName  = 'Mr. Magoo';
369
        $vendorEmail = '[email protected]';
370
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
371
        $vendorDir   = $this->makeVendorDirectory();
372
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
373
374
        $this->createDependencySignedTag($vendor1, $foreignGpgDirectory, $vendorKey);
375
376
        $this->configureCorrectComposerSetup();
377
378
        putenv('GNUPGHOME=' . $personalGpgDirectory);
379
380
        $this->assertWillFailPackageVerification();
381
    }
382
383 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...
384
    {
385
        $gpgHome     = $this->makeGpgHomeDirectory();
386
        $vendorName  = 'Mr. Magoo';
387
        $vendorEmail = '[email protected]';
388
        $vendorKey   = $this->makeKey($gpgHome, $vendorEmail, $vendorName);
389
        $vendorDir   = $this->makeVendorDirectory();
390
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
391
392
        $this->createDependencySignedTag($vendor1, $gpgHome, $vendorKey);
393
394
        (new Process('git commit --allow-empty -m "unsigned commit"', $vendor1))
395
            ->setTimeout(30)
396
            ->mustRun();
397
398
        $this->configureCorrectComposerSetup();
399
400
        putenv('GNUPGHOME=' . $gpgHome);
401
402
        $this->assertWillFailPackageVerification();
403
    }
404
405 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...
406
    {
407
        $gpgHome     = $this->makeGpgHomeDirectory();
408
        $vendorName  = 'Mr. Magoo';
409
        $vendorEmail = '[email protected]';
410
        $vendorKey   = $this->makeKey($gpgHome, $vendorEmail, $vendorName);
411
        $vendorDir   = $this->makeVendorDirectory();
412
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
413
414
        $this->signDependency($vendor1, $gpgHome, $vendorKey);
415
416
        (new Process('git commit --allow-empty -m "unsigned commit"', $vendor1))
417
            ->setTimeout(30)
418
            ->mustRun();
419
420
        $this->configureCorrectComposerSetup();
421
422
        putenv('GNUPGHOME=' . $gpgHome);
423
424
        $this->assertWillFailPackageVerification();
425
    }
426
427 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...
428
    {
429
        $personalGpgDirectory = $this->makeGpgHomeDirectory();
430
        $foreignGpgDirectory  = $this->makeGpgHomeDirectory();
431
432
        $vendorName  = 'Mr. Magoo';
433
        $vendorEmail = '[email protected]';
434
        $vendorKey   = $this->makeKey($foreignGpgDirectory, $vendorEmail, $vendorName);
435
        $vendorDir   = $this->makeVendorDirectory();
436
        $vendor1     = $this->makeDependencyGitRepository($vendorDir, 'vendor1/package1', $vendorEmail, $vendorName);
437
438
        $this->signDependency($vendor1, $foreignGpgDirectory, $vendorKey);
439
440
        $this->configureCorrectComposerSetup();
441
442
        putenv('GNUPGHOME=' . $personalGpgDirectory);
443
444
        $this->assertWillFailPackageVerification();
445
    }
446
447 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...
448
    {
449
        $vendorDirectory = sys_get_temp_dir() . '/' . uniqid('vendor', true);
450
451
        self::assertTrue(mkdir($vendorDirectory));
452
453
        return $vendorDirectory;
454
    }
455
456
    private function signDependency(
457
        string $dependencyDirectory,
458
        string $gpgHomeDirectory,
459
        string $signingKey
460
    ) : void {
461
        (new Process(sprintf('git config --local --add user.signingkey %s', escapeshellarg($signingKey)), $dependencyDirectory))
462
            ->setTimeout(30)
463
            ->mustRun();
464
465
        (new Process(
466
            'git commit --allow-empty -m "signed commit" -S',
467
            $dependencyDirectory,
468
            ['GNUPGHOME' => $gpgHomeDirectory, 'GIT_TRACE' => '2']
469
        ))
470
            ->setTimeout(30)
471
            ->mustRun();
472
    }
473
474
    private function createDependencySignedTag(
475
        string $dependencyDirectory,
476
        string $gpgHomeDirectory,
477
        string $signingKey
478
    ) : void {
479
        (new Process(sprintf('git config --local --add user.signingkey %s', escapeshellarg($signingKey)), $dependencyDirectory))
480
            ->setTimeout(30)
481
            ->mustRun();
482
483
        (new Process('git commit --allow-empty -m "unsigned commit"', $dependencyDirectory))
484
            ->setTimeout(30)
485
            ->mustRun();
486
487
        (new Process(
488
            'git tag -s "tag-name" -m "signed tag"',
489
            $dependencyDirectory,
490
            ['GNUPGHOME' => $gpgHomeDirectory, 'GIT_TRACE' => '2']
491
        ))
492
            ->setTimeout(30)
493
            ->mustRun();
494
    }
495
496
    private function makeDependencyGitRepository(
497
        string $vendorDirectory,
498
        string $packageName,
499
        string $email,
500
        string $name
501
    ) : string {
502
        $dependencyRepository = $vendorDirectory . '/' . $packageName;
503
504
        self::assertTrue(mkdir($dependencyRepository, 0777, true));
505
506
        (new Process('git init', $dependencyRepository))
507
            ->setTimeout(30)
508
            ->mustRun();
509
510
        (new Process(sprintf('git config --local --add user.email %s', escapeshellarg($email)), $dependencyRepository))
511
            ->setTimeout(30)
512
            ->mustRun();
513
514
        (new Process(sprintf('git config --local --add user.name %s', escapeshellarg($name)), $dependencyRepository))
515
            ->setTimeout(30)
516
            ->mustRun();
517
518
        /* @var $package PackageInterface|\PHPUnit_Framework_MockObject_MockObject */
519
        $package = $this->createMock(PackageInterface::class);
520
521
        $package->expects(self::any())->method('getName')->willReturn($packageName);
522
523
        $this->installedPackages[$dependencyRepository] = $package;
524
525
        return $dependencyRepository;
526
    }
527
528 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...
529
    {
530
        $homeDirectory = sys_get_temp_dir() . '/' . uniqid('gpg-verification-test', true);
531
532
        self::assertTrue(mkdir($homeDirectory, 0700));
533
534
        return $homeDirectory;
535
    }
536
537
    private function makeKey(string $gpgHomeDirectory, string $emailAddress, string $name) : string
538
    {
539
        $input = <<<'KEY'
540
%echo Generating a standard key
541
Key-Type: RSA
542
Key-Length: 128
543
Name-Real: <<<NAME>>>
544
Name-Email: <<<EMAIL>>>
545
Expire-Date: 0
546
%no-protection
547
%no-ask-passphrase
548
%commit
549
%echo done
550
551
KEY;
552
        self::assertGreaterThan(
553
            0,
554
            file_put_contents(
555
                $gpgHomeDirectory . '/key-info.txt',
556
                str_replace(['<<<NAME>>>', '<<<EMAIL>>>'], [$name, $emailAddress], $input)
557
            )
558
        );
559
560
        $keyOutput = (new Process(
561
            'gpg --batch --gen-key -a key-info.txt',
562
            $gpgHomeDirectory,
563
            ['GNUPGHOME' => $gpgHomeDirectory]
564
        ))
565
            ->setTimeout(30)
566
            ->mustRun()
567
            ->getErrorOutput();
568
569
        self::assertRegExp('/key [0-9A-F]+ marked as ultimately trusted/i', $keyOutput);
570
571
        preg_match('/key ([0-9A-F]+) marked as ultimately trusted/i', $keyOutput, $matches);
572
573
        return $matches[1];
574
    }
575
576
    private function configureCorrectComposerSetup() : void
577
    {
578
        $this
579
            ->config
580
            ->expects(self::any())
581
            ->method('get')
582
            ->with('preferred-install')
583
            ->willReturn('source');
584
    }
585
586
    private function assertWillFailPackageVerification(string ...$packages) : void
587
    {
588
        $this->expectException(PackagesTrustCheckFailed::class);
589
590
        Verify::verify($this->event);
591
    }
592
593
    private function importForeignKeys(
594
        string $localGpgHome,
595
        string $foreignGpgHome,
596
        string $foreignKey,
597
        bool $sign
598
    ) : void {
599
        $exportPath = sys_get_temp_dir() . '/' . uniqid('exportedKey', true);
600
601
        (new Process(
602
            sprintf('gpg --export --armor > %s', escapeshellarg($exportPath)),
603
            null,
604
            ['GNUPGHOME' => $foreignGpgHome]
605
        ))
606
            ->setTimeout(30)
607
            ->mustRun();
608
609
        self::assertFileExists($exportPath);
610
611
        (new Process(
612
            sprintf('gpg --import < %s', escapeshellarg($exportPath)),
613
            null,
614
            ['GNUPGHOME' => $localGpgHome]
615
        ))
616
            ->setTimeout(30)
617
            ->mustRun();
618
619
        if (! $sign) {
620
            return;
621
        }
622
623
        (new Process(
624
            sprintf('gpg --batch --yes --sign-key %s', escapeshellarg($foreignKey)),
625
            null,
626
            ['GNUPGHOME' => $localGpgHome]
627
        ))
628
            ->setTimeout(30)
629
            ->mustRun();
630
    }
631
}
632