1: <?php
2: 3: 4: 5:
6: class QTypeTests extends QUnitTestCaseBase {
7: public function testCasting() {
8: define('_FAIL_', 'fail');
9: $cases = array(
10:
11: array("25.0", "25.0", (float)25.0, QType::Float),
12: array("25.1", "25.1", (float)25.1, QType::Float),
13: array(25.0, "25.0", (float)25.0, QType::Float),
14: array(25.1, "25.1", (float)25.1, QType::Float),
15: array(true, "true", _FAIL_,QType::Float),
16: array("true", "true", _FAIL_,QType::Float),
17: array(false, "false", _FAIL_,QType::Float),
18: array("false", "false", _FAIL_,QType::Float),
19: array(1, "1", (float)1, QType::Float),
20: array(0, "0", (float)0, QType::Float),
21: array("1", "1", (float)1, QType::Float),
22: array("0", "0", (float)0, QType::Float),
23: array("25", "25", (float)25, QType::Float),
24: array(25, "25", (float)25,QType::Float),
25: array(34.51666666667, "34.51666666667", (float)34.51666666667,QType::Float),
26: array(2147483648, "2147483648", (float)2147483648,QType::Float),
27: array(-2147483648, "-2147483648", (float)-2147483648,QType::Float),
28: array(-2147483649, "-2147483649", (float)-2147483649,QType::Float),
29: array("34.51666666667", "34.51666666667", (float)34.51666666667,QType::Float),
30: array("2147483648", "2147483648", (float)2147483648.0,QType::Float),
31: array("-2147483648", "-2147483648", (float)-2147483648.0,QType::Float),
32: array("-2147483649", "-2147483649", (float)-2147483649.0,QType::Float),
33:
34: array("25.0", "25.0", _FAIL_, QType::Integer),
35: array("25.1", "25.1", _FAIL_, QType::Integer),
36: array(25.0, "25.0", (int)25, QType::Integer),
37: array(25.1, "25.1", _FAIL_, QType::Integer),
38: array(true, "true", _FAIL_, QType::Integer),
39: array("true", "true", _FAIL_, QType::Integer),
40: array(false, "false", _FAIL_, QType::Integer),
41: array("false", "false", _FAIL_, QType::Integer),
42: array(1, "1", 1, QType::Integer),
43: array(0, "0", 0, QType::Integer),
44: array("1", "1", 1, QType::Integer),
45: array("0", "0", 0, QType::Integer),
46: array("25", "25", 25, QType::Integer),
47: array(25, "25", 25, QType::Integer),
48: array(34.51666666667, "34.51666666667", _FAIL_, QType::Integer),
49: array(2147483648, "2147483648", 2147483648, QType::Integer),
50: array(-2147483648, "-2147483648", (int)-2147483648, QType::Integer),
51: array(-2147483649, "-2147483649", -2147483649, QType::Integer),
52: array("34.51666666667", "34.51666666667", _FAIL_, QType::Integer),
53: array("2147483648", "2147483648", 2147483648, QType::Integer),
54: array("-2147483648", "-2147483648", (int)-2147483648, QType::Integer),
55: array("-2147483649", "-2147483649", -2147483649, QType::Integer),
56:
57:
58:
59: array(1844674407370955161616,"1844674407370955161616",(double)1844674407370955161616, QType::Float),
60: array(1844674407370955161616,"1844674407370955161616","fail", QType::Integer),
61:
62:
63: array("1844674407370955161616","1844674407370955161616","1844674407370955161616", QType::Float),
64: array("1844674407370955161616","1844674407370955161616","1844674407370955161616", QType::Integer),
65:
66: array(6, '6', '6', QType::String),
67: array(6.94, '6.94', '6.94', QType::String),
68: array(0.694*10, '6.94', '6.94', QType::String),
69: );
70:
71: foreach($cases as $case)
72: {
73: $value = (string)$case[1].'('.gettype($case[0]).')';
74: if($case[2] === _FAIL_)
75: {
76: $this->setExpectedException('QInvalidCastException');
77: QType::Cast($case[0], $case[3]);
78: $this->setExpectedException(null);
79: }
80: else
81: {
82: $castValue = QType::Cast($case[0], $case[3]);
83: $newValue = $castValue.'('.gettype($castValue).')';
84: $this->assertTrue($castValue === $case[2], "$value cast as a ".$case[3]." is $newValue");
85: }
86: }
87: }
88:
89: public function testDbTypeCasting() {
90: $dt1 = new QDateTime('Jan 15 2006');
91: $dt2 = new QDateTime('Mar 15 2006');
92:
93: $cond = QQ::Between(QQN::Project()->StartDate, $dt1, $dt2);
94: $a = Project::QueryArray($cond);
95: $this->assertEquals(2, count($a), "Between 2 QDateTime types works");
96:
97:
98: $cond = QQ::Between(QQN::Project()->Budget, 2000, 3000);
99: $a = Project::QueryArray($cond);
100: $this->assertEquals(1, count($a), "Between 2 int types works");
101:
102: $cond = QQ::Between(QQN::Project()->Name, 'A', 'C');
103: $a = Project::QueryArray($cond);
104: $this->assertEquals(3, count($a), "Between 2 string types works");
105: }
106:
107:
108:
109: public function testTypeTableTypes() {
110: $val = ProjectStatusType::$GuidelinesArray[2];
111: $this->assertNull($val, 'Generated a null value');
112:
113: $val = ProjectStatusType::$IsActiveArray[1];
114: $this->assertTrue($val, 'Open project is active');
115: $this->assertTrue(is_bool($val), 'Type of variable is boolean.');
116:
117: }
118: }