|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Behat\Context\Context; |
|
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Context for asynchronous file writes. |
|
10
|
|
|
*/ |
|
11
|
|
|
class FeatureContext implements Context |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
private static $workingDir; |
|
15
|
|
|
|
|
16
|
|
|
/** @var Filesystem */ |
|
17
|
|
|
protected static $filesystem; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Initializes the working directory and filesystem. |
|
21
|
|
|
* |
|
22
|
|
|
* @BeforeFeature |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function beforeFeature() |
|
25
|
|
|
{ |
|
26
|
|
|
self::$workingDir = sprintf('%s/%s/', sys_get_temp_dir(), uniqid('', true)); |
|
27
|
|
|
self::$filesystem = new Filesystem(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Ensures that the working directory exists and has the correct permissions. |
|
32
|
|
|
* |
|
33
|
|
|
* @BeforeScenario |
|
34
|
|
|
*/ |
|
35
|
|
|
public function beforeScenario(): void |
|
36
|
|
|
{ |
|
37
|
|
|
self::$filesystem->mkdir(self::$workingDir, 0777); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Removes the working directory. |
|
42
|
|
|
* |
|
43
|
|
|
* @AfterScenario |
|
44
|
|
|
*/ |
|
45
|
|
|
public function afterScenario() |
|
46
|
|
|
{ |
|
47
|
|
|
self::$filesystem->remove(self::$workingDir); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Writes content to a file immediately (non blocking). |
|
52
|
|
|
* |
|
53
|
|
|
* @Given the file :path contents are :content |
|
54
|
|
|
* @param string $file the path to the file to write. |
|
55
|
|
|
* @param string $content the content to write to the file. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function writeFile(string $file, string $content) |
|
58
|
|
|
{ |
|
59
|
|
|
file_put_contents($this->getFilePath($file), $content); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Writes content to a file after a given time (non blocking). |
|
64
|
|
|
* |
|
65
|
|
|
* @Given the file :path contents will be :content in :timeout seconds |
|
66
|
|
|
* @param string $file the path to the file to write. |
|
67
|
|
|
* @param string $content the content to write to the file. |
|
68
|
|
|
* @param int $seconds the number of seconds to wait before writing to the file. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function writeFileDelayed(string $file, string $content, int $seconds = 0) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$script = realpath(__DIR__ . '/../../bin/writeFileDelayed.php')) { |
|
73
|
|
|
throw new RuntimeException('Cannot find writeFileDelayed.php!'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$command = sprintf( |
|
77
|
|
|
'php %s "%s" %s %s > /dev/null 2>/dev/null &', |
|
78
|
|
|
$script, |
|
79
|
|
|
$this->getFilePath($file), |
|
80
|
|
|
$seconds, |
|
81
|
|
|
base64_encode($content) |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
exec($command, $output, $exitCode); |
|
85
|
|
|
|
|
86
|
|
|
if ($exitCode != 0) { |
|
87
|
|
|
throw new RuntimeException("Command '$command' failed with output:\n\n" . implode("\n", $output)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Asserts that the specified file has the specified contents. |
|
93
|
|
|
* |
|
94
|
|
|
* @Then the file :file contents should be :content |
|
95
|
|
|
* @param string $file the path to the file to check. |
|
96
|
|
|
* @param string $content the content to check for. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function assertFileContents(string $file, string $content) |
|
99
|
|
|
{ |
|
100
|
|
|
$path = $this->getFilePath($file); |
|
101
|
|
|
$actual = file_exists($path) ? file_get_contents($path) : ''; |
|
102
|
|
|
if ($content != $actual) { |
|
103
|
|
|
throw new RuntimeException("File contents are \"$actual\" but \"$content\" expected."); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Provides the full path to a file within the working directory. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $file the path of the file |
|
111
|
|
|
* @return string the full path |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function getFilePath(string $file): string |
|
114
|
|
|
{ |
|
115
|
|
|
return self::$workingDir . $file; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|