-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFunctions.php
More file actions
147 lines (121 loc) · 4.17 KB
/
Copy pathFunctions.php
File metadata and controls
147 lines (121 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace AssertWell\PHPUnitGlobalState;
use AssertWell\PHPUnitGlobalState\Exceptions\FunctionExistsException;
use AssertWell\PHPUnitGlobalState\Exceptions\RunkitException;
use AssertWell\PHPUnitGlobalState\Support\Runkit;
trait Functions
{
/**
* All functions being handled by this trait.
*
* @var array{defined:Array<string>,redefined:Array<string,string>}
*/
private $functions = [
'defined' => [],
'redefined' => [],
];
/**
* @after
*
* @return void
*/
protected function restoreFunctions()
{
// Reset anything that was modified.
array_walk($this->functions['redefined'], function ($original, $name) {
if (function_exists($name)) {
Runkit::function_remove($name);
}
// Put the original back into place.
Runkit::function_rename($original, $name);
unset($this->functions['redefined'][$name]);
});
array_map([Runkit::class, 'function_remove'], $this->functions['defined']);
$this->functions['defined'] = [];
Runkit::reset();
}
/**
* Define a new function.
*
* @throws \AssertWell\PHPUnitGlobalState\Exceptions\FunctionExistsException
* @throws \AssertWell\PHPUnitGlobalState\Exceptions\RunkitException
*
* @param string $name The function name.
* @param \Closure $closure The function body.
*
* @return self
*/
protected function defineFunction($name, \Closure $closure)
{
if (! Runkit::isAvailable()) {
$this->markTestSkipped('defineFunction() requires Runkit be available, skipping.');
}
if (function_exists($name)) {
throw new FunctionExistsException(sprintf(
'Function %1$s() already exists. You may redefine it using %2$s::redefineFunction() instead.',
$name,
get_class($this)
));
}
if (! Runkit::function_add($name, $closure)) {
throw new RunkitException(sprintf('Unable to define function %1$s().', $name));
}
$this->functions['defined'][] = $name;
return $this;
}
/**
* Redefine an existing function.
*
* If the function doesn't yet exist, it will be defined.
*
* @param string $name The function name to be redefined.
* @param \Closure $closure The new function body.
*
* @return self
*/
protected function redefineFunction($name, \Closure $closure)
{
if (! function_exists($name)) {
return $this->defineFunction($name, $closure);
}
if (! Runkit::isAvailable()) {
$this->markTestSkipped('redefineFunction() requires Runkit be available, skipping.');
}
// Back up the original version of the function.
if (! isset($this->functions['redefined'][$name])) {
$namespaced = Runkit::makeNamespaced($name);
if (! Runkit::function_rename($name, $namespaced)) {
throw new RunkitException(sprintf('Unable to back up %1$s(), aborting.', $name));
}
$this->functions['redefined'][$name] = $namespaced;
if (! Runkit::function_add($name, $closure)) {
throw new RunkitException(sprintf('Unable to redefine function %1$s().', $name));
}
} else {
Runkit::function_redefine($name, $closure);
}
return $this;
}
/**
* Delete an existing function.
*
* @param string $name The function to be deleted.
*
* @return self
*/
protected function deleteFunction($name)
{
if (! function_exists($name)) {
return $this;
}
if (! Runkit::isAvailable()) {
$this->markTestSkipped('deleteFunction() requires Runkit be available, skipping.');
}
$namespaced = Runkit::makeNamespaced($name);
if (! Runkit::function_rename($name, $namespaced)) {
throw new RunkitException(sprintf('Unable to back up %1$s(), aborting.', $name));
}
$this->functions['redefined'][$name] = $namespaced;
return $this;
}
}