Passed
Push — master ( 447e68...173db2 )
by Alexander
02:21
created

TestCase   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
c 1
b 0
f 0
dl 0
loc 142
rs 10
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare() 0 11 2
A dataProvider() 0 15 1
A getDataProviderData() 0 9 2
A setInaccessibleProperty() 0 11 3
A getInaccessibleProperty() 0 14 3
A assertSameExceptObject() 0 22 5
A invokeMethod() 0 11 2
1
<?php
2
3
namespace Yiisoft\Cache\Apcu\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
            'supported_key_characters' => ['AZaz09_.', 'b'],
93
            '64_characters_key_max' => ['bVGEIeslJXtDPrtK.hgo6HL25_.1BGmzo4VA25YKHveHh7v9tUP8r5BNCyLhx4zy', 'c'],
94
        ];
95
    }
96
97
    public function getDataProviderData($keyPrefix = ''): array
98
    {
99
        $dataProvider = $this->dataProvider();
100
        $data = [];
101
        foreach ($dataProvider as $item) {
102
            $data[$keyPrefix . $item[0]] = $item[1];
103
        }
104
105
        return $data;
106
    }
107
108
109
    /**
110
     * This function configures given cache to match some expectations
111
     * @param CacheInterface $cache
112
     * @return CacheInterface
113
     * @throws InvalidArgumentException
114
     */
115
    public function prepare(CacheInterface $cache): CacheInterface
116
    {
117
        $cache->clear();
118
119
        $data = $this->dataProvider();
120
121
        foreach ($data as $datum) {
122
            $cache->set($datum[0], $datum[1]);
123
        }
124
125
        return $cache;
126
    }
127
128
    public function assertSameExceptObject($expected, $actual): void
129
    {
130
        // assert for all types
131
        $this->assertEquals($expected, $actual);
132
133
        // no more asserts for objects
134
        if (is_object($expected)) {
135
            return;
136
        }
137
138
        // asserts same for all types except objects and arrays that can contain objects
139
        if (!is_array($expected)) {
140
            $this->assertSame($expected, $actual);
141
            return;
142
        }
143
144
        // assert same for each element of the array except objects
145
        foreach ($expected as $key => $value) {
146
            if (!is_object($value)) {
147
                $this->assertSame($expected[$key], $actual[$key]);
148
            } else {
149
                $this->assertEquals($expected[$key], $actual[$key]);
150
            }
151
        }
152
    }
153
}
154