1: <?php
2:
3: namespace Alchemy\tests;
4: use Alchemy\orm\Session;
5: use Alchemy\orm\ManyToOne;
6: use Alchemy\orm\OneToMany;
7: use Datetime;
8:
9:
10: class RelationshipTest extends BaseTest {
11: private $session;
12:
13:
14: public function setUp() {
15: $engine = $this->getSQLiteEngine();
16: $session = new Session($engine);
17:
18: $session->ddl()->dropAll();
19: $session->ddl()->createAll();
20:
21: $this->session = $session;
22: }
23:
24:
25: public function testRelationshipDefinition() {
26: $l = Language::schema()->listRelationships();
27: $f = UploadedFile::schema()->listRelationships();
28:
29: $files = $l['Files'];
30: $this->assertTrue($files instanceof OneToMany);
31: $this->assertEquals($files->getBackref(), 'Language');
32: $this->assertEquals($files->getName(), 'Files');
33: $this->assertEquals($files->getForeignKey()->getSourceTable()->getName(), 'Language');
34: $this->assertFalse($files->isSingleObject());
35: $this->assertTrue($files->isParent());
36:
37: $language = $f['Language'];
38: $this->assertTrue($language instanceof ManyToOne);
39: $this->assertEquals($language->getBackref(), 'Files');
40: $this->assertEquals($language->getName(), 'Language');
41: $this->assertEquals($language->getForeignKey()->getSourceTable()->getName(), 'Language');
42: $this->assertTrue($language->isSingleObject());
43: $this->assertFalse($language->isParent());
44:
45: $parent = $l['Parent'];
46: $this->assertTrue($parent instanceof ManyToOne);
47: $this->assertEquals($parent->getBackref(), 'Children');
48: $this->assertEquals($parent->getName(), 'Parent');
49: $this->assertEquals($parent->getForeignKey()->getSourceTable()->getName(), 'Language');
50: $this->assertTrue($parent->isSingleObject());
51: $this->assertFalse($parent->isParent());
52:
53: $children = $l['Children'];
54: $this->assertTrue($children instanceof OneToMany);
55: $this->assertEquals($children->getBackref(), 'Parent');
56: $this->assertEquals($children->getName(), 'Children');
57: $this->assertEquals($children->getForeignKey()->getSourceTable()->getName(), 'Language');
58: $this->assertFalse($children->isSingleObject());
59: $this->assertTrue($children->isParent());
60: }
61:
62:
63: public function testRelationSetFullTransient() {
64: $english = new Language();
65: $english->ISO2Code = 'en';
66: $english->LatestChangeStamp = new DateTime("1984-01-01");
67:
68: $spanish = new Language();
69: $spanish->ISO2Code = 'es';
70: $spanish->LatestChangeStamp = new DateTime("1984-01-01");
71:
72: $spanish->Parent = $english;
73:
74: $this->session->add($spanish);
75: $this->session->add($english);
76: $this->session->commit();
77:
78: $s = $english->Children->one();
79: $this->assertEquals($s->ISO2Code, 'es');
80: $this->assertEquals($s->Parent->ISO2Code, 'en');
81: }
82:
83:
84: public function testRelationSetNonTransient() {
85: $english = new Language();
86: $english->ISO2Code = 'en';
87: $english->LatestChangeStamp = new DateTime("1984-01-01");
88: $this->session->add($english);
89:
90: $spanish = new Language();
91: $spanish->ISO2Code = 'es';
92: $spanish->LatestChangeStamp = new DateTime("1984-01-01");
93: $this->session->add($spanish);
94: $this->session->commit();
95:
96: $spanish->Parent = $english;
97:
98: $this->session->commit();
99:
100: $s = $english->Children->one();
101: $this->assertEquals($s->ISO2Code, 'es');
102: $this->assertEquals($s->Parent->ISO2Code, 'en');
103: }
104:
105:
106: public function testRelationSetPartialTransient() {
107: $english = new Language();
108: $english->ISO2Code = 'en';
109: $english->LatestChangeStamp = new DateTime("1984-01-01");
110: $this->session->add($english);
111:
112: $spanish = new Language();
113: $spanish->ISO2Code = 'es';
114: $spanish->LatestChangeStamp = new DateTime("1984-01-01");
115:
116: $spanish->Parent = $english;
117: $this->session->add($spanish);
118:
119: $this->session->commit();
120:
121: $s = $english->Children->one();
122: $this->assertEquals($s->ISO2Code, 'es');
123: $this->assertEquals($s->Parent->ISO2Code, 'en');
124: }
125:
126:
127: public function testRelationAddFullTransient() {
128: $english = new Language();
129: $english->ISO2Code = 'en';
130: $english->LatestChangeStamp = new DateTime("1984-01-01");
131:
132: $spanish = new Language();
133: $spanish->ISO2Code = 'es';
134: $spanish->LatestChangeStamp = new DateTime("1984-01-01");
135:
136: $english->Children->add($spanish);
137:
138: $this->session->add($english);
139: $this->session->add($spanish);
140: $this->session->commit();
141:
142: $s = $english->Children->one();
143: $this->assertEquals($s->ISO2Code, 'es');
144: $this->assertEquals($s->Parent->ISO2Code, 'en');
145: }
146:
147:
148: public function testRelationAddNonTransient() {
149: $english = new Language();
150: $english->ISO2Code = 'en';
151: $english->LatestChangeStamp = new DateTime("1984-01-01");
152: $this->session->add($english);
153:
154: $spanish = new Language();
155: $spanish->ISO2Code = 'es';
156: $spanish->LatestChangeStamp = new DateTime("1984-01-01");
157: $this->session->add($spanish);
158: $this->session->commit();
159:
160: $english->Children->add($spanish);
161: $this->session->commit();
162:
163: $s = $english->Children->one();
164: $this->assertEquals($s->ISO2Code, 'es');
165: $this->assertEquals($s->Parent->ISO2Code, 'en');
166: }
167:
168:
169: public function testRelationAddPartialTransient() {
170: $english = new Language();
171: $english->ISO2Code = 'en';
172: $english->LatestChangeStamp = new DateTime("1984-01-01");
173: $this->session->add($english);
174:
175: $spanish = new Language();
176: $spanish->ISO2Code = 'es';
177: $spanish->LatestChangeStamp = new DateTime("1984-01-01");
178:
179: $english->Children->add($spanish);
180:
181: $this->session->add($spanish);
182: $this->session->commit();
183:
184: $s = $english->Children->one();
185: $this->assertEquals($s->ISO2Code, 'es');
186: $this->assertEquals($s->Parent->ISO2Code, 'en');
187: }
188:
189:
190: public function testOneToOne() {
191: $mandarin = new Language();
192: $mandarin->ISO2Code = 'zh';
193: $mandarin->LatestChangeStamp = new DateTime("1984-01-01");
194:
195: $simplified = new Language();
196: $simplified->ISO2Code = 'hans';
197: $simplified->LatestChangeStamp = new DateTime("1984-01-01");
198: $simplified->SpokenLanguage = $mandarin;
199:
200: $this->session->add($mandarin);
201: $this->session->add($simplified);
202: $this->session->commit();
203:
204: $this->assertEquals($simplified->SpokenLanguage->ISO2Code, 'zh');
205: $this->assertEquals($mandarin->WrittenLanguage->ISO2Code, 'hans');
206: }
207: }
208: