Passed
Push — master ( 0ffa51...bdfaa4 )
by Radosław
02:21
created

testNullReturnValueWhenAllFieldsAreDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
use Radowoj\Yaah\Field;
5
6
class FieldTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    protected $defaultValues = [
9
        'fid' => 1,
10
        'fvalueString' => '',
11
        'fvalueInt' => 0,
12
        'fvalueFloat' => 0,
13
        'fvalueImage' => '',
14
        'fvalueDatetime' => 0,
15
        'fvalueDate' => '',
16
        'fvalueRangeInt' => [
17
            'fvalueRangeIntMin' => 0,
18
            'fvalueRangeIntMax' => 0,
19
        ],
20
        'fvalueRangeFloat' => [
21
            'fvalueRangeFloatMin' => 0,
22
            'fvalueRangeFloatMax' => 0,
23
        ],
24
        'fvalueRangeDate' => [
25
            'fvalueRangeDateMin' => '',
26
            'fvalueRangeDateMax' => '',
27
        ],
28
    ];
29
30
31
    /**
32
     * @expectedException Radowoj\Yaah\Exception
33
     * @expectedExceptionMessage fid must be an integer
34
     */
35
    public function testNonIntegerFid()
36
    {
37
        new Field('some string');
38
    }
39
40
41
    /**
42
     * Tests that output of Field->toArray() has the exact keys expected by WebAPI
43
     * @return void
44
     */
45
    public function testReturnedArray()
46
    {
47
        $field = new Field(1, 'some string');
48
49
        $this->assertEquals([
50
            'fid' => 1,
51
            'fvalueString' => 'some string',
52
            'fvalueInt' => 0,
53
            'fvalueFloat' => 0,
54
            'fvalueImage' => '',
55
            'fvalueDatetime' => 0,
56
            'fvalueDate' => '',
57
            'fvalueRangeInt' => [
58
                'fvalueRangeIntMin' => 0,
59
                'fvalueRangeIntMax' => 0,
60
            ],
61
            'fvalueRangeFloat' => [
62
                'fvalueRangeFloatMin' => 0,
63
                'fvalueRangeFloatMax' => 0,
64
            ],
65
            'fvalueRangeDate' => [
66
                'fvalueRangeDateMin' => '',
67
                'fvalueRangeDateMax' => '',
68
            ],
69
70
        ], $field->toArray());
71
    }
72
73
74
    public function valueTypesProvider()
75
    {
76
        return [
77
            'string' => [
78
                'some string',
79
                'fvalueString'
80
            ],
81
            'integer' => [
82
                42,
83
                'fvalueInt'
84
            ],
85
            'float' => [
86
                13.5,
87
                'fvalueFloat'
88
            ],
89
            'date' => [
90
                '01-03-2017',
91
                'fvalueDate'
92
            ],
93
94
        ];
95
    }
96
97
98
    /**
99
     * Test if various value types are properly handled
100
     * @dataProvider valueTypesProvider
101
     */
102 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...
103
    {
104
        $field = new Field(1, $testValue);
105
        $this->assertArrayHasKey($arrayKey, $field->toArray(), 'Key not present in result array');
106
        $this->assertSame($field->toArray()[$arrayKey], $testValue, 'Different value in result array');
107
        $this->assertSame($field->getValue(), $testValue, 'Different getValue() return value');
108
    }
109
110
111
112
    public function rangeValueTypesProvider()
113
    {
114
        return [
115
            'range float' => [
116
                [10.5, 13.5],
117
                'fvalueRangeFloat'
118
            ],
119
            'range int-int' => [
120
                [10, 11],
121
                'fvalueRangeInt'
122
            ],
123
            // '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...
124
            //     ['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...
125
            //     'fvalueRangeDate'
126
            // ]
127
        ];
128
    }
129
130
131
    /**
132
     * Test if various value types are properly handled
133
     * @dataProvider rangeValueTypesProvider
134
     */
