Test Setup Failed
Push — master ( 463f80...c01e84 )
by Gabriel
04:52
created

FormTrait::getFormData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
nc 1
cc 1
nop 1
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;
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...
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)
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...
122
    {
123
        $form = $this->initFormName($form);
124
        $path = $this->getFieldFormPath($field, $form);
0 ignored issues
show
Bug introduced by
It seems like $form defined by $this->initFormName($form) on line 123 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...
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);
0 ignored issues
show
Bug introduced by
It seems like $form defined by $this->initFormName($form) on line 146 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...
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)
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...
159
    {
160
        $form = $this->initFormName($form);
161
        $path = $this->getFieldFormPath($field, $form);
0 ignored issues
show
Bug introduced by
It seems like $form defined by $this->initFormName($form) on line 160 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...
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)
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...
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);
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...
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);
0 ignored issues
show
Bug introduced by
It seems like $form defined by $this->initFormName($form) on line 222 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...
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);
0 ignored issues
show
Bug introduced by
It seems like $name defined by $this->initFormName($name) on line 235 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...
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);
0 ignored issues
show
Bug introduced by
It seems like $name defined by $this->initFormName($name) on line 278 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...
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