Passed
Push — master ( a5c74c...eed2cc )
by Radosław
02:38
created

FieldTest::testExceptionOnInvalidValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
    /**
10
     * @expectedException Radowoj\Yaah\Exception
11
     * @expectedExceptionMessage fid must be an integer
12
     */
13
    public function testNonIntegerFid()
14
    {
15
        $field = new Field('some string');
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...
16
    }
17
18
19
    /**
20
     * Tests that output of Field->toArray() has the exact keys expected by WebAPI
21
     * @return void
22
     */
23
    public function testReturnedArray()
24
    {
25
        $field = new Field(1, 'some string');
26
27
        $this->assertEquals([
28
            'fid' => 1,
29
            'fvalueString' => 'some string',
30
            'fvalueInt' => 0,
31
            'fvalueFloat' => 0,
32
            'fvalueImage' => '',
33
            'fvalueDatetime' => 0,
34
            'fvalueDate' => '',
35
            'fvalueRangeInt' => [
36
                'fvalueRangeIntMin' => 0,
37
                'fvalueRangeIntMax' => 0,
38
            ],
39
            'fvalueRangeFloat' => [
40
                'fvalueRangeFloatMin' => 0,
41
                'fvalueRangeFloatMax' => 0,
42
            ],
43
            'fvalueRangeDate' => [
44
                'fvalueRangeDateMin' => '',
45
                'fvalueRangeDateMax' => '',
46
            ],
47
48
        ], $field->toArray());
49
    }
50
51
52 View Code Duplication
    public function testStringValue()
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...
53
    {
54
        $string = 'lorem ipsum';
55
        $field = new Field(1, $string);
56
        $this->assertArrayHasKey('fvalueString', $field->toArray());
57
        $this->assertSame($field->toArray()['fvalueString'], $string);
58
        $this->assertSame($field->getValue(), $string);
59
60
    }
61
62
    /**
63
     * Tests integer value representation
64
     * @return void
65
     */
66 View Code Duplication
    public function testIntegerValue()
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...
67
    {
68
        $int = 10;
69
        $field = new Field(1, $int);
70
        $this->assertArrayHasKey('fvalueInt', $field->toArray());
71
        $this->assertSame($field->toArray()['fvalueInt'], $int);
72
        $this->assertSame($field->getValue(), $int);
73
    }
74
75
76
    /**
77
     * Tests float value representation
78
     * @return void
79
     */
80 View Code Duplication
    public function testFloatValue()
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...
81
    {
82
        $float = 13.5;
83
        $field = new Field(1, $float);
84
        $this->assertArrayHasKey('fvalueFloat', $field->toArray());
85
        $this->assertSame($field->toArray()['fvalueFloat'], $float);
86
        $this->assertSame($field->getValue(), $float);
87
    }
88
89
90
    /**
91
     * Tests date value representation
92
     * @return void
93
     */
94 View Code Duplication
    public function testDateValue()
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...
95
    {
96
        $date = '01-03-2017';
97
        $field = new Field(1, $date);
98
        $this->assertArrayHasKey('fvalueDate', $field->toArray());
99
        $this->assertSame($field->toArray()['fvalueDate'], $date);
100
        $this->assertSame($field->getValue(), $date);
101
    }
102
103
104
    /**
105
     * Test forced value type - datetime
106
     * @return void
107
     */
108 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...
109
    {
110
        $datetime = time();
111
        $field = new Field(1, $datetime, 'fvalueDatetime');
112
        $this->assertArrayHasKey('fvalueDatetime', $field->toArray());
113
        $this->assertSame($field->toArray()['fvalueDatetime'], $datetime);
114
        $this->assertSame($field->getValue(), $datetime);
115
    }
116
117
    /**
118
     * @expectedException Radowoj\Yaah\Exception
119
     * @expectedExceptionMessage Not supported value type: object; fid=1
120
     */
121
    public function testExceptionOnInvalidValue()
122
    {
123
        $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...
124
    }
125
126
127
    /**
128
     * @expectedException Radowoj\Yaah\Exception
129
     * @expectedExceptionMessage Class Radowoj\Yaah\Field does not have property: fvalueUnicorn
130
     */
131
    public function testExceptionOnInvalidForcedValue()
132
    {
133
        $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...
134
    }
135
136
137
    /**
138
     * Test fid value
139
     * @return void
140
     */
141
    public function testFid()
142
    {
143
        $field = new Field(42, 'don\'t panic!');
144
        $this->assertSame($field->getFid(), 42);
145
    }
146
147
}
148