Completed
Push — master ( ba85a6...34f627 )
by Marco
12s queued 10s
created

testExceptionIsThrownWhenTwoPathsCollide()   A

Complexity

Conditions 2
Paths 7

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 7
nop 0
dl 0
loc 26
rs 9.7
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\GitCheckoutRevisionToTemporaryPath;
10
use Roave\BackwardCompatibility\Git\Revision;
11
use function realpath;
12
13
/**
14
 * @covers \Roave\BackwardCompatibility\Git\GitCheckoutRevisionToTemporaryPath
15
 */
16
final class GitCheckoutRevisionToTemporaryPathTest extends TestCase
17
{
18
    private const TEST_REVISION_TO_CHECKOUT = '428327492a803b6e0c612b157a67a50a47275461';
19
20
    public function testCheckoutAndRemove() : void
21
    {
22
        $git      = new GitCheckoutRevisionToTemporaryPath();
23
        $revision = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT);
24
25
        $temporaryClone = $git->checkout($this->sourceRepository(), $revision);
26
27
        self::assertDirectoryExists((string) $temporaryClone);
28
29
        $git->remove($temporaryClone);
30
    }
31
32
    public function testCanCheckOutSameRevisionTwice() : void
33
    {
34
        $git              = new GitCheckoutRevisionToTemporaryPath();
35
        $sourceRepository = $this->sourceRepository();
36
        $revision         = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT);
37
38
        $first  = $git->checkout($sourceRepository, $revision);
39
        $second = $git->checkout($sourceRepository, $revision);
40
41
        self::assertDirectoryExists((string) $first);
42
        self::assertDirectoryExists((string) $second);
43
44
        $git->remove($first);
45
        $git->remove($second);
46
    }
47
48
    public function testExceptionIsThrownWhenTwoPathsCollide() : void
49
    {
50
        $git              = new GitCheckoutRevisionToTemporaryPath(function () : string {
51
            return 'foo';
52
        });
53
        $sourceRepository = $this->sourceRepository();
54
        $revision         = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT);
55
56
        $first = $git->checkout($sourceRepository, $revision);
57
58
        $successfullyCheckedOutSecondClone = false;
59
60
        try {
61
            $second                            = $git->checkout($sourceRepository, $revision);
62
            $successfullyCheckedOutSecondClone = true;
63
            $git->remove($second);
64
        } catch (\RuntimeException $runtimeException) {
65
            self::assertStringMatchesFormat(
66
                'Tried to check out revision "%s" to directory "%s" which already exists',
67
                $runtimeException->getMessage()
68
            );
69
        } finally {
70
            $git->remove($first);
71
        }
72
73
        self::assertFalse($successfullyCheckedOutSecondClone);
74
    }
75
76
    private function sourceRepository() : CheckedOutRepository
77
    {
78
        $repositoryPath = realpath(__DIR__ . '/../../..');
79
80
        self::assertInternalType('string', $repositoryPath);
81
82
        return CheckedOutRepository::fromPath($repositoryPath);
83
    }
84
}
85