      1: <?php
      2:
      3: /**
      4:  * Test case for the RelationLookup data type
      5:  *
      6:  * @package alpha::tests
      7:  * @since 1.2.1
      8:  * @author John Collins <dev@alphaframework.org>
      9:  * @version $Id: RelationLookup_Test.php 1621 2012-12-20 12:33:13Z 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 RelationLookup_Test extends PHPUnit_Framework_TestCase {
     48:
     49:     /**
     50:      * Called before the test functions will be executed
     51:      * this function is defined in PHPUnit_TestCase and overwritten
     52:      * here
     53:      *
     54:      * @since 1.2.1
     55:      */
     56:     protected function setUp() {
     57:         $rights = new RightsObject();
     58:         $rights->rebuildTable();
     59:
     60:         $person = new PersonObject();
     61:         $person->rebuildTable();
     62:
     63:         $article = new ArticleObject();
     64:         $article->rebuildTable();
     65:     }
     66:
     67:     /**
     68:      * Called after the test functions are executed
     69:      * this function is defined in PHPUnit_TestCase and overwritten
     70:      * here
     71:      *
     72:      * @since 1.0
     73:      */
     74:     protected function tearDown() {
     75:         $person = new PersonObject();
     76:         $person->dropTable();
     77:
     78:         $rights = new RightsObject();
     79:         $rights->dropTable();
     80:         $rights->dropTable('Person2Rights');
     81:         $rights->dropTable('Person2Article');
     82:
     83:         $article = new ArticleObject();
     84:         $article->dropTable();
     85:     }
     86:
     87:     /**
     88:      * Testing the RelationLookup constructor
     89:      *
     90:      * @since 1.2.1
     91:      */
     92:     public function testConstruct() {
     93:
     94:         try{
     95:             $lookup = new RelationLookup('','');
     96:             $this->fail('testing the RelationLookup constructor');
     97:         }catch(IllegalArguementException $e) {
     98:             $this->assertEquals('Cannot create RelationLookup object without providing the left and right class names!', $e->getMessage(), 'testing the RelationLookup constructor');
     99:         }
    100:
    101:         $article = new ArticleObject();
    102:
    103:         try {
    104:             $article->dropTable();
    105:
    106:             $lookup = new RelationLookup('PersonObject','ArticleObject');
    107:             $this->fail('testing the RelationLookup constructor');
    108:         }catch(FailedLookupCreateException $e) {
    109:             $this->assertEquals('Error trying to create a lookup table [Person2Article], as tables for BOs [PersonObject] or [ArticleObject] don\'t exist!', $e->getMessage(), 'testing the RelationLookup constructor');
    110:         }
    111:
    112:         $article->rebuildTable();
    113:
    114:         $lookup = new RelationLookup('PersonObject','ArticleObject');
    115:
    116:         $this->assertTrue($lookup->checkTableExists(), 'testing the RelationLookup constructor');
    117:     }
    118:
    119:     /**
    120:      * Testing the getTableName() method
    121:      *
    122:      * @since 1.2.1
    123:      */
    124:     public function testGetTableName() {
    125:
    126:         $lookup = new RelationLookup('PersonObject','ArticleObject');
    127:         $this->assertEquals('Person2Article', $lookup->getTableName(), 'testing the getTableName() method');
    128:
    129:         $lookup = new RelationLookup('ArticleObject','PersonObject');
    130:         $this->assertEquals('Article2Person', $lookup->getTableName(), 'testing the getTableName() method');
    131:     }
    132:
    133:     /**
    134:      * Testing the setValue() method with good params
    135:      *
    136:      * @since 1.2.1
    137:      */
    138:     public function testSetValuePass() {
    139:         $lookup = new RelationLookup('PersonObject','ArticleObject');
    140:         $lookup->setValue(array(1,2));
    141:
    142:         $this->assertTrue(is_array($lookup->getValue()), 'testing the setValue() method with good params');
    143:         $this->assertTrue(in_array(2, $lookup->getValue()), 'testing the setValue() method with good params');
    144:     }
    145:
    146:     /**
    147:      * Testing the setValue() method with bad params
    148:      *
    149:      * @since 1.2.1
    150:      */
    151:     public function testSetValueFail() {
    152:         $lookup = new RelationLookup('PersonObject','ArticleObject');
    153:
    154:         try {
    155:             $lookup->setValue(2);
    156:             $this->fail('testing the setValue() method with bad params');
    157:         }catch (IllegalArguementException $e) {
    158:             $this->assertEquals('Array value passed to setValue is not valid [2], array should contain two OIDs', $e->getMessage(), 'testing the setValue() method with bad params');
    159:         }
    160:     }
    161:
    162:     /**
    163:      * Testing the loadAllbyAttribute() method
    164:      *
    165:      * @since 1.2.1
    166:      */
    167:     public function testLoadAllbyAttribute() {
    168:         $group = new RightsObject();
    169:         $group->set('name', 'unittestgroup');
    170:         $group->save();
    171:
    172:         $person1 = new PersonObject();
    173:         $person1->set('displayName', 'user1');
    174:         $person1->set('email', 'user1@test.com');
    175:         $person1->set('password', 'password');
    176:         $person1->save();
    177:         $lookup = $person1->getPropObject('rights')->getLookup();
    178:         $lookup->setValue(array($person1->getOID(), $group->getOID()));
    179:         $lookup->save();
    180:
    181:         $person2 = new PersonObject();
    182:         $person2->set('displayName', 'user2');
    183:         $person2->set('email', 'user2@test.com');
    184:         $person2->set('password', 'password');
    185:         $person2->save();
    186:         $lookup = $person2->getPropObject('rights')->getLookup();
    187:         $lookup->setValue(array($person2->getOID(), $group->getOID()));
    188:         $lookup->save();
    189:
    190:         //$person2->getPropObject('rights')->setValue($group->getOID());
    191:
    192:         $lookup = new RelationLookup('PersonObject','RightsObject');
    193:         $this->assertEquals(2, count($lookup->loadAllbyAttribute('rightID', $group->getOID())), 'testing the loadAllbyAttribute() method');
    194:     }
    195: }
    196:
    197: ?>