Passed
Push — master ( 58d3a9...553f37 )
by Marco
02:50
created

testFromRepositoryAllowsVersionPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility\Git;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\Git\CheckedOutRepository;
9
use Roave\BackwardCompatibility\Git\GetVersionCollectionFromGitRepository;
10
use Symfony\Component\Process\Process;
11
use Version\Version;
12
use function array_map;
13
use function file_put_contents;
14
use function iterator_to_array;
15
use function mkdir;
16
use function sys_get_temp_dir;
17
use function uniqid;
18
19
/**
20
 * @covers \Roave\BackwardCompatibility\Git\GetVersionCollectionFromGitRepository
21
 */
22
final class GetVersionCollectionFromGitRepositoryTest extends TestCase
23
{
24
    /** @var CheckedOutRepository */
25
    private $repoPath;
26
27
    public function setUp() : void
28
    {
29
        $tmpGitRepo = sys_get_temp_dir() . '/api-compare-' . uniqid('tmpGitRepo', true);
30
        mkdir($tmpGitRepo, 0777, true);
31
        (new Process(['git', 'init']))->setWorkingDirectory($tmpGitRepo)->mustRun();
32
        file_put_contents($tmpGitRepo . '/test', uniqid('testContent', true));
33
        (new Process(['git', 'add', '.']))->setWorkingDirectory($tmpGitRepo)->mustRun();
34
        (new Process(['git', 'commit', '-m', '"whatever"']))->setWorkingDirectory($tmpGitRepo)->mustRun();
35
        $this->repoPath = CheckedOutRepository::fromPath($tmpGitRepo);
36
    }
37
38
    public function tearDown() : void
39
    {
40
        (new Process(['rm', '-Rf', (string) $this->repoPath]))->mustRun();
41
    }
42
43
    private function makeTag(string $tagName) : void
44
    {
45
        (new Process(['git', 'tag', $tagName]))->setWorkingDirectory((string) $this->repoPath)->mustRun();
46
    }
47
48
    /** @return string[] */
49
    private function getTags() : array
50
    {
51
        return array_map(
52
            function (Version $version) {
53
                return $version->getVersionString();
54
            },
55
            iterator_to_array((new GetVersionCollectionFromGitRepository())->fromRepository($this->repoPath))
56
        );
57
    }
58
59
    public function testFromRepository() : void
60
    {
61
        $this->makeTag('1.0.0');
62
63
        self::assertSame(['1.0.0'], $this->getTags());
64
    }
65
66
    public function testFromRepositoryIgnoresInvalidVersions() : void
67
    {
68
        $this->makeTag('1.0.0');
69
        $this->makeTag('invalid-version');
70
        $this->makeTag('1.1.0');
71
72
        self::assertSame(['1.0.0', '1.1.0'], $this->getTags());
73
    }
74
75
    public function testFromRepositoryAllowsVersionPrefix() : void
76
    {
77
        $this->makeTag('v1.0.0');
78
79
        self::assertSame(['1.0.0'], $this->getTags());
80
    }
81
}
82