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