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( |
26
|
|
|
CheckedOutRepository::fromPath(realpath(__DIR__ . '/../../../')), |
27
|
|
|
$revision |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
self::assertDirectoryExists((string) $temporaryClone); |
31
|
|
|
|
32
|
|
|
$git->remove($temporaryClone); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testCanCheckOutSameRevisionTwice() : void |
36
|
|
|
{ |
37
|
|
|
$git = new GitCheckoutRevisionToTemporaryPath(); |
38
|
|
|
$sourceRepository = CheckedOutRepository::fromPath(realpath(__DIR__ . '/../../../')); |
39
|
|
|
$revision = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT); |
40
|
|
|
|
41
|
|
|
$first = $git->checkout($sourceRepository, $revision); |
42
|
|
|
$second = $git->checkout($sourceRepository, $revision); |
43
|
|
|
|
44
|
|
|
self::assertDirectoryExists((string) $first); |
45
|
|
|
self::assertDirectoryExists((string) $second); |
46
|
|
|
|
47
|
|
|
$git->remove($first); |
48
|
|
|
$git->remove($second); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testExceptionIsThrownWhenTwoPathsCollide() : void |
52
|
|
|
{ |
53
|
|
|
$git = new GitCheckoutRevisionToTemporaryPath(function () : string { |
54
|
|
|
return 'foo'; |
55
|
|
|
}); |
56
|
|
|
$sourceRepository = CheckedOutRepository::fromPath(realpath(__DIR__ . '/../../../')); |
57
|
|
|
$revision = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT); |
58
|
|
|
|
59
|
|
|
$first = $git->checkout($sourceRepository, $revision); |
60
|
|
|
|
61
|
|
|
$successfullyCheckedOutSecondClone = false; |
62
|
|
|
try { |
63
|
|
|
$second = $git->checkout($sourceRepository, $revision); |
64
|
|
|
$successfullyCheckedOutSecondClone = true; |
65
|
|
|
$git->remove($second); |
66
|
|
|
} catch (\RuntimeException $runtimeException) { |
67
|
|
|
self::assertStringMatchesFormat( |
68
|
|
|
'Tried to check out revision %s to directory %s which already exists', |
69
|
|
|
$runtimeException->getMessage() |
70
|
|
|
); |
71
|
|
|
} finally { |
72
|
|
|
$git->remove($first); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
self::assertFalse($successfullyCheckedOutSecondClone); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|