FormTrait::addSelectForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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;
0 ignored issues
show
Documentation introduced by
The doc-type AcceptanceTrait|\Codeception\Actor; could not be parsed: Expected "|" or "end of type", but got ";" at position 34. (view supported doc-types)

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        $form = $this->initFormName($form);
123
        $path = $this->getFieldFormPath($field, $form);
0 ignored issues
show
Bug introduced by
It seems like $form defined by $this->initFormName($form) on line 122 can also be of type string; however, ByTIC\Codeception\Page\A...ait::getFieldFormPath() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $form defined by $this->initFormName($form) on line 145 can also be of type string; however, ByTIC\Codeception\Page\A...ait::getFieldFormPath() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $form = $this->initFormName($form);
160
        $path = $this->getFieldFormPath($field, $form);
0 ignored issues
show
Bug introduced by
It seems like $form defined by $this->initFormName($form) on line 159 can also be of type string; however, ByTIC\Codeception\Page\A...ait::getFieldFormPath() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $this->getFieldFormData($name) (which targets ByTIC\Codeception\Page\A...ait::getFieldFormData()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $form defined by $this->initFormName($form) on line 221 can also be of type string; however, ByTIC\Codeception\Page\A...ait::getFieldFormPath() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $name defined by $this->initFormName($name) on line 234 can also be of type string; however, ByTIC\Codeception\Page\A...s\FormTrait::initForm() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $name defined by $this->initFormName($name) on line 277 can also be of type string; however, ByTIC\Codeception\Page\A...s\FormTrait::initForm() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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