4
4
5
5
use PHPUnit \Framework \TestCase ;
6
6
use Simply \Container \Container ;
7
+ use Simply \Database \Test \TestHouseSchema ;
7
8
use Simply \Database \Test \TestParentSchema ;
8
9
use Simply \Database \Test \TestPersonSchema ;
9
10
@@ -22,18 +23,19 @@ private function getPersonSchema(): TestPersonSchema
22
23
23
24
$ container [TestPersonSchema::class] = $ schema ;
24
25
$ container [TestParentSchema::class] = new TestParentSchema ($ container );
26
+ $ container [TestHouseSchema::class] = new TestHouseSchema ($ container );
25
27
26
28
return $ schema ;
27
29
}
28
- public function testInvalidRererence ()
30
+ public function testInvalidRelationship (): void
29
31
{
30
32
$ schema = $ this ->getPersonSchema ();
31
33
32
34
$ this ->expectException (\InvalidArgumentException::class);
33
- $ schema ->getRelationship ('not-a-valid-reference ' );
35
+ $ schema ->getRelationship ('not-a-valid-relationship ' );
34
36
}
35
37
36
- public function testSchemaEquivalence ()
38
+ public function testSchemaEquivalence (): void
37
39
{
38
40
$ schema = $ this ->getPersonSchema ();
39
41
@@ -69,4 +71,172 @@ public function testInvalidReferencedFields(): void
69
71
$ this ->expectException (\InvalidArgumentException::class);
70
72
new Relationship ('test ' , $ schema , ['id ' ], $ schema , ['mother_id ' ], false );
71
73
}
74
+
75
+ public function testMissingReverseRelationship (): void
76
+ {
77
+ $ container = new Container ();
78
+ $ schema = new class ($ container ) extends Schema {
79
+ protected $ model = 'TestModel ' ;
80
+ protected $ primaryKey = 'id ' ;
81
+ protected $ fields = ['id ' , 'parent_id ' ];
82
+ protected $ table = 'test ' ;
83
+ protected $ relationships = [
84
+ 'parent ' => [
85
+ 'key ' => 'parent_id ' ,
86
+ 'schema ' => 'TestSchema ' ,
87
+ 'field ' => 'id ' ,
88
+ ],
89
+ ];
90
+ };
91
+
92
+ $ container ['TestSchema ' ] = $ schema ;
93
+ $ relationship = $ schema ->getRelationship ('parent ' );
94
+
95
+ $ this ->expectException (\RuntimeException::class);
96
+ $ relationship ->getReverseRelationship ();
97
+ }
98
+
99
+ public function testMultipleReverseRelationships (): void
100
+ {
101
+ $ container = new Container ();
102
+ $ schema = new class ($ container ) extends Schema {
103
+ protected $ model = 'TestModel ' ;
104
+ protected $ primaryKey = 'id ' ;
105
+ protected $ fields = ['id ' , 'parent_id ' ];
106
+ protected $ table = 'test ' ;
107
+ protected $ relationships = [
108
+ 'parent ' => [
109
+ 'key ' => 'parent_id ' ,
110
+ 'schema ' => 'TestSchema ' ,
111
+ 'field ' => 'id ' ,
112
+ ],
113
+ 'child ' => [
114
+ 'key ' => 'id ' ,
115
+ 'schema ' => 'TestSchema ' ,
116
+ 'field ' => 'parent_id ' ,
117
+ ],
118
+ 'son ' => [
119
+ 'key ' => 'id ' ,
120
+ 'schema ' => 'TestSchema ' ,
121
+ 'field ' => 'parent_id ' ,
122
+ ],
123
+ ];
124
+ };
125
+
126
+ $ container ['TestSchema ' ] = $ schema ;
127
+ $ relationship = $ schema ->getRelationship ('parent ' );
128
+
129
+ $ this ->expectException (\RuntimeException::class);
130
+ $ relationship ->getReverseRelationship ();
131
+ }
132
+
133
+ public function testTryingToFillCompositeForeignKey (): void
134
+ {
135
+ $ container = new Container ();
136
+ $ schema = new class ($ container ) extends Schema {
137
+ protected $ model = 'TestModel ' ;
138
+ protected $ primaryKey = ['order_id ' , 'product_id ' ];
139
+ protected $ fields = ['order_id ' , 'product_id ' , 'replaced_order_id ' , 'replaced_product_id ' ];
140
+ protected $ table = 'test ' ;
141
+ protected $ relationships = [
142
+ 'replacement ' => [
143
+ 'key ' => ['order_id ' , 'product_id ' ],
144
+ 'schema ' => 'TestSchema ' ,
145
+ 'field ' => ['replaced_order_id ' , 'replaced_product_id ' ],
146
+ ],
147
+ 'replaced ' => [
148
+ 'key ' => ['replaced_order_id ' , 'replaced_product_id ' ],
149
+ 'schema ' => 'TestSchema ' ,
150
+ 'field ' => ['order_id ' , 'product_id ' ],
151
+ ],
152
+ ];
153
+ };
154
+
155
+ $ container ['TestSchema ' ] = $ schema ;
156
+ $ relationship = $ schema ->getRelationship ('replaced ' );
157
+
158
+ $ this ->expectException (\RuntimeException::class);
159
+ $ relationship ->fillRelationship ([], []);
160
+ }
161
+
162
+ public function testTryingToFillWrongReferringSchema (): void
163
+ {
164
+ $ schema = $ this ->getPersonSchema ();
165
+ $ relationship = $ schema ->getRelationship ('home ' );
166
+
167
+ $ house = new Record ($ relationship ->getReferencedSchema ());
168
+
169
+ $ this ->expectException (\InvalidArgumentException::class);
170
+ $ relationship ->fillRelationship ([$ house ], []);
171
+ }
172
+
173
+ public function testTryingToFillWrongReferredSchema (): void
174
+ {
175
+ $ schema = $ this ->getPersonSchema ();
176
+ $ relationship = $ schema ->getRelationship ('home ' );
177
+
178
+ $ person = new Record ($ schema );
179
+ $ otherPerson = new Record ($ schema );
180
+
181
+ $ this ->expectException (\InvalidArgumentException::class);
182
+ $ relationship ->fillRelationship ([$ person ], [$ otherPerson ]);
183
+ }
184
+
185
+ public function testTryFillingMultipleToUniqueRelationship (): void
186
+ {
187
+ $ schema = $ this ->getPersonSchema ();
188
+ $ relationship = $ schema ->getRelationship ('spouse_reverse ' );
189
+
190
+ $ person = new Record ($ schema );
191
+ $ person ['id ' ] = 1 ;
192
+
193
+ $ wife = new Record ($ schema );
194
+ $ wife ['spouse_id ' ] = 1 ;
195
+
196
+ $ husband = new Record ($ schema );
197
+ $ husband ['spouse_id ' ] = 1 ;
198
+
199
+ $ this ->expectException (\InvalidArgumentException::class);
200
+ $ relationship ->fillRelationship ([$ person ], [$ wife , $ husband ]);
201
+ }
202
+
203
+ public function testTryFillingMultipleToPrimaryRelationship (): void
204
+ {
205
+ $ schema = $ this ->getPersonSchema ();
206
+ $ relationship = $ schema ->getRelationship ('spouse ' );
207
+
208
+ $ person = new Record ($ schema );
209
+ $ person ['spouse_id ' ] = 1 ;
210
+
211
+ $ wife = new Record ($ schema );
212
+ $ wife ['id ' ] = 1 ;
213
+
214
+ $ husband = new Record ($ schema );
215
+ $ husband ['id ' ] = 1 ;
216
+
217
+ $ this ->expectException (\InvalidArgumentException::class);
218
+ $ relationship ->fillRelationship ([$ person ], [$ wife , $ husband ]);
219
+ }
220
+
221
+ public function testFillingWithNullValues (): void
222
+ {
223
+ $ schema = $ this ->getPersonSchema ();
224
+ $ relationship = $ schema ->getRelationship ('spouse ' );
225
+
226
+ $ personA = new Record ($ schema );
227
+ $ personA ['id ' ] = 1 ;
228
+ $ personA ['spouse_id ' ] = 2 ;
229
+
230
+ $ personB = new Record ($ schema );
231
+ $ personB ['id ' ] = 2 ;
232
+ $ personB ['spouse_id ' ] = 1 ;
233
+
234
+ $ personC = new Record ($ schema );
235
+
236
+ $ relationship ->fillRelationship ([$ personA , $ personB , $ personC ], [$ personA , $ personB , $ personC ]);
237
+
238
+ $ this ->assertSame ([$ personB ], $ personA ->getReferencedRecords ('spouse ' ));
239
+ $ this ->assertSame ([$ personA ], $ personB ->getReferencedRecords ('spouse ' ));
240
+ $ this ->assertSame ([], $ personC ->getReferencedRecords ('spouse ' ));
241
+ }
72
242
}
0 commit comments