Passed
Push — master ( 5c2945...175940 )
by Mougrim
06:27
created

TestCase::removeClassConstant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mougrim\PhpunitSoftMocks;
4
5
use QA\SoftMocks;
0 ignored issues
show
Bug introduced by
The type QA\SoftMocks was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * @author Mougrim <[email protected]>
9
 */
10
class TestCase extends \PHPUnit_Framework_TestCase
11
{
12
    private $closuresClass;
13
14 5
    protected function tearDown()
15
    {
16 5
        $this->restoreAll();
17 5
        parent::tearDown();
18
    }
19
20
    /**
21
     * @param callable|string $name
22
     * @param callable $function
23
     */
24 1
    public function redefineFunction($name, callable $function)
25
    {
26 1
        SoftMocks::redefineFunction($name, '', $function);
27
    }
28
29
    /**
30
     * @param callable|string $name
31
     */
32 1
    public function restoreFunction($name)
33
    {
34 1
        SoftMocks::restoreFunction($name);
35
    }
36
37 2
    public function redefineConstant($name, $value)
38
    {
39 2
        SoftMocks::redefineConstant($name, $value);
40
    }
41
42 2
    public function restoreConstant($name)
43
    {
44 2
        SoftMocks::restoreConstant($name);
45
    }
46
47 2
    public function removeConstant($name)
48
    {
49 2
        SoftMocks::removeConstant($name);
50
    }
51
52 1
    public function redefineClassConstant($class, $name, $value)
53
    {
54 1
        $this->redefineConstant($class.'::'.$name, $value);
55
    }
56
57 1
    public function restoreClassConstant($class, $name)
58
    {
59 1
        $this->restoreConstant($class.'::'.$name);
60
    }
61
62 1
    public function removeClassConstant($class, $name)
63
    {
64 1
        $this->removeConstant($class.'::'.$name);
65
    }
66
67 2
    public function restoreAllConstants()
68
    {
69 2
        SoftMocks::restoreAllConstants();
70
    }
71
72
    /**
73
     * Only user-defined method redefinition is supported.
74
     *
75
     * @param callable|array $classAndMethod [$class, $method] $class - a class name or a trait name
76
     * @param callable $function
77
     */
78 2
    public function redefineMethod(array $classAndMethod, callable $function)
79
    {
80 2
        list($class, $name) = $classAndMethod;
81 2
        $closuresClass = $this->getClosuresClass();
82 2
        $closuresClass::${'closures'}[$class][$name] = $function;
83 2
        $code = <<<PHP
84
\$function = {$closuresClass}::\$closures['{$class}']['{$name}'];
85
if (\$function instanceof \Closure) {
86
    \$function = \$function->bindTo(\$this);
87
}
88
return call_user_func_array(\$function, \$params);
89
PHP;
90 2
        SoftMocks::redefineMethod($class, $name, '', $code);
91
    }
92
93 2
    private function getClosuresClass()
94
    {
95 2
        if ($this->closuresClass === null) {
96 2
            $namespace = __NAMESPACE__;
97 2
            $className = str_replace('.', '_', uniqid('ClosuresClass_', true));
98
99 2
            $code = <<<PHP
100
namespace {$namespace};
101
class {$className}
102
{
103
    public static \$closures = [];
104
}
105
PHP;
106
107 2
            eval($code);
1 ignored issue
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
108
109 2
            $this->closuresClass = $namespace.'\\'.$className;
110
        }
111
112 2
        return $this->closuresClass;
113
    }
114
115
    /**
116
     * @param callable|array $classAndMethod [$class, $method] $class - a class name or a trait name
117
     */
118 1
    public function restoreMethod(array $classAndMethod)
119
    {
120 1
        list($class, $name) = $classAndMethod;
121 1
        SoftMocks::restoreMethod($class, $name);
122 1
        $closuresClass = $this->getClosuresClass();
123 1
        unset($closuresClass::${'closures'}[$class][$name]);
124
    }
125
126 2
    public function callOriginal(callable $callable, array $params)
127
    {
128 2
        return SoftMocks::callOriginal($callable, $params);
129
    }
130
131 5
    public function restoreAll()
132
    {
133 5
        SoftMocks::restoreAll();
134 5
        if ($this->closuresClass) {
135 2
            $closuresClass = $this->closuresClass;
136 5
            $closuresClass::${'closures'} = [];
137
        }
138
    }
139
}
140