1: <?php
2: 3: 4: 5: 6:
7:
8:
9: if(!class_exists('Person')){
10: require_once __INCLUDES__ .'/model/Person.class.php';
11: }
12: if(!class_exists('Project')){
13: require_once __INCLUDES__ .'/model/Project.class.php';
14: }
15: if(!class_exists('Login')){
16: require_once __INCLUDES__ .'/model/Login.class.php';
17: }
18: if(!class_exists('Milestone')){
19: require_once __INCLUDES__ .'/model/Milestone.class.php';
20: }
21: if(!class_exists('Address')){
22: require_once __INCLUDES__ .'/model/Address.class.php';
23: }
24: if(!class_exists('PersonType')){
25: require_once __INCLUDES__ .'/model/PersonType.class.php';
26: }
27: if(!class_exists('TwoKey')){
28: require_once __INCLUDES__ .'/model/TwoKey.class.php';
29: }
30: if(!class_exists('ProjectStatusType')){
31: require_once __INCLUDES__ .'/model/ProjectStatusType.class.php';
32: }
33: if(!class_exists('Login')){
34: require_once __INCLUDES__ .'/model/Login.class.php';
35: }
36:
37: class ExpandAsArrayTests extends QUnitTestCaseBase {
38:
39: public function testMultiLevel() {
40: $arrPeople = Person::LoadAll(
41: self::getTestClauses()
42: );
43:
44: $this->assertEquals(12, sizeof($arrPeople), "12 Person objects found");
45: $targetPerson = $this->verifyObjectPropertyHelper($arrPeople, 'LastName', 'Wolfe');
46:
47: $this->helperVerifyKarenWolfe($targetPerson);
48:
49: $objProjectArray = $targetPerson->_ProjectAsManagerArray;
50: $this->assertEquals(2, sizeof($objProjectArray), "2 projects found");
51:
52: foreach ($objProjectArray as $objProject) {
53: $objMilestoneArray = $objProject->_MilestoneArray;
54:
55: switch ($objProject->Id) {
56: case 1:
57: $this->assertEquals(3, sizeof($objMilestoneArray), "3 milestones found");
58: break;
59:
60: case 4:
61: $this->assertEquals(4, sizeof($objMilestoneArray), "4 milestones found");
62: break;
63:
64: default:
65: $this->assertTrue(false, 'Unexpected project found, id: ' . $objProject->Id);
66: break;
67: }
68: }
69:
70:
71: $clauses = QQ::Clause(
72: QQ::ExpandAsArray(QQN::Person()->Address),
73: QQ::Expand(QQN::Person()->ProjectAsManager),
74: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager->Milestone)
75: );
76:
77: $arrPeople = Person::LoadAll(
78: $clauses
79: );
80:
81:
82: $this->assertEquals(13, sizeof($arrPeople), "13 Person objects found");
83: $targetPerson = $this->verifyObjectPropertyHelper($arrPeople, 'LastName', 'Wolfe');
84:
85: $objProjectArray = $targetPerson->_ProjectAsManagerArray;
86: $this->assertNull($objProjectArray, "No project array found");
87:
88: $objProject = $targetPerson->_ProjectAsManager;
89: $this->assertNotNull($objProject, "Project found");
90:
91: $objMilestoneArray = $objProject->_MilestoneArray;
92:
93: switch ($objProject->Id) {
94: case 1:
95: $this->assertEquals(3, sizeof($objMilestoneArray), "3 milestones found");
96: break;
97:
98: case 4:
99: $this->assertEquals(4, sizeof($objMilestoneArray), "4 milestones found");
100: break;
101:
102: default:
103: $this->assertTrue(false, 'Unexpected project found, id: ' . $objProject->Id);
104: break;
105: }
106: }
107:
108: public function testQuerySingle() {
109: $targetPerson = Person::QuerySingle(
110: QQ::Equal(QQN::Person()->Id, 7),
111: self::getTestClauses()
112: );
113:
114: $this->helperVerifyKarenWolfe($targetPerson);
115:
116: $objTwoKey = TwoKey::QuerySingle(
117: QQ::AndCondition (
118: QQ::Equal(QQN::TwoKey()->Server, 'google.com'),
119: QQ::Equal(QQN::TwoKey()->Directory, 'mail')
120: ),
121: QQ::Clause(
122: QQ::ExpandAsArray(QQN::TwoKey()->Project->PersonAsTeamMember)
123: )
124: );
125:
126: $this->assertEquals (count($objTwoKey->Project->_PersonAsTeamMemberArray), 6, '6 team members found.');
127: }
128:
129: public function testEmptyArray() {
130: $arrPeople = Person::QuerySingle(
131: QQ::Equal(QQN::Person()->Id, 2),
132: self::getTestClauses()
133: );
134:
135: $this->assertTrue(is_array($arrPeople->_ProjectAsManagerArray), "_ProjectAsManagerArray is an array");
136: $this->assertEquals(0, count($arrPeople->_ProjectAsManagerArray), "_ProjectAsManagerArray has no Project objects");
137: }
138:
139: public function testNullArray() {
140: $arrPeople = Person::QuerySingle(
141: QQ::Equal(QQN::Person()->Id, 2)
142: );
143:
144: $this->assertTrue(is_null($arrPeople->_ProjectAsManagerArray), "_ProjectAsManagerArray is null");
145: }
146:
147: public function testTypeExpansion() {
148: $clauses = QQ::Clause(
149: QQ::ExpandAsArray (QQN::Person()->PersonType)
150: );
151:
152: $objPerson =
153: Person::QuerySingle(
154: QQ::Equal (QQN::Person()->Id, 7),
155: $clauses
156: );
157:
158: $intPersonTypeArray = $objPerson->_PersonTypeArray;
159: $this->assertEquals(array(
160: PersonType::Manager,
161: PersonType::CompanyCar)
162: , $intPersonTypeArray
163: , "PersonType expansion is correct");
164: }
165:
166: private static function getTestClauses() {
167: return QQ::Clause(
168: QQ::ExpandAsArray(QQN::Person()->Address),
169: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager),
170: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager->Milestone)
171: );
172: }
173:
174: private function helperVerifyKarenWolfe(Person $targetPerson) {
175: $this->assertEquals(2, sizeof($targetPerson->_ProjectAsManagerArray), "2 projects found");
176: $targetProject = $this->verifyObjectPropertyHelper($targetPerson->_ProjectAsManagerArray, 'Name', 'ACME Payment System');
177:
178: $this->assertEquals(4, sizeof($targetProject->_MilestoneArray), "4 milestones found");
179: $this->verifyObjectPropertyHelper($targetProject->_MilestoneArray, 'Name', 'Milestone H');
180: }
181:
182: public function testSelectSubsetInExpand() {
183: $objPersonArray = Person::QueryArray(
184: QQ::OrCondition(
185: QQ::Like(QQN::Person()->ProjectAsManager->Name, '%ACME%'),
186: QQ::Like(QQN::Person()->ProjectAsManager->Name, '%HR%')
187: ),
188:
189: QQ::Clause(
190: QQ::Select(QQN::Person()->LastName),
191: QQ::Expand(QQN::Person()->ProjectAsManager, null, QQ::Select(QQN::Person()->ProjectAsManager->Spent)),
192: QQ::OrderBy(QQN::Person()->LastName, QQN::Person()->FirstName)
193: )
194: );
195:
196: foreach ($objPersonArray as $objPerson) {
197: $this->assertNull($objPerson->FirstName, "FirstName should be null, since it was not selected");
198: $this->assertNotNull($objPerson->Id, "Id should not be null since it's always added to the select list");
199: $this->assertNotNull($objPerson->_ProjectAsManager->Id, "ProjectAsManager->Id should not be null since id's are always added to the select list");
200: $this->assertNull($objPerson->_ProjectAsManager->Name, "ProjectAsManager->Name should be null since it was not selected");
201: }
202: }
203:
204: public function testSelectSubsetInExpandAsArray() {
205: $objPersonArray = Person::LoadAll(
206: QQ::Clause(
207: QQ::Select(QQN::Person()->FirstName),
208: QQ::ExpandAsArray(QQN::Person()->Address, QQ::Select(QQN::Person()->Address->Street, QQN::Person()->Address->City)),
209: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager, QQ::Select(QQN::Person()->ProjectAsManager->StartDate)),
210: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager->Milestone, QQ::Select(QQN::Person()->ProjectAsManager->Milestone->Name))
211: )
212: );
213:
214: foreach ($objPersonArray as $objPerson) {
215: $this->assertNull($objPerson->LastName, "LastName should be null, since it was not selected");
216: $this->assertNotNull($objPerson->Id, "Id should not be null since it's always added to the select list");
217: if (sizeof($objPerson->_AddressArray) > 0) {
218: foreach ($objPerson->_AddressArray as $objAddress) {
219: $this->assertNotNull($objAddress->Id, "Address->Id should not be null since it's always added to the select list");
220: $this->assertNull($objAddress->PersonId, "Address->PersonId should be null, since it was not selected");
221: }
222: }
223: if (sizeof($objPerson->_ProjectAsManagerArray) > 0) {
224: foreach($objPerson->_ProjectAsManagerArray as $objProject) {
225: $this->assertNotNull($objProject->Id, "Project->Id should not be null since it's always added to the select list");
226: $this->assertNull($objProject->Name, "Project->Name should be null, since it was not selected");
227: if (sizeof($objProject->_MilestoneArray) > 0) {
228: foreach ($objProject->_MilestoneArray as $objMilestone) {
229: $this->assertNotNull($objMilestone->Id, "Milestone->Id should not be null since it's always added to the select list");
230: $this->assertNull($objMilestone->ProjectId, "Milestone->ProjectId should be null, since it was not selected");
231: }
232: }
233: }
234: }
235: }
236: }
237:
238: public function testMultiLeafExpansion() {
239: $objMilestone = Milestone::QuerySingle(
240: QQ::Equal (QQN::Milestone()->Id, 1),
241: QQ::Clause(
242: QQ::ExpandAsArray(QQN::Milestone()->Project->ManagerPerson->ProjectAsTeamMember),
243: QQ::ExpandAsArray(QQN::Milestone()->Project->PersonAsTeamMember)
244: )
245: );
246:
247: $objProjectArray = $objMilestone->Project->ManagerPerson->_ProjectAsTeamMemberArray;
248: $objPeopleArray = $objMilestone->Project->_PersonAsTeamMemberArray;
249:
250: $this->assertTrue(is_array($objProjectArray), "_ProjectAsTeamMemberArray is an array");
251: $this->assertEquals(2, count($objProjectArray), "_ProjectAsTeamMemberArray has 2 Project objects");
252:
253: $this->assertTrue(is_array($objPeopleArray), "_PersonAsTeamMemberArray is an array");
254: $this->assertEquals(5, count($objPeopleArray), "_PersonAsTeamMemberArray has 5 People objects");
255:
256:
257: $objLogin = Login::QuerySingle(
258: QQ::Equal (QQN::Login()->PersonId, 7),
259: QQ::Clause(
260: QQ::ExpandAsArray(QQN::Login()->Person->ProjectAsTeamMember),
261: QQ::ExpandAsArray(QQN::Login()->Person->ProjectAsManager)
262: )
263: );
264:
265: $objProjectArray = $objLogin->Person->_ProjectAsTeamMemberArray;
266:
267: $this->assertTrue(is_array($objProjectArray), "_ProjectAsTeamMemberArray is an array");
268: $this->assertEquals(2, count($objProjectArray), "_ProjectAsTeamMemberArray has 2 Project objects");
269:
270: $objProjectArray = $objLogin->Person->_ProjectAsManagerArray;
271:
272: $this->assertTrue(is_array($objProjectArray), "_ProjectAsManagerArray is an array");
273: $this->assertEquals(2, count($objProjectArray), "_ProjectAsManagerArray has 2 Project objects");
274:
275: }
276:
277: public function testConditionalExpansion() {
278: $clauses = QQ::Clause(
279: QQ::ExpandAsArray(QQN::Person()->Address),
280: QQ::Expand(QQN::Person()->ProjectAsManager, QQ::Equal (QQN::Person()->ProjectAsManager->ProjectStatusTypeId, ProjectStatusType::Open)),
281: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager->Milestone),
282: QQ::OrderBy(QQN::Person()->Id)
283: );
284:
285: $targetPersonArray = Person::LoadAll (
286: $clauses
287: );
288:
289: $targetPerson = reset($targetPersonArray);
290:
291: $this->assertEquals ($targetPerson->Id, 1, "Person 1 found.");
292: $this->assertNotNull ($targetPerson->_ProjectAsManager, "Person 1 has a project.");
293:
294: $targetPerson = end($targetPersonArray);
295:
296: $this->assertEquals ($targetPerson->Id, 12, "Person 12 found.");
297: $this->assertNull ($targetPerson->_ProjectAsManager, "Person 12 does not have a project.");
298:
299: }
300:
301: public function testConditionalExpansion2() {
302: $clauses = QQ::Clause(
303: QQ::Expand(QQN::Login()->Person->ProjectAsManager, QQ::Equal (QQN::Login()->Person->ProjectAsManager->ProjectStatusTypeId, ProjectStatusType::Open)),
304: QQ::ExpandAsArray(QQN::Login()->Person->ProjectAsManager->Milestone),
305: QQ::ExpandAsArray(QQN::Login()->Person->Address),
306: QQ::OrderBy(QQN::Login()->Person->Id)
307: );
308:
309: $cond = QQ::In (QQN::Login()->PersonId, [1,3,7]);
310: $targetLoginArray = Login::QueryArray (
311: $cond,
312: $clauses
313: );
314:
315: $targetLogin = reset($targetLoginArray);
316: $this->assertEquals ($targetLogin->Person->Id, 1, "Person 1 found.");
317: $this->assertNotNull ($targetLogin->Person->_ProjectAsManager, "Person 1 has an open project.");
318:
319: $targetLogin = next($targetLoginArray);
320: $this->assertEquals ($targetLogin->Person->Id, 3, "Person 3 found.");
321: $this->assertNull ($targetLogin->Person->_ProjectAsManager, "Person 3 does not have an open project.");
322:
323: $targetLogin = next($targetLoginArray);
324: $this->assertEquals ($targetLogin->Person->Id, 7, "Person 7 found.");
325: $this->assertNull ($targetLogin->Person->_ProjectAsManager, "Person 7 does have an open project.");
326:
327: }
328:
329:
330: public function testConditionalExpansion3() {
331:
332:
333: $clauses = QQ::Clause(
334: QQ::Expand(QQN::Person()->ProjectAsTeamMember->Project, QQ::Equal(QQN::Person()->ProjectAsTeamMember->Project->ProjectStatusTypeId, ProjectStatusType::Completed)),
335: QQ::Expand(QQN::Person()->ProjectAsTeamMember->Project->ProjectAsRelated->Project, QQ::Equal(QQN::Person()->ProjectAsTeamMember->Project->ProjectAsRelated->Project->ProjectStatusTypeId, ProjectStatusType::Open))
336: );
337:
338: $cond = QQ::IsNotNull(QQN::Person()->ProjectAsTeamMember->Project->ProjectAsRelated->Project->Id);
339:
340: $targetPersonArray = Person::QueryArray (
341: $cond,
342: $clauses
343: );
344:
345: $targetPerson = reset($targetPersonArray);
346:
347: $this->assertEquals(ProjectStatusType::Completed, $targetPerson->ProjectAsTeamMember->ProjectStatusTypeId, "Found completed parent project");
348: $this->assertEquals(ProjectStatusType::Open, $targetPerson->ProjectAsTeamMember->ProjectAsRelated->ProjectStatusTypeId, "Found open child project");
349:
350: }
351:
352: public function testConditionalExpansionReverse() {
353:
354: $a = Person::QueryArray(
355: QQ::All(),
356: [
357: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager, QQ::Equal(QQN::Person()->ProjectAsManager->ProjectStatusTypeId, ProjectStatusType::Open)),
358: QQ::OrderBy(QQN::Person()->Id)
359: ]
360: );
361:
362: $this->assertEquals(3, $a[0]->_ProjectAsManagerArray[0]->Id);
363: }
364:
365: public function testConditionalExpansionAssociation() {
366:
367:
368:
369: $a = Project::QueryArray(
370: QQ::All(),
371: [
372: QQ::ExpandAsArray(QQN::Project()->ParentProjectAsRelated, QQ::Equal(QQN::Project()->ParentProjectAsRelated->ProjectId, 1)),
373: QQ::ExpandAsArray(QQN::Project()->ProjectAsRelated, QQ::Equal(QQN::Project()->ProjectAsRelated->Project->Id, 1)),
374: QQ::OrderBy(QQN::Project()->Id)
375: ]
376: );
377:
378: $this->assertEquals(1, $a[2]->_ParentProjectAsRelatedArray[0]->Id);
379: }
380:
381:
382:
383: public function testDataGridHtml() {
384: $objMilestone = Milestone::QuerySingle(
385: QQ::Equal (QQN::Milestone()->Id, 1),
386: QQ::Clause(
387: QQ::Expand(QQN::Milestone()->Project->ManagerPerson)
388: )
389: );
390:
391: $_ITEM =$objMilestone;
392: $node = QQN::Milestone()->Project->ManagerPerson;
393:
394: $html = $node->GetDataGridHtml();
395: $val = eval(sprintf('return %s;', $html));
396: $this->assertEquals ($val, "Person Object 7");
397: }
398:
399: }
400: