Skip to content

Commit a9615fc

Browse files
Sean MolenaarSean Molenaar
authored andcommitted
Move from runkit to UOPZ
1 parent 4259a2a commit a9615fc

File tree

3 files changed

+99
-14
lines changed

3 files changed

+99
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This is a parser for API Blueprint files in PHP.[1](#dependencies)
33

44
## Usage
5-
Requires PHP 5.6+ to run. Unittests require runkit (which is PHP <7.0 only)
5+
Requires PHP 5.6+ to run. Unittests require runkit or uopz
66
For direct usage you can run:
77
```bash
88
$ ./phpdraft.phar -f blueprint-file.apib > blueprint-webpage.html

src/PHPDraft/Core/BaseTest.php

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,63 @@ public function tearDown()
4242
/**
4343
* Mock an internal function
4444
*
45-
* @param string $name function name
46-
* @param string $mock function to replace it with
45+
* @param string $name function name
46+
* @param string $return return to use
4747
*
4848
* @return void
4949
*/
50-
protected function mock_function($name, $mock)
50+
protected function mock_function($name, $return)
5151
{
52-
if (function_exists($name . self::FUNCTION_ID) === FALSE)
53-
{
52+
if (extension_loaded('runkit') === true) {
53+
$this->__runkit_mock_function($name, $return);
54+
55+
return;
56+
}
57+
58+
if (extension_loaded('uopz') === true) {
59+
$this->__uopz_mock_function($name, $return);
60+
61+
return;
62+
}
63+
}
64+
65+
/**
66+
* Mock an internal function with runkit
67+
*
68+
* @param string $name function name
69+
* @param string $return function to replace it with
70+
*
71+
* @return void
72+
*/
73+
private function __runkit_mock_function($name, $return)
74+
{
75+
if (function_exists($name . self::FUNCTION_ID) === false) {
5476
runkit_function_copy($name, $name . self::FUNCTION_ID);
5577
}
5678

57-
runkit_function_redefine($name, '', $mock);
79+
runkit_function_redefine($name, '', 'return '.$return.';');
80+
}
81+
82+
/**
83+
* Mock an internal function with uopz
84+
*
85+
* @param string $name function name
86+
* @param string $return value to return
87+
*
88+
* @return void
89+
*/
90+
private function __uopz_mock_function($name, $return)
91+
{
92+
if (PHP_MAJOR_VERSION < 7) {
93+
\uopz_backup($name);
94+
\uopz_function($name, function () {
95+
global $return;
96+
return $return;
97+
});
98+
99+
return;
100+
}
101+
\uopz_set_return($name, $return);
58102
}
59103

60104
/**
@@ -65,8 +109,49 @@ protected function mock_function($name, $mock)
65109
* @return void
66110
*/
67111
protected function unmock_function($name)
112+
{
113+
if (extension_loaded('runkit') === true) {
114+
$this->__runkit_unmock_function($name);
115+
116+
return;
117+
}
118+
119+
if (extension_loaded('uopz') === true) {
120+
$this->__uopz_unmock_function($name);
121+
122+
return;
123+
}
124+
125+
}
126+
127+
/**
128+
* Unmock a PHP function from runkit.
129+
*
130+
* @param String $name Function name
131+
*
132+
* @return void
133+
*/
134+
private function __runkit_unmock_function($name)
68135
{
69136
runkit_function_remove($name);
70137
runkit_function_rename($name . self::FUNCTION_ID, $name);
71138
}
139+
140+
/**
141+
* Unmock a PHP function from uopz.
142+
*
143+
* @param String $name Function name
144+
*
145+
* @return void
146+
*/
147+
private function __uopz_unmock_function($name)
148+
{
149+
if (PHP_MAJOR_VERSION < 7) {
150+
\uopz_restore($name);
151+
152+
return;
153+
}
154+
155+
uopz_unset_return($name);
156+
}
72157
}

src/PHPDraft/Parse/Tests/DrafterTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class DrafterTest extends BaseTest
2424
*/
2525
public function setUp()
2626
{
27-
$this->mock_function('sys_get_temp_dir', 'return "' . TEST_STATICS . '";');
28-
$this->mock_function('shell_exec', 'return "/some/dir/drafter\n";');
27+
$this->mock_function('sys_get_temp_dir', TEST_STATICS );
28+
$this->mock_function('shell_exec', "/some/dir/drafter\n");
2929
$this->class = new Drafter(file_get_contents(TEST_STATICS . '/drafter/apib'));
3030
$this->reflection = new ReflectionClass('PHPDraft\Parse\Drafter');
3131
$this->unmock_function('shell_exec');
@@ -64,7 +64,7 @@ public function testPreRunStringIsEmpty()
6464
*/
6565
public function testParseToJSON()
6666
{
67-
$this->mock_function('shell_exec', 'return "";');
67+
$this->mock_function('shell_exec', "");
6868
file_put_contents(TEST_STATICS . '/drafter/index.json', file_get_contents(TEST_STATICS . '/drafter/json'));
6969
$this->class->parseToJson();
7070
$this->assertEquals(json_decode(file_get_contents(TEST_STATICS . '/drafter/json')), $this->class->json);
@@ -81,7 +81,7 @@ public function testParseToJSON()
8181
*/
8282
public function testParseToJSONWithErrors()
8383
{
84-
$this->mock_function('shell_exec', 'return "";');
84+
$this->mock_function('shell_exec', "");
8585
file_put_contents(TEST_STATICS . '/drafter/index.json',
8686
file_get_contents(TEST_STATICS . '/drafter/json_errors'));
8787
$this->class->parseToJson();
@@ -99,7 +99,7 @@ public function testParseToJSONWithErrors()
9999
*/
100100
public function testSetupWithoutDrafter()
101101
{
102-
$this->mock_function('shell_exec', 'return "";');
102+
$this->mock_function('shell_exec', "");
103103
new Drafter('hello');
104104
$this->unmock_function('shell_exec');
105105
}
@@ -114,8 +114,8 @@ public function testSetupWithoutDrafter()
114114
*/
115115
public function testParseToJSONWithInvalidJSON()
116116
{
117-
$this->mock_function('json_last_error', 'return ' . JSON_ERROR_DEPTH . ';');
118-
$this->mock_function('json_last_error_msg', 'return "ERROR";');
117+
$this->mock_function('json_last_error', JSON_ERROR_DEPTH );
118+
$this->mock_function('json_last_error_msg', "ERROR");
119119
$this->class->parseToJson();
120120
$this->expectOutputString('ERROR: invalid json in /tmp/drafter/index.json');
121121
$this->unmock_function('json_last_error_msg');

0 commit comments

Comments
 (0)