|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Created by PhpStorm. |
|
7
|
|
|
* User: danchukas |
|
8
|
|
|
* Date: 2017-06-22 18:10 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace DanchukAS\DenyMultiplyRunTest; |
|
12
|
|
|
|
|
13
|
|
|
use DanchukAS\DenyMultiplyRun\DenyMultiplyRun; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class SurprisingTest |
|
19
|
|
|
* поглиблені тести на "всі найбільш можливі ситуації" |
|
20
|
|
|
* @package DanchukAS\DenyMultiplyRunTest |
|
21
|
|
|
*/ |
|
22
|
|
|
class SurprisingTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
private static $noExistFileName; |
|
26
|
|
|
|
|
27
|
|
|
private static $existFileName; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
function setUp() |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
self::$noExistFileName = sys_get_temp_dir() . '/' . uniqid('vd_', true); |
|
33
|
|
|
self::$existFileName = tempnam(sys_get_temp_dir(), 'vo_'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
function tearDown() |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
/** @noinspection PhpUsageOfSilenceOperatorInspection */ |
|
39
|
|
|
@unlink(self::$noExistFileName); |
|
|
|
|
|
|
40
|
|
|
/** @noinspection PhpUsageOfSilenceOperatorInspection */ |
|
41
|
|
|
@unlink(self::$existFileName); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @expectedException \DanchukAS\DenyMultiplyRun\Exception\ProcessExisted |
|
47
|
|
|
*/ |
|
48
|
|
|
function testDoubleCall() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$file_name = self::$noExistFileName; |
|
51
|
|
|
DenyMultiplyRun::setPidFile($file_name); |
|
52
|
|
|
DenyMultiplyRun::setPidFile($file_name); |
|
53
|
|
|
|
|
54
|
|
|
} |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
function testDeleteNoExistedPidFile() |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
self::expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail"); |
|
|
|
|
|
|
60
|
|
|
DenyMultiplyRun::deletePidFile(self::$noExistFileName); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
function testDeletePidFileWrongParam() |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
self::expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail"); |
|
|
|
|
|
|
67
|
|
|
DenyMultiplyRun::deletePidFile(null); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
function testDeleteNoAccessFile() |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
// existed file without write access for current user. |
|
74
|
|
|
// for Ubuntu is /etc/hosts. |
|
75
|
|
|
$file_name = "/etc/hosts"; |
|
76
|
|
|
|
|
77
|
|
|
if (!file_exists($file_name)) { |
|
78
|
|
|
self::markTestSkipped("test only for *nix."); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (is_writable($file_name)) { |
|
82
|
|
|
self::markTestSkipped("test runned under super/admin user. Change user."); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
self::expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail"); |
|
|
|
|
|
|
86
|
|
|
DenyMultiplyRun::deletePidFile($file_name); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @dataProvider notString |
|
91
|
|
|
* @param mixed $notString |
|
92
|
|
|
*/ |
|
93
|
|
|
function testWrongTypeParam($notString) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
self::expectException("TypeError"); |
|
|
|
|
|
|
96
|
|
|
DenyMultiplyRun::setPidFile($notString); |
|
97
|
|
|
|
|
98
|
|
|
} |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @dataProvider WrongParam |
|
102
|
|
|
* @param string $no_valid_file_name |
|
103
|
|
|
*/ |
|
104
|
|
|
function testWrongParam(string $no_valid_file_name) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
self::expectException("Exception"); |
|
|
|
|
|
|
107
|
|
|
DenyMultiplyRun::setPidFile($no_valid_file_name); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
function notString() |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$r = fopen(__FILE__, "r"); |
|
117
|
|
|
fclose($r); |
|
118
|
|
|
|
|
119
|
|
|
return [ |
|
120
|
|
|
[null] |
|
121
|
|
|
, [false] |
|
122
|
|
|
, [0] |
|
123
|
|
|
, [[]] |
|
124
|
|
|
, [function () { |
|
125
|
|
|
}] |
|
126
|
|
|
, [new \Exception] |
|
127
|
|
|
, [$r]]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
|
|
function WrongParam() |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
|
|
return [[""], ["."], ["/"], ['//']]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @dataProvider notString |
|
142
|
|
|
*/ |
|
143
|
|
|
function testLockedFileBeforeClose($badResource) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
$method = new \ReflectionMethod("DanchukAS\DenyMultiplyRun\DenyMultiplyRun", "closePidFile"); |
|
146
|
|
|
|
|
147
|
|
|
$method->setAccessible(true); |
|
148
|
|
|
self::expectException("DanchukAS\DenyMultiplyRun\Exception\CloseFileFail"); |
|
|
|
|
|
|
149
|
|
|
$method->invoke(null, $badResource); |
|
150
|
|
|
$method->setAccessible(false); |
|
151
|
|
|
|
|
152
|
|
|
} |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.
If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with
private, and only raise it toprotectedif a sub-class needs to have access, orpublicif an external class needs access.