Completed
Push — add_bdd ( 07502f )
by Anatoliy
07:38
created

BaseUse::substitution()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
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)
0 ignored issues
show
Unused Code introduced by
The parameter $scope is not used and could be removed. ( Ignorable by Annotation )

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

17
    public function after(/** @scrutinizer ignore-unused */ AfterScenarioScope $scope)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18
    {
19
        foreach ($this->suspension as $var_name => $value) {
20
            if (false === strpos($var_name, 'file')) {
21
                continue;
22
            }
23
            @unlink($value);
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

23
            /** @scrutinizer ignore-unhandled */ @unlink($value);

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...
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));
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
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