1: <?php
2: 3: 4: 5:
6:
7: if(!class_exists('TypeTest')){
8: require_once __INCLUDES__ .'/model/TypeTest.class.php';
9: }
10:
11: class QQMathOpTests extends QUnitTestCaseBase {
12:
13: protected function setUp()
14: {
15: TypeTest::DeleteAll();
16: }
17:
18:
19: public function testMathOp() {
20: $objTest = new TypeTest();
21: $objTest->TestFloat = 1.0;
22: $objTest->Save();
23:
24: $objTest2 = new TypeTest();
25: $objTest2->TestFloat = 2.0;
26: $objTest2->Save();
27:
28: $objResArray = TypeTest::QueryArray(QQ::GreaterThan(QQ::MathOp('*', QQN::TypeTest()->TestFloat, 2.0), 3.0));
29: $this->assertEquals(1, count($objResArray));
30: if (count($objResArray) > 0) {
31: $objRes = $objResArray[0];
32: $this->assertNotNull($objRes);
33: if ($objRes) {
34: $this->assertEquals(2.0, $objRes->TestFloat);
35: }
36: }
37:
38: $objResArray = TypeTest::QueryArray(QQ::GreaterThan(QQ::MathOp('*', 2.0, QQN::TypeTest()->TestFloat), 3.0));
39: $this->assertEquals(1, count($objResArray));
40: if (count($objResArray) > 0) {
41: $objRes = $objResArray[0];
42: $this->assertNotNull($objRes);
43: if ($objRes) {
44: $this->assertEquals(2.0, $objRes->TestFloat);
45: }
46: }
47:
48: $objResArray = TypeTest::QueryArray(QQ::GreaterThan(QQ::MathOp('*', QQN::TypeTest()->TestFloat, QQN::TypeTest()->TestFloat), 3.0));
49: $this->assertEquals(1, count($objResArray));
50: if (count($objResArray) > 0) {
51: $objRes = $objResArray[0];
52: $this->assertNotNull($objRes);
53: if ($objRes) {
54: $this->assertEquals(2.0, $objRes->TestFloat);
55: }
56: }
57:
58: $objTest->Delete();
59: $objTest2->Delete();
60: }
61:
62: public function testMul() {
63: $objTest = new TypeTest();
64: $objTest->TestFloat = 1.0;
65: $objTest->Save();
66:
67: $objTest2 = new TypeTest();
68: $objTest2->TestFloat = 2.0;
69: $objTest2->Save();
70:
71: $objResArray = TypeTest::QueryArray(QQ::GreaterThan(QQ::Mul(QQN::TypeTest()->TestFloat, 2.0), 3.0));
72: $this->assertEquals(1, count($objResArray));
73: if (count($objResArray) > 0) {
74: $objRes = $objResArray[0];
75: $this->assertNotNull($objRes);
76: if ($objRes) {
77: $this->assertEquals(2.0, $objRes->TestFloat);
78: }
79: }
80:
81: $objTest->Delete();
82: $objTest2->Delete();
83: }
84:
85: public function testDiv() {
86: $objTest = new TypeTest();
87: $objTest->TestFloat = 4.0;
88: $objTest->Save();
89:
90: $objTest2 = new TypeTest();
91: $objTest2->TestFloat = 8.0;
92: $objTest2->Save();
93:
94: $objResArray = TypeTest::QueryArray(QQ::GreaterThan(QQ::Div(QQN::TypeTest()->TestFloat, 2.0), 3.0));
95: $this->assertEquals(1, count($objResArray));
96: if (count($objResArray) > 0) {
97: $objRes = $objResArray[0];
98: $this->assertNotNull($objRes);
99: if ($objRes) {
100: $this->assertEquals(8.0, $objRes->TestFloat);
101: }
102: }
103:
104: $objTest->Delete();
105: $objTest2->Delete();
106: }
107:
108: public function testSub() {
109: $objTest = new TypeTest();
110: $objTest->TestFloat = 2.0;
111: $objTest->Save();
112:
113: $objTest2 = new TypeTest();
114: $objTest2->TestFloat = 4.0;
115: $objTest2->Save();
116:
117: $objResArray = TypeTest::QueryArray(QQ::GreaterOrEqual(QQ::Sub(QQN::TypeTest()->TestFloat, 1.0), 3.0));
118: $this->assertEquals(1, count($objResArray));
119: if (count($objResArray) > 0) {
120: $objRes = $objResArray[0];
121: $this->assertNotNull($objRes);
122: if ($objRes) {
123: $this->assertEquals(4.0, $objRes->TestFloat);
124: }
125: }
126:
127: $objTest->Delete();
128: $objTest2->Delete();
129: }
130:
131: public function testAdd() {
132: $objTest = new TypeTest();
133: $objTest->TestFloat = 1.0;
134: $objTest->Save();
135:
136: $objTest2 = new TypeTest();
137: $objTest2->TestFloat = 2.0;
138: $objTest2->Save();
139:
140: $objResArray = TypeTest::QueryArray(QQ::GreaterThan(QQ::Add(QQN::TypeTest()->TestFloat, 1.5), 3.0));
141: $this->assertEquals(1, count($objResArray));
142: if (count($objResArray) > 0) {
143: $objRes = $objResArray[0];
144: $this->assertNotNull($objRes);
145: if ($objRes) {
146: $this->assertEquals(2.0, $objRes->TestFloat);
147: }
148: }
149:
150: $objTest->Delete();
151: $objTest2->Delete();
152: }
153:
154: public function testNeg() {
155: $objTest = new TypeTest();
156: $objTest->TestFloat = -1.0;
157: $objTest->Save();
158:
159: $objTest2 = new TypeTest();
160: $objTest2->TestFloat = -2.0;
161: $objTest2->Save();
162:
163: $objResArray = TypeTest::QueryArray(QQ::GreaterThan(QQ::Neg(QQN::TypeTest()->TestFloat), 1.0));
164: $this->assertEquals(1, count($objResArray));
165: if (count($objResArray) > 0) {
166: $objRes = $objResArray[0];
167: $this->assertNotNull($objRes);
168: if ($objRes) {
169: $this->assertEquals(-2.0, $objRes->TestFloat);
170: }
171: }
172:
173: $objTest->Delete();
174: $objTest2->Delete();
175: }
176:
177:
178:
179: public function testOrderBy() {
180: $objTest = new TypeTest();
181: $objTest->TestFloat = 1.0;
182: $objTest->Save();
183:
184: $objTest2 = new TypeTest();
185: $objTest2->TestFloat = 2.0;
186: $objTest2->Save();
187:
188: $objResArray = TypeTest::QueryArray(QQ::LessThan(
189: QQ::Virtual('mul1', QQ::Mul(QQN::TypeTest()->TestFloat, -2.0))
190: , -1.0
191: ),
192: QQ::Clause(
193: QQ::OrderBy(QQ::Virtual('mul1'))
194: , QQ::Expand(QQ::Virtual('mul1'))
195: ));
196: $this->assertEquals(2, count($objResArray));
197: if (2 == count($objResArray)) {
198: $objRes = $objResArray[0];
199: $this->assertNotNull($objRes);
200: if ($objRes) {
201: $this->assertEquals(2.0, $objRes->TestFloat);
202: $this->assertEquals(-4.0, $objRes->GetVirtualAttribute('mul1'));
203: }
204: $objRes = $objResArray[1];
205: $this->assertNotNull($objRes);
206: if ($objRes) {
207: $this->assertEquals(1.0, $objRes->TestFloat);
208: $this->assertEquals(-2.0, $objRes->GetVirtualAttribute('mul1'));
209: }
210: }
211:
212: $objTest->Delete();
213: $objTest2->Delete();
214: }
215:
216: public function testOrderByDesc() {
217: $objTest = new TypeTest();
218: $objTest->TestFloat = 1.0;
219: $objTest->Save();
220:
221: $objTest2 = new TypeTest();
222: $objTest2->TestFloat = 2.0;
223: $objTest2->Save();
224:
225: $objResArray = TypeTest::QueryArray(QQ::LessThan(
226: QQ::Virtual('mul1', QQ::Mul(QQN::TypeTest()->TestFloat, -2.0))
227: , -1.0
228: ),
229: QQ::Clause(
230: QQ::OrderBy(QQ::Virtual('mul1'), 'DESC')
231: , QQ::Expand(QQ::Virtual('mul1'))
232: ));
233: $this->assertEquals(2, count($objResArray));
234: if (2 == count($objResArray)) {
235: $objRes = $objResArray[0];
236: $this->assertNotNull($objRes);
237: if ($objRes) {
238: $this->assertEquals(1.0, $objRes->TestFloat);
239: $this->assertEquals(-2.0, $objRes->GetVirtualAttribute('mul1'));
240: }
241: $objRes = $objResArray[1];
242: $this->assertNotNull($objRes);
243: if ($objRes) {
244: $this->assertEquals(2.0, $objRes->TestFloat);
245: $this->assertEquals(-4.0, $objRes->GetVirtualAttribute('mul1'));
246: }
247: }
248:
249: $objTest->Delete();
250: $objTest2->Delete();
251: }
252:
253: public function testSelect() {
254: $objTest = new TypeTest();
255: $objTest->TestFloat = 1.0;
256: $objTest->Save();
257:
258: $objTest2 = new TypeTest();
259: $objTest2->TestFloat = 2.0;
260: $objTest2->Save();
261:
262: $objResArray = TypeTest::QueryArray(QQ::LessThan(
263: QQ::Virtual('mul1', QQ::Mul(QQN::TypeTest()->TestFloat, -2.0))
264: , -1.0
265: ),
266: QQ::Clause(
267: QQ::OrderBy(QQ::Virtual('mul1'))
268: , QQ::Expand(QQ::Virtual('mul1'))
269: , QQ::Select(QQ::Virtual('mul1'))
270: ));
271: $this->assertEquals(2, count($objResArray));
272: if (2 == count($objResArray)) {
273: $objRes = $objResArray[0];
274: $this->assertNotNull($objRes);
275: if ($objRes) {
276: $this->assertNull($objRes->TestFloat);
277: $this->assertEquals(-4.0, $objRes->GetVirtualAttribute('mul1'));
278: }
279: $objRes = $objResArray[1];
280: $this->assertNotNull($objRes);
281: if ($objRes) {
282: $this->assertNull($objRes->TestFloat);
283: $this->assertEquals(-2.0, $objRes->GetVirtualAttribute('mul1'));
284: }
285: }
286:
287: $objTest->Delete();
288: $objTest2->Delete();
289: }
290: 291: 292:
293: public function testExample() {
294: $objPersonArray = Person::QueryArray(
295:
296: QQ::GreaterThan(QQ::Sub(QQN::Person()->ProjectAsManager->Spent, QQN::Person()->ProjectAsManager->Budget), 20)
297: );
298: $this->assertGreaterThan(0, count($objPersonArray));
299:
300: foreach ($objPersonArray as $objPerson) {
301: $this->assertNotNull($objPerson->FirstName);
302: $this->assertNotNull($objPerson->LastName);
303: }
304:
305: $objPersonArray = Person::QueryArray(
306:
307: QQ::GreaterThan(
308: QQ::Virtual('diff', QQ::Sub(
309: QQN::Person()->ProjectAsManager->Spent
310: , QQN::Person()->ProjectAsManager->Budget
311: ))
312: , 20
313: ),
314: QQ::Clause(
315:
316: QQ::OrderBy(QQ::Virtual('diff'), 'DESC')
317:
318: , QQ::Expand(QQ::Virtual('diff'))
319: )
320: );
321: $this->assertGreaterThan(0, count($objPersonArray));
322:
323: foreach ($objPersonArray as $objPerson) {
324: $this->assertNotNull($objPerson->FirstName);
325: $this->assertNotNull($objPerson->LastName);
326: $this->assertNotNull($objPerson->GetVirtualAttribute('diff'));
327: }
328:
329: $objPersonArray = Person::QueryArray(
330:
331: QQ::GreaterThan(
332: QQ::Virtual('diff', QQ::MathOp(
333: '-',
334: QQN::Person()->ProjectAsManager->Spent
335: , QQN::Person()->ProjectAsManager->Budget
336: ))
337: , 20
338: ),
339: QQ::Clause(
340:
341: QQ::OrderBy(QQ::Virtual('diff'), 'DESC')
342:
343: , QQ::Expand(QQ::Virtual('diff'))
344: , QQ::Select(array(
345: QQ::Virtual('diff')
346: , QQN::Person()->FirstName
347: , QQN::Person()->LastName
348: ))
349: )
350: );
351: $this->assertGreaterThan(0, count($objPersonArray));
352:
353: foreach ($objPersonArray as $objPerson) {
354: $this->assertNotNull($objPerson->FirstName);
355: $this->assertNotNull($objPerson->LastName);
356: $this->assertNotNull($objPerson->GetVirtualAttribute('diff'));
357: }
358:
359: $objPersonArray = Person::QueryArray(
360:
361: QQ::GreaterThan(
362: QQ::Virtual('absdiff', QQ::Abs(
363: QQ::Sub(
364: QQN::Person()->ProjectAsManager->Spent
365: , QQN::Person()->ProjectAsManager->Budget
366: )
367: ))
368: , 20
369: ),
370: QQ::Clause(
371:
372: QQ::OrderBy(QQ::Virtual('absdiff'), 'DESC')
373:
374: , QQ::Expand(QQ::Virtual('absdiff'))
375: , QQ::Select(array(
376: QQ::Virtual('absdiff')
377: , QQN::Person()->FirstName
378: , QQN::Person()->LastName
379: ))
380: )
381: );
382: $this->assertGreaterThan(0, count($objPersonArray));
383:
384: foreach ($objPersonArray as $objPerson) {
385: $this->assertNotNull($objPerson->FirstName);
386: $this->assertNotNull($objPerson->LastName);
387: $this->assertNotNull($objPerson->GetVirtualAttribute('absdiff'));
388: }
389: }
390: }