|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BasicTests; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use CommonTestClass; |
|
7
|
|
|
use kalanis\kw_forms\Adapters; |
|
8
|
|
|
use kalanis\kw_forms\Exceptions\FormsException; |
|
9
|
|
|
use kalanis\kw_input\Filtered; |
|
10
|
|
|
use kalanis\kw_input\Interfaces\IEntry; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class AdaptersTest extends CommonTestClass |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param Adapters\AAdapter $adapter |
|
17
|
|
|
* @param string $inputType |
|
18
|
|
|
* @param bool $canCount |
|
19
|
|
|
* @param bool $canThrough |
|
20
|
|
|
* @param bool $canStep |
|
21
|
|
|
* @param bool $canSet |
|
22
|
|
|
* @throws FormsException |
|
23
|
|
|
* @dataProvider adapterProvider |
|
24
|
|
|
*/ |
|
25
|
|
|
public function testAdapter(Adapters\AAdapter $adapter, string $inputType, bool $canCount, bool $canThrough, bool $canStep, bool $canSet): void |
|
26
|
|
|
{ |
|
27
|
|
|
$adapter->loadEntries($inputType); |
|
28
|
|
|
$this->assertNotEmpty($adapter->getSource()); |
|
29
|
|
|
if ($canCount) { |
|
30
|
|
|
$this->assertEquals(9, $adapter->count()); |
|
31
|
|
|
$this->assertEquals('aff', $adapter->offsetGet('foo')); |
|
32
|
|
|
} |
|
33
|
|
|
if ($canThrough) { |
|
34
|
|
|
foreach ($adapter as $key => $item) { |
|
35
|
|
|
$this->assertNotEmpty($key); |
|
36
|
|
|
$this->assertNotEmpty($item); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
if ($canStep) { |
|
40
|
|
|
$adapter->rewind(); |
|
41
|
|
|
$adapter->next(); |
|
42
|
|
|
$this->assertNotEmpty($adapter->getKey()); |
|
43
|
|
|
$this->assertNotEmpty($adapter->getValue()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ($canSet) { |
|
47
|
|
|
$this->assertFalse($adapter->offsetExists('fee')); |
|
48
|
|
|
$adapter->offsetSet('fee','nnn'); |
|
49
|
|
|
$this->assertTrue($adapter->offsetExists('fee')); |
|
50
|
|
|
$adapter->offsetUnset('fee'); |
|
51
|
|
|
$this->assertFalse($adapter->offsetExists('fee')); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function adapterProvider(): array |
|
56
|
|
|
{ |
|
57
|
|
|
$entry = new \kalanis\kw_input\Entries\Entry(); |
|
58
|
|
|
$entry->setEntry(\kalanis\kw_input\Interfaces\IEntry::SOURCE_EXTERNAL, 'xggxgx', 'lkjhdf'); |
|
59
|
|
|
$_GET = [ |
|
60
|
|
|
'foo' => 'aff', |
|
61
|
|
|
'bar' => 'poa', |
|
62
|
|
|
'baz' => 'cdd', |
|
63
|
|
|
'sgg' => 'arr', |
|
64
|
|
|
'sd#,\'srs' => 'ggsd<$=', |
|
65
|
|
|
'dsr.!>sd' => 'zfd?-"', |
|
66
|
|
|
'dg-[]' => 'dc^&#~\\€`~°', |
|
67
|
|
|
'dg[]' => '<?php =!@#dc^&#~', |
|
68
|
|
|
'xggxgx' => $entry, |
|
69
|
|
|
]; |
|
70
|
|
|
return [ |
|
71
|
|
|
[new \Adapter(), '', true, true, true, true ], |
|
72
|
|
|
[new Adapters\ArrayAdapter($_GET), IEntry::SOURCE_GET, true, true, true, true ], |
|
73
|
|
|
[new Adapters\VarsAdapter(), IEntry::SOURCE_GET, true, false, true, true ], |
|
74
|
|
|
[new Adapters\VarsAdapter(), IEntry::SOURCE_POST, false, false, false, true ], |
|
75
|
|
|
[new Adapters\SessionAdapter(), '', false, false, false, true ], |
|
76
|
|
|
[new \Files(), '', false, false, false, false ], |
|
77
|
|
|
[new Adapters\InputVarsAdapter(new Filtered\Json( |
|
78
|
|
|
'{"foo":"aff","bar":"poa","baz":"cdd","sgg":"arr","sd#,\'srs":"ggsd<$=","dsr.!>sd":"zfd?-\"","dg-[]":"dc^&#~\\\u20ac`~\u00b0","dg[]":"<?php =!@#dc^&#~","xggxgx":"free"}' |
|
79
|
|
|
)), IEntry::SOURCE_JSON, true, true, true, true ], |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Because it's necessary to test constructor |
|
85
|
|
|
* @throws FormsException |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testArrayAdapter(): void |
|
88
|
|
|
{ |
|
89
|
|
|
$this->testAdapter( |
|
90
|
|
|
new Adapters\ArrayAdapter([ |
|
91
|
|
|
'foo' => 'aff', |
|
92
|
|
|
'bar' => 'poa', |
|
93
|
|
|
'baz' => 'cdd', |
|
94
|
|
|
'sgg' => 'arr', |
|
95
|
|
|
'sd#,\'srs' => 'ggsd<$=', |
|
96
|
|
|
'dsr.!>sd' => 'zfd?-"', |
|
97
|
|
|
'dg-[]' => 'dc^&#~\\€`~°', |
|
98
|
|
|
'dg[]' => '<?php =!@#dc^&#~', |
|
99
|
|
|
'xggxgx' => 'free', |
|
100
|
|
|
]), |
|
101
|
|
|
IEntry::SOURCE_GET, |
|
102
|
|
|
true, |
|
103
|
|
|
true, |
|
104
|
|
|
true, |
|
105
|
|
|
true |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @throws FormsException |
|
111
|
|
|
*/ |
|
112
|
|
|
public function testVarsAdapterDie(): void |
|
113
|
|
|
{ |
|
114
|
|
|
$adapter = new Adapters\VarsAdapter(); |
|
115
|
|
|
$this->expectException(FormsException::class); |
|
116
|
|
|
$adapter->loadEntries('unknown_one'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Because it's necessary to test constructor |
|
121
|
|
|
* @throws FormsException |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testInputVarsAdapter(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$this->testAdapter( |
|
126
|
|
|
new Adapters\InputVarsAdapter(new Filtered\SimpleArrays([ |
|
127
|
|
|
'foo' => 'aff', |
|
128
|
|
|
'bar' => 'poa', |
|
129
|
|
|
'baz' => 'cdd', |
|
130
|
|
|
'sgg' => 'arr', |
|
131
|
|
|
'sd#,\'srs' => 'ggsd<$=', |
|
132
|
|
|
'dsr.!>sd' => 'zfd?-"', |
|
133
|
|
|
'dg-[]' => 'dc^&#~\\€`~°', |
|
134
|
|
|
'dg[]' => '<?php =!@#dc^&#~', |
|
135
|
|
|
'xggxgx' => 'free', |
|
136
|
|
|
])), |
|
137
|
|
|
IEntry::SOURCE_CLI, |
|
138
|
|
|
true, |
|
139
|
|
|
true, |
|
140
|
|
|
true, |
|
141
|
|
|
true |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @throws FormsException |
|
147
|
|
|
*/ |
|
148
|
|
|
public function testInputVarsAdapterPassInputs(): void |
|
149
|
|
|
{ |
|
150
|
|
|
$adapter = new Adapters\InputVarsAdapter(new Filtered\SimpleArrays([ |
|
151
|
|
|
'foo' => 'aff', |
|
152
|
|
|
], IEntry::SOURCE_CLI)); |
|
153
|
|
|
// input type there does not matter, because simple arrays have no information about source - there is no source |
|
154
|
|
|
$adapter->loadEntries(IEntry::SOURCE_CLI); |
|
155
|
|
|
$this->assertEquals(1, $adapter->count()); |
|
156
|
|
|
$adapter->loadEntries(IEntry::SOURCE_GET); |
|
157
|
|
|
$this->assertEquals(1, $adapter->count()); |
|
158
|
|
|
$adapter->loadEntries(IEntry::SOURCE_POST); |
|
159
|
|
|
$this->assertEquals(1, $adapter->count()); |
|
160
|
|
|
$adapter->loadEntries(IEntry::SOURCE_JSON); |
|
161
|
|
|
$this->assertEquals(1, $adapter->count()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @throws FormsException |
|
166
|
|
|
*/ |
|
167
|
|
|
public function testInputVarsAdapterDieBadInput(): void |
|
168
|
|
|
{ |
|
169
|
|
|
$adapter = new Adapters\InputVarsAdapter(new Filtered\SimpleArrays([ |
|
170
|
|
|
'foo' => 'aff', |
|
171
|
|
|
])); |
|
172
|
|
|
// session is not available as data source |
|
173
|
|
|
$this->expectException(FormsException::class); |
|
174
|
|
|
$adapter->loadEntries(IEntry::SOURCE_SESSION); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @throws FormsException |
|
179
|
|
|
*/ |
|
180
|
|
|
public function testInputVarsAdapterDieOutOfRange(): void |
|
181
|
|
|
{ |
|
182
|
|
|
$adapter = new Adapters\InputVarsAdapter(new Filtered\SimpleArrays([ |
|
183
|
|
|
'foo' => 'aff', |
|
184
|
|
|
])); |
|
185
|
|
|
$adapter->rewind(); |
|
186
|
|
|
$adapter->next(); |
|
187
|
|
|
|
|
188
|
|
|
$this->expectException(FormsException::class); |
|
189
|
|
|
$adapter->current(); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @throws FormsException |
|
194
|
|
|
*/ |
|
195
|
|
|
public function testAdapterFile(): void |
|
196
|
|
|
{ |
|
197
|
|
|
$adapter = new \Files(); |
|
198
|
|
|
$adapter->loadEntries(''); |
|
199
|
|
|
$adapter->rewind(); |
|
200
|
|
|
$adapter->next(); |
|
201
|
|
|
$this->assertNotEmpty($adapter->getKey()); |
|
202
|
|
|
$this->assertNotEmpty($adapter->getValue()); |
|
203
|
|
|
$this->assertNotEmpty($adapter->current()->getMimeType()); |
|
204
|
|
|
$this->assertNotEmpty($adapter->current()->getTempName()); |
|
205
|
|
|
$this->assertNotEmpty($adapter->current()->getError()); |
|
206
|
|
|
$this->assertNotEmpty($adapter->current()->getSize()); |
|
207
|
|
|
$this->assertEquals(IEntry::SOURCE_FILES, $adapter->current()->getSource()); |
|
208
|
|
|
$adapter->next(); |
|
209
|
|
|
$adapter->next(); |
|
210
|
|
|
$adapter->next(); |
|
211
|
|
|
$adapter->next(); |
|
212
|
|
|
$this->expectException(FormsException::class); |
|
213
|
|
|
$adapter->getValue(); // not exists |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @throws FormsException |
|
218
|
|
|
*/ |
|
219
|
|
|
public function testInputFilesAdapterProcess(): void |
|
220
|
|
|
{ |
|
221
|
|
|
$adapter = new Adapters\InputFilesAdapter(new Filtered\SimpleArrays([ |
|
222
|
|
|
'foo' => 'aff', |
|
223
|
|
|
], IEntry::SOURCE_FILES)); |
|
224
|
|
|
// input type there does not matter, because simple arrays have no information about source - there is no source |
|
225
|
|
|
$adapter->loadEntries(IEntry::SOURCE_CLI); |
|
226
|
|
|
$this->assertEquals(1, $adapter->count()); |
|
227
|
|
|
|
|
228
|
|
|
$adapter->rewind(); |
|
229
|
|
|
$adapter->current(); |
|
230
|
|
|
$adapter->next(); |
|
231
|
|
|
|
|
232
|
|
|
$this->expectException(FormsException::class); |
|
233
|
|
|
$adapter->current(); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|