|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix; |
|
6
|
|
|
|
|
7
|
|
|
use Ecodev\Felix\Model\Model; |
|
8
|
|
|
use Ecodev\Felix\Utility; |
|
9
|
|
|
use EcodevTests\Felix\Blog\Model\User; |
|
10
|
|
|
use EcodevTests\Felix\Traits\TestWithEntityManager; |
|
11
|
|
|
use GraphQL\Doctrine\Definition\EntityID; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use stdClass; |
|
14
|
|
|
use Throwable; |
|
15
|
|
|
|
|
16
|
|
|
final class UtilityTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
use TestWithEntityManager; |
|
19
|
|
|
|
|
20
|
|
|
public function testGetShortClassName(): void |
|
21
|
|
|
{ |
|
22
|
|
|
self::assertSame('User', Utility::getShortClassName(new User())); |
|
23
|
|
|
self::assertSame('User', Utility::getShortClassName(User::class)); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testEntityIdToModel(): void |
|
27
|
|
|
{ |
|
28
|
|
|
$fakeEntity = new stdClass(); |
|
29
|
|
|
$input = [ |
|
30
|
|
|
3 => new stdClass(), |
|
31
|
|
|
4 => 1, |
|
32
|
|
|
'model' => new User(), |
|
33
|
|
|
'entity' => new class($fakeEntity) extends EntityID { |
|
34
|
|
|
public function __construct(private readonly object $fakeEntity) |
|
35
|
|
|
{ |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getEntity(): object |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->fakeEntity; |
|
41
|
|
|
} |
|
42
|
|
|
}, |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
$actual = Utility::entityIdToModel($input); |
|
46
|
|
|
|
|
47
|
|
|
$expected = $input; |
|
48
|
|
|
$expected['entity'] = $fakeEntity; |
|
49
|
|
|
|
|
50
|
|
|
self::assertSame($expected, $actual, 'keys and non model values should be preserved'); |
|
51
|
|
|
self::assertNull(Utility::entityIdToModel(null)); |
|
52
|
|
|
self::assertSame([], Utility::entityIdToModel([])); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testModelToId(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$input = [ |
|
58
|
|
|
'entity' => new class() implements Model { |
|
59
|
|
|
public function getId(): ?int |
|
60
|
|
|
{ |
|
61
|
|
|
return 123456; |
|
62
|
|
|
} |
|
63
|
|
|
}, |
|
64
|
|
|
4 => 1, |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
$actual = Utility::modelToId($input); |
|
68
|
|
|
|
|
69
|
|
|
$expected = $input; |
|
70
|
|
|
$expected['entity'] = 123456; |
|
71
|
|
|
|
|
72
|
|
|
self::assertSame($expected, $actual, 'models must be replaced by their ids, other values should be preserved'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function createArray(): array |
|
76
|
|
|
{ |
|
77
|
|
|
$object1 = new class() { |
|
78
|
|
|
}; |
|
79
|
|
|
|
|
80
|
|
|
$object2 = new class() { |
|
81
|
|
|
}; |
|
82
|
|
|
|
|
83
|
|
|
return [ |
|
84
|
|
|
$object1, |
|
85
|
|
|
3, |
|
86
|
|
|
$object2, |
|
87
|
|
|
3, |
|
88
|
|
|
$object1, |
|
89
|
|
|
2, |
|
90
|
|
|
'2', |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testUnique(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$array = $this->createArray(); |
|
97
|
|
|
$actual = Utility::unique($array); |
|
98
|
|
|
|
|
99
|
|
|
$expected = [ |
|
100
|
|
|
$array[0], |
|
101
|
|
|
$array[1], |
|
102
|
|
|
$array[2], |
|
103
|
|
|
$array[5], |
|
104
|
|
|
$array[6], |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
|
|
self::assertSame($expected, $actual); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @dataProvider providerNativeUniqueWillThrowWithOurTestObject |
|
112
|
|
|
*/ |
|
113
|
|
|
public function testNativeUniqueWillThrowWithOurTestObject(int $flag): void |
|
114
|
|
|
{ |
|
115
|
|
|
try { |
|
116
|
|
|
$foo = array_unique($this->createArray(), $flag); |
|
|
|
|
|
|
117
|
|
|
} catch (Throwable $e) { |
|
118
|
|
|
self::assertStringStartsWith('Object of class class@anonymous could not be converted to ', $e->getMessage()); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function providerNativeUniqueWillThrowWithOurTestObject(): iterable |
|
123
|
|
|
{ |
|
124
|
|
|
yield [SORT_REGULAR]; |
|
125
|
|
|
yield [SORT_NUMERIC]; |
|
126
|
|
|
yield [SORT_STRING]; |
|
127
|
|
|
yield [SORT_LOCALE_STRING]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @dataProvider providerQuoteArray |
|
132
|
|
|
*/ |
|
133
|
|
|
public function testQuoteArray(array $input, string $expected): void |
|
134
|
|
|
{ |
|
135
|
|
|
self::assertSame($expected, Utility::quoteArray($input)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function providerQuoteArray(): iterable |
|
139
|
|
|
{ |
|
140
|
|
|
yield [[1, 2], "'1', '2'"]; |
|
141
|
|
|
yield [['foo bar', 'baz'], "'foo bar', 'baz'"]; |
|
142
|
|
|
yield [[], '']; |
|
143
|
|
|
yield [[''], "''"]; |
|
144
|
|
|
yield [[false], '']; |
|
145
|
|
|
yield [[null], '']; |
|
146
|
|
|
yield [[0], "'0'"]; |
|
147
|
|
|
yield [[true], "'1'"]; |
|
148
|
|
|
yield [[1.23], "'1.23'"]; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|