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