Completed
Push — master ( 922e61...fe7e59 )
by Marco
10s
created

FallbackVersionsTest::testValidVersions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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