Passed
Push — master ( 508260...eb09d4 )
by Sylvain
09:03
created

UtilityTest.php$0 ➔ testEntityIdToModel()   A

Complexity

Conditions 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 26
rs 9.504
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A UtilityTest.php$0 ➔ __construct() 0 2 1
A UtilityTest.php$0 ➔ getEntity() 0 3 1
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