Passed
Push — master ( 61f3a0...6b4c98 )
by Alexander
01:39
created

TestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 45
rs 10
c 1
b 0
f 0
wmc 6
1
<?php
2
3
namespace Yiisoft\Log\Tests;
4
5
abstract class TestCase extends \PHPUnit\Framework\TestCase
6
{
7
    /**
8
     * Sets an inaccessible object property to a designated value.
9
     * @param $object
10
     * @param $propertyName
11
     * @param $value
12
     * @param bool $revoke whether to make property inaccessible after setting
13
     * @throws \ReflectionException
14
     */
15
    protected function setInaccessibleProperty($object, $propertyName, $value, bool $revoke = true): void
16
    {
17
        $class = new \ReflectionClass($object);
18
        while (!$class->hasProperty($propertyName)) {
19
            $class = $class->getParentClass();
20
        }
21
        $property = $class->getProperty($propertyName);
22
        $property->setAccessible(true);
23
        $property->setValue($object, $value);
24
        if ($revoke) {
25
            $property->setAccessible(false);
26
        }
27
    }
28
29
    /**
30
     * Gets an inaccessible object property.
31
     * @param $object
32
     * @param $propertyName
33
     * @param bool $revoke whether to make property inaccessible after getting
34
     * @return mixed
35
     * @throws \ReflectionException
36
     */
37
    protected function getInaccessibleProperty($object, $propertyName, bool $revoke = true)
38
    {
39
        $class = new \ReflectionClass($object);
40
        while (!$class->hasProperty($propertyName)) {
41
            $class = $class->getParentClass();
42
        }
43
        $property = $class->getProperty($propertyName);
44
        $property->setAccessible(true);
45
        $result = $property->getValue($object);
46
        if ($revoke) {
47
            $property->setAccessible(false);
48
        }
49
        return $result;
50
    }
51
}
52