1<?php
2class Doctrine_Ticket_1113_TestCase extends Doctrine_UnitTestCase
3{
4    public function prepareData()
5    { }
6    public function prepareTables()
7    {
8        $this->tables = array('VIH_Model_Course', 'VIH_Model_Course_Period', 'VIH_Model_Course_SubjectGroup', 'VIH_Model_Subject', 'VIH_Model_Course_SubjectGroup_Subject', 'VIH_Model_Course_Registration', 'VIH_Model_Course_Registration_Subject');
9
10        parent::prepareTables();
11    }
12
13    public function testSubjectsCanBeRetrievedWhenReopeningTheRegistrationEvenThoughNoSubjectsWasSavedInitally()
14    {
15        $course1 = new VIH_Model_Course();
16        $course1->navn = 'Course 1';
17
18        $period1 = new VIH_Model_Course_Period();
19        $period1->name = 'Period 1';
20        $period1->Course = $course1;
21        $period1->save();
22
23        $group1 = new VIH_Model_Course_SubjectGroup();
24        $group1->name = 'SubjectGroup 1';
25        $group1->Period = $period1;
26
27        $subject1 = new VIH_Model_Subject();
28        $subject1->identifier = 'Subject 1';
29
30        $subject2 = new VIH_Model_Subject();
31        $subject2->identifier = 'Subject 2';
32
33        $group1->Subjects[] = $subject1;
34        $group1->Subjects[] = $subject2;
35
36        $group1->save();
37
38        $group1->Subjects[] = $subject1;
39        $group1->Subjects[] = $subject2;
40        $group1->save();
41
42        $course1->SubjectGroups[] = $group1;
43
44        $course1->save();
45
46        // saved without Subjects
47        try {
48            $registrar = new VIH_Model_Course_Registration();
49            $registrar->Course = $course1;
50            // $registrar->Subjects; // if this is uncommented the test will pass
51            $registrar->save();
52        } catch (Doctrine_Record_Exception $e) {
53            $this->fail($e->getMessage());
54        }
55
56        $reopend = Doctrine_Core::getTable('VIH_Model_Course_Registration')->findOneById($registrar->id);
57
58        try {
59            $reopend->Subjects[] = $subject1;
60        } catch (Doctrine_Record_Exception $e) {
61            $this->fail($e->getMessage());
62        }
63
64        $reopend->save();
65
66        try {
67            $subject = $reopend->Subjects[0];
68            $this->assertTrue(is_object($subject));
69            $this->assertEqual('VIH_Model_Subject', get_class($reopend->Subjects[0]));
70        } catch (Doctrine_Record_Exception $e) {
71            $this->fail($e->getMessage());
72        }
73
74    }
75}
76
77class VIH_Model_Subject extends Doctrine_Record
78{
79    public function setTableDefinition()
80    {
81        $this->hasColumn('identifier', 'string', 255);
82        $this->hasColumn('navn', 'string', 255);
83        $this->hasColumn('active', 'boolean');
84    }
85
86    public function setUp()
87    {
88        $this->hasMany(
89            'VIH_Model_Course_SubjectGroup as SubjectGroups',
90            array(
91                'refClass' => 'VIH_Model_Course_SubjectGroup_Subject',
92                'local'    => 'subject_id',
93                'foreign'  => 'subject_group_id'
94            )
95        );
96
97        $this->hasMany(
98            'VIH_Model_Course_Registration as Registrations',
99            array(
100                'refClass' => 'VIH_Model_Course_Registration_Subject',
101                'local'    => 'subject_id',
102                'foreign'  => 'registration_id'
103            )
104        );
105    }
106}
107
108class VIH_Model_Course extends Doctrine_Record
109{
110    public function setTableDefinition()
111    {
112        $this->setTableName('langtkursus');
113        $this->hasColumn('navn', 'string', 255);
114    }
115
116    public function setUp()
117    {
118        $this->hasMany('VIH_Model_Course_Period as Periods', array('local' => 'id',
119                                                                   'foreign' => 'course_id'));
120        $this->hasMany('VIH_Model_Course_SubjectGroup as SubjectGroups', array('local' => 'id',
121                                                                               'foreign' => 'course_id'));
122    }
123}
124
125class VIH_Model_Course_Period extends Doctrine_Record
126{
127    public function setTableDefinition()
128    {
129        $this->hasColumn('name', 'string', 255);
130        $this->hasColumn('course_id', 'integer');
131        $this->hasColumn('date_start', 'date');
132        $this->hasColumn('date_end', 'date');
133    }
134
135    public function setUp()
136    {
137        $this->hasOne('VIH_Model_Course as Course', array('local' => 'course_id', 'foreign' => 'id'));
138    }
139
140}
141
142class VIH_Model_Course_Registration extends Doctrine_Record
143{
144    public function setTableDefinition()
145    {
146        $this->setTableName('langtkursus_tilmelding');
147        $this->hasColumn('vaerelse', 'integer');
148        $this->hasColumn('kursus_id', 'integer');
149        $this->hasColumn('adresse_id', 'integer');
150        $this->hasColumn('kontakt_adresse_id', 'integer');
151    }
152
153    public function setUp()
154    {
155        $this->hasOne('VIH_Model_Course as Course', array('local'   => 'kursus_id',
156                                                          'foreign' => 'id'));
157
158        $this->hasMany('VIH_Model_Subject as Subjects', array('refClass' => 'VIH_Model_Course_Registration_Subject',
159                                                             'local'    => 'registration_id',
160                                                             'foreign'  => 'subject_id'));
161    }
162}
163
164class VIH_Model_Course_SubjectGroup extends Doctrine_Record
165{
166    public function setTableDefinition()
167    {
168        $this->hasColumn('name', 'string', 255);
169        $this->hasColumn('period_id', 'integer');
170        $this->hasColumn('course_id', 'integer');
171    }
172
173    public function setUp()
174    {
175        $this->hasOne('VIH_Model_Course_Period as Period', array('local'   => 'period_id',
176                                                                 'foreign' => 'id'));
177
178        $this->hasOne('VIH_Model_Course as Course', array('local'   => 'course_id',
179                                                          'foreign' => 'id'));
180
181        $this->hasMany('VIH_Model_Subject as Subjects', array('refClass' => 'VIH_Model_Course_SubjectGroup_Subject',
182                                                             'local'    => 'subject_group_id',
183                                                             'foreign'  => 'subject_id'));
184    }
185}
186
187class VIH_Model_Course_SubjectGroup_Subject extends Doctrine_Record
188{
189    public function setTableDefinition()
190    {
191        $this->hasColumn('subject_group_id', 'integer', null, array('primary' => true));
192        $this->hasColumn('subject_id', 'integer', null, array('primary' => true));
193    }
194}
195
196class VIH_Model_Course_Registration_Subject extends Doctrine_Record
197{
198    public function setTableDefinition()
199    {
200        $this->hasColumn('registration_id', 'integer', null, array('primary' => true));
201        $this->hasColumn('subject_id', 'integer', null, array('primary' => true));
202    }
203}