|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
use Behat\Behat\Context\Context; |
|
5
|
|
|
use Behat\Behat\Hook\Scope\AfterScenarioScope; |
|
6
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
|
7
|
|
|
use PHPUnit\Framework\Assert; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Defines application Behat from the specific context. |
|
11
|
|
|
*/ |
|
12
|
|
|
class BaseUse implements Context |
|
13
|
|
|
{ |
|
14
|
|
|
public $suspension = []; |
|
15
|
|
|
|
|
16
|
|
|
/** @AfterScenario */ |
|
17
|
|
|
public function after(AfterScenarioScope $scope) |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
foreach ($this->suspension as $var_name => $value) { |
|
20
|
|
|
if (false === strpos($var_name, 'file')) { |
|
21
|
|
|
continue; |
|
22
|
|
|
} |
|
23
|
|
|
@unlink($value); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$this->suspension = []; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @Transform :current_pid |
|
32
|
|
|
*/ |
|
33
|
|
|
public function currentPid() |
|
34
|
|
|
{ |
|
35
|
|
|
return getmypid(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @Transform :no_exists_pid |
|
40
|
|
|
*/ |
|
41
|
|
|
public function notExistPid() |
|
42
|
|
|
{ |
|
43
|
|
|
$no_exists_pid = 1; |
|
44
|
|
|
while (++$no_exists_pid < PHP_INT_MAX) { |
|
45
|
|
|
if (false === posix_kill($no_exists_pid, 0) |
|
46
|
|
|
&& 3 === posix_get_last_error() |
|
47
|
|
|
) { |
|
48
|
|
|
break; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $no_exists_pid; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @Given an existed file named :pid_file with :no_exists_pid |
|
57
|
|
|
*/ |
|
58
|
|
|
public function anExistedFileNamed($pid_file, $no_exists_pid) |
|
59
|
|
|
{ |
|
60
|
|
|
$file_name = tempnam(sys_get_temp_dir(), 'vo_'); |
|
61
|
|
|
|
|
62
|
|
|
file_put_contents($file_name, $no_exists_pid); |
|
63
|
|
|
|
|
64
|
|
|
$this->suspension[$pid_file] = $file_name; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @Given a non existed :pid_file |
|
69
|
|
|
*/ |
|
70
|
|
|
public function aNonExistedFileAs($pid_file) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->suspension[$pid_file] = sys_get_temp_dir() . '/' . uniqid('vd_', true); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @When I run |
|
77
|
|
|
*/ |
|
78
|
|
|
public function iRun(PyStringNode $command) |
|
79
|
|
|
{ |
|
80
|
|
|
eval($this->substitution($command)); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @Transform :need_suspension_command |
|
85
|
|
|
*/ |
|
86
|
|
|
public function substitution($string) |
|
87
|
|
|
{ |
|
88
|
|
|
$replacement = array_values($this->suspension); |
|
89
|
|
|
foreach ($replacement as &$value) { |
|
90
|
|
|
if (is_string($value)) { |
|
91
|
|
|
$value = '"' . $value . '"'; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
$suspended = str_ireplace(array_keys($this->suspension), $replacement, $string); |
|
95
|
|
|
|
|
96
|
|
|
return $suspended; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @Then file :pid_file should updated with :current_pid |
|
101
|
|
|
*/ |
|
102
|
|
|
public function fileShouldUpdatedWith($pid_file, $current_pid) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->fileShouldCreatedWith($pid_file, $current_pid); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @Then file :pid_file should created with :current_pid |
|
109
|
|
|
*/ |
|
110
|
|
|
public function fileShouldCreatedWith($pid_file, $current_pid) |
|
111
|
|
|
{ |
|
112
|
|
|
Assert::assertStringEqualsFile($this->suspension[$pid_file], $current_pid); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.