Passed
Push — master ( 79fe9e...36c6ba )
by Adrien
13:46
created

UtilityTest::testQuoteArray()

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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