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

FallbackVersionsTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 50
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testWillFailWithoutValidComposerLockLocation() 0 24 2
A testValidVersions() 0 15 2
A testInvalidVersionsAreRejected() 0 6 1
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