/
ComposerLockBackupTest.php
97 lines (75 loc) · 2.64 KB
/
ComposerLockBackupTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace Tests\Composer;
use Statamic\Console\Composer\Lock;
/**
* Test that we can backup a composer lock file using vanilla PHP so that it can be run in a Composer hook.
*/
class ComposerLockBackupTest extends \PHPUnit\Framework\TestCase
{
public function setUp(): void
{
parent::setUp();
$this->lockPath = './composer.lock';
$this->customLockPath = './custom/composer.lock';
$this->backupLockPath = './storage/statamic/updater/composer.lock.bak';
$this->customBackupLockPath = './custom/storage/statamic/updater/composer.lock.bak';
$this->removeLockFiles();
}
public function tearDown(): void
{
$this->removeLockFiles();
parent::tearDown();
}
/** @test */
public function it_can_backup_existing_lock_file()
{
file_put_contents($this->lockPath, $content = 'test lock file content');
$this->assertFileExists($this->lockPath);
$this->assertFileNotExists($this->backupLockPath);
Lock::backup();
$this->assertFileExists($this->backupLockPath);
$this->assertEquals($content, file_get_contents($this->backupLockPath));
}
/** @test */
public function it_doesnt_throw_exception_when_attempting_to_backup_non_existend_lock_file()
{
Lock::backup('non-existent-file.lock');
$this->assertFileNotExists($this->backupLockPath);
}
/** @test */
public function it_can_backup_lock_file_from_custom_location()
{
if (! is_dir($dir = './custom')) {
mkdir($dir);
}
file_put_contents($this->customLockPath, $content = 'custom lock file content');
$this->assertFileExists($this->customLockPath);
$this->assertFileNotExists($this->customBackupLockPath);
Lock::backup($this->customLockPath);
$this->assertFileExists($this->customBackupLockPath);
$this->assertEquals($content, file_get_contents($this->customBackupLockPath));
}
private function removeLockFiles()
{
$files = [
$this->lockPath,
$this->customLockPath,
$this->backupLockPath,
$this->customBackupLockPath,
];
foreach ($files as $lockFile) {
if (is_file($lockFile)) {
unlink($lockFile);
}
}
}
/**
* @deprecated
*/
public static function assertFileNotExists(string $filename, string $message = ''): void
{
method_exists(static::class, 'assertFileDoesNotExist')
? static::assertFileDoesNotExist($filename, $message)
: parent::assertFileNotExists($filename, $message);
}
}