@@ -23,6 +23,9 @@ abstract class IntegrationTestCase extends TestCase
23
23
/** @var TestParentSchema */
24
24
protected $ parentSchema ;
25
25
26
+ /** @var TestHouseSchema */
27
+ protected $ houseSchema ;
28
+
26
29
abstract protected function createConnection (): Connection ;
27
30
abstract protected function setUpDatabase (Connection $ connection ): void ;
28
31
@@ -32,17 +35,19 @@ protected function setUp()
32
35
33
36
$ this ->personSchema = new TestPersonSchema ($ container );
34
37
$ this ->parentSchema = new TestParentSchema ($ container );
38
+ $ this ->houseSchema = new TestHouseSchema ($ container );
35
39
36
40
$ container [TestPersonSchema::class] = $ this ->personSchema ;
37
41
$ container [TestParentSchema::class] = $ this ->parentSchema ;
42
+ $ container [TestHouseSchema::class] = $ this ->houseSchema ;
38
43
39
44
$ this ->connection = $ this ->createConnection ();
40
45
$ this ->setUpDatabase ($ this ->connection );
41
46
}
42
47
43
- private function getTestPersonRepository (): TestPersonRepository
48
+ private function getTestPersonRepository (): TestRepository
44
49
{
45
- return new TestPersonRepository ($ this ->connection , $ this ->personSchema , $ this ->parentSchema );
50
+ return new TestRepository ($ this ->connection , $ this ->personSchema , $ this ->parentSchema , $ this -> houseSchema );
46
51
}
47
52
48
53
public function testCrudOperations (): void
@@ -161,20 +166,29 @@ public function testBooleanFields(): void
161
166
$ this ->assertCount (0 , $ repository ->findByHasLicense (false ));
162
167
}
163
168
164
- public function testLoadingRelationships (): void
169
+ public function testRelationships (): void
165
170
{
166
171
$ repository = $ this ->getTestPersonRepository ();
167
172
173
+ $ home = $ repository ->createHouse ('Anonymous Street ' );
174
+ $ repository ->saveHouse ($ home );
175
+
168
176
$ jane = $ repository ->createPerson ('Jane ' , 'Doe ' , 20 );
169
177
$ john = $ repository ->createPerson ('John ' , 'Doe ' , 20 );
170
178
$ mama = $ repository ->createPerson ('Mama ' , 'Doe ' , 40 );
171
179
$ papa = $ repository ->createPerson ('Papa ' , 'Doe ' , 40 );
172
180
181
+ $ home ->movePeople ([$ jane , $ john , $ mama , $ papa ]);
182
+
173
183
$ repository ->savePerson ($ jane );
174
184
$ repository ->savePerson ($ john );
175
185
$ repository ->savePerson ($ mama );
176
186
$ repository ->savePerson ($ papa );
177
187
188
+ $ mama ->marry ($ papa );
189
+ $ repository ->savePerson ($ mama );
190
+ $ repository ->savePerson ($ papa );
191
+
178
192
$ repository ->makeParent ($ jane , $ mama );
179
193
$ repository ->makeParent ($ jane , $ papa );
180
194
$ repository ->makeParent ($ john , $ mama );
@@ -184,37 +198,33 @@ public function testLoadingRelationships(): void
184
198
185
199
$ repository ->loadFamily ([$ person ]);
186
200
201
+ $ this ->assertSame ('Anonymous Street ' , $ person ->getHome ()->getStreet ());
202
+ $ this ->assertCount (4 , $ person ->getHome ()->getResidents ());
203
+
187
204
$ firstName = function (TestPersonModel $ model ): string {
188
205
return $ model ->getFirstName ();
189
206
};
190
207
191
- $ childRecords = function (TestPersonModel $ model ): array {
192
- $ childRecords = [];
193
-
194
- foreach ($ model ->getChildren () as $ child ) {
195
- $ childRecords [] = $ child ->getDatabaseRecord ();
196
- }
197
-
198
- return $ childRecords ;
199
- };
200
-
201
208
$ parents = $ person ->getParents ();
202
209
203
210
$ this ->assertCount (2 , $ parents );
204
211
212
+ $ residentNames = array_map ($ firstName , $ person ->getHome ()->getResidents ());
205
213
$ names = array_map ($ firstName , $ parents );
206
214
sort ($ names );
215
+ sort ($ residentNames );
207
216
208
217
$ this ->assertSame (['Mama ' , 'Papa ' ], $ names );
218
+ $ this ->assertSame (['Jane ' , 'John ' , 'Mama ' , 'Papa ' ], $ residentNames );
209
219
210
220
foreach ($ parents as $ parent ) {
211
- $ this ->assertTrue ( \in_array ( $ person-> getDatabaseRecord () , $ childRecords ( $ parent), true ));
221
+ $ this ->assertContains ( $ person , $ parent-> getChildren ( ));
212
222
}
213
223
214
224
$ repository ->loadFamily ($ parents );
215
225
216
226
foreach ($ parents as $ parent ) {
217
- $ this ->assertTrue ( \in_array ( $ person-> getDatabaseRecord () , $ childRecords ( $ parent), true ));
227
+ $ this ->assertContains ( $ person , $ parent-> getChildren ( ));
218
228
}
219
229
}
220
230
0 commit comments