Passed
Pull Request — master (#30)
by Alexander
02:06
created

TestCase::invokeMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 4
1
<?php
2
3
namespace Yiisoft\Cache\Tests;
4
5
use Psr\SimpleCache\CacheInterface;
6
use Psr\SimpleCache\InvalidArgumentException;
7
8
abstract class TestCase extends \PHPUnit\Framework\TestCase
9
{
10
    /**
11
     * Invokes a inaccessible method.
12
     * @param $object
13
     * @param $method
14
     * @param array $args
15
     * @param bool $revoke whether to make method inaccessible after execution
16
     * @return mixed
17
     * @throws \ReflectionException
18
     */
19
    protected function invokeMethod($object, $method, array $args = [], bool $revoke = true)
20
    {
21
        $reflection = new \ReflectionObject($object);
22
        $method = $reflection->getMethod($method);
23
        $method->setAccessible(true);
24
        $result = $method->invokeArgs($object, $args);
25
        if ($revoke) {
26
            $method->setAccessible(false);
27
        }
28
29
        return $result;
30
    }
31
32
    /**
33
     * Sets an inaccessible object property to a designated value.
34
     * @param $object
35
     * @param $propertyName
36
     * @param $value
37
     * @param bool $revoke whether to make property inaccessible after setting
38
     * @throws \ReflectionException
39
     */
40
    protected function setInaccessibleProperty($object, $propertyName, $value, bool $revoke = true): void
41
    {
42
        $class = new \ReflectionClass($object);
43
        while (!$class->hasProperty($propertyName)) {
44
            $class = $class->getParentClass();
45
        }
46
        $property = $class->getProperty($propertyName);
47
        $property->setAccessible(true);
48
        $property->setValue($object, $value);
49
        if ($revoke) {
50
            $property->setAccessible(false);
51
        }
52
    }
53
54
55
    /**
56
     * Gets an inaccessible object property.
57
     * @param $object
58
     * @param $propertyName
59
     * @param bool $revoke whether to make property inaccessible after getting
60
     * @return mixed
61
     * @throws \ReflectionException
62
     */
63
    protected function getInaccessibleProperty($object, $propertyName, bool $revoke = true)
64
    {
65
        $class = new \ReflectionClass($object);
66
        while (!$class->hasProperty($propertyName)) {
67
            $class = $class->getParentClass();
68
        }
69
        $property = $class->getProperty($propertyName);
70
        $property->setAccessible(true);
71
        $result = $property->getValue($object);
72
        if ($revoke) {
73
            $property->setAccessible(false);
74
        }
75
76
        return $result;
77
    }
78
79
    public function dataProvider(): array
80
    {
81
        $object = new \stdClass();
82
        $object->test_field = 'test_value';
83
        return [
84
            'integer' => ['test_integer', 1],
85
            'double' => ['test_double', 1.1],
86
            'string' => ['test_string', 'a'],
87
            'boolean_true' => ['test_boolean_true', true],
88
            'boolean_false' => ['test_boolean_false', false],
89
            'object' => ['test_object', $object],
90
            'array' => ['test_array', ['test_key' => 'test_value']],
91
            'null' => ['test_null', null],
92
        ];
93
    }
94
95
    public function getDataProviderData($keyPrefix = ''): array
96
    {
97
        $dataProvider = $this->dataProvider();
98
        $data = [];
99
        foreach ($dataProvider as $item) {
100
            $data[$keyPrefix . $item[0]] = $item[1];
101
        }
102
103
        return $data;
104
    }
105
106
107
    /**
108
     * This function configures given cache to match some expectations
109
     * @param CacheInterface $cache
110
     * @return CacheInterface
111
     * @throws InvalidArgumentException
112
     */
113
    public function prepare(CacheInterface $cache): CacheInterface
114
    {
115
        $cache->clear();
116
117
        $data = $this->dataProvider();
118
119
        foreach ($data as $datum) {
120
            $cache->set($datum[0], $datum[1]);
121
        }
122
123
        return $cache;
124
    }
125
126
    public function assertSameExceptObject($expected, $actual): void
127
    {
128
        // assert for all types
129
        $this->assertEquals($expected, $actual);
130
131
        // no more asserts for objects
132
        if (is_object($expected)) {
133
            return;
134
        }
135
136
        // asserts same for all types except objects and arrays that can contain objects
137
        if (!is_array($expected)) {
138
            $this->assertSame($expected, $actual);
139
            return;
140
        }
141
142
        // assert same for each element of the array except objects
143
        foreach ($expected as $key => $value) {
144
            if (!is_object($value)) {
145
                $this->assertSame($expected[$key], $actual[$key]);
146
            } else {
147
                $this->assertEquals($expected[$key], $actual[$key]);
148
            }
149
        }
150
    }
151
}
152