| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class Doctrine_Inheritance_Joined_TestCase extends Doctrine_UnitTestCase |
|---|
| 4 | { |
|---|
| 5 | public function prepareData() |
|---|
| 6 | { } |
|---|
| 7 | |
|---|
| 8 | public function prepareTables() |
|---|
| 9 | { |
|---|
| 10 | $this->tables[] = 'CTI_User'; |
|---|
| 11 | $this->tables[] = 'CTI_Manager'; |
|---|
| 12 | $this->tables[] = 'CTI_Customer'; |
|---|
| 13 | $this->tables[] = 'CTI_SuperManager'; |
|---|
| 14 | |
|---|
| 15 | parent::prepareTables(); |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | public function setUp() |
|---|
| 19 | { |
|---|
| 20 | parent::setUp(); |
|---|
| 21 | $this->prepareTables(); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | public function testMetadataSetup() |
|---|
| 25 | { |
|---|
| 26 | $suManagerTable = $this->conn->getMetadata('CTI_SuperManager'); |
|---|
| 27 | $userTable = $this->conn->getMetadata('CTI_User'); |
|---|
| 28 | $customerTable = $this->conn->getMetadata('CTI_Customer'); |
|---|
| 29 | $managerTable = $this->conn->getMetadata('CTI_Manager'); |
|---|
| 30 | $this->assertTrue($suManagerTable !== $userTable); |
|---|
| 31 | $this->assertTrue($suManagerTable !== $customerTable); |
|---|
| 32 | $this->assertTrue($userTable !== $customerTable); |
|---|
| 33 | $this->assertTrue($managerTable !== $suManagerTable); |
|---|
| 34 | |
|---|
| 35 | // expected column counts |
|---|
| 36 | $this->assertEqual(6, count($suManagerTable->getColumns())); |
|---|
| 37 | $this->assertEqual(4, count($userTable->getColumns())); |
|---|
| 38 | $this->assertEqual(5, count($managerTable->getColumns())); |
|---|
| 39 | $this->assertEqual(5, count($customerTable->getColumns())); |
|---|
| 40 | |
|---|
| 41 | // expected table names |
|---|
| 42 | $this->assertEqual('cti_user', $userTable->getTableName()); |
|---|
| 43 | $this->assertEqual('cti_manager', $managerTable->getTableName()); |
|---|
| 44 | $this->assertEqual('cti_customer', $customerTable->getTableName()); |
|---|
| 45 | $this->assertEqual('cti_supermanager', $suManagerTable->getTableName()); |
|---|
| 46 | |
|---|
| 47 | // expected joined parents option |
|---|
| 48 | $this->assertEqual(array(), $userTable->getParentClasses()); |
|---|
| 49 | $this->assertEqual(array('CTI_User'), $managerTable->getParentClasses()); |
|---|
| 50 | $this->assertEqual(array('CTI_User'), $customerTable->getParentClasses()); |
|---|
| 51 | $this->assertEqual(array('CTI_Manager', 'CTI_User'), $suManagerTable->getParentClasses()); |
|---|
| 52 | |
|---|
| 53 | // check inheritance map |
|---|
| 54 | $this->assertEqual(array(1 => 'CTI_User', 2 => 'CTI_Manager', |
|---|
| 55 | 3 => 'CTI_Customer', 4 => 'CTI_SuperManager'), $userTable->getInheritanceOption('discriminatorMap')); |
|---|
| 56 | |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | protected function _createManager() |
|---|
| 60 | { |
|---|
| 61 | $manager = new CTI_Manager(); |
|---|
| 62 | $manager->salary = 80000; |
|---|
| 63 | $manager->name = 'John Smith'; |
|---|
| 64 | try { |
|---|
| 65 | $manager->save(); |
|---|
| 66 | $this->pass(); |
|---|
| 67 | return $manager; |
|---|
| 68 | } catch (Exception $e) { |
|---|
| 69 | $this->fail("Inserting record in class table inheritance failed: " . $e->getMessage()); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | protected function _createSuperManager() |
|---|
| 74 | { |
|---|
| 75 | $manager = new CTI_SuperManager(); |
|---|
| 76 | $manager->salary = 1000000; |
|---|
| 77 | $manager->name = 'Bill Gates'; |
|---|
| 78 | $manager->gosutitle = 'BillyBoy'; |
|---|
| 79 | try { |
|---|
| 80 | $manager->save(); |
|---|
| 81 | $this->pass(); |
|---|
| 82 | return $manager; |
|---|
| 83 | } catch (Exception $e) { |
|---|
| 84 | $this->fail("Inserting record in class table inheritance failed: " . $e->getMessage()); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | public function testSaveInsertsDataAcrossJoinedTablesTransparently() |
|---|
| 90 | { |
|---|
| 91 | $manager = $this->_createManager(); |
|---|
| 92 | $this->assertEqual(1, $manager->id); |
|---|
| 93 | $this->assertEqual(80000, $manager->salary); |
|---|
| 94 | $this->assertEqual('John Smith', $manager->name); |
|---|
| 95 | $this->assertTrue($manager instanceof CTI_Manager); |
|---|
| 96 | |
|---|
| 97 | $superManager = $this->_createSuperManager(); |
|---|
| 98 | $this->assertEqual(2, $superManager->id); |
|---|
| 99 | $this->assertEqual(1000000, $superManager->salary); |
|---|
| 100 | $this->assertEqual('Bill Gates', $superManager->name); |
|---|
| 101 | $this->assertEqual('BillyBoy', $superManager->gosutitle); |
|---|
| 102 | $this->assertTrue($superManager instanceof CTI_SuperManager); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | public function testUpdateUpdatesOnlyChangedFields() |
|---|
| 106 | { |
|---|
| 107 | $manager = $this->_createManager(); |
|---|
| 108 | try { |
|---|
| 109 | $manager->salary = 12; |
|---|
| 110 | $manager->save(); |
|---|
| 111 | $this->pass(); |
|---|
| 112 | } catch (Exception $e) { |
|---|
| 113 | $this->fail("Update failed [{$e->getMessage()}]."); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | public function testUpdateUpdatesDataAcrossJoinedTablesTransparently() |
|---|
| 119 | { |
|---|
| 120 | $manager = $this->_createManager(); |
|---|
| 121 | $manager->salary = 90000; // he got a pay rise... |
|---|
| 122 | $manager->name = 'John Locke'; // he got married ... |
|---|
| 123 | try { |
|---|
| 124 | $manager->save(); |
|---|
| 125 | $this->pass(); |
|---|
| 126 | } catch (Exception $e) { |
|---|
| 127 | $this->fail("Updating record in class table inheritance failed: " . $e->getMessage()); |
|---|
| 128 | } |
|---|
| 129 | $this->assertEqual(1, $manager->id); |
|---|
| 130 | $this->assertEqual(90000, $manager->salary); |
|---|
| 131 | $this->assertEqual('John Locke', $manager->name); |
|---|
| 132 | $this->assertTrue($manager instanceof CTI_Manager); |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | $superManager = $this->_createSuperManager(); |
|---|
| 136 | $superManager->salary = 0; // he got fired... |
|---|
| 137 | $superManager->name = 'Bill Clinton'; // he got married ... again |
|---|
| 138 | $superManager->gosutitle = 'Billy the Kid'; // ... and went mad |
|---|
| 139 | try { |
|---|
| 140 | $superManager->save(); |
|---|
| 141 | $this->pass(); |
|---|
| 142 | } catch (Exception $e) { |
|---|
| 143 | $this->fail("Updating record in class table inheritance failed: " . $e->getMessage()); |
|---|
| 144 | } |
|---|
| 145 | $this->assertEqual(2, $superManager->id); |
|---|
| 146 | $this->assertEqual(0, $superManager->salary); |
|---|
| 147 | $this->assertEqual('Bill Clinton', $superManager->name); |
|---|
| 148 | $this->assertEqual('Billy the Kid', $superManager->gosutitle); |
|---|
| 149 | $this->assertTrue($superManager instanceof CTI_SuperManager); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | public function testDqlQueryJoinsTransparentlyAcrossParents() |
|---|
| 153 | { |
|---|
| 154 | $this->_createManager(); |
|---|
| 155 | $this->conn->clear('CTI_Manager'); |
|---|
| 156 | |
|---|
| 157 | $query = $this->conn->createQuery(); |
|---|
| 158 | $query->parseQuery("SELECT m.* FROM CTI_Manager m"); |
|---|
| 159 | $manager = $query->execute()->getFirst(); |
|---|
| 160 | |
|---|
| 161 | $this->assertTrue($manager instanceof CTI_Manager); |
|---|
| 162 | $this->assertEqual(1, $manager->id); |
|---|
| 163 | $this->assertEqual(80000, $manager->salary); |
|---|
| 164 | $this->assertEqual('John Smith', $manager->name); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | public function testQueryingBaseClassOuterJoinsSubClassesAndReturnsSubclassInstances() |
|---|
| 168 | { |
|---|
| 169 | $this->_createManager(); |
|---|
| 170 | $this->conn->clear('CTI_Manager'); |
|---|
| 171 | $this->conn->clear('CTI_User'); |
|---|
| 172 | |
|---|
| 173 | $query = $this->conn->createQuery(); |
|---|
| 174 | $query->parseQuery("SELECT u.* FROM CTI_User u"); |
|---|
| 175 | //echo $query->getSql(); |
|---|
| 176 | $user = $query->execute()->getFirst(); |
|---|
| 177 | |
|---|
| 178 | $this->assertTrue($user instanceof CTI_Manager); |
|---|
| 179 | $this->assertEqual(1, $user->id); |
|---|
| 180 | $this->assertEqual(80000, $user->salary); |
|---|
| 181 | $this->assertEqual('John Smith', $user->name); |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | class CTI_User extends Doctrine_Entity |
|---|
| 187 | { |
|---|
| 188 | public static function initMetadata($class) |
|---|
| 189 | { |
|---|
| 190 | $class->setInheritanceType(Doctrine::INHERITANCE_TYPE_JOINED, array( |
|---|
| 191 | 'discriminatorColumn' => 'dtype', |
|---|
| 192 | 'discriminatorMap' => array(1 => 'CTI_User', 2 => 'CTI_Manager', |
|---|
| 193 | 3 => 'CTI_Customer', 4 => 'CTI_SuperManager') |
|---|
| 194 | )); |
|---|
| 195 | $class->setSubclasses(array('CTI_Manager', 'CTI_Customer', 'CTI_SuperManager')); |
|---|
| 196 | $class->setTableName('cti_user'); |
|---|
| 197 | $class->setColumn('cti_id as id', 'integer', 4, array('primary' => true, 'autoincrement' => true)); |
|---|
| 198 | $class->setColumn('cti_foo as foo', 'integer', 4); |
|---|
| 199 | $class->setColumn('cti_name as name', 'string', 50, array('notnull' => true)); |
|---|
| 200 | $class->setColumn('dtype', 'integer', 2); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | class CTI_Manager extends CTI_User |
|---|
| 205 | { |
|---|
| 206 | protected $name; |
|---|
| 207 | |
|---|
| 208 | public static function initMetadata($class) |
|---|
| 209 | { |
|---|
| 210 | $class->setTableName('cti_manager'); |
|---|
| 211 | $class->setSubclasses(array('CTI_SuperManager')); |
|---|
| 212 | $class->setColumn('ctim_salary as salary', 'varchar', 50, array()); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | class CTI_Customer extends CTI_User |
|---|
| 217 | { |
|---|
| 218 | public static function initMetadata($class) |
|---|
| 219 | { |
|---|
| 220 | $class->setTableName('cti_customer'); |
|---|
| 221 | $class->setColumn('ctic_bonuspoints as bonuspoints', 'varchar', 50, array()); |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | class CTI_SuperManager extends CTI_Manager |
|---|
| 226 | { |
|---|
| 227 | public static function initMetadata($class) |
|---|
| 228 | { |
|---|
| 229 | $class->setTableName('cti_supermanager'); |
|---|
| 230 | $class->setColumn('ctism_gosutitle as gosutitle', 'varchar', 50, array()); |
|---|
| 231 | } |
|---|
| 232 | } |
|---|