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\SimpleArrays; |
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
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Because it's necessary to test constructor |
82
|
|
|
* @throws FormsException |
83
|
|
|
*/ |
84
|
|
|
public function testArrayAdapter(): void |
85
|
|
|
{ |
86
|
|
|
$this->testAdapter( |
87
|
|
|
new Adapters\ArrayAdapter([ |
88
|
|
|
'foo' => 'aff', |
89
|
|
|
'bar' => 'poa', |
90
|
|
|
'baz' => 'cdd', |
91
|
|
|
'sgg' => 'arr', |
92
|
|
|
'sd#,\'srs' => 'ggsd<$=', |
93
|
|
|
'dsr.!>sd' => 'zfd?-"', |
94
|
|
|
'dg-[]' => 'dc^&#~\\€`~°', |
95
|
|
|
'dg[]' => '<?php =!@#dc^&#~', |
96
|
|
|
'xggxgx' => 'free', |
97
|
|
|
]), |
98
|
|
|
IEntry::SOURCE_GET, |
99
|
|
|
true, |
100
|
|
|
true, |
101
|
|
|
true, |
102
|
|
|
true |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @throws FormsException |
108
|
|
|
*/ |
109
|
|
|
public function testVarsAdapterDie(): void |
110
|
|
|
{ |
111
|
|
|
$adapter = new Adapters\VarsAdapter(); |
112
|
|
|
$this->expectException(FormsException::class); |
113
|
|
|
$adapter->loadEntries('unknown_one'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Because it's necessary to test constructor |
118
|
|
|
* @throws FormsException |
119
|
|
|
*/ |
120
|
|
|
public function testInputVarsAdapter(): void |
121
|
|
|
{ |
122
|
|
|
$this->testAdapter( |
123
|
|
|
new Adapters\InputVarsAdapter(new SimpleArrays([ |
124
|
|
|
'foo' => 'aff', |
125
|
|
|
'bar' => 'poa', |
126
|
|
|
'baz' => 'cdd', |
127
|
|
|
'sgg' => 'arr', |
128
|
|
|
'sd#,\'srs' => 'ggsd<$=', |
129
|
|
|
'dsr.!>sd' => 'zfd?-"', |
130
|
|
|
'dg-[]' => 'dc^&#~\\€`~°', |
131
|
|
|
'dg[]' => '<?php =!@#dc^&#~', |
132
|
|
|
'xggxgx' => 'free', |
133
|
|
|
])), |
134
|
|
|
IEntry::SOURCE_CLI, |
135
|
|
|
true, |
136
|
|
|
true, |
137
|
|
|
true, |
138
|
|
|
true |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @throws FormsException |
144
|
|
|
*/ |
145
|
|
|
public function testInputVarsAdapterPassInputs(): void |
146
|
|
|
{ |
147
|
|
|
$adapter = new Adapters\InputVarsAdapter(new SimpleArrays([ |
148
|
|
|
'foo' => 'aff', |
149
|
|
|
], IEntry::SOURCE_CLI)); |
150
|
|
|
// input type there does not matter, because simple arrays have no information about source - there is no source |
151
|
|
|
$adapter->loadEntries(IEntry::SOURCE_CLI); |
152
|
|
|
$this->assertEquals(1, $adapter->count()); |
153
|
|
|
$adapter->loadEntries(IEntry::SOURCE_GET); |
154
|
|
|
$this->assertEquals(1, $adapter->count()); |
155
|
|
|
$adapter->loadEntries(IEntry::SOURCE_POST); |
156
|
|
|
$this->assertEquals(1, $adapter->count()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @throws FormsException |
161
|
|
|
*/ |
162
|
|
|
public function testInputVarsAdapterDieBadInput(): void |
163
|
|
|
{ |
164
|
|
|
$adapter = new Adapters\InputVarsAdapter(new SimpleArrays([ |
165
|
|
|
'foo' => 'aff', |
166
|
|
|
])); |
167
|
|
|
// session is not available as data source |
168
|
|
|
$this->expectException(FormsException::class); |
169
|
|
|
$adapter->loadEntries(IEntry::SOURCE_SESSION); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @throws FormsException |
174
|
|
|
*/ |
175
|
|
|
public function testInputVarsAdapterDieOutOfRange(): void |
176
|
|
|
{ |
177
|
|
|
$adapter = new Adapters\InputVarsAdapter(new SimpleArrays([ |
178
|
|
|
'foo' => 'aff', |
179
|
|
|
])); |
180
|
|
|
$adapter->rewind(); |
181
|
|
|
$adapter->next(); |
182
|
|
|
|
183
|
|
|
$this->expectException(FormsException::class); |
184
|
|
|
$adapter->current(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @throws FormsException |
189
|
|
|
*/ |
190
|
|
|
public function testAdapterFile(): void |
191
|
|
|
{ |
192
|
|
|
$adapter = new \Files(); |
193
|
|
|
$adapter->loadEntries(''); |
194
|
|
|
$adapter->rewind(); |
195
|
|
|
$adapter->next(); |
196
|
|
|
$this->assertNotEmpty($adapter->getKey()); |
197
|
|
|
$this->assertNotEmpty($adapter->getValue()); |
198
|
|
|
$this->assertNotEmpty($adapter->current()->getMimeType()); |
199
|
|
|
$this->assertNotEmpty($adapter->current()->getTempName()); |
200
|
|
|
$this->assertNotEmpty($adapter->current()->getError()); |
201
|
|
|
$this->assertNotEmpty($adapter->current()->getSize()); |
202
|
|
|
$this->assertEquals(IEntry::SOURCE_FILES, $adapter->current()->getSource()); |
203
|
|
|
$adapter->next(); |
204
|
|
|
$adapter->next(); |
205
|
|
|
$adapter->next(); |
206
|
|
|
$adapter->next(); |
207
|
|
|
$this->expectException(FormsException::class); |
208
|
|
|
$adapter->getValue(); // not exists |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @throws FormsException |
213
|
|
|
*/ |
214
|
|
|
public function testInputFilesAdapterProcess(): void |
215
|
|
|
{ |
216
|
|
|
$adapter = new Adapters\InputFilesAdapter(new SimpleArrays([ |
217
|
|
|
'foo' => 'aff', |
218
|
|
|
], IEntry::SOURCE_FILES)); |
219
|
|
|
// input type there does not matter, because simple arrays have no information about source - there is no source |
220
|
|
|
$adapter->loadEntries(IEntry::SOURCE_CLI); |
221
|
|
|
$this->assertEquals(1, $adapter->count()); |
222
|
|
|
|
223
|
|
|
$adapter->rewind(); |
224
|
|
|
$adapter->current(); |
225
|
|
|
$adapter->next(); |
226
|
|
|
|
227
|
|
|
$this->expectException(FormsException::class); |
228
|
|
|
$adapter->current(); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|