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 stdClass; |
12
|
|
|
|
13
|
|
|
final class UtilityTest extends \PHPUnit\Framework\TestCase |
14
|
|
|
{ |
15
|
|
|
public function testGetShortClassName(): void |
16
|
|
|
{ |
17
|
|
|
self::assertSame('User', Utility::getShortClassName(new User())); |
18
|
|
|
self::assertSame('User', Utility::getShortClassName(User::class)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testEntityIdToModel(): void |
22
|
|
|
{ |
23
|
|
|
$input = [ |
24
|
|
|
3 => new stdClass(), |
25
|
|
|
4 => 1, |
26
|
|
|
'model' => new User(), |
27
|
|
|
'entity' => new class() extends EntityID { |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getEntity() |
33
|
|
|
{ |
34
|
|
|
return 'real entity'; |
35
|
|
|
} |
36
|
|
|
}, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
$actual = Utility::entityIdToModel($input); |
40
|
|
|
|
41
|
|
|
$expected = $input; |
42
|
|
|
$expected['entity'] = 'real entity'; |
43
|
|
|
|
44
|
|
|
self::assertSame($expected, $actual, 'keys and non model values should be preserved'); |
45
|
|
|
self::assertNull(Utility::entityIdToModel(null)); |
46
|
|
|
self::assertSame([], Utility::entityIdToModel([])); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testModelToId(): void |
50
|
|
|
{ |
51
|
|
|
$input = [ |
52
|
|
|
'entity' => new class() implements Model { |
53
|
|
|
public function getId(): ?int |
54
|
|
|
{ |
55
|
|
|
return 123456; |
56
|
|
|
} |
57
|
|
|
}, |
58
|
|
|
4 => 1, |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$actual = Utility::modelToId($input); |
62
|
|
|
|
63
|
|
|
$expected = $input; |
64
|
|
|
$expected['entity'] = 123456; |
65
|
|
|
|
66
|
|
|
self::assertSame($expected, $actual, 'models must be replaced by their ids, other values should be preserved'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|