Passed
Push — master ( 845d88...54a270 )
by Alexander
02:56
created

TestCase::dataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
nc 1
nop 0
dl 0
loc 17
c 1
b 0
f 1
cc 1
rs 9.7666
1
<?php
2
3
namespace Yiisoft\Cache\Memcached\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
            'string_with_number_key' => ['111', 11],
95
            'string_with_number_key_1' => ['022', 22],
96
        ];
97
    }
98
99
    public function getDataProviderData($keyPrefix = ''): array
100
    {
101
        $dataProvider = $this->dataProvider();
102
        $data = [];
103
        foreach ($dataProvider as $item) {
104
            $data[$keyPrefix . $item[0]] = $item[1];
105
        }
106
107
        return $data;
108
    }
109
110
111
    /**
112
     * This function configures given cache to match some expectations
113
     * @param CacheInterface $cache
114
     * @return CacheInterface
115
     * @throws InvalidArgumentException
116
     */
117
    public function prepare(CacheInterface $cache): CacheInterface
118
    {
119
        $cache->clear();
120
121
        $data = $this->dataProvider();
122
123
        foreach ($data as $datum) {
124
            $cache->set($datum[0], $datum[1]);
125
        }
126
127
        return $cache;
128
    }
129
130
    public function assertSameExceptObject($expected, $actual): void
131
    {
132
        // assert for all types
133
        $this->assertEquals($expected, $actual);
134
135
        // no more asserts for objects
136
        if (is_object($expected)) {
137
            return;
138
        }
139
140
        // asserts same for all types except objects and arrays that can contain objects
141
        if (!is_array($expected)) {
142
            $this->assertSame($expected, $actual);
143
            return;
144
        }
145
146
        // assert same for each element of the array except objects
147
        foreach ($expected as $key => $value) {
148
            if (!is_object($value)) {
149
                $this->assertSame($expected[$key], $actual[$key]);
150
            } else {
151
                $this->assertEquals($expected[$key], $actual[$key]);
152
            }
153
        }
154
    }
155
}
156