Passed
Push — master ( 804000...66add2 )
by Radosław
02:28
created

FieldTest::testRangeValues()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 17
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
    /**
9
     * @expectedException Radowoj\Yaah\Exception
10
     * @expectedExceptionMessage fid must be an integer
11
     */
12
    public function testNonIntegerFid()
13
    {
14
        new Field('some string');
15
    }
16
17
18
    /**
19
     * Tests that output of Field->toArray() has the exact keys expected by WebAPI
20
     * @return void
21
     */
22
    public function testReturnedArray()
23
    {
24
        $field = new Field(1, 'some string');
25
26
        $this->assertEquals([
27
            'fid' => 1,
28
            'fvalueString' => 'some string',
29
            'fvalueInt' => 0,
30
            'fvalueFloat' => 0,
31
            'fvalueImage' => '',
32
            'fvalueDatetime' => 0,
33
            'fvalueDate' => '',
34
            'fvalueRangeInt' => [
35
                'fvalueRangeIntMin' => 0,
36
                'fvalueRangeIntMax' => 0,
37
            ],
38
            'fvalueRangeFloat' => [
39
                'fvalueRangeFloatMin' => 0,
40
                'fvalueRangeFloatMax' => 0,
41
            ],
42
            'fvalueRangeDate' => [
43
                'fvalueRangeDateMin' => '',
44
                'fvalueRangeDateMax' => '',
45
            ],
46
47
        ], $field->toArray());
48
    }
49
50
51
    public function valueTypesProvider()
52
    {
53
        return [
54
            'string' => [
55
                'some string',
56
                'fvalueString'
57
            ],
58
            'integer' => [
59
                42,
60
                'fvalueInt'
61
            ],
62
            'float' => [
63
                13.5,
64
                'fvalueFloat'
65
            ],
66
            'date' => [
67
                '01-03-2017',
68
                'fvalueDate'
69
            ],
70
71
        ];
72
    }
73
74
75
    /**
76
     * Test if various value types are properly handled
77
     * @dataProvider valueTypesProvider
78
     */
79
    public function testValueTypes($testValue, $arrayKey)
80
    {
81
        $field = new Field(1, $testValue);
82
        $this->assertArrayHasKey($arrayKey, $field->toArray());
83
        $this->assertSame($field->toArray()[$arrayKey], $testValue);
84
        $this->assertSame($field->getValue(), $testValue);
85
    }
86
87
88
89
    public function rangeValueTypesProvider()
90
    {
91
        return [
92
            'range float' => [
93
                [10.5, 13.5],
94
                'fvalueRangeFloat'
95
            ],
96
            'range int-int' => [
97
                [10, 11],
98
                'fvalueRangeInt'
99
            ],
100
            //@TODO
101
            // '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...
102
            //     ['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...
103
            //     'fvalueRangeDate'
104
            // ]
105
        ];
106
    }
107
108
109
    /**
110
     * Test if various value types are properly handled
111
     * @dataProvider rangeValueTypesProvider
112
     */
113
    public function testRangeValues($testRange, $rangeKey)
114
    {
115
        $expectedArrayKeys = [
116
            "{$rangeKey}Min",
117
            "{$rangeKey}Max"
118
        ];
119
120
        $field = new Field(2, $testRange);
121
        $this->assertArrayHasKey(
122
            $rangeKey,
123
            $field->toArray(),
124
            'key doesn\'t exist in array representation'
125
        );
126
        $this->assertSame(
127
            $field->toArray()[$rangeKey],
128
            array_combine($expectedArrayKeys, $testRange),
129
            'array representation contains wrong value'
130
        );
131
        $this->assertSame(
132
            $field->getValue(),
133
            $testRange,
134
            'getValue() returns wrong value'
135
        );
136
    }
137
138
139
    /**
140
     * Test forced value type - datetime
141
     * @return void
142
     */
143
    public function testDatetimeValue()
144
    {
145
        $datetime = time();
146
        $field = new Field(1, $datetime, 'fvalueDatetime');
147
        $this->assertArrayHasKey('fvalueDatetime', $field->toArray());
148
        $this->assertSame($field->toArray()['fvalueDatetime'], $datetime);
149
        $this->assertSame($field->getValue(), $datetime);
150
    }
151
152
    /**
153
     * @expectedException Radowoj\Yaah\Exception
154
     * @expectedExceptionMessage Not supported value type: object; fid=1
155
     */
156
    public function testExceptionOnInvalidValue()
157
    {
158
        $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...
159
    }
160
161
162
    /**
163
     * @expectedException Radowoj\Yaah\Exception
164
     * @expectedExceptionMessage Class Radowoj\Yaah\Field does not have property: fvalueUnicorn
165
     */
166
    public function testExceptionOnInvalidForcedValue()
167
    {
168
        $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...
169
    }
170
171
172
    /**
173
     * Test fid value
174
     * @return void
175
     */
176
    public function testFid()
177
    {
178
        $field = new Field(42, 'don\'t panic!');
179
        $this->assertSame($field->getFid(), 42);
180
    }
181
182
}
183