Repository navigation
Expand file tree
/
Copy pathFileLockTest.php
More file actions
95 lines (84 loc) · 2.79 KB
/
Copy pathFileLockTest.php
File metadata and controls
95 lines (84 loc) · 2.79 KB
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
<?php
/*
* This file is part of FileLock.
* (c) Fabrice de Stefanis / https://github.com/fab2s/FileLock
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/
namespace fab2s\FileLock\Tests;
use fab2s\FileLock\FileLock;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
/**
* Class FileLockTest
*/
#[CoversClass(FileLock::class)]
class FileLockTest extends TestCase
{
#[DataProvider('lockMethodCases')]
public function test_lock(string $lockMethod, string $lockCall)
{
$tmpFile = $this->getTmpFile();
if (! $tmpFile) {
$this->markTestSkipped('Could not generate temporary file');
}
$lock = new FileLock($tmpFile, $lockMethod);
$lock->$lockCall();
/* @var FileLock $lock */
$this->assertTrue($lock->isLocked());
$this->assertTrue(is_resource($lock->getHandle()));
// same process I know
$opened = FileLock::open($tmpFile, 'wb');
$otherLock = new FileLock($tmpFile, $lockMethod);
$otherLock->$lockCall();
$this->assertFalse($otherLock->isLocked());
if ($lockMethod === FileLock::LOCK_EXTERNAL) {
$this->assertTrue(file_exists($tmpFile . '.lock'));
// since it is external, we can also lock Self
$this->assertTrue($opened instanceof FileLock);
$this->assertTrue(is_resource($opened->getHandle()));
$this->assertFalse($opened->unLock()->isLocked());
} else {
$this->assertFalse(file_exists($tmpFile . '.lock'));
$this->assertNull($opened);
}
$this->assertFalse(is_resource($otherLock->getHandle()));
$lock->unLock();
$this->assertFalse(is_resource($lock->getHandle()));
$this->assertFalse($lock->isLocked());
$this->assertTrue($otherLock->$lockCall());
$this->assertTrue(is_resource($otherLock->getHandle()));
$otherLock->__destruct();
$this->assertFalse($otherLock->isLocked());
$this->assertFalse(is_resource($otherLock->getHandle()));
}
public static function lockMethodCases(): array
{
return [
[
FileLock::LOCK_SELF,
'doLock',
],
[
FileLock::LOCK_SELF,
'obtainLock',
],
[
FileLock::LOCK_EXTERNAL,
'doLock',
],
[
FileLock::LOCK_EXTERNAL,
'obtainLock',
],
];
}
/**
* @return bool|string
*/
protected function getTmpFile()
{
return tempnam(sys_get_temp_dir(), 'Fl_');
}
}