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