Passed
Push — master ( bdfaa4...00ab5d )
by Radosław
02:20
created

FieldTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 259
Duplicated Lines 5.79 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 15
loc 259
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testNonIntegerFid() 0 4 1
B testReturnedArray() 0 27 1
A valueTypesProvider() 0 22 1
A testValueTypes() 7 7 1
A rangeValueTypesProvider() 0 17 1
B testRangeValues() 0 24 1
A testDatetimeValue() 8 8 1
A testExceptionOnInvalidValue() 0 4 1
A testExceptionOnInvalidForcedValue() 0 4 1
A testFid() 0 5 1
A testCreatingFromArray() 0 10 1
A testExceptionOnInvalidRangeArrayItemCount() 0 4 1
A testExceptionOnFromArrayMissingFid() 0 7 1
A testExceptionOnFromArrayInvalidArrayKey() 0 7 1
A testNullReturnValueWhenAllFieldsAreDefault() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Radowoj\Yaah;
4
5
use PHPUnit\Framework\TestCase;
6
use Radowoj\Yaah\Field;
7
8
class FieldTest extends TestCase
9
{
10
    protected $defaultValues = [
11
        'fid' => 1,
12
        'fvalueString' => '',
13
        'fvalueInt' => 0,
14
        'fvalueFloat' => 0,
15
        'fvalueImage' => '',
16
        'fvalueDatetime' => 0,
17
        'fvalueDate' => '',
18
        'fvalueRangeInt' => [
19
            'fvalueRangeIntMin' => 0,
20
            'fvalueRangeIntMax' => 0,
21
        ],
22
        'fvalueRangeFloat' => [
23
            'fvalueRangeFloatMin' => 0,
24
            'fvalueRangeFloatMax' => 0,
25
        ],
26
        'fvalueRangeDate' => [
27
            'fvalueRangeDateMin' => '',
28
            'fvalueRangeDateMax' => '',
29
        ],
30
    ];
31
32
33
    /**
34
     * @expectedException InvalidArgumentException
35
     * @expectedExceptionMessage fid must be an integer
36
     */
37
    public function testNonIntegerFid()
38
    {
39
        new Field('some string');
40
    }
41
42
43
    /**
44
     * Tests that output of Field->toArray() has the exact keys expected by WebAPI
45
     * @return void
46
     */
47
    public function testReturnedArray()
48
    {
49
        $field = new Field(1, 'some string');
50
51
        $this->assertEquals([
52
            'fid' => 1,
53
            'fvalueString' => 'some string',
54
            'fvalueInt' => 0,
55
            'fvalueFloat' => 0,
56
            'fvalueImage' => '',
57
            'fvalueDatetime' => 0,
58
            'fvalueDate' => '',
59
            'fvalueRangeInt' => [
60
                'fvalueRangeIntMin' => 0,
61
                'fvalueRangeIntMax' => 0,
62
            ],
63
            'fvalueRangeFloat' => [
64
                'fvalueRangeFloatMin' => 0,
65
                'fvalueRangeFloatMax' => 0,
66
            ],
67
            'fvalueRangeDate' => [
68
                'fvalueRangeDateMin' => '',
69
                'fvalueRangeDateMax' => '',
70
            ],
71
72
        ], $field->toArray());
73
    }
74
75
76
    public function valueTypesProvider()
77
    {
78
        return [
79
            'string' => [
80
                'some string',
81
                'fvalueString'
82
            ],
83
            'integer' => [
84
                42,
85
                'fvalueInt'
86
            ],
87
            'float' => [
88
                13.5,
89
                'fvalueFloat'
90
            ],
91
            'date' => [
92
                '01-03-2017',
93
                'fvalueDate'
94
            ],
95
96
        ];
97
    }
98
99
100
    /**
101
     * Test if various value types are properly handled
102
     * @dataProvider valueTypesProvider
103
     */
104 View Code Duplication
    public function testValueTypes($testValue, $arrayKey)
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...
105
    {
106
        $field = new Field(1, $testValue);
107
        $this->assertArrayHasKey($arrayKey, $field->toArray(), 'Key not present in result array');
108
        $this->assertSame($field->toArray()[$arrayKey], $testValue, 'Different value in result array');
109
        $this->assertSame($field->getValue(), $testValue, 'Different getValue() return value');
110
    }
111
112
113
114
    public function rangeValueTypesProvider()
115
    {
116
        return [
117
            'range float' => [
118
                [10.5, 13.5],
119
                'fvalueRangeFloat'
120
            ],
121
            'range int-int' => [
122
                [10, 11],
123
                'fvalueRangeInt'
124
            ],
125
            // 'date range' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
126
            //     ['01-03-2017', '03-03-2017'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127
            //     'fvalueRangeDate'
128
            // ]
129
        ];
130
    }
131
132
133
    /**
134
     * Test if various value types are properly handled
135
     * @dataProvider rangeValueTypesProvider
136
     */
137
    public function testRangeValues($testRange, $rangeKey)
138
    {
139
        $expectedArrayKeys = [
140
            "{$rangeKey}Min",
141
            "{$rangeKey}Max"
142
        ];
143
144
        $field = new Field(2, $testRange);
145
        $this->assertArrayHasKey(
146
            $rangeKey,
147
            $field->toArray(),
148
            'key doesn\'t exist in array representation'
149
        );
150
        $this->assertSame(
151
            $field->toArray()[$rangeKey],
152
            array_combine($expectedArrayKeys, $testRange),
153
            'array representation contains wrong value'
154
        );
155
        $this->assertSame(
156
            $field->getValue(),
157
            $testRange,
158
            'getValue() returns wrong value'
159
        );
160
    }
161
162
163
    /**
164
     * Test forced value type - datetime
165
     * @return void
166
     */
167 View Code Duplication
    public function testDatetimeValue()
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...
168
    {
169
        $datetime = time();
170
        $field = new Field(1, $datetime, 'fvalueDatetime');
171
        $this->assertArrayHasKey('fvalueDatetime', $field->toArray());
172
        $this->assertSame($field->toArray()['fvalueDatetime'], $datetime);
173
        $this->assertSame($field->getValue(), $datetime);
174
    }
175
176
177
    /**
178
     * @expectedException InvalidArgumentException
179
     * @expectedExceptionMessage Not supported value type: object; fid=1
180
     */
181
    public function testExceptionOnInvalidValue()
182
    {
183
        $field = new Field(1, (object)['foo' => 'bar']);
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
184
    }
185
186
187
    /**
188
     * @expectedException InvalidArgumentException
189
     * @expectedExceptionMessage Class Radowoj\Yaah\Field does not have property: fvalueUnicorn
190
     */
191
    public function testExceptionOnInvalidForcedValue()
192
    {
193
        $field = new Field(1, 'something', 'fvalueUnicorn');
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
194
    }
195
196
197
    /**
198
     * Test fid value
199
     * @return void
200
     */
201
    public function testFid()
202
    {
203
        $field = new Field(42, 'don\'t panic!');
204
        $this->assertSame($field->getFid(), 42);
205
    }
206
207
208
    /**
209
     * @dataProvider valueTypesProvider
210
     */
211
    public function testCreatingFromArray($value, $key)
212
    {
213
        $array = $this->defaultValues;
214
        $array[$key] = $value;
215
        $field = new Field(42);
216
        $field->fromArray($array);
217
218
        $this->assertSame($field->getValue(), $value);
219
        $this->assertSame($field->getFid(), $this->defaultValues['fid']);
220
    }
221
222
    /**
223
     * @expectedException InvalidArgumentException
224
     * @expectedExceptionMessage Range array must have exactly 2 elements
225
     */
226
    public function testExceptionOnInvalidRangeArrayItemCount()
227
    {
228
        $field = new Field(12, [1, 2, 3]);
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
229
    }
230
231
232
    /**
233
     * @expectedException InvalidArgumentException
234
     * @expectedExceptionMessage Fid is required
235
     */
236
    public function testExceptionOnFromArrayMissingFid()
237
    {
238
        $fieldArray = $this->defaultValues;
239
        unset($fieldArray['fid']);
240
        $field = new Field();
241
        $field->fromArray($fieldArray);
242
    }
243
244
245
    /**
246
     * @expectedException InvalidArgumentException
247
     * @expectedExceptionMessage Unknown Field property: thisKeyIsInvalid
248
     */
249
    public function testExceptionOnFromArrayInvalidArrayKey()
250
    {
251
        $fieldArray = $this->defaultValues;
252
        $fieldArray['thisKeyIsInvalid'] = 'whatever';
253
        $field = new Field();
254
        $field->fromArray($fieldArray);
255
    }
256
257
258
    public function testNullReturnValueWhenAllFieldsAreDefault()
259
    {
260
        $field = new Field();
261
        $field->fromArray($this->defaultValues);
262
        $this->assertNull($field->getValue());
263
    }
264
265
266
}
267