1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
use Ecodev\Felix\Model\Model; |
9
|
|
|
use Ecodev\Felix\Utility; |
10
|
|
|
use EcodevTests\Felix\Blog\Model\User; |
11
|
|
|
use EcodevTests\Felix\Traits\TestWithEntityManager; |
12
|
|
|
use Exception; |
13
|
|
|
use GraphQL\Doctrine\Definition\EntityID; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use stdClass; |
16
|
|
|
use Throwable; |
17
|
|
|
|
18
|
|
|
final class UtilityTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
use TestWithEntityManager; |
21
|
|
|
|
22
|
|
|
public function testGetShortClassName(): void |
23
|
|
|
{ |
24
|
|
|
self::assertSame('User', Utility::getShortClassName(new User())); |
25
|
|
|
self::assertSame('User', Utility::getShortClassName(User::class)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testEntityIdToModel(): void |
29
|
|
|
{ |
30
|
|
|
$fakeEntity = new stdClass(); |
31
|
|
|
$input = [ |
32
|
|
|
3 => new stdClass(), |
33
|
|
|
4 => 1, |
34
|
|
|
'model' => new User(), |
35
|
|
|
'entity' => new class($fakeEntity) extends EntityID { |
36
|
|
|
public function __construct(private readonly object $fakeEntity) |
37
|
|
|
{ |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getEntity(): object |
41
|
|
|
{ |
42
|
|
|
return $this->fakeEntity; |
43
|
|
|
} |
44
|
|
|
}, |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
$actual = Utility::entityIdToModel($input); |
48
|
|
|
|
49
|
|
|
$expected = $input; |
50
|
|
|
$expected['entity'] = $fakeEntity; |
51
|
|
|
|
52
|
|
|
self::assertSame($expected, $actual, 'keys and non model values should be preserved'); |
53
|
|
|
self::assertNull(Utility::entityIdToModel(null)); |
54
|
|
|
self::assertSame([], Utility::entityIdToModel([])); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testModelToId(): void |
58
|
|
|
{ |
59
|
|
|
$input = [ |
60
|
|
|
3 => new stdClass(), |
61
|
|
|
'model' => new class() implements Model { |
62
|
|
|
public function getId(): ?int |
63
|
|
|
{ |
64
|
|
|
return 123456; |
65
|
|
|
} |
66
|
|
|
}, |
67
|
|
|
4 => 1, |
68
|
|
|
'entityId' => new EntityID(_em(), User::class, '456'), |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
$actual = Utility::modelToId($input); |
72
|
|
|
|
73
|
|
|
$expected = $input; |
74
|
|
|
$expected['model'] = 123456; |
75
|
|
|
$expected['entityId'] = '456'; |
76
|
|
|
|
77
|
|
|
self::assertSame($expected, $actual, 'models must be replaced by their ids, other values should be preserved'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function createArray(): array |
81
|
|
|
{ |
82
|
|
|
$object1 = new class() { |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
$object2 = new class() { |
86
|
|
|
}; |
87
|
|
|
|
88
|
|
|
return [ |
89
|
|
|
$object1, |
90
|
|
|
3, |
91
|
|
|
$object2, |
92
|
|
|
3, |
93
|
|
|
$object1, |
94
|
|
|
2, |
95
|
|
|
'2', |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testUnique(): void |
100
|
|
|
{ |
101
|
|
|
$array = $this->createArray(); |
102
|
|
|
$actual = Utility::unique($array); |
103
|
|
|
|
104
|
|
|
$expected = [ |
105
|
|
|
$array[0], |
106
|
|
|
$array[1], |
107
|
|
|
$array[2], |
108
|
|
|
$array[5], |
109
|
|
|
$array[6], |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
self::assertSame($expected, $actual); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @dataProvider providerNativeUniqueWillThrowWithOurTestObject |
117
|
|
|
*/ |
118
|
|
|
public function testNativeUniqueWillThrowWithOurTestObject(int $flag): void |
119
|
|
|
{ |
120
|
|
|
set_error_handler( |
121
|
|
|
function (int $errno, string $message): void { |
122
|
|
|
restore_error_handler(); |
123
|
|
|
|
124
|
|
|
throw new Exception($message); |
125
|
|
|
} |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
try { |
129
|
|
|
$foo = array_unique($this->createArray(), $flag); |
|
|
|
|
130
|
|
|
} catch (Throwable $e) { |
131
|
|
|
self::assertStringStartsWith('Object of class class@anonymous could not be converted to ', $e->getMessage()); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public static function providerNativeUniqueWillThrowWithOurTestObject(): iterable |
136
|
|
|
{ |
137
|
|
|
yield [SORT_REGULAR]; |
138
|
|
|
yield [SORT_NUMERIC]; |
139
|
|
|
yield [SORT_STRING]; |
140
|
|
|
yield [SORT_LOCALE_STRING]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @dataProvider providerQuoteArray |
145
|
|
|
*/ |
146
|
|
|
public function testQuoteArray(array $input, string $expected): void |
147
|
|
|
{ |
148
|
|
|
self::assertSame($expected, Utility::quoteArray($input)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public static function providerQuoteArray(): iterable |
152
|
|
|
{ |
153
|
|
|
yield [[1, 2], "'1', '2'"]; |
154
|
|
|
yield [['foo bar', 'baz'], "'foo bar', 'baz'"]; |
155
|
|
|
yield [[], '']; |
156
|
|
|
yield [[''], "''"]; |
157
|
|
|
yield [[false], "''"]; |
158
|
|
|
yield [[null], "''"]; |
159
|
|
|
yield [[0], "'0'"]; |
160
|
|
|
yield [[true], "'1'"]; |
161
|
|
|
yield [[1.23], "'1.23'"]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function getCookieDomainProvider(): iterable |
165
|
|
|
{ |
166
|
|
|
yield ['', null]; |
167
|
|
|
yield ['localhost', null]; |
168
|
|
|
yield ['example.com', '.example.com']; |
169
|
|
|
yield ['www.example.com', '.example.com']; |
170
|
|
|
yield ['example.com:123', '.example.com']; |
171
|
|
|
yield ['www.example.com:123', '.example.com']; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @dataProvider getCookieDomainProvider |
176
|
|
|
*/ |
177
|
|
|
public function testGetCookieDomain(string $input, ?string $expected): void |
178
|
|
|
{ |
179
|
|
|
$actual = Utility::getCookieDomain($input); |
180
|
|
|
self::assertSame($expected, $actual); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testConcat(): void |
184
|
|
|
{ |
185
|
|
|
$iterable = new ArrayIterator([1 => 'one', 'a' => 'overridden', 'c' => 'c']); |
186
|
|
|
$actual = Utility::concat(['a' => 'a', 2 => 'two'], $iterable); |
187
|
|
|
|
188
|
|
|
self::assertSame([ |
189
|
|
|
'a' => 'overridden', |
190
|
|
|
2 => 'two', |
191
|
|
|
1 => 'one', |
192
|
|
|
'c' => 'c', |
193
|
|
|
], iterator_to_array($actual)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testFilterByKeys(): void |
197
|
|
|
{ |
198
|
|
|
$things = new ArrayIterator(['a' => 'a', 'b' => 'b', 'c' => 'c']); |
199
|
|
|
|
200
|
|
|
self::assertSame(['a' => 'a'], iterator_to_array(Utility::filterByKeys($things, 'a'))); |
201
|
|
|
self::assertSame(['a' => 'a', 'c' => 'c'], iterator_to_array(Utility::filterByKeys($things, 'c', 'a'))); |
202
|
|
|
self::assertSame([], iterator_to_array(Utility::filterByKeys($things, 'unknown-key'))); |
203
|
|
|
self::assertSame([], iterator_to_array(Utility::filterByKeys($things))); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|