Completed
Pull Request — master (#82)
by
unknown
02:15
created

FallbackVersionsTest::tearDown()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PackageVersionsTest;
6
7
use OutOfBoundsException;
8
use PackageVersions\FallbackVersions;
9
use PHPUnit\Framework\TestCase;
10
use UnexpectedValueException;
11
use function array_merge;
12
use function file_get_contents;
13
use function json_decode;
14
use function json_encode;
15
use function realpath;
16
use function rename;
17
use function uniqid;
18
19
/**
20
 * @covers \PackageVersions\FallbackVersions
21
 */
22
final class FallbackVersionsTest extends TestCase
23
{
24
    public function testWillFailWithoutValidComposerLockLocation() : void
25
    {
26
        rename(__DIR__ . '/../../vendor/composer/installed.json', __DIR__ . '/../../vendor/composer/installed.json.backup');
27
        rename(__DIR__ . '/../../composer.lock', __DIR__ . '/../../composer.lock.backup');
28
29
        $this->expectException(\UnexpectedValueException::class);
30
        $this->expectExceptionMessageRegExp(
31
            '@PackageVersions could not locate the `vendor/composer/installed\.json` or your `composer\.lock` '
32
            . 'location\. This is assumed to be in \[[^]]+?\]\. If you customized your composer vendor directory and ran composer '
33
            . 'installation with --no-scripts or if you deployed without the required composer files, then you are on '
34
            . 'your own, and we can\'t really help you\. Fix your shit and cut the tooling some slack\.@'
35
        );
36
37
        FallbackVersions::getVersion('phpunit/phpunit');
38
    }
39
40
    public function testValidVersions() : void
41
    {
42
        $lockData = json_decode(file_get_contents(__DIR__ . '/../../composer.lock'), true);
43
44
        $packages = array_merge($lockData['packages'], $lockData['packages-dev']);
45
46
        self::assertNotEmpty($packages);
47
48
        foreach ($packages as $package) {
49
            self::assertSame(
50
                $package['version'] . '@' . $package['source']['reference'],
51
                FallbackVersions::getVersion($package['name'])
52
            );
53
        }
54
    }
55
56
    public function testInvalidVersionsAreRejected() : void
57
    {
58
        $this->expectException(OutOfBoundsException::class);
59
60
        FallbackVersions::getVersion(uniqid('', true) . '/' . uniqid('', true));
61
    }
62
63
    protected function tearDown()
64
    {
65
        if (file_exists(__DIR__ . '/../../vendor/composer/installed.json.backup')) {
66
            rename(__DIR__ . '/../../vendor/composer/installed.json.backup', __DIR__ . '/../../vendor/composer/installed.json');
67
        }
68
69
        if (file_exists(__DIR__ . '/../../composer.lock.backup')) {
70
            rename(__DIR__ . '/../../composer.lock.backup', __DIR__ . '/../../composer.lock');
71
        }
72
    }
73
}
74