Overview

Packages

  • alpha::controller
  • alpha::controller::front
  • alpha::exceptions
  • alpha::model
  • alpha::model::types
  • alpha::tasks
  • alpha::tests
  • alpha::util
  • alpha::util::cache
  • alpha::util::codehighlight
  • alpha::util::convertors
  • alpha::util::feeds
  • alpha::util::filters
  • alpha::util::graphs
  • alpha::util::helpers
  • alpha::util::metrics
  • alpha::view
  • alpha::view::renderers
  • alpha::view::widgets

Classes

  • AlphaAgentUtils_Test
  • AlphaConfig_Test
  • AlphaController_Test
  • AlphaDAO_Test
  • AlphaDAOProviderFactory_Test
  • AlphaFeed_Test
  • AlphaFilters_Test
  • AlphaPHPServerUtils_Test
  • AlphaValidator_Test
  • AlphaView_Test
  • Boolean_Test
  • Date_Test
  • DEnum_Test
  • Double_Test
  • Enum_Test
  • Exceptions_Test
  • FrontController_Test
  • Image_Test
  • Integer_Test
  • Relation_Test
  • RelationLookup_Test
  • Sequence_Test
  • String_Test
  • Tag_Test
  • Text_Test
  • Timestamp_Test
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * Test case for the Relation data type
  5:  *
  6:  * @package alpha::tests
  7:  * @since 1.0
  8:  * @author John Collins <dev@alphaframework.org>
  9:  * @version $Id: Relation_Test.php 1617 2012-12-19 15:05:31Z alphadevx $
 10:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 11:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
 12:  * All rights reserved.
 13:  *
 14:  * <pre>
 15:  * Redistribution and use in source and binary forms, with or
 16:  * without modification, are permitted provided that the
 17:  * following conditions are met:
 18:  *
 19:  * * Redistributions of source code must retain the above
 20:  *   copyright notice, this list of conditions and the
 21:  *   following disclaimer.
 22:  * * Redistributions in binary form must reproduce the above
 23:  *   copyright notice, this list of conditions and the
 24:  *   following disclaimer in the documentation and/or other
 25:  *   materials provided with the distribution.
 26:  * * Neither the name of the Alpha Framework nor the names
 27:  *   of its contributors may be used to endorse or promote
 28:  *   products derived from this software without specific
 29:  *   prior written permission.
 30:  *
 31:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 32:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 33:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 35:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 36:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 37:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 38:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 39:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 40:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 41:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 42:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 43:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 44:  * </pre>
 45:  *
 46:  */
 47: class Relation_Test extends PHPUnit_Framework_TestCase {
 48:     /**
 49:      * A Relation for testing
 50:      *
 51:      * @var Relation
 52:      * @since 1.0
 53:      */
 54:     private $rel1;
 55: 
 56:     /**
 57:      * A PersonObject for testing
 58:      *
 59:      * @var PersonObject
 60:      * @since 1.2.1
 61:      */
 62:     private $person;
 63: 
 64:     /**
 65:      * Called before the test functions will be executed
 66:      * this function is defined in PHPUnit_TestCase and overwritten
 67:      * here
 68:      *
 69:      * @since 1.0
 70:      */
 71:     protected function setUp() {
 72:         $this->rel1 = new Relation();
 73: 
 74:         $rights = new RightsObject();
 75:         $rights->rebuildTable();
 76: 
 77:         $article = new ArticleObject();
 78:         $article->rebuildTable();
 79: 
 80:         $comment = new ArticleCommentObject();
 81:         $comment->rebuildTable();
 82: 
 83:         $this->person = new PersonObject();
 84:         $this->person->set('displayName', $_SESSION['currentUser']->getDisplayName());
 85:         $this->person->set('email', $_SESSION['currentUser']->get('email'));
 86:         $this->person->set('password', 'password');
 87:         $this->person->rebuildTable();
 88:         $this->person->save();
 89:     }
 90: 
 91:     /**
 92:      * Called after the test functions are executed
 93:      * this function is defined in PHPUnit_TestCase and overwritten
 94:      * here
 95:      *
 96:      * @since 1.0
 97:      */
 98:     protected function tearDown() {
 99:         unset($this->rel1);
100:         $person = new PersonObject();
101:         $person->dropTable();
102: 
103:         $rights = new RightsObject();
104:         $rights->dropTable();
105:         $rights->dropTable('Person2Rights');
106: 
107:         $comment = new ArticleCommentObject();
108:         $comment->dropTable();
109: 
110:         $article = new ArticleObject();
111:         $article->dropTable();
112:     }
113: 
114:     /**
115:      * Testing passing a valid BO name to setRelatedClass
116:      *
117:      * @since 1.0
118:      */
119:     public function testSetRelatedClassPass() {
120:         try {
121:             $this->rel1->setRelatedClass('ArticleObject');
122:         }catch (AlphaException $e) {
123:             $this->fail('Testing passing a valid BO name to setRelatedClass');
124:         }
125:     }
126: 
127:     /**
128:      * Testing passing an invalid BO name to setRelatedClass
129:      *
130:      * @since 1.0
131:      */
132:     public function testSetRelatedClassFail() {
133:         try {
134:             $this->rel1->setRelatedClass('XyzObject');
135:             $this->fail('Testing passing an invalid BO name to setRelatedClass');
136:         }catch (AlphaException $e) {
137:             $this->assertEquals('The class [XyzObject] is not defined anywhere!'
138:                 , $e->getMessage()
139:                 , 'Testing passing an invalid BO name to setRelatedClass');
140:         }
141:     }
142: 
143:     /**
144:      * Testing passing a valid field name to setRelatedClassField
145:      *
146:      * @since 1.0
147:      */
148:     public function testSetRelatedClassFieldPass() {
149:         try {
150:             $this->rel1->setRelatedClass('PersonObject');
151:             $this->rel1->setRelatedClassField('email');
152:         }catch (AlphaException $e) {
153:             $this->fail('Testing passing a valid field name to setRelatedClassField');
154:         }
155:     }
156: 
157:     /**
158:      * Testing passing an invalid field name to setRelatedClassField
159:      *
160:      * @since 1.0
161:      */
162:     public function testSetRelatedClassFieldFail() {
163:         try {
164:             $this->rel1->setRelatedClass('PersonObject');
165:             $this->rel1->setRelatedClassField('doesNotExist');
166:             $this->fail('Testing passing an invalid field name to setRelatedClassField');
167:         }catch (AlphaException $e) {
168:             $this->assertEquals('The field [doesNotExist] was not found in the class [PersonObject]'
169:                 , $e->getMessage()
170:                 , 'Testing passing an invalid field name to setRelatedClassField');
171:         }
172:     }
173: 
174:     /**
175:      * Testing passing a valid type name to setRelationType
176:      *
177:      * @since 1.0
178:      */
179:     public function testSetRelationTypePass() {
180:         try {
181:             $this->rel1->setRelationType('MANY-TO-ONE');
182:         }catch (AlphaException $e) {
183:             $this->fail('Testing passing a valid type name to setRelationType');
184:         }
185:     }
186: 
187:     /**
188:      * Testing passing an invalid type name to setRelationType
189:      *
190:      * @since 1.0
191:      */
192:     public function testSetRelationTypeFail() {
193:         try {
194:             $this->rel1->setRelationType('blah');
195:             $this->fail('Testing passing an invalid type name to setRelationType');
196:         }catch (AlphaException $e) {
197:             $this->assertEquals('Relation type of [blah] is invalid!'
198:                 , $e->getMessage()
199:                 , 'Testing passing an invalid type name to setRelationType');
200:         }
201:     }
202: 
203:     /**
204:      * Testing setValue method with a valid value
205:      *
206:      * @since 1.0
207:      */
208:     public function testSetValuePass() {
209:         try {
210:             $this->rel1->setValue(100);
211:             $this->rel1->setValue('2777');
212:         }catch (AlphaException $e) {
213:             $this->fail('Testing setValue method with a valid value');
214:         }
215:     }
216: 
217:     /**
218:      * Testing setValue method with an invalid value
219:      *
220:      * @since 1.0
221:      */
222:     public function testSetValueFail() {
223:         try {
224:             $this->rel1->setValue('xyz');
225:             $this->fail('Testing setValue method with an invalid value');
226:         }catch (AlphaException $e) {
227:             $this->assertEquals('[xyz] not a valid Relation value!  A maximum of 11 characters is allowed.'
228:                 , $e->getMessage()
229:                 , 'Testing setValue method with an invalid value');
230:         }
231:     }
232: 
233:     /**
234:      * Testing that the display field value of the related class is accessed correctly
235:      *
236:      * @since 1.0
237:      */
238:     public function testSetRelatedClassDisplayFieldPass() {
239:         try {
240:             $this->rel1->setRelatedClass('PersonObject');
241:             // assuming here that user #1 is the default Administrator account
242:             $this->rel1->setValue(1);
243:             $this->rel1->setRelatedClassDisplayField('state');
244:             $this->assertEquals('Active', $this->rel1->getRelatedClassDisplayFieldValue(), 'Testing that the display field value of the related class is accessed correctly');          
245:         }catch (AlphaException $e) {
246:             $this->fail('Testing that the display field value of the related class is accessed correctly');
247:         }
248:     }
249: 
250:     /**
251:      * Testing that getRelatedClassDisplayFieldValue() will fail to load an invalid class definition
252:      *
253:      * @since 1.0
254:      */
255:     public function testGetRelatedClassDisplayFieldValueFail() {
256:         try {
257:             $this->rel1->setRelatedClassDisplayField('someField');
258:             $value = $this->rel1->getRelatedClassDisplayFieldValue();
259:             $this->fail('Testing that getRelatedClassDisplayFieldValue() will fail to load an invalid class definition');
260:         }catch (AlphaException $e) {
261:             $this->assertEquals('The class [] is not defined anywhere!'
262:                 , $e->getMessage()
263:                 , 'Testing that getRelatedClassDisplayFieldValue() will fail to load an invalid class definition');
264:         }
265:     }
266: 
267:     /**
268:      * Testing the getRelatedClassDisplayFieldValue() method on ONE-TO-MANY and MANY-TO-MANY relations
269:      *
270:      * @since 1.2.1
271:      */
272:     public function testGetRelatedClassDisplayFieldValuePass() {
273:         $oneToManyRel = new Relation();
274:         $oneToManyRel->setRelatedClass('PersonObject');
275:         $oneToManyRel->setRelatedClassField('OID');
276:         $oneToManyRel->setRelatedClassDisplayField('displayName');
277:         $oneToManyRel->setRelationType('ONE-TO-MANY');
278:         $oneToManyRel->setValue($this->person->getOID());
279: 
280:         $this->assertEquals($_SESSION['currentUser']->getDisplayName(), $oneToManyRel->getRelatedClassDisplayFieldValue(), 'testing the getRelatedClassDisplayFieldValue() method on ONE-TO-MANY relation');
281: 
282:         $group = new RightsObject();
283:         $group->set('name', 'unittestgroup');
284:         $group->save();
285: 
286:         $person1 = new PersonObject();
287:         $person1->set('displayName', 'user1');
288:         $person1->set('email', 'user1@test.com');
289:         $person1->set('password', 'password');
290:         $person1->save();
291:         $lookup = $person1->getPropObject('rights')->getLookup();
292:         $lookup->setValue(array($person1->getOID(), $group->getOID()));
293:         $lookup->save();
294: 
295:         $person2 = new PersonObject();
296:         $person2->set('displayName', 'user2');
297:         $person2->set('email', 'user2@test.com');
298:         $person2->set('password', 'password');
299:         $person2->save();
300:         $lookup = $person2->getPropObject('rights')->getLookup();
301:         $lookup->setValue(array($person2->getOID(), $group->getOID()));
302:         $lookup->save();
303: 
304:         $person2->getPropObject('rights')->setValue($group->getOID());
305: 
306:         try {
307:             $this->assertEquals('user1@test.com,user2@test.com', $person2->getPropObject('rights')->getRelatedClassDisplayFieldValue(), 'testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation');
308:             $this->fail('testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation');
309:         }catch (IllegalArguementException $e) {
310:             $this->assertEquals($e->getMessage(), 'Tried to load related MANY-TO-MANY fields but no accessingClassName parameter set on the call to getRelatedClassDisplayFieldValue!', 'testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation');
311:         }
312: 
313:         $this->assertEquals('user1@test.com,user2@test.com', $person2->getPropObject('rights')->getRelatedClassDisplayFieldValue('RightsObject'), 'testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation');
314:     }
315: 
316:     /**
317:      * Testing the getRelatedClass() method with different relation types.
318:      *
319:      * @since 1.2.1
320:      */
321:     public function testGetRelatedClass() {
322:         $oneToOneRel = new Relation();
323:         $oneToOneRel->setRelatedClass('ArticleCommentObject');
324:         $oneToOneRel->setRelatedClassField('articleOID');
325:         $oneToOneRel->setRelatedClassDisplayField('content');
326:         $oneToOneRel->setRelationType('ONE-TO-ONE');
327: 
328:         $this->assertEquals('ArticleCommentObject', $oneToOneRel->getRelatedClass(), 'testing the getRelatedClass() method on a ONE-TO-ONE relation');
329: 
330:         $oneToManyRel = new Relation();
331:         $oneToManyRel->setRelatedClass('ArticleCommentObject');
332:         $oneToManyRel->setRelatedClassField('articleOID');
333:         $oneToManyRel->setRelatedClassDisplayField('content');
334:         $oneToManyRel->setRelationType('ONE-TO-MANY');
335: 
336:         $this->assertEquals('ArticleCommentObject', $oneToManyRel->getRelatedClass(), 'testing the getRelatedClass() method on a ONE-TO-MANY relation');
337: 
338:         $manyToManyRel = new Relation();
339:         $manyToManyRel->setRelatedClass('PersonObject', 'left');
340:         $manyToManyRel->setRelatedClassDisplayField('email', 'left');
341:         $manyToManyRel->setRelatedClass('RightsObject', 'right');
342:         $manyToManyRel->setRelatedClassDisplayField('name', 'right');
343:         $manyToManyRel->setRelationType('MANY-TO-MANY');
344: 
345:         $this->assertEquals('PersonObject', $manyToManyRel->getRelatedClass('left'), 'testing the getRelatedClass() method on a MANY-TO-MANY relation');
346:         $this->assertEquals('RightsObject', $manyToManyRel->getRelatedClass('right'), 'testing the getRelatedClass() method on a MANY-TO-MANY relation');
347:     }
348: 
349:     /**
350:      * Testing the getSide() method on a MANY-TO-MANY relation
351:      *
352:      * @since 1.2.1
353:      */
354:     public function testGetSidePass() {
355: 
356:         $manyToManyRel = new Relation();
357:         $manyToManyRel->setRelatedClass('PersonObject', 'left');
358:         $manyToManyRel->setRelatedClassDisplayField('email', 'left');
359:         $manyToManyRel->setRelatedClass('RightsObject', 'right');
360:         $manyToManyRel->setRelatedClassDisplayField('name', 'right');
361:         $manyToManyRel->setRelationType('MANY-TO-MANY');
362: 
363:         $this->assertEquals('left', $manyToManyRel->getSide('PersonObject'), 'testing the getSide() method on a MANY-TO-MANY relation');
364:         $this->assertEquals('right', $manyToManyRel->getSide('RightsObject'), 'testing the getSide() method on a MANY-TO-MANY relation');
365:     }
366: 
367:     /**
368:      * Testing the getSide() method on a ONE-TO-MANY relation
369:      *
370:      * @since 1.2.1
371:      */
372:     public function testGetSideFail() {
373: 
374:         $oneToManyRel = new Relation();
375:         $oneToManyRel->setRelatedClass('ArticleCommentObject');
376:         $oneToManyRel->setRelatedClassField('articleOID');
377:         $oneToManyRel->setRelatedClassDisplayField('content');
378:         $oneToManyRel->setRelationType('ONE-TO-MANY');
379: 
380:         try{
381:             $oneToManyRel->getSide('ArticleCommentObject');
382:             $this->fail('testing the getSide() method on a ONE-TO-MANY relation');
383:         }catch (IllegalArguementException $e) {
384:             $this->assertEquals('Error trying to determine the MANY-TO-MANY relationship side for the classname [ArticleCommentObject]', $e->getMessage(), 'testing the getSide() method on a ONE-TO-MANY relation');
385:         }
386: 
387:     }
388: 
389:     /**
390:      * Testing the getRelatedObject method
391:      *
392:      * @since 1.2.1
393:      */
394:     public function testGetRelatedObject() {
395:         $oneToOneRel = new Relation();
396:         $oneToOneRel->setRelatedClass('PersonObject');
397:         $oneToOneRel->setRelatedClassField('OID');
398:         $oneToOneRel->setRelatedClassDisplayField('displayName');
399:         $oneToOneRel->setRelationType('ONE-TO-ONE');
400:         $oneToOneRel->setValue($this->person->getOID());
401: 
402:         $this->assertEquals($_SESSION['currentUser']->getDisplayName(), $oneToOneRel->getRelatedObject()->get('displayName'), 'testing the getRelatedObject method');
403:     }
404: 
405:     /**
406:      * Testing the getRelatedObjects method with a ONE-TO-MANY and MANY-TO-MANY relation
407:      *
408:      * @since 1.2.1
409:      */
410:     public function testGetRelatedObjects() {
411: 
412:         $group = new RightsObject();
413:         $group->set('name', 'unittestgroup');
414:         $group->save();
415: 
416:         $person1 = new PersonObject();
417:         $person1->set('displayName', 'user1');
418:         $person1->set('email', 'user1@test.com');
419:         $person1->set('password', 'password');
420:         $person1->save();
421:         $lookup = $person1->getPropObject('rights')->getLookup();
422:         $lookup->setValue(array($person1->getOID(), $group->getOID()));
423:         $lookup->save();
424: 
425:         $person2 = new PersonObject();
426:         $person2->set('displayName', 'user2');
427:         $person2->set('email', 'user2@test.com');
428:         $person2->set('password', 'password');
429:         $person2->save();
430:         $lookup = $person2->getPropObject('rights')->getLookup();
431:         $lookup->setValue(array($person2->getOID(), $group->getOID()));
432:         $lookup->save();
433: 
434:         $person2->getPropObject('rights')->setValue($group->getOID());
435: 
436:         $this->assertEquals(2, count($group->getPropObject('members')->getRelatedObjects('RightsObject')), 'testing the getRelatedObjects method with a MANY-TO-MANY relation');
437:         $this->assertTrue($group->getPropObject('members')->getRelatedObjects('RightsObject')[0] instanceof PersonObject, 'testing the getRelatedObjects method with a MANY-TO-MANY relation');
438: 
439:         $article = new ArticleObject();
440:         $article->set('title', 'unit test');
441:         $article->set('description', 'unit test');
442:         $article->set('content', 'unit test');
443:         $article->set('author', 'unit test');
444:         $article->save();
445: 
446:         $comment1 = new ArticleCommentObject();
447:         $comment1->set('content', 'unit test');
448:         $comment1->getPropObject('articleOID')->setValue($article->getOID());
449:         $comment1->save();
450: 
451:         $comment2 = new ArticleCommentObject();
452:         $comment2->set('content', 'unit test');
453:         $comment2->getPropObject('articleOID')->setValue($article->getOID());
454:         $comment2->save();
455: 
456:         $this->assertEquals(2, count($article->getPropObject('comments')->getRelatedObjects()), 'testing the getRelatedObjects method with a ONE-TO-MANY relation');
457:         $this->assertTrue($article->getPropObject('comments')->getRelatedObjects()[0] instanceof ArticleCommentObject, 'testing the getRelatedObjects method with a ONE-TO-MANY relation');
458:     }
459: }
460: 
461: ?>
Alpha Framework ${alpha.version.new} API Documentation API documentation generated by ApiGen 2.8.0