Completed
Pull Request — master (#61)
by Matthieu
04:14
created

testWillFailWithoutValidComposerLockLocation()   A

Complexity

Conditions 2
Paths 7

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 16
nc 7
nop 0
1
<?php
2
3
namespace PackageVersionsTest;
4
5
use PackageVersions\FallbackVersions;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * @covers \PackageVersions\FallbackVersions
10
 */
11
final class FallbackVersionsTest extends TestCase
12
{
13
    public function testWillFailWithoutValidComposerLockLocation() : void
14
    {
15
        rename(__DIR__ . '/../../composer.lock', __DIR__ . '/../../composer.lock.backup');
16
17
        try {
18
            FallbackVersions::getVersion('phpunit/phpunit');
19
20
            self::fail('An exception was supposed to be thrown');
21
        } catch (\UnexpectedValueException $lockFileNotFound) {
22
            $srcDir = realpath(__DIR__ . '/../../src/PackageVersions');
23
24
            self::assertSame(
25
                'PackageVersions could not locate your `composer.lock` location. '
26
                . 'This is assumed to be in '
27
                . json_encode([$srcDir . '/../../../../../composer.lock', $srcDir . '/../../composer.lock'])
28
                . '. If you customized your composer vendor directory and ran composer installation with --no-scripts, '
29
                . 'then you are on your own, and we can\'t really help you.',
30
                $lockFileNotFound->getMessage()
31
            );
32
        } finally {
33
            rename(__DIR__ . '/../../composer.lock.backup', __DIR__ . '/../../composer.lock');
34
        }
35
    }
36
37
    public function testValidVersions() : void
38
    {
39
        $lockData = json_decode(file_get_contents(__DIR__ . '/../../composer.lock'), true);
40
41
        $packages = array_merge($lockData['packages'], $lockData['packages-dev']);
42
43
        self::assertNotEmpty($packages);
44
45
        foreach ($packages as $package) {
46
            self::assertSame(
47
                $package['version'] . '@' . $package['source']['reference'],
48
                FallbackVersions::getVersion($package['name'])
49
            );
50
        }
51
    }
52
53
    public function testInvalidVersionsAreRejected() : void
54
    {
55
        $this->expectException(\OutOfBoundsException::class);
56
57
        FallbackVersions::getVersion(uniqid('', true) . '/' . uniqid('', true));
58
    }
59
}
60