1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase;
|
4
|
|
|
use Radowoj\Yaah\Field;
|
5
|
|
|
|
6
|
|
|
class FieldTest extends TestCase
|
|
|
|
|
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');
|
|
|
|
|
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()
|
|
|
|
|
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()
|
|
|
|
|
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()
|
|
|
|
|
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()
|
|
|
|
|
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()
|
|
|
|
|
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']);
|
|
|
|
|
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');
|
|
|
|
|
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
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.