Passed
Branch master (804000)
by Radosław
02:22
created

FieldTest::valueTypesProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
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
    /**
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' => ['some string', 'fvalueString'],
55
            'integer' => [42, 'fvalueInt'],
56
            'float' => [13.5, 'fvalueFloat'],
57
            'date' => ['01-03-2017', 'fvalueDate'],
58
        ];
59
    }
60
61
62
    /**
63
     * Test if various value types are properly handled
64
     * @dataProvider valueTypesProvider
65
     */
66
    public function testValueTypes($testValue, $arrayKey)
67
    {
68
        $field = new Field(1, $testValue);
69
        $this->assertArrayHasKey($arrayKey, $field->toArray());
70
        $this->assertSame($field->toArray()[$arrayKey], $testValue);
71
        $this->assertSame($field->getValue(), $testValue);
72
    }
73
74
75
    /**
76
     * Test forced value type - datetime
77
     * @return void
78
     */
79
    public function testDatetimeValue()
80
    {
81
        $datetime = time();
82
        $field = new Field(1, $datetime, 'fvalueDatetime');
83
        $this->assertArrayHasKey('fvalueDatetime', $field->toArray());
84
        $this->assertSame($field->toArray()['fvalueDatetime'], $datetime);
85
        $this->assertSame($field->getValue(), $datetime);
86
    }
87
88
    /**
89
     * @expectedException Radowoj\Yaah\Exception
90
     * @expectedExceptionMessage Not supported value type: object; fid=1
91
     */
92
    public function testExceptionOnInvalidValue()
93
    {
94
        $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...
95
    }
96
97
98
    /**
99
     * @expectedException Radowoj\Yaah\Exception
100
     * @expectedExceptionMessage Class Radowoj\Yaah\Field does not have property: fvalueUnicorn
101
     */
102
    public function testExceptionOnInvalidForcedValue()
103
    {
104
        $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...
105
    }
106
107
108
    /**
109
     * Test fid value
110
     * @return void
111
     */
112
    public function testFid()
113
    {
114
        $field = new Field(42, 'don\'t panic!');
115
        $this->assertSame($field->getFid(), 42);
116
    }
117
118
}
119