1<?php 2 3/** 4 * Doctrine_Ticket_638_TestCase 5 * 6 * @package Doctrine 7 * @author Tamcy <7am.online@gmail.com> 8 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 9 * @category Object Relational Mapping 10 * @link www.doctrine-project.org 11 * @since 1.0 12 * @version $Revision$ 13 */ 14 15class Doctrine_Ticket_638_TestCase extends Doctrine_UnitTestCase 16{ 17 public function prepareData() 18 { } 19 20 public function prepareTables() 21 { 22 $this->tables = array('T638_Student', 'T638_Course', 'T638_StudentCourse'); 23 parent::prepareTables(); 24 } 25 26 protected function newCourse($id, $name) 27 { 28 $course = new T638_Course(); 29 $course->id = $id; 30 $course->name = $name; 31 $course->save(); 32 return $course; 33 } 34 35 protected function newStudent($id, $name) 36 { 37 $u = new T638_Student(); 38 $u->id = $id; 39 $u->name = $name; 40 $u->group_id = 1; 41 $u->save(); 42 return $u; 43 } 44 45 protected function newStudentCourse($student, $course) 46 { 47 $sc = new T638_StudentCourse; 48 $sc->student_id = $student->id; 49 $sc->course_id = $course->id; 50 $sc->save(); 51 return $sc; 52 } 53 54 public function testTicket() 55 { 56 $student1 = $this->newStudent('07090002', 'First Student'); 57 $course1 = $this->newCourse('MATH001', 'Maths'); 58 $course2 = $this->newCourse('ENG002', 'English Literature'); 59 60 $sc = new T638_StudentCourse; 61 $sc->set('Student', $student1); 62 $sc->set('Course', $course1); 63 64 if ($student1->get('id') instanceof T638_StudentCourse) 65 { 66 $this->fail('Student Id incorrectly replaced!'); 67 } 68 else 69 { 70 $this->pass(); 71 } 72 73 if ($student1->get('id') != '07090002') 74 { 75 $this->fail('Student Id is not correct after assignment!'); 76 } 77 else 78 { 79 $this->pass(); 80 } 81 82 if ($course1->get('id') instanceof T638_StudentCourse) 83 { 84 $this->fail('Course Id incorrectly replaced!'); 85 } 86 else 87 { 88 $this->pass(); 89 } 90 91 if ($course1->get('id') != 'MATH001') 92 { 93 $this->fail('Course Id is not correct after assignment!'); 94 } 95 else 96 { 97 $this->pass(); 98 } 99 100 $this->assertEqual($sc->get('student_id'), '07090002'); 101 $this->assertEqual($sc->get('course_id'), 'MATH001'); 102 $this->assertIdentical($sc->get('Student'), $student1); 103 $this->assertIdentical($sc->get('Course'), $course1); 104 } 105} 106 107 108class T638_Student extends Doctrine_Record 109{ 110 public function setTableDefinition() 111 { 112 $this->setTableName('T638_student'); 113 114 $this->hasColumn('s_id as id', 'varchar', 30, array ( 'primary' => true,)); 115 $this->hasColumn('s_g_id as group_id', 'varchar', 30, array ('notnull'=>true)); 116 $this->hasColumn('s_name as name', 'varchar', 50, array ('notnull'=>true)); 117 } 118 119 public function setUp() 120 { 121 } 122} 123 124class T638_Course extends Doctrine_Record 125{ 126 public function setTableDefinition() 127 { 128 $this->setTableName('T638_course'); 129 130 $this->hasColumn('c_id as id', 'varchar', 20, array ( 'primary' => true,)); 131 $this->hasColumn('c_name as name', 'varchar', 50, array ('notnull'=>true)); 132 } 133 134 public function setUp() 135 { 136 } 137 138 public function set($fieldName, $value, $load = true) 139 { 140 parent::set($fieldName, $value, $load); 141 } 142} 143 144class T638_StudentCourse extends Doctrine_Record 145{ 146 public function setTableDefinition() 147 { 148 $this->setTableName('T638_Student_course'); 149 150 $this->hasColumn('sc_student_id as student_id', 'varchar', 30, array ( 'primary' => true,)); 151 $this->hasColumn('sc_course_id as course_id', 'varchar', 20, array ( 'primary' => true,)); 152 $this->hasColumn('sc_remark as remark', 'varchar', 500, array ('notnull'=>true)); 153 } 154 155 public function setUp() 156 { 157 $this->hasOne('T638_Student as Student', array('local' => 'sc_student_id', 'foreign' => 's_id')); 158 $this->hasOne('T638_Course as Course', array('local' => 'sc_course_id', 'foreign' => 'c_id')); 159 } 160} 161 162