-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFunctionMocker.php
More file actions
97 lines (76 loc) · 2.73 KB
/
FunctionMocker.php
File metadata and controls
97 lines (76 loc) · 2.73 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
<?php
namespace PHPUnit\Extension;
use PHPUnit\Extension\FunctionMocker\CodeGenerator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use function bin2hex;
use function random_bytes;
class FunctionMocker
{
/** @var TestCase */
private $testCase;
/** @var string */
private $namespace;
/** @var array */
private $functions = array();
/** @var array */
private $constants = [];
/** @var array */
private static $mockedFunctions = array();
private function __construct(TestCase $testCase, $namespace)
{
$this->testCase = $testCase;
$this->namespace = trim($namespace, '\\');
}
/**
* Create a mock for the given namespace to override global namespace functions.
*
* Example: PHP global namespace function setcookie() needs to be overridden in order to test
* if a cookie gets set. When setcookie() is called from inside a class in the namespace
* \Foo\Bar the mock setcookie() created here will be used instead to the real function.
*/
public static function start(TestCase $testCase, string $namespace): self
{
return new static($testCase, $namespace);
}
public static function tearDown(): void
{
unset($GLOBALS['__PHPUNIT_EXTENSION_FUNCTIONMOCKER']);
}
public function mockFunction(string $function): self
{
$function = trim(strtolower($function));
if (!in_array($function, $this->functions, true)) {
$this->functions[] = $function;
}
return $this;
}
public function mockConstant(string $constant, $value): self
{
$this->constants[trim($constant)] = $value;
return $this;
}
public function getMock(): MockObject
{
$mock = $this->testCase->getMockBuilder('stdClass')
->setMethods($this->functions)
->setMockClassName('PHPUnit_Extension_FunctionMocker_' . bin2hex(random_bytes(16)))
->getMock();
foreach ($this->constants as $constant => $value) {
CodeGenerator::defineConstant($this->namespace, $constant, $value);
}
foreach ($this->functions as $function) {
$fqFunction = $this->namespace . '\\' . $function;
if (in_array($fqFunction, static::$mockedFunctions, true)) {
continue;
}
CodeGenerator::defineFunction($this->namespace, $function);
static::$mockedFunctions[] = $fqFunction;
}
if (!isset($GLOBALS['__PHPUNIT_EXTENSION_FUNCTIONMOCKER'])) {
$GLOBALS['__PHPUNIT_EXTENSION_FUNCTIONMOCKER'] = array();
}
$GLOBALS['__PHPUNIT_EXTENSION_FUNCTIONMOCKER'][$this->namespace] = $mock;
return $mock;
}
}