Completed
Push — master ( 4eb33b...9d87b5 )
by Marco
11s queued 10s
created

GitCheckoutRevisionToTemporaryPathTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 122
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testExceptionIsThrownWhenTwoPathsCollide() 0 26 2
A testCheckoutAndRemove() 0 12 1
A sourceRepository() 0 3 1
A testCanCheckOutSameRevisionTwice() 0 17 1
A testCheckedOutRevisionIsAtExpectedRevisionState() 0 52 1
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 RuntimeException;
12
use Symfony\Component\Process\Process;
13
use function Safe\file_put_contents;
14
use function Safe\mkdir;
15
use function Safe\realpath;
16
use function Safe\tempnam;
17
use function Safe\unlink;
18
use function sys_get_temp_dir;
19
20
/**
21
 * @covers \Roave\BackwardCompatibility\Git\GitCheckoutRevisionToTemporaryPath
22
 */
23
final class GitCheckoutRevisionToTemporaryPathTest extends TestCase
24
{
25
    private const TEST_REVISION_TO_CHECKOUT = '428327492a803b6e0c612b157a67a50a47275461';
26
27
    public function testCheckoutAndRemove() : void
28
    {
29
        $git      = new GitCheckoutRevisionToTemporaryPath();
30
        $revision = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT);
31
32
        $temporaryClone = $git->checkout($this->sourceRepository(), $revision);
33
34
        self::assertDirectoryExists((string) $temporaryClone);
0 ignored issues
show
Bug introduced by
The method assertDirectoryExists() does not exist on RoaveTest\BackwardCompat...sionToTemporaryPathTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        self::/** @scrutinizer ignore-call */ 
35
              assertDirectoryExists((string) $temporaryClone);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
36
        $git->remove($temporaryClone);
37
38
        self::assertDirectoryNotExists((string) $temporaryClone);
0 ignored issues
show
Bug introduced by
The method assertDirectoryNotExists() does not exist on RoaveTest\BackwardCompat...sionToTemporaryPathTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        self::/** @scrutinizer ignore-call */ 
39
              assertDirectoryNotExists((string) $temporaryClone);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
    }
40
41
    public function testCanCheckOutSameRevisionTwice() : void
42
    {
43
        $git              = new GitCheckoutRevisionToTemporaryPath();
44
        $sourceRepository = $this->sourceRepository();
45
        $revision         = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT);
46
47
        $first  = $git->checkout($sourceRepository, $revision);
48
        $second = $git->checkout($sourceRepository, $revision);
49
50
        self::assertDirectoryExists((string) $first);
51
        self::assertDirectoryExists((string) $second);
52
53
        $git->remove($first);
54
        $git->remove($second);
55
56
        self::assertDirectoryNotExists((string) $first);
57
        self::assertDirectoryNotExists((string) $second);
58
    }
59
60
    public function testCheckedOutRevisionIsAtExpectedRevisionState() : void
61
    {
62
        $repoPath = tempnam(sys_get_temp_dir(), 'test-git-repo-');
63
64
        unlink($repoPath);
65
        mkdir($repoPath);
66
67
        (new Process(['git', 'init'], $repoPath))
68
            ->mustRun();
69
70
        (new Process(['git', 'config', 'user.email', '[email protected]'], $repoPath))
71
            ->mustRun();
72
73
        (new Process(['git', 'config', 'user.name', 'Mr Magoo'], $repoPath))
74
            ->mustRun();
75
76
        (new Process(['git', 'commit', '-m', 'initial commit', '--allow-empty'], $repoPath))
77
            ->mustRun();
78
79
        $firstCommit = Revision::fromSha1(
80
            (new Process(['git', 'rev-parse', 'HEAD'], $repoPath))
81
                ->mustRun()
82
                ->getOutput()
83
        );
84
85
        file_put_contents($repoPath . '/a-file.txt', 'file contents');
86
87
        (new Process(['git', 'add', 'a-file.txt'], $repoPath))
88
            ->mustRun();
89
90
        (new Process(['git', 'commit', '-m', 'second commit', '--allow-empty'], $repoPath))
91
            ->mustRun();
92
93
        $secondCommit = Revision::fromSha1(
94
            (new Process(['git', 'rev-parse', 'HEAD'], $repoPath))
95
                ->mustRun()
96
                ->getOutput()
97
        );
98
99
        $git = new GitCheckoutRevisionToTemporaryPath();
100
101
        $sourceRepository = CheckedOutRepository::fromPath($repoPath);
102
        $first            = $git->checkout($sourceRepository, $firstCommit);
103
        $second           = $git->checkout($sourceRepository, $secondCommit);
104
105
        self::assertFileNotExists($first->__toString() . '/a-file.txt');
0 ignored issues
show
Bug introduced by
The method assertFileNotExists() does not exist on RoaveTest\BackwardCompat...sionToTemporaryPathTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        self::/** @scrutinizer ignore-call */ 
106
              assertFileNotExists($first->__toString() . '/a-file.txt');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
        self::assertFileExists($second->__toString() . '/a-file.txt');
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not exist on RoaveTest\BackwardCompat...sionToTemporaryPathTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        self::/** @scrutinizer ignore-call */ 
107
              assertFileExists($second->__toString() . '/a-file.txt');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
108
        $git->remove($first);
109
        $git->remove($second);
110
111
        (new Process(['rm', '-rf', $repoPath]))->mustRun();
112
    }
113
114
    public function testExceptionIsThrownWhenTwoPathsCollide() : void
115
    {
116
        $git              = new GitCheckoutRevisionToTemporaryPath(static function () : string {
117
            return 'foo';
118
        });
119
        $sourceRepository = $this->sourceRepository();
120
        $revision         = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT);
121
122
        $first = $git->checkout($sourceRepository, $revision);
123
124
        $successfullyCheckedOutSecondClone = false;
125
126
        try {
127
            $second                            = $git->checkout($sourceRepository, $revision);
128
            $successfullyCheckedOutSecondClone = true;
129
            $git->remove($second);
130
        } catch (RuntimeException $runtimeException) {
131
            self::assertStringMatchesFormat(
0 ignored issues
show
Bug introduced by
The method assertStringMatchesFormat() does not exist on RoaveTest\BackwardCompat...sionToTemporaryPathTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
            self::/** @scrutinizer ignore-call */ 
132
                  assertStringMatchesFormat(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
                'Tried to check out revision "%s" to directory "%s" which already exists',
133
                $runtimeException->getMessage()
134
            );
135
        } finally {
136
            $git->remove($first);
137
        }
138
139
        self::assertFalse($successfullyCheckedOutSecondClone);
140
    }
141
142
    private function sourceRepository() : CheckedOutRepository
143
    {
144
        return CheckedOutRepository::fromPath(realpath(__DIR__ . '/../../..'));
145
    }
146
}
147