1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Codeception\Page\AbstractTraits; |
4
|
|
|
|
5
|
|
|
use ByTIC\Codeception\Helper\AcceptanceTrait; |
6
|
|
|
use Codeception\Template\Acceptance; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class FormTrait |
10
|
|
|
* @package ByTIC\Codeception\Page\AbstractTraits |
11
|
|
|
*/ |
12
|
|
|
trait FormTrait |
13
|
|
|
{ |
14
|
|
|
protected $forms = []; |
15
|
|
|
|
16
|
|
|
protected $formNameDefault = 'default'; |
17
|
|
|
|
18
|
|
|
protected $formData = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param null $name |
22
|
|
|
*/ |
23
|
|
|
public function checkForm($name = null) |
24
|
|
|
{ |
25
|
|
|
$name = $this->initFormName($name); |
26
|
|
|
$form = $this->forms[$name]; |
27
|
|
|
|
28
|
|
|
$this->getTester()->seeElement($form['path']); |
29
|
|
|
|
30
|
|
|
$fields = $form['fields']; |
31
|
|
|
foreach ($fields as $field => $params) { |
32
|
|
|
$this->getTester()->seeElement($params['path']); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param null $name |
38
|
|
|
* @return null|string |
39
|
|
|
*/ |
40
|
|
|
public function initFormName($name = null) |
41
|
|
|
{ |
42
|
|
|
return $name ? $name : $this->getFormNameDefault(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getFormNameDefault() |
49
|
|
|
{ |
50
|
|
|
return $this->formNameDefault; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return AcceptanceTrait|\Codeception\Actor; |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
abstract protected function getTester(); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $params |
60
|
|
|
* @param null $name |
61
|
|
|
*/ |
62
|
|
|
public function setFieldsAndsubmitForm($params = [], $name = null) |
63
|
|
|
{ |
64
|
|
|
$params = array_merge($this->formData, $params); |
65
|
|
|
foreach ($params as $field => $value) { |
66
|
|
|
$this->setFieldValue($field, $value); |
67
|
|
|
} |
68
|
|
|
$this->submitForm($name); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Sets a form field value based on field type |
73
|
|
|
* @param $field |
74
|
|
|
* @param $value |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function setFieldValue($field, $value) |
78
|
|
|
{ |
79
|
|
|
$type = $this->getFieldFormType($field); |
80
|
|
|
switch ($type) { |
81
|
|
|
case 'select': |
82
|
|
|
return $this->selectOptionForm($field, $value); |
83
|
|
|
case 'checkbox': |
84
|
|
|
case 'checkboxGroup': |
85
|
|
|
return $this->checkOptionForm($field); |
86
|
|
|
default: |
87
|
|
|
return $this->fillFieldForm($field, $value); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $field |
93
|
|
|
* @param null $form |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function getFieldFormType($field, $form = null) |
97
|
|
|
{ |
98
|
|
|
return $this->getFieldFormParam($field, 'type', $form); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $field |
103
|
|
|
* @param $param |
104
|
|
|
* @param null $form |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
|
|
public function getFieldFormParam($field, $param, $form = null) |
108
|
|
|
{ |
109
|
|
|
$form = $this->initFormName($form); |
110
|
|
|
|
111
|
|
|
return $this->forms[$form]['fields'][$field][$param]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $field |
116
|
|
|
* @param $value |
117
|
|
|
* @param null $form |
118
|
|
|
* @return mixed|null |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function selectOptionForm($field, $value, $form = null) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$form = $this->initFormName($form); |
123
|
|
|
$path = $this->getFieldFormPath($field, $form); |
|
|
|
|
124
|
|
|
|
125
|
|
|
return $this->getTester()->selectOption($path, $value); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $field |
130
|
|
|
* @param null $form |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function getFieldFormPath($field, $form = null) |
134
|
|
|
{ |
135
|
|
|
return $this->getFieldFormParam($field, 'path', $form); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $field |
140
|
|
|
* @param null $form |
141
|
|
|
* @return mixed|null |
142
|
|
|
*/ |
143
|
|
|
public function checkOptionForm($field, $form = null) |
144
|
|
|
{ |
145
|
|
|
$form = $this->initFormName($form); |
146
|
|
|
$path = $this->getFieldFormPath($field, $form); |
|
|
|
|
147
|
|
|
|
148
|
|
|
return $this->getTester()->checkOption($path); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $field |
153
|
|
|
* @param $value |
154
|
|
|
* @param null $form |
155
|
|
|
* @return mixed|null |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function fillFieldForm($field, $value, $form = null) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$form = $this->initFormName($form); |
160
|
|
|
$path = $this->getFieldFormPath($field, $form); |
|
|
|
|
161
|
|
|
|
162
|
|
|
return $this->getTester()->fillField($path, $value); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param null $name |
167
|
|
|
* @param array $params |
168
|
|
|
*/ |
169
|
|
|
public function submitForm($name = null, $params = []) |
170
|
|
|
{ |
171
|
|
|
$name = $this->initFormName($name); |
172
|
|
|
$form = $this->forms[$name]; |
173
|
|
|
$this->getTester()->submitForm($form['path'], $params); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param $name |
178
|
|
|
* @param $value |
179
|
|
|
*/ |
180
|
|
|
public function setFieldFormData($name, $value) |
181
|
|
|
{ |
182
|
|
|
$this->formData[$name] = $value; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param null $name |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
public function getFormData($name = null) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
return $this->formData; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* sets a field value from data object |
196
|
|
|
* @param $name |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
|
|
public function setFieldFromData($name) |
200
|
|
|
{ |
201
|
|
|
$value = $this->getFieldFormData($name); |
|
|
|
|
202
|
|
|
return $this->setFieldValue($name, $value); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param $name |
207
|
|
|
* @return null |
208
|
|
|
*/ |
209
|
|
|
public function getFieldFormData($name) |
210
|
|
|
{ |
211
|
|
|
return isset($this->formData[$name]) ? $this->formData[$name] : null; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param $field |
216
|
|
|
* @param $count |
217
|
|
|
* @param null $form |
218
|
|
|
*/ |
219
|
|
|
public function selectOptionCountForm($field, $count, $form = null) |
220
|
|
|
{ |
221
|
|
|
$form = $this->initFormName($form); |
222
|
|
|
$path = $this->getFieldFormPath($field, $form); |
|
|
|
|
223
|
|
|
|
224
|
|
|
$option = $this->getTester()->grabTextFrom($path . ' option:nth-child(' . $count . ')'); |
225
|
|
|
$this->selectOptionForm($field, $option); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param $path |
230
|
|
|
* @param null $name |
231
|
|
|
*/ |
232
|
|
|
protected function addForm($path, $name = null) |
233
|
|
|
{ |
234
|
|
|
$name = $this->initFormName($name); |
235
|
|
|
$this->initForm($name); |
|
|
|
|
236
|
|
|
$this->forms[$name]['path'] = $path; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param null $name |
241
|
|
|
* @return $this |
242
|
|
|
*/ |
243
|
|
|
protected function initForm($name = null) |
244
|
|
|
{ |
245
|
|
|
$name = $this->initFormName($name); |
246
|
|
|
if (!isset($this->forms[$name]) || !is_array($this->forms[$name])) { |
247
|
|
|
$this->forms[$name] = [ |
248
|
|
|
'path' => 'form', |
249
|
|
|
'fields' => [], |
250
|
|
|
'submit' => 'button[type=submit]', |
251
|
|
|
]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param $field |
259
|
|
|
* @param $params |
260
|
|
|
* @param null $name |
261
|
|
|
* @return $this |
262
|
|
|
*/ |
263
|
|
|
protected function addInputForm($field, $params, $name = null) |
264
|
|
|
{ |
265
|
|
|
return $this->addFieldForm($field, 'input', $params, $name); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param $field |
270
|
|
|
* @param $type |
271
|
|
|
* @param $params |
272
|
|
|
* @param null $name |
273
|
|
|
* @return $this |
274
|
|
|
*/ |
275
|
|
|
protected function addFieldForm($field, $type, $params, $name = null) |
276
|
|
|
{ |
277
|
|
|
$name = $this->initFormName($name); |
278
|
|
|
$this->initForm($name); |
|
|
|
|
279
|
|
|
|
280
|
|
|
$params = is_string($params) ? ['path' => $params] : $params; |
281
|
|
|
$params['type'] = $type; |
282
|
|
|
$this->forms[$name]['fields'][$field] = $params; |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param $field |
289
|
|
|
* @param $params |
290
|
|
|
* @param null $name |
291
|
|
|
* @return $this |
292
|
|
|
*/ |
293
|
|
|
protected function addSelectForm($field, $params, $name = null) |
294
|
|
|
{ |
295
|
|
|
return $this->addFieldForm($field, 'select', $params, $name); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param $field |
300
|
|
|
* @param $params |
301
|
|
|
* @param null $name |
302
|
|
|
* @return $this |
303
|
|
|
*/ |
304
|
|
|
protected function addRadioForm($field, $params, $name = null) |
305
|
|
|
{ |
306
|
|
|
return $this->addFieldForm($field, 'radio', $params, $name); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param $field |
311
|
|
|
* @param $params |
312
|
|
|
* @param null $name |
313
|
|
|
* @return $this |
314
|
|
|
*/ |
315
|
|
|
protected function addCheckboxForm($field, $params, $name = null) |
316
|
|
|
{ |
317
|
|
|
return $this->addFieldForm($field, 'checkbox', $params, $name); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param $field |
322
|
|
|
* @param $params |
323
|
|
|
* @param null $name |
324
|
|
|
* @return $this |
325
|
|
|
*/ |
326
|
|
|
protected function addCheckboxGroupForm($field, $params, $name = null) |
327
|
|
|
{ |
328
|
|
|
return $this->addFieldForm($field, 'checkboxGroup', $params, $name); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param $field |
333
|
|
|
* @param $params |
334
|
|
|
* @param null $name |
335
|
|
|
* @return $this |
336
|
|
|
*/ |
337
|
|
|
protected function addTextareaForm($field, $params, $name = null) |
338
|
|
|
{ |
339
|
|
|
return $this->addFieldForm($field, 'textarea', $params, $name); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.