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 CacheTests extends QUnitTestCaseBase {
38:
39: public function setUp() {
40: QApplication::$blnLocalCache = true;
41: }
42:
43: public function tearDown() {
44: QApplication::$blnLocalCache = false;
45: }
46:
47:
48: public function testMultiLevel() {
49: $arrPeople = Person::LoadAll(
50: self::getTestClauses()
51: );
52:
53: $this->assertEquals(12, sizeof($arrPeople), "12 Person objects found");
54: $targetPerson = $this->verifyObjectPropertyHelper($arrPeople, 'LastName', 'Wolfe');
55:
56: $this->helperVerifyKarenWolfe($targetPerson);
57:
58: $objProjectArray = $targetPerson->_ProjectAsManagerArray;
59: $this->assertEquals(2, sizeof($objProjectArray), "2 projects found");
60:
61: foreach ($objProjectArray as $objProject) {
62: $objMilestoneArray = $objProject->_MilestoneArray;
63:
64: switch ($objProject->Id) {
65: case 1:
66: $this->assertEquals(3, sizeof($objMilestoneArray), "3 milestones found");
67: break;
68:
69: case 4:
70: $this->assertEquals(4, sizeof($objMilestoneArray), "4 milestones found");
71: break;
72:
73: default:
74: $this->assertTrue(false, 'Unexpected project found, id: ' . $objProject->Id);
75: break;
76: }
77: }
78:
79:
80: $clauses = QQ::Clause(
81: QQ::ExpandAsArray(QQN::Person()->Address),
82: QQ::Expand(QQN::Person()->ProjectAsManager),
83: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager->Milestone)
84: );
85:
86: $arrPeople = Person::LoadAll(
87: $clauses
88: );
89:
90:
91: $this->assertEquals(13, sizeof($arrPeople), "13 Person objects found");
92: $targetPerson = $this->verifyObjectPropertyHelper($arrPeople, 'LastName', 'Wolfe');
93:
94: $objProjectArray = $targetPerson->_ProjectAsManagerArray;
95: $this->assertNull($objProjectArray, "No project array found");
96:
97: $objProject = $targetPerson->_ProjectAsManager;
98: $this->assertNotNull($objProject, "Project found");
99:
100: $objMilestoneArray = $objProject->_MilestoneArray;
101:
102: switch ($objProject->Id) {
103: case 1:
104: $this->assertEquals(3, sizeof($objMilestoneArray), "3 milestones found");
105: break;
106:
107: case 4:
108: $this->assertEquals(4, sizeof($objMilestoneArray), "4 milestones found");
109: break;
110:
111: default:
112: $this->assertTrue(false, 'Unexpected project found, id: ' . $objProject->Id);
113: break;
114: }
115:
116:
117:
118: $objProject2 = Project::Load ($objProject->Id, array (QQ::Select (QQN::Project()->Name)));
119:
120: $this->assertNotNull($objProject2->ManagerPersonId, "ManagerPersonId found");
121:
122: }
123:
124: public function testQuerySingle() {
125: $targetPerson = Person::QuerySingle(
126: QQ::Equal(QQN::Person()->Id, 7),
127: self::getTestClauses()
128: );
129:
130: $this->helperVerifyKarenWolfe($targetPerson);
131:
132: $targetPerson2 = Person::QuerySingle(
133: QQ::Equal(QQN::Person()->Id, 7),
134: array (QQ::Select(QQN::Person()->FirstName))
135: );
136:
137: $this->assertNotNull($targetPerson2->LastName, "Used a cached object");
138:
139: $targetPerson2->Save();
140:
141: $targetPerson2 = Person::QuerySingle(
142: QQ::Equal(QQN::Person()->Id, 7),
143: array (QQ::Select(QQN::Person()->FirstName))
144: );
145: $this->assertNull($targetPerson2->LastName, "Saving an object deleted it from the cache");
146:
147: $objTwoKey = TwoKey::QuerySingle(
148: QQ::AndCondition (
149: QQ::Equal(QQN::TwoKey()->Server, 'google.com'),
150: QQ::Equal(QQN::TwoKey()->Directory, 'mail')
151: ),
152: QQ::Clause(
153: QQ::ExpandAsArray(QQN::TwoKey()->Project->PersonAsTeamMember)
154: )
155: );
156:
157: $this->assertEquals (count($objTwoKey->Project->_PersonAsTeamMemberArray), 6, '6 team members found.');
158: }
159:
160: public function testEmptyArray() {
161: $arrPeople = Person::QuerySingle(
162: QQ::Equal(QQN::Person()->Id, 2),
163: self::getTestClauses()
164: );
165:
166: $this->assertTrue(is_array($arrPeople->_ProjectAsManagerArray), "_ProjectAsManagerArray is an array");
167: $this->assertEquals(0, count($arrPeople->_ProjectAsManagerArray), "_ProjectAsManagerArray has no Project objects");
168: }
169:
170: public function testNullArray() {
171: $arrPeople = Person::QuerySingle(
172: QQ::Equal(QQN::Person()->Id, 2)
173: );
174:
175: $this->assertTrue(is_null($arrPeople->_ProjectAsManagerArray), "_ProjectAsManagerArray is null");
176: }
177:
178: public function testTypeExpansion() {
179: $clauses = QQ::Clause(
180: QQ::ExpandAsArray (QQN::Person()->PersonType)
181: );
182:
183: $objPerson =
184: Person::QuerySingle(
185: QQ::Equal (QQN::Person()->Id, 7),
186: $clauses
187: );
188:
189: $intPersonTypeArray = $objPerson->_PersonTypeArray;
190: $this->assertEquals(array(
191: PersonType::Manager,
192: PersonType::CompanyCar)
193: , $intPersonTypeArray
194: , "PersonType expansion is correct");
195: }
196:
197: private static function getTestClauses() {
198: return QQ::Clause(
199: QQ::ExpandAsArray(QQN::Person()->Address),
200: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager),
201: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager->Milestone)
202: );
203: }
204:
205: private function helperVerifyKarenWolfe(Person $targetPerson) {
206: $this->assertEquals(2, sizeof($targetPerson->_ProjectAsManagerArray), "2 projects found");
207: $targetProject = $this->verifyObjectPropertyHelper($targetPerson->_ProjectAsManagerArray, 'Name', 'ACME Payment System');
208:
209: $this->assertEquals(4, sizeof($targetProject->_MilestoneArray), "4 milestones found");
210: $this->verifyObjectPropertyHelper($targetProject->_MilestoneArray, 'Name', 'Milestone H');
211: }
212:
213: public function testSelectSubsetInExpand() {
214: Project::ClearCache();
215: Person::ClearCache();
216: Milestone::ClearCache();
217:
218: $objPersonArray = Person::QueryArray(
219: QQ::OrCondition(
220: QQ::Like(QQN::Person()->ProjectAsManager->Name, '%ACME%'),
221: QQ::Like(QQN::Person()->ProjectAsManager->Name, '%HR%')
222: ),
223:
224: QQ::Clause(
225: QQ::Select(QQN::Person()->LastName),
226: QQ::Expand(QQN::Person()->ProjectAsManager, null, QQ::Select(QQN::Person()->ProjectAsManager->Spent)),
227: QQ::OrderBy(QQN::Person()->LastName, QQN::Person()->FirstName)
228: )
229: );
230:
231: foreach ($objPersonArray as $objPerson) {
232: $this->assertNull($objPerson->FirstName, "FirstName should be null, since it was not selected");
233: $this->assertNotNull($objPerson->Id, "Id should not be null since it's always added to the select list");
234: $this->assertNotNull($objPerson->_ProjectAsManager->Id, "ProjectAsManager->Id should not be null since id's are always added to the select list");
235: $this->assertNull($objPerson->_ProjectAsManager->Name, "ProjectAsManager->Name should be null since it was not selected");
236: }
237:
238:
239: $objPersonArray = Person::QueryArray(
240: QQ::OrCondition(
241: QQ::Like(QQN::Person()->ProjectAsManager->Name, '%ACME%'),
242: QQ::Like(QQN::Person()->ProjectAsManager->Name, '%HR%')
243: ),
244:
245: QQ::Clause(
246: QQ::Expand(QQN::Person()->ProjectAsManager),
247: QQ::OrderBy(QQN::Person()->LastName, QQN::Person()->FirstName)
248: )
249: );
250:
251: $objPersonArray = Person::QueryArray(
252: QQ::OrCondition(
253: QQ::Like(QQN::Person()->ProjectAsManager->Name, '%ACME%'),
254: QQ::Like(QQN::Person()->ProjectAsManager->Name, '%HR%')
255: ),
256:
257: QQ::Clause(
258: QQ::Select(QQN::Person()->LastName),
259: QQ::Expand(QQN::Person()->ProjectAsManager, null, QQ::Select(QQN::Person()->ProjectAsManager->Spent)),
260: QQ::OrderBy(QQN::Person()->LastName, QQN::Person()->FirstName)
261: )
262: );
263:
264: foreach ($objPersonArray as $objPerson) {
265: $this->assertNotNull($objPerson->FirstName, "FirstName should not be null, because it has been cached");
266: $this->assertNotNull($objPerson->Id, "Id should not be null since it's always added to the select list");
267: $this->assertNotNull($objPerson->_ProjectAsManager->Id, "ProjectAsManager->Id should not be null since id's are always added to the select list");
268: $this->assertNotNull($objPerson->_ProjectAsManager->Name, "ProjectAsManager->Name should not be null since it was cached");
269: }
270:
271: }
272:
273: public function testMultiLeafExpansion() {
274: $objMilestone = Milestone::QuerySingle(
275: QQ::Equal (QQN::Milestone()->Id, 1),
276: QQ::Clause(
277: QQ::ExpandAsArray(QQN::Milestone()->Project->ManagerPerson->ProjectAsTeamMember),
278: QQ::ExpandAsArray(QQN::Milestone()->Project->PersonAsTeamMember)
279: )
280: );
281:
282: $objProjectArray = $objMilestone->Project->ManagerPerson->_ProjectAsTeamMemberArray;
283: $objPeopleArray = $objMilestone->Project->_PersonAsTeamMemberArray;
284:
285: $this->assertTrue(is_array($objProjectArray), "_ProjectAsTeamMemberArray is an array");
286: $this->assertEquals(2, count($objProjectArray), "_ProjectAsTeamMemberArray has 2 Project objects");
287:
288: $this->assertTrue(is_array($objPeopleArray), "_PersonAsTeamMemberArray is an array");
289: $this->assertEquals(5, count($objPeopleArray), "_PersonAsTeamMemberArray has 5 People objects");
290:
291:
292: $objLogin = Login::QuerySingle(
293: QQ::Equal (QQN::Login()->PersonId, 7),
294: QQ::Clause(
295: QQ::ExpandAsArray(QQN::Login()->Person->ProjectAsTeamMember),
296: QQ::ExpandAsArray(QQN::Login()->Person->ProjectAsManager)
297: )
298: );
299:
300: $objProjectArray = $objLogin->Person->_ProjectAsTeamMemberArray;
301:
302: $this->assertTrue(is_array($objProjectArray), "_ProjectAsTeamMemberArray is an array");
303: $this->assertEquals(2, count($objProjectArray), "_ProjectAsTeamMemberArray has 2 Project objects");
304:
305: $objProjectArray = $objLogin->Person->_ProjectAsManagerArray;
306:
307: $this->assertTrue(is_array($objProjectArray), "_ProjectAsManagerArray is an array");
308: $this->assertEquals(2, count($objProjectArray), "_ProjectAsManagerArray has 2 Project objects");
309:
310: }
311:
312: public function testConditionalExpansion() {
313: $clauses = QQ::Clause(
314: QQ::ExpandAsArray(QQN::Person()->Address),
315: QQ::Expand(QQN::Person()->ProjectAsManager, QQ::Equal (QQN::Person()->ProjectAsManager->ProjectStatusTypeId, ProjectStatusType::Open)),
316: QQ::ExpandAsArray(QQN::Person()->ProjectAsManager->Milestone),
317: QQ::OrderBy(QQN::Person()->Id)
318: );
319:
320: $targetPersonArray = Person::LoadAll (
321: $clauses
322: );
323:
324: $targetPerson = reset($targetPersonArray);
325:
326: $this->assertEquals ($targetPerson->Id, 1, "Person 1 found.");
327: $this->assertNotNull ($targetPerson->_ProjectAsManager, "Person 1 has a project.");
328:
329: $targetPerson = end($targetPersonArray);
330:
331: $this->assertEquals ($targetPerson->Id, 12, "Person 12 found.");
332: $this->assertNull ($targetPerson->_ProjectAsManager, "Person 12 does not have a project.");
333:
334:
335:
336: }
337:
338: }
339: