Passed
Push — master ( bdfe7b...8f2aab )
by Alexander
01:12
created

TestCase::assertSameExceptObject()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 5
eloc 11
nc 5
nop 2
dl 0
loc 22
rs 9.6111
c 1
b 1
f 1
1
<?php
2
3
namespace Yiisoft\Security\Tests;
4
5
abstract class TestCase extends \PHPUnit\Framework\TestCase
6
{
7
    /**
8
     * Invokes a inaccessible method.
9
     * @param $object
10
     * @param $method
11
     * @param array $args
12
     * @param bool $revoke whether to make method inaccessible after execution
13
     * @return mixed
14
     * @throws \ReflectionException
15
     */
16
    protected function invokeMethod($object, $method, array $args = [], bool $revoke = true)
17
    {
18
        $reflection = new \ReflectionObject($object);
19
        $method = $reflection->getMethod($method);
20
        $method->setAccessible(true);
21
        $result = $method->invokeArgs($object, $args);
22
        if ($revoke) {
23
            $method->setAccessible(false);
24
        }
25
26
        return $result;
27
    }
28
29
    /**
30
     * Sets an inaccessible object property to a designated value.
31
     * @param $object
32
     * @param $propertyName
33
     * @param $value
34
     * @param bool $revoke whether to make property inaccessible after setting
35
     * @throws \ReflectionException
36
     */
37
    protected function setInaccessibleProperty($object, $propertyName, $value, bool $revoke = true): void
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
        $property->setValue($object, $value);
46
        if ($revoke) {
47
            $property->setAccessible(false);
48
        }
49
    }
50
51
52
    /**
53
     * Gets an inaccessible object property.
54
     * @param $object
55
     * @param $propertyName
56
     * @param bool $revoke whether to make property inaccessible after getting
57
     * @return mixed
58
     * @throws \ReflectionException
59
     */
60
    protected function getInaccessibleProperty($object, $propertyName, bool $revoke = true)
61
    {
62
        $class = new \ReflectionClass($object);
63
        while (!$class->hasProperty($propertyName)) {
64
            $class = $class->getParentClass();
65
        }
66
        $property = $class->getProperty($propertyName);
67
        $property->setAccessible(true);
68
        $result = $property->getValue($object);
69
        if ($revoke) {
70
            $property->setAccessible(false);
71
        }
72
73
        return $result;
74
    }
75
76
    public function assertSameExceptObject($expected, $actual): void
77
    {
78
        // assert for all types
79
        $this->assertEquals($expected, $actual);
80
81
        // no more asserts for objects
82
        if (is_object($expected)) {
83
            return;
84
        }
85
86
        // asserts same for all types except objects and arrays that can contain objects
87
        if (!is_array($expected)) {
88
            $this->assertSame($expected, $actual);
89
            return;
90
        }
91
92
        // assert same for each element of the array except objects
93
        foreach ($expected as $key => $value) {
94
            if (!is_object($value)) {
95
                $this->assertSame($expected[$key], $actual[$key]);
96
            } else {
97
                $this->assertEquals($expected[$key], $actual[$key]);
98
            }
99
        }
100
    }
101
}
102