135
    public function testRangeValues($testRange, $rangeKey)
136
    {
137
        $expectedArrayKeys = [
138
            "{$rangeKey}Min",
139
            "{$rangeKey}Max"
140
        ];
141
142
        $field = new Field(2, $testRange);
143
        $this->assertArrayHasKey(
144
            $rangeKey,
145
            $field->toArray(),
146
            'key doesn\'t exist in array representation'
147
        );
148
        $this->assertSame(
149
            $field->toArray()[$rangeKey],
150
            array_combine($expectedArrayKeys, $testRange),
151
            'array representation contains wrong value'
152
        );
153
        $this->assertSame(
154
            $field->getValue(),
155
            $testRange,
156
            'getValue() returns wrong value'
157
        );
158
    }
159
160
161
    /**
162
     * Test forced value type - datetime
163
     * @return void
164
     */
165 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...
166
    {
167
        $datetime = time();
168
        $field = new Field(1, $datetime, 'fvalueDatetime');
169
        $this->assertArrayHasKey('fvalueDatetime', $field->toArray());
170
        $this->assertSame($field->toArray()['fvalueDatetime'], $datetime);
171
        $this->assertSame($field->getValue(), $datetime);
172
    }
173
174
    /**
175
     * @expectedException Radowoj\Yaah\Exception
176
     * @expectedExceptionMessage Not supported value type: object; fid=1
177
     */
178
    public function testExceptionOnInvalidValue()
179
    {
180
        $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...
181
    }
182
183
184
    /**
185
     * @expectedException Radowoj\Yaah\Exception
186
     * @expectedExceptionMessage Class Radowoj\Yaah\Field does not have property: fvalueUnicorn
187
     */
188
    public function testExceptionOnInvalidForcedValue()
189
    {
190
        $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...
191
    }
192
193
194
    /**
195
     * Test fid value
196
     * @return void
197
     */
198
    public function testFid()
199
    {
200
        $field = new Field(42, 'don\'t panic!');
201
        $this->assertSame($field->getFid(), 42);
202
    }
203
204
205
    /**
206
     * @dataProvider valueTypesProvider
207
     */
208
    public function testCreatingFromArray($value, $key)
209
    {
210
        $array = $this->defaultValues;
211
        $array[$key] = $value;
212
        $field = new Field(42);
213
        $field->fromArray($array);
214
215
        $this->assertSame($field->getValue(), $value);
216
        $this->assertSame($field->getFid(), $this->defaultValues['fid']);
217
    }
218
219
    /**
220
     * @expectedException Radowoj\Yaah\Exception
221
     * @expectedExceptionMessage Range array must have exactly 2 elements
222
     */
223
    public function testExceptionOnInvalidRangeArrayItemCount()
224
    {
225
        $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...
226
    }
227
228
229
    /**
230
     * @expectedException Radowoj\Yaah\Exception
231
     * @expectedExceptionMessage Fid is required
232
     */
233
    public function testExceptionOnFromArrayMissingFid()
234
    {
235
        $fieldArray = $this->defaultValues;
236
        unset($fieldArray['fid']);
237
        $field = new Field();
238
        $field->fromArray($fieldArray);
239
    }
240
241
242
    /**
243
     * @expectedException Radowoj\Yaah\Exception
244
     * @expectedExceptionMessage Unknown Field property: thisKeyIsInvalid
245
     */
246
    public function testExceptionOnFromArrayInvalidArrayKey()
247
    {
248
        $fieldArray = $this->defaultValues;
249
        $fieldArray['thisKeyIsInvalid'] = 'whatever';
250
        $field = new Field();
251
        $field->fromArray($fieldArray);
252
    }
253
254
255
    public function testNullReturnValueWhenAllFieldsAreDefault()
256
    {
257
        $field = new Field();
258
        $field->fromArray($this->defaultValues);
259
        $this->assertNull($field->getValue());
260
    }
261
262
263
}
264