Passed
Branch master (d2f0c3)
by Radosław
02:15
created

FieldTest::testCreatingFromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
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
            //@TODO
124
            // '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...
125
            //     ['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...
126
            //     'fvalueRangeDate'
127
            // ]
128
        ];
129
    }
130
131
132
    /**
133
     * Test if various value types are properly handled
134
     * @dataProvider rangeValueTypesProvider
135
     */
136
    public function testRangeValues($testRange, $rangeKey)
137
    {
138
        $expectedArrayKeys = [
139
            "{$rangeKey}Min",
140
            "{$rangeKey}Max"
141
        ];
142
143
        $field = new Field(2, $testRange);
144
        $this->assertArrayHasKey(
145
            $rangeKey,
146
            $field->toArray(),
147
            'key doesn\'t exist in array representation'
148
        );
149
        $this->assertSame(
150
            $field->toArray()[$rangeKey],
151
            array_combine($expectedArrayKeys, $testRange),
152
            'array representation contains wrong value'
153
        );
154
        $this->assertSame(
155
            $field->getValue(),
156
            $testRange,
157
            'getValue() returns wrong value'
158
        );
159
    }
160
161
162
    /**
163
     * Test forced value type - datetime
164
     * @return void
165
     */
166 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...
167
    {
168
        $datetime = time();
169
        $field = new Field(1, $datetime, 'fvalueDatetime');
170
        $this->assertArrayHasKey('fvalueDatetime', $field->toArray());
171
        $this->assertSame($field->toArray()['fvalueDatetime'], $datetime);
172
        $this->assertSame($field->getValue(), $datetime);
173
    }
174
175
    /**
176
     * @expectedException Radowoj\Yaah\Exception
177
     * @expectedExceptionMessage Not supported value type: object; fid=1
178
     */
179
    public function testExceptionOnInvalidValue()
180
    {
181
        $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...
182
    }
183
184
185
    /**
186
     * @expectedException Radowoj\Yaah\Exception
187
     * @expectedExceptionMessage Class Radowoj\Yaah\Field does not have property: fvalueUnicorn
188
     */
189
    public function testExceptionOnInvalidForcedValue()
190
    {
191
        $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...
192
    }
193
194
195
    /**
196
     * Test fid value
197
     * @return void
198
     */
199
    public function testFid()
200
    {
201
        $field = new Field(42, 'don\'t panic!');
202
        $this->assertSame($field->getFid(), 42);
203
    }
204
205
206
    /**
207
     * @dataProvider valueTypesProvider
208
     */
209
    public function testCreatingFromArray($value, $key)
210
    {
211
        $array = $this->defaultValues;
212
        $array[$key] = $value;
213
        $field = new Field(42);
214
        $field->fromArray($array);
215
216
        $this->assertSame($field->getValue(), $value);
217
        $this->assertSame($field->getFid(), $this->defaultValues['fid']);
218
219
    }
220
221
}
222