1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Pfilsx \PostgreSQLDoctrine \Tests \Unit \Tools ;
6
+
7
+
8
+ use Pfilsx \PostgreSQLDoctrine \Enum \ArrayTypeEnum ;
9
+ use Pfilsx \PostgreSQLDoctrine \Tools \ArrayTypeTool ;
10
+ use PHPUnit \Framework \TestCase ;
11
+
12
+ /**
13
+ * @see ArrayTypeTool
14
+ */
15
+ final class ArrayTypeToolTest extends TestCase
16
+ {
17
+ /**
18
+ * @dataProvider providerTestConvertPHPArrayToDatabaseArrayString
19
+ * @see ArrayTypeTool::convertPHPArrayToDatabaseArrayString()
20
+ */
21
+ public function testConvertPHPArrayToDatabaseArrayString (array $ in , ArrayTypeEnum $ type , string $ expectedOut ): void
22
+ {
23
+ self ::assertSame ($ expectedOut , ArrayTypeTool::convertPHPArrayToDatabaseArrayString ($ in , $ type ));
24
+ }
25
+
26
+ public static function providerTestConvertPHPArrayToDatabaseArrayString (): array
27
+ {
28
+ return [
29
+ 'empty array ' => [
30
+ [],
31
+ ArrayTypeEnum::IntArray,
32
+ '{} ' ,
33
+ ],
34
+ 'int array ' => [
35
+ [1 ,2 ,3 ],
36
+ ArrayTypeEnum::SmallIntArray,
37
+ '{1,2,3} ' ,
38
+ ],
39
+ 'text array ' => [
40
+ ['1 ' , 'text ' , 'text with whitespace and quote " ' ],
41
+ ArrayTypeEnum::TextArray,
42
+ '{1,"text","text with whitespace and quote \""} '
43
+ ],
44
+ 'boolean array without platform ' => [
45
+ [1 , true , false , 'yes ' , 'off ' ],
46
+ ArrayTypeEnum::BooleanArray,
47
+ '{true,true,false,true,false} ' ,
48
+ ],
49
+ 'json array ' => [
50
+ [['key1 ' => 1 , 'key2 ' => null , 'key3 ' => ['sub_key1 ' => false ]], ['key1 ' => 2 ]],
51
+ ArrayTypeEnum::JsonArray,
52
+ '{"{\"key1\":1,\"key2\":null,\"key3\":{\"sub_key1\":false}}","{\"key1\":2}"} '
53
+ ],
54
+ ];
55
+ }
56
+
57
+ /**
58
+ * @dataProvider providerTestConvertPHPArrayToDatabaseArrayStringOnInvalidData
59
+ * @see ArrayTypeTool::convertPHPArrayToDatabaseArrayString()
60
+ */
61
+ public function testConvertPHPArrayToDatabaseArrayStringOnInvalidData (array $ in , ArrayTypeEnum $ type , string $ exMessage ): void
62
+ {
63
+ self ::expectException (\InvalidArgumentException::class);
64
+ self ::expectExceptionMessage ($ exMessage );
65
+ ArrayTypeTool::convertPHPArrayToDatabaseArrayString ($ in , $ type );
66
+ }
67
+
68
+ public static function providerTestConvertPHPArrayToDatabaseArrayStringOnInvalidData (): array
69
+ {
70
+ return [
71
+ 'int array with not int value ' => [
72
+ ['test ' ],
73
+ ArrayTypeEnum::IntArray,
74
+ 'Item at key 0 has invalid type. Expected type "int", "string" provided. '
75
+ ],
76
+ 'int array with not in range int value ' => [
77
+ [32768 ],
78
+ ArrayTypeEnum::SmallIntArray,
79
+ 'Item at key 0 is invalid for "smallint". Expected integer between -32768 and 32767. '
80
+ ],
81
+ 'text array ' => [
82
+ [false ],
83
+ ArrayTypeEnum::TextArray,
84
+ 'Item at key 0 has invalid type. Expected type "string", "bool" provided. '
85
+ ],
86
+ 'boolean array ' => [
87
+ ['test ' ],
88
+ ArrayTypeEnum::BooleanArray,
89
+ 'Item at key 0 has invalid type. Expected boolean, 0, 1 or boolean string literal, \'test \' provided. '
90
+ ],
91
+ ];
92
+ }
93
+
94
+
95
+ /**
96
+ * @dataProvider providerTestConvertDatabaseArrayStringToPHPArray
97
+ * @see ArrayTypeTool::convertDatabaseArrayStringToPHPArray()
98
+ */
99
+ public function testConvertDatabaseArrayStringToPHPArray (string $ in , ArrayTypeEnum $ type , array $ expectedOut ): void
100
+ {
101
+ self ::assertSame ($ expectedOut , ArrayTypeTool::convertDatabaseArrayStringToPHPArray ($ in , $ type ));
102
+ }
103
+
104
+ public static function providerTestConvertDatabaseArrayStringToPHPArray (): array
105
+ {
106
+ return [
107
+ 'empty array ' => [
108
+ '{} ' ,
109
+ ArrayTypeEnum::IntArray,
110
+ [],
111
+ ],
112
+ 'int array ' => [
113
+ '{1,2,3} ' ,
114
+ ArrayTypeEnum::SmallIntArray,
115
+ [1 ,2 ,3 ],
116
+ ],
117
+ 'text array ' => [
118
+ '{1,text,"text with whitespace and quote \"",null} ' ,
119
+ ArrayTypeEnum::TextArray,
120
+ ['1 ' , 'text ' , 'text with whitespace and quote " ' , null ],
121
+ ],
122
+ 'boolean array without platform ' => [
123
+ '{true,false} ' ,
124
+ ArrayTypeEnum::BooleanArray,
125
+ [true , false ],
126
+ ],
127
+ 'json array ' => [
128
+ '{"{\"key1\":1,\"key2\":null,\"key3\":{\"sub_key1\":false}}","{\"key1\":2}"} ' ,
129
+ ArrayTypeEnum::JsonArray,
130
+ [['key1 ' => 1 , 'key2 ' => null , 'key3 ' => ['sub_key1 ' => false ]], ['key1 ' => 2 ]],
131
+ ],
132
+ ];
133
+ }
134
+ }
0 commit comments