Skip to content

Commit 582f8db

Browse files
Merge pull request #15 from assertwell/feature/runkit-proxy
Introduce a Runkit support class
2 parents 2df1431 + 0e214b4 commit 582f8db

9 files changed

+130
-28
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"@test:analysis"
5757
],
5858
"test:analysis": [
59-
"phpstan analyse"
59+
"simple-phpunit --version",
60+
"phpstan analyse -c phpstan.neon.dist"
6061
],
6162
"test:coverage": [
6263
"phpdbg -qrr -d memory_limit=-1 ./vendor/bin/simple-phpunit --colors=always --testdox --coverage-html=tests/coverage"

composer.lock

+18-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon.dist

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@ parameters:
33
paths:
44
- src
55
- tests
6+
7+
bootstrapFiles:
8+
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
9+
610
ignoreErrors:
7-
# PHPUnit framework classes are provided by symfony/phpunit-bridge.
11+
# Don't require return type hinting in tests.
812
-
9-
message: '#^Class PHPUnit\\Framework\\.+ not found\.$#'
10-
path: *
13+
message: '#Method \S+ has no return typehint specified\.#'
14+
path: tests/*
15+
16+
# Strings are a valid callable type.
17+
-
18+
message: '#Parameter \#1 \$function of function call_user_func_array expects callable\(\): mixed, string given\.#'
19+
path: src/Support/Runkit.php

src/Concerns/Runkit.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ trait Runkit
1010
* Mark a test as skipped if Runkit is not available.
1111
*
1212
* @throws \PHPUnit\Framework\SkippedTestError
13+
*
14+
* @param string $message Optional. A message to include if the SkippedTestError exception
15+
* is thrown. Default is empty.
16+
*
17+
* @return void
1318
*/
1419
protected function requiresRunkit($message = '')
1520
{
@@ -22,9 +27,12 @@ protected function requiresRunkit($message = '')
2227

2328
/**
2429
* Determine whether or not Runkit is available in the current environment.
30+
*
31+
* @return bool
2532
*/
2633
protected function isRunkitAvailable()
2734
{
28-
return function_exists('runkit_constant_redefine');
35+
return function_exists('runkit7_constant_redefine')
36+
|| function_exists('runkit_constant_redefine');
2937
}
3038
}

src/Constants.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace AssertWell\PHPUnitGlobalState;
44

55
use AssertWell\PHPUnitGlobalState\Exceptions\RedefineException;
6+
use AssertWell\PHPUnitGlobalState\Support\Runkit;
67

78
trait Constants
89
{
@@ -17,6 +18,8 @@ trait Constants
1718

1819
/**
1920
* @before
21+
*
22+
* @return void
2023
*/
2124
protected function resetConstants()
2225
{
@@ -28,20 +31,22 @@ protected function resetConstants()
2831

2932
/**
3033
* @after
34+
*
35+
* @return void
3136
*/
3237
protected function restoreConstants()
3338
{
3439
foreach ($this->_constants['updated'] as $name => $value) {
3540
if (defined($name)) {
36-
runkit_constant_redefine($name, $value);
41+
Runkit::constant_redefine($name, $value);
3742
} else {
3843
define($name, $value);
3944
}
4045
}
4146

4247
foreach ($this->_constants['created'] as $name) {
4348
if (defined($name)) {
44-
runkit_constant_remove($name);
49+
Runkit::constant_remove($name);
4550
}
4651
}
4752
}
@@ -55,6 +60,8 @@ protected function restoreConstants()
5560
*
5661
* @param string $name The constant name.
5762
* @param mixed $value The scalar value to store in the constant.
63+
*
64+
* @return self
5865
*/
5966
protected function setConstant($name, $value = null)
6067
{
@@ -66,7 +73,7 @@ protected function setConstant($name, $value = null)
6673
}
6774

6875
try {
69-
runkit_constant_redefine($name, $value);
76+
Runkit::constant_redefine($name, $value);
7077
} catch (\Exception $e) {
7178
throw new RedefineException(sprintf(
7279
'Unable to redefine constant "%s" with value "%s".',
@@ -86,6 +93,8 @@ protected function setConstant($name, $value = null)
8693
* Delete a constant.
8794
*
8895
* @param string $name The constant name.
96+
*
97+
* @return self
8998
*/
9099
protected function deleteConstant($name)
91100
{
@@ -99,7 +108,7 @@ protected function deleteConstant($name)
99108
$this->_constants['updated'][$name] = constant($name);
100109
}
101110

102-
runkit_constant_remove($name);
111+
Runkit::constant_remove($name);
103112

104113
return $this;
105114
}

src/EnvironmentVariables.php

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ trait EnvironmentVariables
1313

1414
/**
1515
* @before
16+
*
17+
* @return void
1618
*/
1719
protected function resetEnvironmentVariableRegistry()
1820
{
@@ -21,6 +23,8 @@ protected function resetEnvironmentVariableRegistry()
2123

2224
/**
2325
* @after
26+
*
27+
* @return void
2428
*/
2529
protected function restoreEnvironmentVariables()
2630
{
@@ -37,6 +41,8 @@ protected function restoreEnvironmentVariables()
3741
* @param string $variable The environment variable name.
3842
* @param mixed $value The value to store in the environment variable. Passing NULL will
3943
* delete the environment variable.
44+
*
45+
* @return self
4046
*/
4147
protected function setEnvironmentVariable($variable, $value = null)
4248
{
@@ -53,6 +59,8 @@ protected function setEnvironmentVariable($variable, $value = null)
5359
* Delete an environment variable.
5460
*
5561
* @param string $variable The variable name.
62+
*
63+
* @return self
5664
*/
5765
protected function deleteEnvironmentVariable($variable)
5866
{

src/GlobalVariables.php

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ trait GlobalVariables
1111

1212
/**
1313
* @before
14+
*
15+
* @return void
1416
*/
1517
protected function resetGlobalVariables()
1618
{
@@ -22,6 +24,8 @@ protected function resetGlobalVariables()
2224

2325
/**
2426
* @after
27+
*
28+
* @return void
2529
*/
2630
protected function restoreGlobalVariables()
2731
{
@@ -42,6 +46,8 @@ protected function restoreGlobalVariables()
4246
* @param string $variable The global variable name.
4347
* @param mixed $value The new, temporary value. Passing NULL will unset the given
4448
* $variable, if it exists.
49+
*
50+
* @return void
4551
*/
4652
protected function setGlobalVariable($variable, $value)
4753
{

src/Support/Runkit.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* A utility class to ensure we're calling the runkit7_* functions when available, as the runkit_*
5+
* versions are deprecated in newer versions of PHP.
6+
*/
7+
8+
namespace AssertWell\PHPUnitGlobalState\Support;
9+
10+
/**
11+
* phpcs:disable Generic.Files.LineLength.TooLong
12+
* @method static bool constant_add(string $constname, mixed $value, int $newVisibility = NULL)
13+
* @method static bool constant_redefine(string $constname, mixed $value, int $newVisibility = NULL)
14+
* @method static bool constant_remove(string $constname)
15+
* @method static bool function_add(string $funcname, string $arglist, string $code, bool $return_by_reference = NULL, string $doc_comment = NULL, string $return_type, bool $is_strict = NULL)
16+
* @method static bool function_copy(string $funcname, string $targetname)
17+
* @method static bool function_redefine(string $funcname, string $arglist, string $code, bool $return_by_reference = NULL, string $doc_comment = NULL, string $return_type = NULL, bool $is_strict)
18+
* @method static bool function_remove(string $funcname)
19+
* @method static bool function_rename(string $funcname, string $newname)
20+
* @method static bool import(string $filename, int $flags = NULL)
21+
* @method static bool method_add(string $classname, string $methodname, string $args, string $code, int $flags = RUNKIT7_ACC_PUBLIC, string $doc_comment = NULL, string $return_type = NULL, bool $is_strict = NULL)
22+
* @method static bool method_copy(string $dClass, string $dMethod, string $sClass, string $sMethod = NULL)
23+
* @method static bool method_redefine(string $classname, string $methodname, string $args, string $code, int $flags = RUNKIT7_ACC_PUBLIC, string $doc_comment = NULL, string $return_type, bool $is_strict = NULL)
24+
* @method static bool method_remove(string $classname, string $methodname)
25+
* @method static bool method_rename(string $classname, string $methodname, string $newname)
26+
* @method static int object_id(object $obj)
27+
* @method static array superglobals()
28+
* @method static array zval_inspect(string $value)
29+
* phpcs:enable Generic.Files.LineLength.TooLong
30+
*/
31+
class Runkit
32+
{
33+
/**
34+
* Dynamically alias methods to the underlying Runkit functions.
35+
*
36+
* @throws \BadFunctionCallException if the underlying function does not exist.
37+
*
38+
* @param string $name The method name.
39+
* @param mixed[] $args Method arguments.
40+
*
41+
* @return mixed The return value of the corresponding runkit(7)_* functions.
42+
*/
43+
public static function __callStatic($name, array $args = [])
44+
{
45+
if (function_exists('runkit7_' . $name)) {
46+
return call_user_func_array('runkit7_' . $name, $args);
47+
}
48+
49+
if (function_exists('runkit_' . $name)) {
50+
return call_user_func_array('runkit_' . $name, $args);
51+
}
52+
53+
throw new \BadFunctionCallException(sprintf(
54+
'Runkit7 does not include a runkit7_%1$s() function.',
55+
$name
56+
));
57+
}
58+
}

tests/ConstantsTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function defineConstants()
3131
*/
3232
public function setConstant_should_be_able_to_handle_newly_defined_constants()
3333
{
34-
$this->requiresRunkit('This test depends on runkit being unavailable.');
34+
$this->requiresRunkit('This test depends on runkit being available.');
3535

3636
$this->assertFalse(defined('SOME_CONSTANT'));
3737

@@ -48,7 +48,7 @@ public function setConstant_should_be_able_to_handle_newly_defined_constants()
4848
*/
4949
public function setConstant_should_be_able_to_redefine_existing_constants()
5050
{
51-
$this->requiresRunkit('This test depends on runkit being unavailable.');
51+
$this->requiresRunkit('This test depends on runkit being available.');
5252

5353
$this->setConstant('EXISTING_CONSTANT', 'some other value');
5454
$this->assertSame('some other value', constant('EXISTING_CONSTANT'));
@@ -67,7 +67,7 @@ public function setConstant_should_be_able_to_redefine_existing_constants()
6767
*/
6868
public function setConstant_should_throw_an_exception_if_it_cannot_redefine_a_constant()
6969
{
70-
$this->requiresRunkit('This test depends on runkit being unavailable.');
70+
$this->requiresRunkit('This test depends on runkit being available.');
7171

7272
$this->expectException(RedefineException::class);
7373
$this->setConstant('EXISTING_CONSTANT', (object) ['some' => 'object']);
@@ -85,7 +85,7 @@ public function setConstant_should_throw_an_exception_if_it_cannot_redefine_a_co
8585
*/
8686
public function deleteConstant_should_remove_an_existing_constant()
8787
{
88-
$this->requiresRunkit('This test depends on runkit being unavailable.');
88+
$this->requiresRunkit('This test depends on runkit being available.');
8989

9090
$this->deleteConstant('DELETE_THIS_CONSTANT');
9191
$this->assertFalse(defined('DELETE_THIS_CONSTANT'));

0 commit comments

Comments
 (0)