Completed
Push — develope ( 62f2b8...1e8565 )
by Anatoliy
05:37
created

PidFileTestCase::notStringProvider()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: danchukas
6
 * Date: 2017-07-17 21:51
7
 */
8
9
namespace DanchukAS\DenyMultiplyRun;
10
11
use DanchukAS\Mock\TypeList\NotStringList;
12
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * Class PidFileTestCase
16
 * included shared test settings.
17
 * @package DanchukAS\DenyMultiplyRunTest
18
 */
19
abstract class PidFileTestCase extends TestCase
20
{
21
    protected static $noExistFileName;
22
23
    protected static $existFileName;
24
25
    private static $tempFileList = [];
26
27
    public function setUp()
28
    {
29
        self::$noExistFileName = self::getFileName();
30
        self::$existFileName = self::newTempFileName();
31
    }
32
33
    /**
34
     * @return string
35
     */
36
    private static function getFileName(): string
37
    {
38
        $file_name = sys_get_temp_dir() . '/' . uniqid('vd_', true);
39
        self::$tempFileList[] = $file_name;
40
        return $file_name;
41
    }
42
43
    /**
44
     * @return bool|string
45
     */
46
    private static function newTempFileName()
47
    {
48
        $file_name = tempnam(sys_get_temp_dir(), 'vo_');
49
        self::$tempFileList[] = $file_name;
50
        return $file_name;
51
    }
52
53
    public function tearDown()
54
    {
55
        foreach (self::$tempFileList as $file_name) {
56
            /** @noinspection PhpUsageOfSilenceOperatorInspection */
57
            @unlink($file_name);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

57
            /** @scrutinizer ignore-unhandled */ @unlink($file_name);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
58
        }
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function notFileNameProvider()
65
    {
66
        static $not_file_name = null;
67
68
        if (is_null($not_file_name)) {
69
            $not_file_name = $this->notStringProvider() + $this->noValidFileNameProvider();
70
        }
71
72
        return $not_file_name;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function notStringProvider()
79
    {
80
        static $not_string_list = null;
81
        if (is_null($not_string_list)) {
82
            $not_string_list = NotStringList::getList();
83
84
            foreach ($not_string_list as &$param) {
85
                $param["throw"] = "TypeError";
86
            }
87
        }
88
89
        return $not_string_list;
90
    }
91
92
93
    /**
94
     * @return array
95
     */
96
    public function noValidFileNameProvider()
97
    {
98
        static $file_name_list = null;
99
100
        if (is_null($file_name_list)) {
101
            $file_name_list = [
102
                [""]
103
                , ["."]
104
                , ["/"]
105
                , ['//']
106
            ];
107
108
            foreach ($file_name_list as &$param) {
109
                $param["throw"] = "Exception";
110
            }
111
        }
112
113
        return $file_name_list;
114
    }
115
116
    /**
117
     * return array
118
     */
119
    public function deletePidFileParam()
120
    {
121
        static $param = null;
122
123
        if (is_null($param)) {
124
            // existed file without write access for current user.
125
            // for Ubuntu is /etc/hosts.
126
            $file_name = "/etc/hosts";
127
128
            $message = $this->sudoOrNotUnix($file_name);
129
130
            $param = [
131
                "noExistFileName" => [self::getFileName()]
132
                , "wrongParam" => [null]
133
                , "accessDenied" => [$file_name, $message]
134
            ];
135
        }
136
137
        return $param;
138
    }
139
140
    /**
141
     * @param $file_name
142
     * @return null|string
143
     */
144
    private function sudoOrNotUnix($file_name)
145
    {
146
        $message = null;
147
148
        if (!file_exists($file_name)) {
149
            $message = "test only for *nix.";
150
        } elseif (is_writable($file_name)) {
151
            $message = "test runned under super/admin user. Change user.";
152
        }
153
        return $message;
154
    }
155
156
    /**
157
     * return array
158
     */
159
    public function setPidFileParam()
160
    {
161
        static $param = null;
162
163
        if (is_null($param)) {
164
            $param = [
165
                "lockedPidFile" => [
166
                    self::lockedPidFile()
167
                    , "DanchukAS\DenyMultiplyRun\Exception\LockFileFail"
168
                ]
169
                , "fileHasExistPid" => [
170
                    self::fileWithExistedPid()
171
                    , "DanchukAS\DenyMultiplyRun\Exception\ProcessExisted"
172
                ]
173
                , "" => [
174
                    self::noValidPidFile()
175
                    , "DanchukAS\DenyMultiplyRun\Exception\ConvertPidFail"
176
                ]
177
            ];
178
        }
179
180
        return $param;
181
    }
182
183
    /**
184
     *
185
     */
186
    private static function lockedPidFile()
187
    {
188
        while (true) {
189
            $file_name = self::newTempFileName();
190
            $file_resource = fopen($file_name, "r+");
191
            flock($file_resource, LOCK_EX);
192
193
            yield $file_name;
194
        }
195
    }
196
197
    /**
198
     *
199
     */
200
    private static function fileWithExistedPid()
201
    {
202
        while (true) {
203
            $file_name = self::newTempFileName();
204
            file_put_contents($file_name, getmypid());
205
206
            yield $file_name;
207
        }
208
    }
209
210
    /**
211
     *
212
     */
213
    private static function noValidPidFile()
214
    {
215
        while (true) {
216
            $file_name = self::newTempFileName();
217
            file_put_contents($file_name, "12as");
218
219
            yield $file_name;
220
        }
221
    }
222
}
223