Completed
Push — 3.7 ( 11b87a...bb5701 )
by
unknown
09:09 queued 01:14
created

DBFieldTest::testPrepValueForDBArrayValue()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 3
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * Tests for DBField objects.
6
 * @package framework
7
 * @subpackage tests
8
 *
9
 */
10
class DBFieldTest extends SapphireTest {
11
12
	/**
13
	 * Test the nullValue() method on DBField.
14
	 */
15
	public function testNullValue() {
16
		/* Float and Double use 0 for "null" value representation */
17
		$this->assertEquals(0, singleton('Float')->nullValue());
18
		$this->assertEquals(0, singleton('Double')->nullValue());
19
	}
20
21
	/**
22
	 * Test the prepValueForDB() method on DBField.
23
	 */
24
	public function testPrepValueForDB() {
25
		$db = DB::get_conn();
26
27
		/* Float behaviour, asserting we have 0 */
28
		$this->assertEquals(0, singleton('Float')->prepValueForDB(0));
29
		$this->assertEquals(0, singleton('Float')->prepValueForDB(null));
30
		$this->assertEquals(0, singleton('Float')->prepValueForDB(false));
31
		$this->assertEquals(0, singleton('Float')->prepValueForDB(''));
32
		$this->assertEquals('0', singleton('Float')->prepValueForDB('0'));
33
34
		/* Double behaviour, asserting we have 0 */
35
		$this->assertEquals(0, singleton('Double')->prepValueForDB(0));
36
		$this->assertEquals(0, singleton('Double')->prepValueForDB(null));
37
		$this->assertEquals(0, singleton('Double')->prepValueForDB(false));
38
		$this->assertEquals(0, singleton('Double')->prepValueForDB(''));
39
		$this->assertEquals('0', singleton('Double')->prepValueForDB('0'));
40
41
		/* Integer behaviour, asserting we have 0 */
42
		$this->assertEquals(0, singleton('Int')->prepValueForDB(0));
43
		$this->assertEquals(0, singleton('Int')->prepValueForDB(null));
44
		$this->assertEquals(0, singleton('Int')->prepValueForDB(false));
45
		$this->assertEquals(0, singleton('Int')->prepValueForDB(''));
46
		$this->assertEquals('0', singleton('Int')->prepValueForDB('0'));
47
48
		/* Integer behaviour, asserting we have 1 */
49
		$this->assertEquals(1, singleton('Int')->prepValueForDB(true));
50
		$this->assertEquals(1, singleton('Int')->prepValueForDB(1));
51
		$this->assertEquals('1', singleton('Int')->prepValueForDB('1'));
52
53
		/* Decimal behaviour, asserting we have 0 */
54
		$this->assertEquals(0, singleton('Decimal')->prepValueForDB(0));
55
		$this->assertEquals(0, singleton('Decimal')->prepValueForDB(null));
56
		$this->assertEquals(0, singleton('Decimal')->prepValueForDB(false));
57
		$this->assertEquals(0, singleton('Decimal')->prepValueForDB(''));
58
		$this->assertEquals('0', singleton('Decimal')->prepValueForDB('0'));
59
60
		/* Decimal behaviour, asserting we have 1 */
61
		$this->assertEquals(1, singleton('Decimal')->prepValueForDB(true));
62
		$this->assertEquals(1, singleton('Decimal')->prepValueForDB(1));
63
		$this->assertEquals('1', singleton('Decimal')->prepValueForDB('1'));
64
65
		/* Boolean behaviour, asserting we have 0 */
66
		$this->assertEquals(false, singleton('Boolean')->prepValueForDB(0));
67
		$this->assertEquals(false, singleton('Boolean')->prepValueForDB(null));
68
		$this->assertEquals(false, singleton('Boolean')->prepValueForDB(false));
69
		$this->assertEquals(false, singleton('Boolean')->prepValueForDB('false'));
70
		$this->assertEquals(false, singleton('Boolean')->prepValueForDB('f'));
71
		$this->assertEquals(false, singleton('Boolean')->prepValueForDB(''));
72
		$this->assertEquals(false, singleton('Boolean')->prepValueForDB('0'));
73
74
		/* Boolean behaviour, asserting we have 1 */
75
		$this->assertEquals(true, singleton('Boolean')->prepValueForDB(true));
76
		$this->assertEquals(true, singleton('Boolean')->prepValueForDB('true'));
77
		$this->assertEquals(true, singleton('Boolean')->prepValueForDB('t'));
78
		$this->assertEquals(true, singleton('Boolean')->prepValueForDB(1));
79
		$this->assertEquals(true, singleton('Boolean')->prepValueForDB('1'));
80
81
		// @todo - Revisit Varchar to evaluate correct behaviour of nullifyEmpty
82
83
		/* Varchar behaviour */
84
		$this->assertEquals(0, singleton('Varchar')->prepValueForDB(0));
85
		$this->assertEquals(null, singleton('Varchar')->prepValueForDB(null));
86
		$this->assertEquals(null, singleton('Varchar')->prepValueForDB(false));
87
		$this->assertEquals(null, singleton('Varchar')->prepValueForDB(''));
88
		$this->assertEquals('0', singleton('Varchar')->prepValueForDB('0'));
89
		$this->assertEquals(1, singleton('Varchar')->prepValueForDB(1));
90
		$this->assertEquals(true, singleton('Varchar')->prepValueForDB(true));
91
		$this->assertEquals('1', singleton('Varchar')->prepValueForDB('1'));
92
		$this->assertEquals('00000', singleton('Varchar')->prepValueForDB('00000'));
93
		$this->assertEquals(0, singleton('Varchar')->prepValueForDB(0000));
94
		$this->assertEquals('test', singleton('Varchar')->prepValueForDB('test'));
95
		$this->assertEquals(123, singleton('Varchar')->prepValueForDB(123));
96
97
		/* AllowEmpty Varchar behaviour */
98
		$varcharField = new Varchar("testfield", 50, array("nullifyEmpty"=>false));
99
		$this->assertSame(0, $varcharField->prepValueForDB(0));
100
		$this->assertSame(null, $varcharField->prepValueForDB(null));
101
		$this->assertSame(null, $varcharField->prepValueForDB(false));
102
		$this->assertSame('', $varcharField->prepValueForDB(''));
103
		$this->assertSame('0', $varcharField->prepValueForDB('0'));
104
		$this->assertSame(1, $varcharField->prepValueForDB(1));
105
		$this->assertSame(true, $varcharField->prepValueForDB(true));
106
		$this->assertSame('1', $varcharField->prepValueForDB('1'));
107
		$this->assertSame('00000', $varcharField->prepValueForDB('00000'));
108
		$this->assertSame(0, $varcharField->prepValueForDB(0000));
109
		$this->assertSame('test', $varcharField->prepValueForDB('test'));
110
		$this->assertSame(123, $varcharField->prepValueForDB(123));
111
		unset($varcharField);
112
113
		/* Text behaviour */
114
		$this->assertEquals(0, singleton('Text')->prepValueForDB(0));
115
		$this->assertEquals(null, singleton('Text')->prepValueForDB(null));
116
		$this->assertEquals(null, singleton('Text')->prepValueForDB(false));
117
		$this->assertEquals(null, singleton('Text')->prepValueForDB(''));
118
		$this->assertEquals('0', singleton('Text')->prepValueForDB('0'));
119
		$this->assertEquals(1, singleton('Text')->prepValueForDB(1));
120
		$this->assertEquals(true, singleton('Text')->prepValueForDB(true));
121
		$this->assertEquals('1', singleton('Text')->prepValueForDB('1'));
122
		$this->assertEquals('00000', singleton('Text')->prepValueForDB('00000'));
123
		$this->assertEquals(0, singleton('Text')->prepValueForDB(0000));
124
		$this->assertEquals('test', singleton('Text')->prepValueForDB('test'));
125
		$this->assertEquals(123, singleton('Text')->prepValueForDB(123));
126
127
		/* AllowEmpty Text behaviour */
128
		$textField = new Text("testfield", array("nullifyEmpty"=>false));
129
		$this->assertSame(0, $textField->prepValueForDB(0));
130
		$this->assertSame(null, $textField->prepValueForDB(null));
131
		$this->assertSame(null, $textField->prepValueForDB(false));
132
		$this->assertSame('', $textField->prepValueForDB(''));
133
		$this->assertSame('0', $textField->prepValueForDB('0'));
134
		$this->assertSame(1, $textField->prepValueForDB(1));
135
		$this->assertSame(true, $textField->prepValueForDB(true));
136
		$this->assertSame('1', $textField->prepValueForDB('1'));
137
		$this->assertSame('00000', $textField->prepValueForDB('00000'));
138
		$this->assertSame(0, $textField->prepValueForDB(0000));
139
		$this->assertSame('test', $textField->prepValueForDB('test'));
140
		$this->assertSame(123, $textField->prepValueForDB(123));
141
		unset($textField);
142
143
		/* Time behaviour */
144
		$time = singleton('Time');
145
		$time->setValue('00:01am');
146
		$this->assertEquals("00:01:00", $time->getValue());
147
		$time->setValue('00:59am');
148
		$this->assertEquals("00:59:00", $time->getValue());
149
		$time->setValue('11:59am');
150
		$this->assertEquals("11:59:00", $time->getValue());
151
		$time->setValue('12:00pm');
152
		$this->assertEquals("12:00:00", $time->getValue());
153
		$time->setValue('12:59am');
154
		$this->assertEquals("12:59:00", $time->getValue());
155
		$time->setValue('1:00pm');
156
		$this->assertEquals("13:00:00", $time->getValue());
157
		$time->setValue('11:59pm');
158
		$this->assertEquals("23:59:00", $time->getValue());
159
		$time->setValue('00:00am');
160
		$this->assertEquals("00:00:00", $time->getValue());
161
		$time->setValue('00:00:00');
162
		$this->assertEquals("00:00:00", $time->getValue());
163
164
		/* BigInt behaviour */
165
		$bigInt = singleton('BigInt');
166
		$bigInt->setValue(PHP_INT_MAX);
167
		$this->assertEquals(PHP_INT_MAX, $bigInt->getValue());
168
	}
169
170
    /**
171
     * @dataProvider dataProviderPrepValueForDBArrayValue
172
     */
173
    public function testPrepValueForDBArrayValue($dbFieldName, $scalarValueOnly, $extraArgs = array())
174
    {
175
        $reflection = new ReflectionClass($dbFieldName);
176
        /** @var DBField $dbField  */
177
        $dbField = $reflection->newInstanceArgs($extraArgs);
178
        $dbField->setName('SomeField');
179
        $payload = array('GREATEST(0,?)' => '2');
180
        $preparedValue = $dbField->prepValueForDB($payload);
181
        $this->assertTrue(
182
            !$scalarValueOnly || !is_array($preparedValue),
183
            '`prepValueForDB` can not return an array if scalarValueOnly is true'
184
        );
185
        $this->assertEquals($scalarValueOnly, $dbField->scalarValueOnly());
186
    }
187
188
    public function dataProviderPrepValueForDBArrayValue()
189
    {
190
        return array(
191
            array('BigInt', true),
192
            array('Boolean', true),
193
            array('Currency', true),
194
            array('Date', true),
195
            array('SS_Datetime', true),
196
            array('DBLocale', true),
197
            array('Decimal', true),
198
            array('Double', true),
199
            array('Enum', true),
200
            array('Float', true),
201
            array('ForeignKey', true, array('SomeField')),
202
            array('HTMLText', true),
203
            array('HTMLVarchar', true),
204
            array('Int', true),
205
            array('Money', false),
206
            array('MultiEnum', true, array('SomeField', array('One', 'Two', 'Three'))),
207
            array('Percentage', true),
208
            array('PolymorphicForeignKey', false, array('SomeField')),
209
            array('PrimaryKey', true, array('SomeField', singleton('Image'))),
210
            array('Text', true),
211
            array('Time', true),
212
            array('Varchar', true),
213
            array('Year', true),
214
        );
215
    }
216
217
	public function testExists() {
218
		$varcharField = new Varchar("testfield");
219
		$this->assertTrue($varcharField->getNullifyEmpty());
220
		$varcharField->setValue('abc');
221
		$this->assertTrue($varcharField->exists());
222
		$varcharField->setValue('');
223
		$this->assertFalse($varcharField->exists());
224
		$varcharField->setValue(null);
225
		$this->assertFalse($varcharField->exists());
226
227
		$varcharField = new Varchar("testfield", 50, array('nullifyEmpty'=>false));
228
		$this->assertFalse($varcharField->getNullifyEmpty());
229
		$varcharField->setValue('abc');
230
		$this->assertTrue($varcharField->exists());
231
		$varcharField->setValue('');
232
		$this->assertTrue($varcharField->exists());
233
		$varcharField->setValue(null);
234
		$this->assertFalse($varcharField->exists());
235
236
		$textField = new Text("testfield");
237
		$this->assertTrue($textField->getNullifyEmpty());
238
		$textField->setValue('abc');
239
		$this->assertTrue($textField->exists());
240
		$textField->setValue('');
241
		$this->assertFalse($textField->exists());
242
		$textField->setValue(null);
243
		$this->assertFalse($textField->exists());
244
245
		$textField = new Text("testfield", array('nullifyEmpty'=>false));
246
		$this->assertFalse($textField->getNullifyEmpty());
247
		$textField->setValue('abc');
248
		$this->assertTrue($textField->exists());
249
		$textField->setValue('');
250
		$this->assertTrue($textField->exists());
251
		$textField->setValue(null);
252
		$this->assertFalse($textField->exists());
253
	}
254
255
	public function testStringFieldsWithMultibyteData() {
256
		$plainFields = array('Varchar', 'Text');
257
		$htmlFields = array('HTMLVarchar', 'HTMLText');
258
		$allFields = array_merge($plainFields, $htmlFields);
259
260
		$value = 'üåäöÜÅÄÖ';
261
		foreach ($allFields as $stringField) {
262
			$stringField = DBField::create_field($stringField, $value);
263
			for ($i = 1; $i < mb_strlen($value); $i++) {
264
				$expected = mb_substr($value, 0, $i) . '...';
265
				$this->assertEquals($expected, $stringField->LimitCharacters($i));
266
			}
267
		}
268
269
		$value = '<p>üåäö&amp;ÜÅÄÖ</p>';
270
		foreach ($htmlFields as $stringField) {
271
			$stringField = DBField::create_field($stringField, $value);
272
			$this->assertEquals('üåäö&amp;ÜÅÄ...', $stringField->LimitCharacters(8));
273
		}
274
275
		$this->assertEquals('ÅÄÖ', DBField::create_field('Text', 'åäö')->UpperCase());
276
		$this->assertEquals('åäö', DBField::create_field('Text', 'ÅÄÖ')->LowerCase());
277
	}
278
279
	public function testIntFloatPhp5Behaviour() {
280
		if (PHP_MAJOR_VERSION < 7) {
281
			// PHP 5 - Int class exists and is an instance of DBInt
282
			// Can't use the reserved words for these classes or we'll get a compile error on PHP7
283
			$classname = "int";
284
			$obj = new $classname();
285
			$this->assertInstanceOf('DBInt', $obj);
286
287
			$classname = "float";
288
			$obj = new $classname();
289
			$this->assertInstanceOf('DBFloat', $obj);
290
291
		} else {
292
			// PHP 7 - classes don't exist
293
			$this->assertFalse(class_exists("Int"));
294
			$this->assertFalse(class_exists("Float"));
295
		}
296
297
	}
298
}
299
300
301