Completed
Pull Request — 3.6 (#7850)
by Jono
07:19
created

DBFieldTest::testIntFloatPhp5Behaviour()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 19
rs 9.4285
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
	public function testExists() {
171
		$varcharField = new Varchar("testfield");
172
		$this->assertTrue($varcharField->getNullifyEmpty());
173
		$varcharField->setValue('abc');
174
		$this->assertTrue($varcharField->exists());
175
		$varcharField->setValue('');
176
		$this->assertFalse($varcharField->exists());
177
		$varcharField->setValue(null);
178
		$this->assertFalse($varcharField->exists());
179
180
		$varcharField = new Varchar("testfield", 50, array('nullifyEmpty'=>false));
181
		$this->assertFalse($varcharField->getNullifyEmpty());
182
		$varcharField->setValue('abc');
183
		$this->assertTrue($varcharField->exists());
184
		$varcharField->setValue('');
185
		$this->assertTrue($varcharField->exists());
186
		$varcharField->setValue(null);
187
		$this->assertFalse($varcharField->exists());
188
189
		$textField = new Text("testfield");
190
		$this->assertTrue($textField->getNullifyEmpty());
191
		$textField->setValue('abc');
192
		$this->assertTrue($textField->exists());
193
		$textField->setValue('');
194
		$this->assertFalse($textField->exists());
195
		$textField->setValue(null);
196
		$this->assertFalse($textField->exists());
197
198
		$textField = new Text("testfield", array('nullifyEmpty'=>false));
199
		$this->assertFalse($textField->getNullifyEmpty());
200
		$textField->setValue('abc');
201
		$this->assertTrue($textField->exists());
202
		$textField->setValue('');
203
		$this->assertTrue($textField->exists());
204
		$textField->setValue(null);
205
		$this->assertFalse($textField->exists());
206
	}
207
208
	public function testStringFieldsWithMultibyteData() {
209
		$plainFields = array('Varchar', 'Text');
210
		$htmlFields = array('HTMLVarchar', 'HTMLText');
211
		$allFields = array_merge($plainFields, $htmlFields);
212
213
		$value = 'üåäöÜÅÄÖ';
214
		foreach ($allFields as $stringField) {
215
			$stringField = DBField::create_field($stringField, $value);
216
			for ($i = 1; $i < mb_strlen($value); $i++) {
217
				$expected = mb_substr($value, 0, $i) . '...';
218
				$this->assertEquals($expected, $stringField->LimitCharacters($i));
219
			}
220
		}
221
222
		$value = '<p>üåäö&amp;ÜÅÄÖ</p>';
223
		foreach ($htmlFields as $stringField) {
224
			$stringField = DBField::create_field($stringField, $value);
225
			$this->assertEquals('üåäö&amp;ÜÅÄ...', $stringField->LimitCharacters(8));
226
		}
227
228
		$this->assertEquals('ÅÄÖ', DBField::create_field('Text', 'åäö')->UpperCase());
229
		$this->assertEquals('åäö', DBField::create_field('Text', 'ÅÄÖ')->LowerCase());
230
	}
231
232
}
233
234
235