Total Complexity | 95 |
Total Lines | 2260 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DataObjectTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataObjectTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class DataObjectTest extends SapphireTest |
||
27 | { |
||
28 | |||
29 | protected static $fixture_file = 'DataObjectTest.yml'; |
||
30 | |||
31 | /** |
||
32 | * Standard set of dataobject test classes |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | public static $extra_data_objects = array( |
||
37 | DataObjectTest\Team::class, |
||
38 | DataObjectTest\Fixture::class, |
||
39 | DataObjectTest\SubTeam::class, |
||
40 | DataObjectTest\OtherSubclassWithSameField::class, |
||
41 | DataObjectTest\FieldlessTable::class, |
||
42 | DataObjectTest\FieldlessSubTable::class, |
||
43 | DataObjectTest\ValidatedObject::class, |
||
44 | DataObjectTest\Player::class, |
||
45 | DataObjectTest\TeamComment::class, |
||
46 | DataObjectTest\EquipmentCompany::class, |
||
47 | DataObjectTest\SubEquipmentCompany::class, |
||
48 | DataObjectTest\ExtendedTeamComment::class, |
||
49 | DataObjectTest\Company::class, |
||
50 | DataObjectTest\Staff::class, |
||
51 | DataObjectTest\CEO::class, |
||
52 | DataObjectTest\Fan::class, |
||
53 | DataObjectTest\Play::class, |
||
54 | DataObjectTest\Ploy::class, |
||
55 | DataObjectTest\Bogey::class, |
||
56 | DataObjectTest\Sortable::class, |
||
57 | DataObjectTest\Bracket::class, |
||
58 | DataObjectTest\RelationParent::class, |
||
59 | DataObjectTest\RelationChildFirst::class, |
||
60 | DataObjectTest\RelationChildSecond::class, |
||
61 | DataObjectTest\MockDynamicAssignmentDataObject::class |
||
62 | ); |
||
63 | |||
64 | public static function getExtraDataObjects() |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @dataProvider provideSingletons |
||
74 | */ |
||
75 | public function testSingleton($inst, $defaultValue, $altDefaultValue) |
||
76 | { |
||
77 | $inst = $inst(); |
||
78 | // Test that populateDefaults() isn't called on singletons |
||
79 | // which can lead to SQL errors during build, and endless loops |
||
80 | if ($defaultValue) { |
||
81 | $this->assertEquals($defaultValue, $inst->MyFieldWithDefault); |
||
82 | } else { |
||
83 | $this->assertEmpty($inst->MyFieldWithDefault); |
||
84 | } |
||
85 | |||
86 | if ($altDefaultValue) { |
||
87 | $this->assertEquals($altDefaultValue, $inst->MyFieldWithAltDefault); |
||
88 | } else { |
||
89 | $this->assertEmpty($inst->MyFieldWithAltDefault); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | public function provideSingletons() |
||
94 | { |
||
95 | // because PHPUnit evalutes test providers *before* setUp methods |
||
96 | // any extensions added in the setUp methods won't be available |
||
97 | // we must return closures to generate the arguments at run time |
||
98 | return array( |
||
99 | 'create() static method' => array(function () { |
||
100 | return DataObjectTest\Fixture::create(); |
||
101 | }, 'Default Value', 'Default Value'), |
||
102 | 'New object creation' => array(function () { |
||
103 | return new DataObjectTest\Fixture(); |
||
104 | }, 'Default Value', 'Default Value'), |
||
105 | 'singleton() function' => array(function () { |
||
106 | return singleton(DataObjectTest\Fixture::class); |
||
107 | }, null, null), |
||
108 | 'singleton() static method' => array(function () { |
||
109 | return DataObjectTest\Fixture::singleton(); |
||
110 | }, null, null), |
||
111 | 'Manual constructor args' => array(function () { |
||
112 | return new DataObjectTest\Fixture(null, true); |
||
113 | }, null, null), |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | public function testDb() |
||
118 | { |
||
119 | $schema = DataObject::getSchema(); |
||
120 | $dbFields = $schema->fieldSpecs(DataObjectTest\TeamComment::class); |
||
121 | |||
122 | // Assert fields are included |
||
123 | $this->assertArrayHasKey('Name', $dbFields); |
||
124 | |||
125 | // Assert the base fields are included |
||
126 | $this->assertArrayHasKey('Created', $dbFields); |
||
127 | $this->assertArrayHasKey('LastEdited', $dbFields); |
||
128 | $this->assertArrayHasKey('ClassName', $dbFields); |
||
129 | $this->assertArrayHasKey('ID', $dbFields); |
||
130 | |||
131 | // Assert that the correct field type is returned when passing a field |
||
132 | $this->assertEquals('Varchar', $schema->fieldSpec(DataObjectTest\TeamComment::class, 'Name')); |
||
133 | $this->assertEquals('Text', $schema->fieldSpec(DataObjectTest\TeamComment::class, 'Comment')); |
||
134 | |||
135 | // Test with table required |
||
136 | $this->assertEquals( |
||
137 | DataObjectTest\TeamComment::class . '.Varchar', |
||
138 | $schema->fieldSpec(DataObjectTest\TeamComment::class, 'Name', DataObjectSchema::INCLUDE_CLASS) |
||
139 | ); |
||
140 | $this->assertEquals( |
||
141 | DataObjectTest\TeamComment::class . '.Text', |
||
142 | $schema->fieldSpec(DataObjectTest\TeamComment::class, 'Comment', DataObjectSchema::INCLUDE_CLASS) |
||
143 | ); |
||
144 | $dbFields = $schema->fieldSpecs(DataObjectTest\ExtendedTeamComment::class); |
||
145 | |||
146 | // fixed fields are still included in extended classes |
||
147 | $this->assertArrayHasKey('Created', $dbFields); |
||
148 | $this->assertArrayHasKey('LastEdited', $dbFields); |
||
149 | $this->assertArrayHasKey('ClassName', $dbFields); |
||
150 | $this->assertArrayHasKey('ID', $dbFields); |
||
151 | |||
152 | // Assert overloaded fields have correct data type |
||
153 | $this->assertEquals('HTMLText', $schema->fieldSpec(DataObjectTest\ExtendedTeamComment::class, 'Comment')); |
||
154 | $this->assertEquals( |
||
155 | 'HTMLText', |
||
156 | $dbFields['Comment'], |
||
157 | 'Calls to DataObject::db without a field specified return correct data types' |
||
158 | ); |
||
159 | |||
160 | // assertEquals doesn't verify the order of array elements, so access keys manually to check order: |
||
161 | // expected: array('Name' => 'Varchar', 'Comment' => 'HTMLText') |
||
162 | $this->assertEquals( |
||
163 | array( |
||
164 | 'Name', |
||
165 | 'Comment' |
||
166 | ), |
||
167 | array_slice(array_keys($dbFields), 4, 2), |
||
168 | 'DataObject::db returns fields in correct order' |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | public function testConstructAcceptsValues() |
||
173 | { |
||
174 | // Values can be an array... |
||
175 | $player = new DataObjectTest\Player( |
||
176 | array( |
||
177 | 'FirstName' => 'James', |
||
178 | 'Surname' => 'Smith' |
||
179 | ) |
||
180 | ); |
||
181 | |||
182 | $this->assertEquals('James', $player->FirstName); |
||
183 | $this->assertEquals('Smith', $player->Surname); |
||
184 | |||
185 | // ... or a stdClass inst |
||
186 | $data = new stdClass(); |
||
187 | $data->FirstName = 'John'; |
||
188 | $data->Surname = 'Doe'; |
||
189 | $player = new DataObjectTest\Player($data); |
||
190 | |||
191 | $this->assertEquals('John', $player->FirstName); |
||
192 | $this->assertEquals('Doe', $player->Surname); |
||
193 | |||
194 | // IDs should be stored as integers, not strings |
||
195 | $player = new DataObjectTest\Player(array('ID' => '5')); |
||
196 | $this->assertSame(5, $player->ID); |
||
197 | } |
||
198 | |||
199 | public function testValidObjectsForBaseFields() |
||
200 | { |
||
201 | $obj = new DataObjectTest\ValidatedObject(); |
||
202 | |||
203 | foreach (array('Created', 'LastEdited', 'ClassName', 'ID') as $field) { |
||
204 | $helper = $obj->dbObject($field); |
||
205 | $this->assertTrue( |
||
206 | ($helper instanceof DBField), |
||
207 | "for {$field} expected helper to be DBField, but was " . (is_object($helper) ? get_class($helper) : "null") |
||
208 | ); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | public function testDataIntegrityWhenTwoSubclassesHaveSameField() |
||
213 | { |
||
214 | // Save data into DataObjectTest_SubTeam.SubclassDatabaseField |
||
215 | $obj = new DataObjectTest\SubTeam(); |
||
216 | $obj->SubclassDatabaseField = "obj-SubTeam"; |
||
217 | $obj->write(); |
||
218 | |||
219 | // Change the class |
||
220 | $obj->ClassName = DataObjectTest\OtherSubclassWithSameField::class; |
||
221 | $obj->write(); |
||
222 | $obj->flushCache(); |
||
223 | |||
224 | // Re-fetch from the database and confirm that the data is sourced from |
||
225 | // OtherSubclassWithSameField.SubclassDatabaseField |
||
226 | $obj = DataObject::get_by_id(DataObjectTest\Team::class, $obj->ID); |
||
227 | $this->assertNull($obj->SubclassDatabaseField); |
||
228 | |||
229 | // Confirm that save the object in the other direction. |
||
230 | $obj->SubclassDatabaseField = 'obj-Other'; |
||
231 | $obj->write(); |
||
232 | |||
233 | $obj->ClassName = DataObjectTest\SubTeam::class; |
||
234 | $obj->write(); |
||
235 | $obj->flushCache(); |
||
236 | |||
237 | // If we restore the class, the old value has been lying dormant and will be available again. |
||
238 | // NOTE: This behaviour is volatile; we may change this in the future to clear fields that |
||
239 | // are no longer relevant when changing ClassName |
||
240 | $obj = DataObject::get_by_id(DataObjectTest\Team::class, $obj->ID); |
||
241 | $this->assertEquals('obj-SubTeam', $obj->SubclassDatabaseField); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Test deletion of DataObjects |
||
246 | * - Deleting using delete() on the DataObject |
||
247 | * - Deleting using DataObject::delete_by_id() |
||
248 | */ |
||
249 | public function testDelete() |
||
250 | { |
||
251 | // Test deleting using delete() on the DataObject |
||
252 | // Get the first page |
||
253 | $obj = $this->objFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
254 | $objID = $obj->ID; |
||
255 | // Check the page exists before deleting |
||
256 | $this->assertTrue(is_object($obj) && $obj->exists()); |
||
257 | // Delete the page |
||
258 | $obj->delete(); |
||
259 | // Check that page does not exist after deleting |
||
260 | $obj = DataObject::get_by_id(DataObjectTest\Player::class, $objID); |
||
261 | $this->assertTrue(!$obj || !$obj->exists()); |
||
262 | |||
263 | |||
264 | // Test deleting using DataObject::delete_by_id() |
||
265 | // Get the second page |
||
266 | $obj = $this->objFromFixture(DataObjectTest\Player::class, 'captain2'); |
||
267 | $objID = $obj->ID; |
||
268 | // Check the page exists before deleting |
||
269 | $this->assertTrue(is_object($obj) && $obj->exists()); |
||
270 | // Delete the page |
||
271 | DataObject::delete_by_id(DataObjectTest\Player::class, $obj->ID); |
||
272 | // Check that page does not exist after deleting |
||
273 | $obj = DataObject::get_by_id(DataObjectTest\Player::class, $objID); |
||
274 | $this->assertTrue(!$obj || !$obj->exists()); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Test methods that get DataObjects |
||
279 | * - DataObject::get() |
||
280 | * - All records of a DataObject |
||
281 | * - Filtering |
||
282 | * - Sorting |
||
283 | * - Joins |
||
284 | * - Limit |
||
285 | * - Container class |
||
286 | * - DataObject::get_by_id() |
||
287 | * - DataObject::get_one() |
||
288 | * - With and without caching |
||
289 | * - With and without ordering |
||
290 | */ |
||
291 | public function testGet() |
||
292 | { |
||
293 | // Test getting all records of a DataObject |
||
294 | $comments = DataObject::get(DataObjectTest\TeamComment::class); |
||
295 | $this->assertEquals(3, $comments->count()); |
||
296 | |||
297 | // Test WHERE clause |
||
298 | $comments = DataObject::get(DataObjectTest\TeamComment::class, "\"Name\"='Bob'"); |
||
299 | $this->assertEquals(1, $comments->count()); |
||
300 | foreach ($comments as $comment) { |
||
301 | $this->assertEquals('Bob', $comment->Name); |
||
302 | } |
||
303 | |||
304 | // Test sorting |
||
305 | $comments = DataObject::get(DataObjectTest\TeamComment::class, '', "\"Name\" ASC"); |
||
306 | $this->assertEquals(3, $comments->count()); |
||
307 | $this->assertEquals('Bob', $comments->first()->Name); |
||
308 | $comments = DataObject::get(DataObjectTest\TeamComment::class, '', "\"Name\" DESC"); |
||
309 | $this->assertEquals(3, $comments->count()); |
||
310 | $this->assertEquals('Phil', $comments->first()->Name); |
||
311 | |||
312 | // Test limit |
||
313 | $comments = DataObject::get(DataObjectTest\TeamComment::class, '', "\"Name\" ASC", '', '1,2'); |
||
314 | $this->assertEquals(2, $comments->count()); |
||
315 | $this->assertEquals('Joe', $comments->first()->Name); |
||
316 | $this->assertEquals('Phil', $comments->last()->Name); |
||
317 | |||
318 | // Test get_by_id() |
||
319 | $captain1ID = $this->idFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
320 | $captain1 = DataObject::get_by_id(DataObjectTest\Player::class, $captain1ID); |
||
321 | $this->assertEquals('Captain', $captain1->FirstName); |
||
322 | |||
323 | // Test get_one() without caching |
||
324 | $comment1 = DataObject::get_one( |
||
325 | DataObjectTest\TeamComment::class, |
||
326 | array( |
||
327 | '"DataObjectTest_TeamComment"."Name"' => 'Joe' |
||
328 | ), |
||
329 | false |
||
330 | ); |
||
331 | $comment1->Comment = "Something Else"; |
||
332 | |||
333 | $comment2 = DataObject::get_one( |
||
334 | DataObjectTest\TeamComment::class, |
||
335 | array( |
||
336 | '"DataObjectTest_TeamComment"."Name"' => 'Joe' |
||
337 | ), |
||
338 | false |
||
339 | ); |
||
340 | $this->assertNotEquals($comment1->Comment, $comment2->Comment); |
||
341 | |||
342 | // Test get_one() with caching |
||
343 | $comment1 = DataObject::get_one( |
||
344 | DataObjectTest\TeamComment::class, |
||
345 | array( |
||
346 | '"DataObjectTest_TeamComment"."Name"' => 'Bob' |
||
347 | ), |
||
348 | true |
||
349 | ); |
||
350 | $comment1->Comment = "Something Else"; |
||
351 | |||
352 | $comment2 = DataObject::get_one( |
||
353 | DataObjectTest\TeamComment::class, |
||
354 | array( |
||
355 | '"DataObjectTest_TeamComment"."Name"' => 'Bob' |
||
356 | ), |
||
357 | true |
||
358 | ); |
||
359 | $this->assertEquals((string)$comment1->Comment, (string)$comment2->Comment); |
||
360 | |||
361 | // Test get_one() with order by without caching |
||
362 | $comment = DataObject::get_one(DataObjectTest\TeamComment::class, '', false, "\"Name\" ASC"); |
||
363 | $this->assertEquals('Bob', $comment->Name); |
||
364 | |||
365 | $comment = DataObject::get_one(DataObjectTest\TeamComment::class, '', false, "\"Name\" DESC"); |
||
366 | $this->assertEquals('Phil', $comment->Name); |
||
367 | |||
368 | // Test get_one() with order by with caching |
||
369 | $comment = DataObject::get_one(DataObjectTest\TeamComment::class, '', true, '"Name" ASC'); |
||
370 | $this->assertEquals('Bob', $comment->Name); |
||
371 | $comment = DataObject::get_one(DataObjectTest\TeamComment::class, '', true, '"Name" DESC'); |
||
372 | $this->assertEquals('Phil', $comment->Name); |
||
373 | } |
||
374 | |||
375 | public function testGetByIDCallerClass() |
||
376 | { |
||
377 | $captain1ID = $this->idFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
378 | $captain1 = DataObjectTest\Player::get_by_id($captain1ID); |
||
379 | $this->assertInstanceOf(DataObjectTest\Player::class, $captain1); |
||
380 | $this->assertEquals('Captain', $captain1->FirstName); |
||
381 | |||
382 | $captain2ID = $this->idFromFixture(DataObjectTest\Player::class, 'captain2'); |
||
383 | // make sure we can call from any class but get the one passed as an argument |
||
384 | $captain2 = DataObjectTest\TeamComment::get_by_id(DataObjectTest\Player::class, $captain2ID); |
||
385 | $this->assertInstanceOf(DataObjectTest\Player::class, $captain2); |
||
386 | $this->assertEquals('Captain 2', $captain2->FirstName); |
||
387 | } |
||
388 | |||
389 | public function testGetCaseInsensitive() |
||
390 | { |
||
391 | // Test get_one() with bad case on the classname |
||
392 | // Note: This will succeed only if the underlying DB server supports case-insensitive |
||
393 | // table names (e.g. such as MySQL, but not SQLite3) |
||
394 | if (!(DB::get_conn() instanceof MySQLDatabase)) { |
||
395 | $this->markTestSkipped('MySQL only'); |
||
396 | } |
||
397 | |||
398 | $subteam1 = DataObject::get_one( |
||
399 | strtolower(DataObjectTest\SubTeam::class), |
||
400 | array( |
||
401 | '"DataObjectTest_Team"."Title"' => 'Subteam 1' |
||
402 | ), |
||
403 | true |
||
404 | ); |
||
405 | $this->assertNotEmpty($subteam1); |
||
406 | $this->assertEquals($subteam1->Title, "Subteam 1"); |
||
407 | } |
||
408 | |||
409 | public function testGetSubclassFields() |
||
410 | { |
||
411 | /* Test that fields / has_one relations from the parent table and the subclass tables are extracted */ |
||
412 | $captain1 = $this->objFromFixture(DataObjectTest\Player::class, "captain1"); |
||
413 | // Base field |
||
414 | $this->assertEquals('Captain', $captain1->FirstName); |
||
415 | // Subclass field |
||
416 | $this->assertEquals('007', $captain1->ShirtNumber); |
||
417 | // Subclass has_one relation |
||
418 | $this->assertEquals($this->idFromFixture(DataObjectTest\Team::class, 'team1'), $captain1->FavouriteTeamID); |
||
419 | } |
||
420 | |||
421 | public function testGetRelationClass() |
||
422 | { |
||
423 | $obj = new DataObjectTest\Player(); |
||
424 | $this->assertEquals( |
||
425 | singleton(DataObjectTest\Player::class)->getRelationClass('FavouriteTeam'), |
||
426 | DataObjectTest\Team::class, |
||
427 | 'has_one is properly inspected' |
||
428 | ); |
||
429 | $this->assertEquals( |
||
430 | singleton(DataObjectTest\Company::class)->getRelationClass('CurrentStaff'), |
||
431 | DataObjectTest\Staff::class, |
||
432 | 'has_many is properly inspected' |
||
433 | ); |
||
434 | $this->assertEquals( |
||
435 | singleton(DataObjectTest\Team::class)->getRelationClass('Players'), |
||
436 | DataObjectTest\Player::class, |
||
437 | 'many_many is properly inspected' |
||
438 | ); |
||
439 | $this->assertEquals( |
||
440 | singleton(DataObjectTest\Player::class)->getRelationClass('Teams'), |
||
441 | DataObjectTest\Team::class, |
||
442 | 'belongs_many_many is properly inspected' |
||
443 | ); |
||
444 | $this->assertEquals( |
||
445 | singleton(DataObjectTest\CEO::class)->getRelationClass('Company'), |
||
446 | DataObjectTest\Company::class, |
||
447 | 'belongs_to is properly inspected' |
||
448 | ); |
||
449 | $this->assertEquals( |
||
450 | singleton(DataObjectTest\Fan::class)->getRelationClass('Favourite'), |
||
451 | DataObject::class, |
||
452 | 'polymorphic has_one is properly inspected' |
||
453 | ); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Test that has_one relations can be retrieved |
||
458 | */ |
||
459 | public function testGetHasOneRelations() |
||
460 | { |
||
461 | $captain1 = $this->objFromFixture(DataObjectTest\Player::class, "captain1"); |
||
462 | $team1ID = $this->idFromFixture(DataObjectTest\Team::class, 'team1'); |
||
463 | |||
464 | // There will be a field called (relname)ID that contains the ID of the |
||
465 | // object linked to via the has_one relation |
||
466 | $this->assertEquals($team1ID, $captain1->FavouriteTeamID); |
||
467 | |||
468 | // There will be a method called $obj->relname() that returns the object itself |
||
469 | $this->assertEquals($team1ID, $captain1->FavouriteTeam()->ID); |
||
470 | |||
471 | // Test that getNonReciprocalComponent can find has_one from the has_many end |
||
472 | $this->assertEquals( |
||
473 | $team1ID, |
||
474 | $captain1->inferReciprocalComponent(DataObjectTest\Team::class, 'PlayerFans')->ID |
||
475 | ); |
||
476 | |||
477 | // Check entity with polymorphic has-one |
||
478 | $fan1 = $this->objFromFixture(DataObjectTest\Fan::class, "fan1"); |
||
479 | $this->assertTrue((bool)$fan1->hasValue('Favourite')); |
||
480 | |||
481 | // There will be fields named (relname)ID and (relname)Class for polymorphic |
||
482 | // entities |
||
483 | $this->assertEquals($team1ID, $fan1->FavouriteID); |
||
484 | $this->assertEquals(DataObjectTest\Team::class, $fan1->FavouriteClass); |
||
485 | |||
486 | // There will be a method called $obj->relname() that returns the object itself |
||
487 | $favourite = $fan1->Favourite(); |
||
488 | $this->assertEquals($team1ID, $favourite->ID); |
||
489 | $this->assertInstanceOf(DataObjectTest\Team::class, $favourite); |
||
490 | |||
491 | // check behaviour of dbObject with polymorphic relations |
||
492 | $favouriteDBObject = $fan1->dbObject('Favourite'); |
||
493 | $favouriteValue = $favouriteDBObject->getValue(); |
||
494 | $this->assertInstanceOf(DBPolymorphicForeignKey::class, $favouriteDBObject); |
||
495 | $this->assertEquals($favourite->ID, $favouriteValue->ID); |
||
496 | $this->assertEquals($favourite->ClassName, $favouriteValue->ClassName); |
||
497 | } |
||
498 | |||
499 | public function testLimitAndCount() |
||
500 | { |
||
501 | $players = DataObject::get(DataObjectTest\Player::class); |
||
502 | |||
503 | // There's 4 records in total |
||
504 | $this->assertEquals(4, $players->count()); |
||
505 | |||
506 | // Testing "##, ##" syntax |
||
507 | $this->assertEquals(4, $players->limit(20)->count()); |
||
508 | $this->assertEquals(4, $players->limit(20, 0)->count()); |
||
509 | $this->assertEquals(0, $players->limit(20, 20)->count()); |
||
510 | $this->assertEquals(2, $players->limit(2, 0)->count()); |
||
511 | $this->assertEquals(1, $players->limit(5, 3)->count()); |
||
512 | } |
||
513 | |||
514 | public function testWriteNoChangesDoesntUpdateLastEdited() |
||
515 | { |
||
516 | // set mock now so we can be certain of LastEdited time for our test |
||
517 | DBDatetime::set_mock_now('2017-01-01 00:00:00'); |
||
518 | $obj = new Player(); |
||
519 | $obj->FirstName = 'Test'; |
||
520 | $obj->Surname = 'Plater'; |
||
521 | $obj->Email = '[email protected]'; |
||
522 | $obj->write(); |
||
523 | $this->assertEquals('2017-01-01 00:00:00', $obj->LastEdited); |
||
524 | $writtenObj = Player::get()->byID($obj->ID); |
||
525 | $this->assertEquals('2017-01-01 00:00:00', $writtenObj->LastEdited); |
||
526 | |||
527 | // set mock now so we get a new LastEdited if, for some reason, it's updated |
||
528 | DBDatetime::set_mock_now('2017-02-01 00:00:00'); |
||
529 | $writtenObj->write(); |
||
530 | $this->assertEquals('2017-01-01 00:00:00', $writtenObj->LastEdited); |
||
531 | $this->assertEquals($obj->ID, $writtenObj->ID); |
||
532 | |||
533 | $reWrittenObj = Player::get()->byID($writtenObj->ID); |
||
534 | $this->assertEquals('2017-01-01 00:00:00', $reWrittenObj->LastEdited); |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * Test writing of database columns which don't correlate to a DBField, |
||
539 | * e.g. all relation fields on has_one/has_many like "ParentID". |
||
540 | */ |
||
541 | public function testWritePropertyWithoutDBField() |
||
542 | { |
||
543 | $obj = $this->objFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
544 | $obj->FavouriteTeamID = 99; |
||
545 | $obj->write(); |
||
546 | |||
547 | // reload the page from the database |
||
548 | $savedObj = DataObject::get_by_id(DataObjectTest\Player::class, $obj->ID); |
||
549 | $this->assertTrue($savedObj->FavouriteTeamID == 99); |
||
550 | |||
551 | // Test with porymorphic relation |
||
552 | $obj2 = $this->objFromFixture(DataObjectTest\Fan::class, "fan1"); |
||
553 | $obj2->FavouriteID = 99; |
||
554 | $obj2->FavouriteClass = DataObjectTest\Player::class; |
||
555 | $obj2->write(); |
||
556 | |||
557 | $savedObj2 = DataObject::get_by_id(DataObjectTest\Fan::class, $obj2->ID); |
||
558 | $this->assertTrue($savedObj2->FavouriteID == 99); |
||
559 | $this->assertTrue($savedObj2->FavouriteClass == DataObjectTest\Player::class); |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * Test has many relationships |
||
564 | * - Test getComponents() gets the ComponentSet of the other side of the relation |
||
565 | * - Test the IDs on the DataObjects are set correctly |
||
566 | */ |
||
567 | public function testHasManyRelationships() |
||
568 | { |
||
569 | $team1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
570 | |||
571 | // Test getComponents() gets the ComponentSet of the other side of the relation |
||
572 | $this->assertTrue($team1->Comments()->count() == 2); |
||
573 | |||
574 | $team1Comments = [ |
||
575 | ['Comment' => 'This is a team comment by Joe'], |
||
576 | ['Comment' => 'This is a team comment by Bob'], |
||
577 | ]; |
||
578 | |||
579 | // Test the IDs on the DataObjects are set correctly |
||
580 | $this->assertListEquals($team1Comments, $team1->Comments()); |
||
581 | |||
582 | // Test that has_many can be infered from the has_one via getNonReciprocalComponent |
||
583 | $this->assertListEquals( |
||
584 | $team1Comments, |
||
585 | $team1->inferReciprocalComponent(DataObjectTest\TeamComment::class, 'Team') |
||
586 | ); |
||
587 | |||
588 | // Test that we can add and remove items that already exist in the database |
||
589 | $newComment = new DataObjectTest\TeamComment(); |
||
590 | $newComment->Name = "Automated commenter"; |
||
591 | $newComment->Comment = "This is a new comment"; |
||
592 | $newComment->write(); |
||
593 | $team1->Comments()->add($newComment); |
||
594 | $this->assertEquals($team1->ID, $newComment->TeamID); |
||
595 | |||
596 | $comment1 = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment1'); |
||
597 | $comment2 = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment2'); |
||
598 | $team1->Comments()->remove($comment2); |
||
599 | |||
600 | $team1CommentIDs = $team1->Comments()->sort('ID')->column('ID'); |
||
601 | $this->assertEquals(array($comment1->ID, $newComment->ID), $team1CommentIDs); |
||
602 | |||
603 | // Test that removing an item from a list doesn't remove it from the same |
||
604 | // relation belonging to a different object |
||
605 | $team1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
606 | $team2 = $this->objFromFixture(DataObjectTest\Team::class, 'team2'); |
||
607 | $team2->Comments()->remove($comment1); |
||
608 | $team1CommentIDs = $team1->Comments()->sort('ID')->column('ID'); |
||
609 | $this->assertEquals(array($comment1->ID, $newComment->ID), $team1CommentIDs); |
||
610 | } |
||
611 | |||
612 | |||
613 | /** |
||
614 | * Test has many relationships against polymorphic has_one fields |
||
615 | * - Test getComponents() gets the ComponentSet of the other side of the relation |
||
616 | * - Test the IDs on the DataObjects are set correctly |
||
617 | */ |
||
618 | public function testHasManyPolymorphicRelationships() |
||
657 | } |
||
658 | |||
659 | |||
660 | public function testHasOneRelationship() |
||
661 | { |
||
662 | $team1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
663 | $player1 = $this->objFromFixture(DataObjectTest\Player::class, 'player1'); |
||
664 | $player2 = $this->objFromFixture(DataObjectTest\Player::class, 'player2'); |
||
665 | $fan1 = $this->objFromFixture(DataObjectTest\Fan::class, 'fan1'); |
||
666 | |||
667 | // Test relation probing |
||
668 | $this->assertFalse((bool)$team1->hasValue('Captain', null, false)); |
||
669 | $this->assertFalse((bool)$team1->hasValue('CaptainID', null, false)); |
||
670 | |||
671 | // Add a captain to team 1 |
||
672 | $team1->setField('CaptainID', $player1->ID); |
||
673 | $team1->write(); |
||
674 | |||
675 | $this->assertTrue((bool)$team1->hasValue('Captain', null, false)); |
||
676 | $this->assertTrue((bool)$team1->hasValue('CaptainID', null, false)); |
||
677 | |||
678 | $this->assertEquals( |
||
679 | $player1->ID, |
||
680 | $team1->Captain()->ID, |
||
681 | 'The captain exists for team 1' |
||
682 | ); |
||
683 | $this->assertEquals( |
||
684 | $player1->ID, |
||
685 | $team1->getComponent('Captain')->ID, |
||
686 | 'The captain exists through the component getter' |
||
687 | ); |
||
688 | |||
689 | $this->assertEquals( |
||
690 | $team1->Captain()->FirstName, |
||
691 | 'Player 1', |
||
692 | 'Player 1 is the captain' |
||
693 | ); |
||
694 | $this->assertEquals( |
||
695 | $team1->getComponent('Captain')->FirstName, |
||
696 | 'Player 1', |
||
697 | 'Player 1 is the captain' |
||
698 | ); |
||
699 | |||
700 | $team1->CaptainID = $player2->ID; |
||
701 | $team1->write(); |
||
702 | |||
703 | $this->assertEquals($player2->ID, $team1->Captain()->ID); |
||
704 | $this->assertEquals($player2->ID, $team1->getComponent('Captain')->ID); |
||
705 | $this->assertEquals('Player 2', $team1->Captain()->FirstName); |
||
706 | $this->assertEquals('Player 2', $team1->getComponent('Captain')->FirstName); |
||
707 | |||
708 | |||
709 | // Set the favourite team for fan1 |
||
710 | $fan1->setField('FavouriteID', $team1->ID); |
||
711 | $fan1->setField('FavouriteClass', get_class($team1)); |
||
712 | |||
713 | $this->assertEquals($team1->ID, $fan1->Favourite()->ID, 'The team is assigned to fan 1'); |
||
714 | $this->assertInstanceOf(get_class($team1), $fan1->Favourite(), 'The team is assigned to fan 1'); |
||
715 | $this->assertEquals( |
||
716 | $team1->ID, |
||
717 | $fan1->getComponent('Favourite')->ID, |
||
718 | 'The team exists through the component getter' |
||
719 | ); |
||
720 | $this->assertInstanceOf( |
||
721 | get_class($team1), |
||
722 | $fan1->getComponent('Favourite'), |
||
723 | 'The team exists through the component getter' |
||
724 | ); |
||
725 | |||
726 | $this->assertEquals( |
||
727 | $fan1->Favourite()->Title, |
||
728 | 'Team 1', |
||
729 | 'Team 1 is the favourite' |
||
730 | ); |
||
731 | $this->assertEquals( |
||
732 | $fan1->getComponent('Favourite')->Title, |
||
733 | 'Team 1', |
||
734 | 'Team 1 is the favourite' |
||
735 | ); |
||
736 | } |
||
737 | |||
738 | /** |
||
739 | * Test has_one used as field getter/setter |
||
740 | */ |
||
741 | public function testHasOneAsField() |
||
761 | } |
||
762 | |||
763 | /** |
||
764 | * @todo Extend type change tests (e.g. '0'==NULL) |
||
765 | */ |
||
766 | public function testChangedFields() |
||
767 | { |
||
813 | ); |
||
814 | } |
||
815 | |||
816 | /** |
||
817 | * @skipUpgrade |
||
818 | */ |
||
819 | public function testIsChanged() |
||
820 | { |
||
821 | $obj = $this->objFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
822 | $obj->NonDBField = 'bob'; |
||
823 | $obj->FirstName = 'Captain-changed'; |
||
824 | $obj->IsRetired = true; // type change only, database stores "1" |
||
825 | |||
826 | // Now that DB fields are changed, isChanged is true |
||
827 | $this->assertTrue($obj->isChanged('NonDBField')); |
||
828 | $this->assertFalse($obj->isChanged('NonField')); |
||
829 | $this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_STRICT)); |
||
830 | $this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_VALUE)); |
||
831 | $this->assertTrue($obj->isChanged('IsRetired', DataObject::CHANGE_STRICT)); |
||
832 | $this->assertFalse($obj->isChanged('IsRetired', DataObject::CHANGE_VALUE)); |
||
833 | $this->assertFalse($obj->isChanged('Email', 1), 'Doesnt change mark unchanged property'); |
||
834 | $this->assertFalse($obj->isChanged('Email', 2), 'Doesnt change mark unchanged property'); |
||
835 | |||
836 | $newObj = new DataObjectTest\Player(); |
||
837 | $newObj->FirstName = "New Player"; |
||
838 | $this->assertTrue($newObj->isChanged('FirstName', DataObject::CHANGE_STRICT)); |
||
839 | $this->assertTrue($newObj->isChanged('FirstName', DataObject::CHANGE_VALUE)); |
||
840 | $this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_STRICT)); |
||
841 | $this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_VALUE)); |
||
842 | |||
843 | $newObj->write(); |
||
844 | $this->assertFalse($newObj->ischanged()); |
||
845 | $this->assertFalse($newObj->isChanged('FirstName', DataObject::CHANGE_STRICT)); |
||
846 | $this->assertFalse($newObj->isChanged('FirstName', DataObject::CHANGE_VALUE)); |
||
847 | $this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_STRICT)); |
||
848 | $this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_VALUE)); |
||
849 | |||
850 | $obj = $this->objFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
851 | $obj->FirstName = null; |
||
852 | $this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_STRICT)); |
||
853 | $this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_VALUE)); |
||
854 | |||
855 | /* Test when there's not field provided */ |
||
856 | $obj = $this->objFromFixture(DataObjectTest\Player::class, 'captain2'); |
||
857 | $this->assertFalse($obj->isChanged()); |
||
858 | $obj->NonDBField = 'new value'; |
||
859 | $this->assertFalse($obj->isChanged()); |
||
860 | $obj->FirstName = "New Player"; |
||
861 | $this->assertTrue($obj->isChanged()); |
||
862 | |||
863 | $obj->write(); |
||
864 | $this->assertFalse($obj->isChanged()); |
||
865 | } |
||
866 | |||
867 | public function testRandomSort() |
||
868 | { |
||
869 | /* If we perform the same regularly sorted query twice, it should return the same results */ |
||
870 | $itemsA = DataObject::get(DataObjectTest\TeamComment::class, "", "ID"); |
||
871 | foreach ($itemsA as $item) { |
||
872 | $keysA[] = $item->ID; |
||
873 | } |
||
874 | |||
875 | $itemsB = DataObject::get(DataObjectTest\TeamComment::class, "", "ID"); |
||
876 | foreach ($itemsB as $item) { |
||
877 | $keysB[] = $item->ID; |
||
878 | } |
||
879 | |||
880 | /* Test when there's not field provided */ |
||
881 | $obj = $this->objFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
882 | $obj->FirstName = "New Player"; |
||
883 | $this->assertTrue($obj->isChanged()); |
||
884 | |||
885 | $obj->write(); |
||
886 | $this->assertFalse($obj->isChanged()); |
||
887 | |||
888 | /* If we perform the same random query twice, it shouldn't return the same results */ |
||
889 | $itemsA = DataObject::get(DataObjectTest\TeamComment::class, "", DB::get_conn()->random()); |
||
890 | $itemsB = DataObject::get(DataObjectTest\TeamComment::class, "", DB::get_conn()->random()); |
||
891 | $itemsC = DataObject::get(DataObjectTest\TeamComment::class, "", DB::get_conn()->random()); |
||
892 | $itemsD = DataObject::get(DataObjectTest\TeamComment::class, "", DB::get_conn()->random()); |
||
893 | foreach ($itemsA as $item) { |
||
894 | $keysA[] = $item->ID; |
||
895 | } |
||
896 | foreach ($itemsB as $item) { |
||
897 | $keysB[] = $item->ID; |
||
898 | } |
||
899 | foreach ($itemsC as $item) { |
||
900 | $keysC[] = $item->ID; |
||
901 | } |
||
902 | foreach ($itemsD as $item) { |
||
903 | $keysD[] = $item->ID; |
||
904 | } |
||
905 | |||
906 | // These shouldn't all be the same (run it 4 times to minimise chance of an accidental collision) |
||
907 | // There's about a 1 in a billion chance of an accidental collision |
||
908 | $this->assertTrue($keysA != $keysB || $keysB != $keysC || $keysC != $keysD); |
||
909 | } |
||
910 | |||
911 | public function testWriteSavesToHasOneRelations() |
||
912 | { |
||
913 | /* DataObject::write() should save to a has_one relationship if you set a field called (relname)ID */ |
||
914 | $team = new DataObjectTest\Team(); |
||
915 | $captainID = $this->idFromFixture(DataObjectTest\Player::class, 'player1'); |
||
916 | $team->CaptainID = $captainID; |
||
917 | $team->write(); |
||
918 | $this->assertEquals( |
||
919 | $captainID, |
||
920 | DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $team->ID")->value() |
||
921 | ); |
||
922 | |||
923 | /* After giving it a value, you should also be able to set it back to null */ |
||
924 | $team->CaptainID = ''; |
||
925 | $team->write(); |
||
926 | $this->assertEquals( |
||
927 | 0, |
||
928 | DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $team->ID")->value() |
||
929 | ); |
||
930 | |||
931 | /* You should also be able to save a blank to it when it's first created */ |
||
932 | $team = new DataObjectTest\Team(); |
||
933 | $team->CaptainID = ''; |
||
934 | $team->write(); |
||
935 | $this->assertEquals( |
||
936 | 0, |
||
937 | DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $team->ID")->value() |
||
938 | ); |
||
939 | |||
940 | /* Ditto for existing records without a value */ |
||
941 | $existingTeam = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
942 | $existingTeam->CaptainID = ''; |
||
943 | $existingTeam->write(); |
||
944 | $this->assertEquals( |
||
945 | 0, |
||
946 | DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $existingTeam->ID")->value() |
||
947 | ); |
||
948 | } |
||
949 | |||
950 | public function testCanAccessHasOneObjectsAsMethods() |
||
951 | { |
||
952 | /* If you have a has_one relation 'Captain' on $obj, and you set the $obj->CaptainID = (ID), then the |
||
953 | * object itself should be accessible as $obj->Captain() */ |
||
954 | $team = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
955 | $captainID = $this->idFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
956 | |||
957 | $team->CaptainID = $captainID; |
||
958 | $this->assertNotNull($team->Captain()); |
||
959 | $this->assertEquals($captainID, $team->Captain()->ID); |
||
960 | |||
961 | // Test for polymorphic has_one relations |
||
962 | $fan = $this->objFromFixture(DataObjectTest\Fan::class, 'fan1'); |
||
963 | $fan->FavouriteID = $team->ID; |
||
964 | $fan->FavouriteClass = DataObjectTest\Team::class; |
||
965 | $this->assertNotNull($fan->Favourite()); |
||
966 | $this->assertEquals($team->ID, $fan->Favourite()->ID); |
||
967 | $this->assertInstanceOf(DataObjectTest\Team::class, $fan->Favourite()); |
||
968 | } |
||
969 | |||
970 | public function testFieldNamesThatMatchMethodNamesWork() |
||
971 | { |
||
972 | /* Check that a field name that corresponds to a method on DataObject will still work */ |
||
973 | $obj = new DataObjectTest\Fixture(); |
||
974 | $obj->Data = "value1"; |
||
975 | $obj->DbObject = "value2"; |
||
976 | $obj->Duplicate = "value3"; |
||
977 | $obj->write(); |
||
978 | |||
979 | $this->assertNotNull($obj->ID); |
||
980 | $this->assertEquals( |
||
981 | 'value1', |
||
982 | DB::query("SELECT \"Data\" FROM \"DataObjectTest_Fixture\" WHERE \"ID\" = $obj->ID")->value() |
||
983 | ); |
||
984 | $this->assertEquals( |
||
985 | 'value2', |
||
986 | DB::query("SELECT \"DbObject\" FROM \"DataObjectTest_Fixture\" WHERE \"ID\" = $obj->ID")->value() |
||
987 | ); |
||
988 | $this->assertEquals( |
||
989 | 'value3', |
||
990 | DB::query("SELECT \"Duplicate\" FROM \"DataObjectTest_Fixture\" WHERE \"ID\" = $obj->ID")->value() |
||
991 | ); |
||
992 | } |
||
993 | |||
994 | /** |
||
995 | * @todo Re-enable all test cases for field existence after behaviour has been fixed |
||
996 | */ |
||
997 | public function testFieldExistence() |
||
998 | { |
||
999 | $teamInstance = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
1000 | $teamSingleton = singleton(DataObjectTest\Team::class); |
||
1001 | |||
1002 | $subteamInstance = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam1'); |
||
1003 | $schema = DataObject::getSchema(); |
||
1004 | |||
1005 | /* hasField() singleton checks */ |
||
1006 | $this->assertTrue( |
||
1007 | $teamSingleton->hasField('ID'), |
||
1008 | 'hasField() finds built-in fields in singletons' |
||
1009 | ); |
||
1010 | $this->assertTrue( |
||
1011 | $teamSingleton->hasField('Title'), |
||
1012 | 'hasField() finds custom fields in singletons' |
||
1013 | ); |
||
1014 | |||
1015 | /* hasField() instance checks */ |
||
1016 | $this->assertFalse( |
||
1017 | $teamInstance->hasField('NonExistingField'), |
||
1018 | 'hasField() doesnt find non-existing fields in instances' |
||
1019 | ); |
||
1020 | $this->assertTrue( |
||
1021 | $teamInstance->hasField('ID'), |
||
1022 | 'hasField() finds built-in fields in instances' |
||
1023 | ); |
||
1024 | $this->assertTrue( |
||
1025 | $teamInstance->hasField('Created'), |
||
1026 | 'hasField() finds built-in fields in instances' |
||
1027 | ); |
||
1028 | $this->assertTrue( |
||
1029 | $teamInstance->hasField('DatabaseField'), |
||
1030 | 'hasField() finds custom fields in instances' |
||
1031 | ); |
||
1032 | //$this->assertFalse($teamInstance->hasField('SubclassDatabaseField'), |
||
1033 | //'hasField() doesnt find subclass fields in parentclass instances'); |
||
1034 | $this->assertTrue( |
||
1035 | $teamInstance->hasField('DynamicField'), |
||
1036 | 'hasField() finds dynamic getters in instances' |
||
1037 | ); |
||
1038 | $this->assertTrue( |
||
1039 | $teamInstance->hasField('HasOneRelationshipID'), |
||
1040 | 'hasField() finds foreign keys in instances' |
||
1041 | ); |
||
1042 | $this->assertTrue( |
||
1043 | $teamInstance->hasField('ExtendedDatabaseField'), |
||
1044 | 'hasField() finds extended fields in instances' |
||
1045 | ); |
||
1046 | $this->assertTrue( |
||
1047 | $teamInstance->hasField('ExtendedHasOneRelationshipID'), |
||
1048 | 'hasField() finds extended foreign keys in instances' |
||
1049 | ); |
||
1050 | //$this->assertTrue($teamInstance->hasField('ExtendedDynamicField'), |
||
1051 | //'hasField() includes extended dynamic getters in instances'); |
||
1052 | |||
1053 | /* hasField() subclass checks */ |
||
1054 | $this->assertTrue( |
||
1055 | $subteamInstance->hasField('ID'), |
||
1056 | 'hasField() finds built-in fields in subclass instances' |
||
1057 | ); |
||
1058 | $this->assertTrue( |
||
1059 | $subteamInstance->hasField('Created'), |
||
1060 | 'hasField() finds built-in fields in subclass instances' |
||
1061 | ); |
||
1062 | $this->assertTrue( |
||
1063 | $subteamInstance->hasField('DatabaseField'), |
||
1064 | 'hasField() finds custom fields in subclass instances' |
||
1065 | ); |
||
1066 | $this->assertTrue( |
||
1067 | $subteamInstance->hasField('SubclassDatabaseField'), |
||
1068 | 'hasField() finds custom fields in subclass instances' |
||
1069 | ); |
||
1070 | $this->assertTrue( |
||
1071 | $subteamInstance->hasField('DynamicField'), |
||
1072 | 'hasField() finds dynamic getters in subclass instances' |
||
1073 | ); |
||
1074 | $this->assertTrue( |
||
1075 | $subteamInstance->hasField('HasOneRelationshipID'), |
||
1076 | 'hasField() finds foreign keys in subclass instances' |
||
1077 | ); |
||
1078 | $this->assertTrue( |
||
1079 | $subteamInstance->hasField('ExtendedDatabaseField'), |
||
1080 | 'hasField() finds extended fields in subclass instances' |
||
1081 | ); |
||
1082 | $this->assertTrue( |
||
1083 | $subteamInstance->hasField('ExtendedHasOneRelationshipID'), |
||
1084 | 'hasField() finds extended foreign keys in subclass instances' |
||
1085 | ); |
||
1086 | |||
1087 | /* hasDatabaseField() singleton checks */ |
||
1088 | //$this->assertTrue($teamSingleton->hasDatabaseField('ID'), |
||
1089 | //'hasDatabaseField() finds built-in fields in singletons'); |
||
1090 | $this->assertNotEmpty( |
||
1091 | $schema->fieldSpec(DataObjectTest\Team::class, 'Title'), |
||
1092 | 'hasDatabaseField() finds custom fields in singletons' |
||
1093 | ); |
||
1094 | |||
1095 | /* hasDatabaseField() instance checks */ |
||
1096 | $this->assertNull( |
||
1097 | $schema->fieldSpec(DataObjectTest\Team::class, 'NonExistingField'), |
||
1098 | 'hasDatabaseField() doesnt find non-existing fields in instances' |
||
1099 | ); |
||
1100 | //$this->assertNotEmpty($schema->fieldSpec(DataObjectTest_Team::class, 'ID'), |
||
1101 | //'hasDatabaseField() finds built-in fields in instances'); |
||
1102 | $this->assertNotEmpty( |
||
1103 | $schema->fieldSpec(DataObjectTest\Team::class, 'Created'), |
||
1104 | 'hasDatabaseField() finds built-in fields in instances' |
||
1105 | ); |
||
1106 | $this->assertNotEmpty( |
||
1107 | $schema->fieldSpec(DataObjectTest\Team::class, 'DatabaseField'), |
||
1108 | 'hasDatabaseField() finds custom fields in instances' |
||
1109 | ); |
||
1110 | $this->assertNull( |
||
1111 | $schema->fieldSpec(DataObjectTest\Team::class, 'SubclassDatabaseField'), |
||
1112 | 'hasDatabaseField() doesnt find subclass fields in parentclass instances' |
||
1113 | ); |
||
1114 | //$this->assertNull($schema->fieldSpec(DataObjectTest_Team::class, 'DynamicField'), |
||
1115 | //'hasDatabaseField() doesnt dynamic getters in instances'); |
||
1116 | $this->assertNotEmpty( |
||
1117 | $schema->fieldSpec(DataObjectTest\Team::class, 'HasOneRelationshipID'), |
||
1118 | 'hasDatabaseField() finds foreign keys in instances' |
||
1119 | ); |
||
1120 | $this->assertNotEmpty( |
||
1121 | $schema->fieldSpec(DataObjectTest\Team::class, 'ExtendedDatabaseField'), |
||
1122 | 'hasDatabaseField() finds extended fields in instances' |
||
1123 | ); |
||
1124 | $this->assertNotEmpty( |
||
1125 | $schema->fieldSpec(DataObjectTest\Team::class, 'ExtendedHasOneRelationshipID'), |
||
1126 | 'hasDatabaseField() finds extended foreign keys in instances' |
||
1127 | ); |
||
1128 | $this->assertNull( |
||
1129 | $schema->fieldSpec(DataObjectTest\Team::class, 'ExtendedDynamicField'), |
||
1130 | 'hasDatabaseField() doesnt include extended dynamic getters in instances' |
||
1131 | ); |
||
1132 | |||
1133 | /* hasDatabaseField() subclass checks */ |
||
1134 | $this->assertNotEmpty( |
||
1135 | $schema->fieldSpec(DataObjectTest\SubTeam::class, 'DatabaseField'), |
||
1136 | 'hasField() finds custom fields in subclass instances' |
||
1137 | ); |
||
1138 | $this->assertNotEmpty( |
||
1139 | $schema->fieldSpec(DataObjectTest\SubTeam::class, 'SubclassDatabaseField'), |
||
1140 | 'hasField() finds custom fields in subclass instances' |
||
1141 | ); |
||
1142 | } |
||
1143 | |||
1144 | /** |
||
1145 | * @todo Re-enable all test cases for field inheritance aggregation after behaviour has been fixed |
||
1146 | */ |
||
1147 | public function testFieldInheritance() |
||
1148 | { |
||
1149 | $schema = DataObject::getSchema(); |
||
1150 | |||
1151 | // Test logical fields (including composite) |
||
1152 | $teamSpecifications = $schema->fieldSpecs(DataObjectTest\Team::class); |
||
1153 | $expected = array( |
||
1154 | 'ID', |
||
1155 | 'ClassName', |
||
1156 | 'LastEdited', |
||
1157 | 'Created', |
||
1158 | 'Title', |
||
1159 | 'DatabaseField', |
||
1160 | 'ExtendedDatabaseField', |
||
1161 | 'CaptainID', |
||
1162 | 'FounderID', |
||
1163 | 'HasOneRelationshipID', |
||
1164 | 'ExtendedHasOneRelationshipID' |
||
1165 | ); |
||
1166 | $actual = array_keys($teamSpecifications); |
||
1167 | sort($expected); |
||
1168 | sort($actual); |
||
1169 | $this->assertEquals( |
||
1170 | $expected, |
||
1171 | $actual, |
||
1172 | 'fieldSpecifications() contains all fields defined on instance: base, extended and foreign keys' |
||
1173 | ); |
||
1174 | |||
1175 | $teamFields = $schema->databaseFields(DataObjectTest\Team::class, false); |
||
1176 | $expected = array( |
||
1177 | 'ID', |
||
1178 | 'ClassName', |
||
1179 | 'LastEdited', |
||
1180 | 'Created', |
||
1181 | 'Title', |
||
1182 | 'DatabaseField', |
||
1183 | 'ExtendedDatabaseField', |
||
1184 | 'CaptainID', |
||
1185 | 'FounderID', |
||
1186 | 'HasOneRelationshipID', |
||
1187 | 'ExtendedHasOneRelationshipID' |
||
1188 | ); |
||
1189 | $actual = array_keys($teamFields); |
||
1190 | sort($expected); |
||
1191 | sort($actual); |
||
1192 | $this->assertEquals( |
||
1193 | $expected, |
||
1194 | $actual, |
||
1195 | 'databaseFields() contains only fields defined on instance, including base, extended and foreign keys' |
||
1196 | ); |
||
1197 | |||
1198 | $subteamSpecifications = $schema->fieldSpecs(DataObjectTest\SubTeam::class); |
||
1199 | $expected = array( |
||
1200 | 'ID', |
||
1201 | 'ClassName', |
||
1202 | 'LastEdited', |
||
1203 | 'Created', |
||
1204 | 'Title', |
||
1205 | 'DatabaseField', |
||
1206 | 'ExtendedDatabaseField', |
||
1207 | 'CaptainID', |
||
1208 | 'FounderID', |
||
1209 | 'HasOneRelationshipID', |
||
1210 | 'ExtendedHasOneRelationshipID', |
||
1211 | 'SubclassDatabaseField', |
||
1212 | 'ParentTeamID', |
||
1213 | ); |
||
1214 | $actual = array_keys($subteamSpecifications); |
||
1215 | sort($expected); |
||
1216 | sort($actual); |
||
1217 | $this->assertEquals( |
||
1218 | $expected, |
||
1219 | $actual, |
||
1220 | 'fieldSpecifications() on subclass contains all fields, including base, extended and foreign keys' |
||
1221 | ); |
||
1222 | |||
1223 | $subteamFields = $schema->databaseFields(DataObjectTest\SubTeam::class, false); |
||
1224 | $expected = array( |
||
1225 | 'ID', |
||
1226 | 'SubclassDatabaseField', |
||
1227 | 'ParentTeamID', |
||
1228 | ); |
||
1229 | $actual = array_keys($subteamFields); |
||
1230 | sort($expected); |
||
1231 | sort($actual); |
||
1232 | $this->assertEquals( |
||
1233 | $expected, |
||
1234 | $actual, |
||
1235 | 'databaseFields() on subclass contains only fields defined on instance' |
||
1236 | ); |
||
1237 | } |
||
1238 | |||
1239 | public function testSearchableFields() |
||
1240 | { |
||
1241 | $player = $this->objFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
1242 | $fields = $player->searchableFields(); |
||
1243 | $this->assertArrayHasKey( |
||
1244 | 'IsRetired', |
||
1245 | $fields, |
||
1246 | 'Fields defined by $searchable_fields static are correctly detected' |
||
1247 | ); |
||
1248 | $this->assertArrayHasKey( |
||
1249 | 'ShirtNumber', |
||
1250 | $fields, |
||
1251 | 'Fields defined by $searchable_fields static are correctly detected' |
||
1252 | ); |
||
1253 | |||
1254 | $team = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
1255 | $fields = $team->searchableFields(); |
||
1256 | $this->assertArrayHasKey( |
||
1257 | 'Title', |
||
1258 | $fields, |
||
1259 | 'Fields can be inherited from the $summary_fields static, including methods called on fields' |
||
1260 | ); |
||
1261 | $this->assertArrayHasKey( |
||
1262 | 'Captain.ShirtNumber', |
||
1263 | $fields, |
||
1264 | 'Fields on related objects can be inherited from the $summary_fields static' |
||
1265 | ); |
||
1266 | $this->assertArrayHasKey( |
||
1267 | 'Captain.FavouriteTeam.Title', |
||
1268 | $fields, |
||
1269 | 'Fields on related objects can be inherited from the $summary_fields static' |
||
1270 | ); |
||
1271 | |||
1272 | $testObj = new DataObjectTest\Fixture(); |
||
1273 | $fields = $testObj->searchableFields(); |
||
1274 | $this->assertEmpty($fields); |
||
1275 | } |
||
1276 | |||
1277 | public function testCastingHelper() |
||
1278 | { |
||
1279 | $team = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
1280 | |||
1281 | $this->assertEquals('Varchar', $team->castingHelper('Title'), 'db field wasn\'t casted correctly'); |
||
1282 | $this->assertEquals('HTMLVarchar', $team->castingHelper('DatabaseField'), 'db field wasn\'t casted correctly'); |
||
1283 | |||
1284 | $sponsor = $team->Sponsors()->first(); |
||
1285 | $this->assertEquals('Int', $sponsor->castingHelper('SponsorFee'), 'many_many_extraFields not casted correctly'); |
||
1286 | } |
||
1287 | |||
1288 | public function testSummaryFieldsCustomLabels() |
||
1289 | { |
||
1290 | $team = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
1291 | $summaryFields = $team->summaryFields(); |
||
1292 | |||
1293 | $this->assertEquals( |
||
1294 | [ |
||
1295 | 'Title' => 'Custom Title', |
||
1296 | 'Title.UpperCase' => 'Title', |
||
1297 | 'Captain.ShirtNumber' => 'Captain\'s shirt number', |
||
1298 | 'Captain.FavouriteTeam.Title' => 'Captain\'s favourite team', |
||
1299 | ], |
||
1300 | $summaryFields |
||
1301 | ); |
||
1302 | } |
||
1303 | |||
1304 | public function testDataObjectUpdate() |
||
1305 | { |
||
1306 | /* update() calls can use the dot syntax to reference has_one relations and other methods that return |
||
1307 | * objects */ |
||
1308 | $team1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
1309 | $team1->CaptainID = $this->idFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
1310 | |||
1311 | $team1->update( |
||
1312 | array( |
||
1313 | 'DatabaseField' => 'Something', |
||
1314 | 'Captain.FirstName' => 'Jim', |
||
1315 | 'Captain.Email' => '[email protected]', |
||
1316 | 'Captain.FavouriteTeam.Title' => 'New and improved team 1', |
||
1317 | ) |
||
1318 | ); |
||
1319 | |||
1320 | /* Test the simple case of updating fields on the object itself */ |
||
1321 | $this->assertEquals('Something', $team1->DatabaseField); |
||
1322 | |||
1323 | /* Setting Captain.Email and Captain.FirstName will have updated DataObjectTest_Captain.captain1 in |
||
1324 | * the database. Although update() doesn't usually write, it does write related records automatically. */ |
||
1325 | $captain1 = $this->objFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
1326 | $this->assertEquals('Jim', $captain1->FirstName); |
||
1327 | $this->assertEquals('[email protected]', $captain1->Email); |
||
1328 | |||
1329 | /* Jim's favourite team is team 1; we need to reload the object to the the change that setting Captain. |
||
1330 | * FavouriteTeam.Title made */ |
||
1331 | $reloadedTeam1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
1332 | $this->assertEquals('New and improved team 1', $reloadedTeam1->Title); |
||
1333 | } |
||
1334 | |||
1335 | public function testDataObjectUpdateNew() |
||
1336 | { |
||
1337 | /* update() calls can use the dot syntax to reference has_one relations and other methods that return |
||
1338 | * objects */ |
||
1339 | $team1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
1340 | $team1->CaptainID = 0; |
||
1341 | |||
1342 | $team1->update( |
||
1343 | array( |
||
1344 | 'Captain.FirstName' => 'Jim', |
||
1345 | 'Captain.FavouriteTeam.Title' => 'New and improved team 1', |
||
1346 | ) |
||
1347 | ); |
||
1348 | /* Test that the captain ID has been updated */ |
||
1349 | $this->assertGreaterThan(0, $team1->CaptainID); |
||
1350 | |||
1351 | /* Fetch the newly created captain */ |
||
1352 | $captain1 = DataObjectTest\Player::get()->byID($team1->CaptainID); |
||
1353 | $this->assertEquals('Jim', $captain1->FirstName); |
||
1354 | |||
1355 | /* Grab the favourite team and make sure it has the correct values */ |
||
1356 | $reloadedTeam1 = $captain1->FavouriteTeam(); |
||
1357 | $this->assertEquals($reloadedTeam1->ID, $captain1->FavouriteTeamID); |
||
1358 | $this->assertEquals('New and improved team 1', $reloadedTeam1->Title); |
||
1359 | } |
||
1360 | |||
1361 | |||
1362 | /** |
||
1363 | * @expectedException \SilverStripe\ORM\ValidationException |
||
1364 | */ |
||
1365 | public function testWritingInvalidDataObjectThrowsException() |
||
1366 | { |
||
1367 | $validatedObject = new DataObjectTest\ValidatedObject(); |
||
1368 | $validatedObject->write(); |
||
1369 | } |
||
1370 | |||
1371 | public function testWritingValidDataObjectDoesntThrowException() |
||
1372 | { |
||
1373 | $validatedObject = new DataObjectTest\ValidatedObject(); |
||
1374 | $validatedObject->Name = "Mr. Jones"; |
||
1375 | |||
1376 | $validatedObject->write(); |
||
1377 | $this->assertTrue($validatedObject->isInDB(), "Validated object was not saved to database"); |
||
1378 | } |
||
1379 | |||
1380 | public function testSubclassCreation() |
||
1381 | { |
||
1382 | /* Creating a new object of a subclass should set the ClassName field correctly */ |
||
1383 | $obj = new DataObjectTest\SubTeam(); |
||
1384 | $obj->write(); |
||
1385 | $this->assertEquals( |
||
1386 | DataObjectTest\SubTeam::class, |
||
1387 | DB::query("SELECT \"ClassName\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $obj->ID")->value() |
||
1388 | ); |
||
1389 | } |
||
1390 | |||
1391 | public function testForceInsert() |
||
1392 | { |
||
1393 | /* If you set an ID on an object and pass forceInsert = true, then the object should be correctly created */ |
||
1394 | $conn = DB::get_conn(); |
||
1395 | if (method_exists($conn, 'allowPrimaryKeyEditing')) { |
||
1396 | $conn->allowPrimaryKeyEditing(DataObjectTest\Team::class, true); |
||
1397 | } |
||
1398 | $obj = new DataObjectTest\SubTeam(); |
||
1399 | $obj->ID = 1001; |
||
1400 | $obj->Title = 'asdfasdf'; |
||
1401 | $obj->SubclassDatabaseField = 'asdfasdf'; |
||
1402 | $obj->write(false, true); |
||
1403 | if (method_exists($conn, 'allowPrimaryKeyEditing')) { |
||
1404 | $conn->allowPrimaryKeyEditing(DataObjectTest\Team::class, false); |
||
1405 | } |
||
1406 | |||
1407 | $this->assertEquals( |
||
1408 | DataObjectTest\SubTeam::class, |
||
1409 | DB::query("SELECT \"ClassName\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $obj->ID")->value() |
||
1410 | ); |
||
1411 | |||
1412 | /* Check that it actually saves to the database with the correct ID */ |
||
1413 | $this->assertEquals( |
||
1414 | "1001", |
||
1415 | DB::query( |
||
1416 | "SELECT \"ID\" FROM \"DataObjectTest_SubTeam\" WHERE \"SubclassDatabaseField\" = 'asdfasdf'" |
||
1417 | )->value() |
||
1418 | ); |
||
1419 | $this->assertEquals( |
||
1420 | "1001", |
||
1421 | DB::query("SELECT \"ID\" FROM \"DataObjectTest_Team\" WHERE \"Title\" = 'asdfasdf'")->value() |
||
1422 | ); |
||
1423 | } |
||
1424 | |||
1425 | public function testHasOwnTable() |
||
1426 | { |
||
1427 | $schema = DataObject::getSchema(); |
||
1428 | /* Test DataObject::has_own_table() returns true if the object has $has_one or $db values */ |
||
1429 | $this->assertTrue($schema->classHasTable(DataObjectTest\Player::class)); |
||
1430 | $this->assertTrue($schema->classHasTable(DataObjectTest\Team::class)); |
||
1431 | $this->assertTrue($schema->classHasTable(DataObjectTest\Fixture::class)); |
||
1432 | |||
1433 | /* Root DataObject that always have a table, even if they lack both $db and $has_one */ |
||
1434 | $this->assertTrue($schema->classHasTable(DataObjectTest\FieldlessTable::class)); |
||
1435 | |||
1436 | /* Subclasses without $db or $has_one don't have a table */ |
||
1437 | $this->assertFalse($schema->classHasTable(DataObjectTest\FieldlessSubTable::class)); |
||
1438 | |||
1439 | /* Return false if you don't pass it a subclass of DataObject */ |
||
1440 | $this->assertFalse($schema->classHasTable(DataObject::class)); |
||
1441 | $this->assertFalse($schema->classHasTable(ViewableData::class)); |
||
1442 | |||
1443 | /* Invalid class name */ |
||
1444 | $this->assertFalse($schema->classHasTable("ThisIsntADataObject")); |
||
1445 | } |
||
1446 | |||
1447 | public function testMerge() |
||
1448 | { |
||
1449 | // test right merge of subclasses |
||
1450 | $left = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam1'); |
||
1451 | $right = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam2_with_player_relation'); |
||
1452 | $leftOrigID = $left->ID; |
||
1453 | $left->merge($right, 'right', false, false); |
||
1454 | $this->assertEquals( |
||
1455 | $left->Title, |
||
1456 | 'Subteam 2', |
||
1457 | 'merge() with "right" priority overwrites fields with existing values on subclasses' |
||
1458 | ); |
||
1459 | $this->assertEquals( |
||
1460 | $left->ID, |
||
1461 | $leftOrigID, |
||
1462 | 'merge() with "right" priority doesnt overwrite database ID' |
||
1463 | ); |
||
1464 | |||
1465 | // test overwriteWithEmpty flag on existing left values |
||
1466 | $left = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam2_with_player_relation'); |
||
1467 | $right = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam3_with_empty_fields'); |
||
1468 | $left->merge($right, 'right', false, true); |
||
1469 | $this->assertEquals( |
||
1470 | $left->Title, |
||
1471 | 'Subteam 3', |
||
1472 | 'merge() with $overwriteWithEmpty overwrites non-empty fields on left object' |
||
1473 | ); |
||
1474 | |||
1475 | // test overwriteWithEmpty flag on empty left values |
||
1476 | $left = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam1'); |
||
1477 | // $SubclassDatabaseField is empty on here |
||
1478 | $right = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam2_with_player_relation'); |
||
1479 | $left->merge($right, 'right', false, true); |
||
1480 | $this->assertEquals( |
||
1481 | $left->SubclassDatabaseField, |
||
1482 | null, |
||
1483 | 'merge() with $overwriteWithEmpty overwrites empty fields on left object' |
||
1484 | ); |
||
1485 | |||
1486 | // @todo test "left" priority flag |
||
1487 | // @todo test includeRelations flag |
||
1488 | // @todo test includeRelations in combination with overwriteWithEmpty |
||
1489 | // @todo test has_one relations |
||
1490 | // @todo test has_many and many_many relations |
||
1491 | } |
||
1492 | |||
1493 | public function testPopulateDefaults() |
||
1494 | { |
||
1495 | $obj = new DataObjectTest\Fixture(); |
||
1496 | $this->assertEquals( |
||
1497 | $obj->MyFieldWithDefault, |
||
1498 | 'Default Value', |
||
1499 | 'Defaults are populated for in-memory object from $defaults array' |
||
1500 | ); |
||
1501 | |||
1502 | $this->assertEquals( |
||
1503 | $obj->MyFieldWithAltDefault, |
||
1504 | 'Default Value', |
||
1505 | 'Defaults are populated from overloaded populateDefaults() method' |
||
1506 | ); |
||
1507 | } |
||
1508 | |||
1509 | /** |
||
1510 | * @expectedException \InvalidArgumentException |
||
1511 | */ |
||
1512 | public function testValidateModelDefinitionsFailsWithArray() |
||
1513 | { |
||
1514 | Config::modify()->merge(DataObjectTest\Team::class, 'has_one', array('NotValid' => array('NoArraysAllowed'))); |
||
1515 | DataObject::getSchema()->hasOneComponent(DataObjectTest\Team::class, 'NotValid'); |
||
1516 | } |
||
1517 | |||
1518 | /** |
||
1519 | * @expectedException \InvalidArgumentException |
||
1520 | */ |
||
1521 | public function testValidateModelDefinitionsFailsWithIntKey() |
||
1522 | { |
||
1523 | Config::modify()->set(DataObjectTest\Team::class, 'has_many', array(0 => DataObjectTest\Player::class)); |
||
1524 | DataObject::getSchema()->hasManyComponent(DataObjectTest\Team::class, 0); |
||
1525 | } |
||
1526 | |||
1527 | /** |
||
1528 | * @expectedException \InvalidArgumentException |
||
1529 | */ |
||
1530 | public function testValidateModelDefinitionsFailsWithIntValue() |
||
1531 | { |
||
1532 | Config::modify()->merge(DataObjectTest\Team::class, 'many_many', array('Players' => 12)); |
||
1533 | DataObject::getSchema()->manyManyComponent(DataObjectTest\Team::class, 'Players'); |
||
1534 | } |
||
1535 | |||
1536 | public function testNewClassInstance() |
||
1537 | { |
||
1538 | $dataObject = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
1539 | $changedDO = $dataObject->newClassInstance(DataObjectTest\SubTeam::class); |
||
1540 | $changedFields = $changedDO->getChangedFields(); |
||
1541 | |||
1542 | // Don't write the record, it will reset changed fields |
||
1543 | $this->assertInstanceOf(DataObjectTest\SubTeam::class, $changedDO); |
||
1544 | $this->assertEquals($changedDO->ClassName, DataObjectTest\SubTeam::class); |
||
1545 | $this->assertEquals($changedDO->RecordClassName, DataObjectTest\SubTeam::class); |
||
1546 | $this->assertContains('ClassName', array_keys($changedFields)); |
||
1547 | $this->assertEquals($changedFields['ClassName']['before'], DataObjectTest\Team::class); |
||
1548 | $this->assertEquals($changedFields['ClassName']['after'], DataObjectTest\SubTeam::class); |
||
1549 | $this->assertEquals($changedFields['RecordClassName']['before'], DataObjectTest\Team::class); |
||
1550 | $this->assertEquals($changedFields['RecordClassName']['after'], DataObjectTest\SubTeam::class); |
||
1551 | |||
1552 | $changedDO->write(); |
||
1553 | |||
1554 | $this->assertInstanceOf(DataObjectTest\SubTeam::class, $changedDO); |
||
1555 | $this->assertEquals($changedDO->ClassName, DataObjectTest\SubTeam::class); |
||
1556 | |||
1557 | // Test invalid classes fail |
||
1558 | $this->expectException(InvalidArgumentException::class); |
||
1559 | $this->expectExceptionMessage('Controller is not a valid subclass of DataObject'); |
||
1560 | /** |
||
1561 | * @skipUpgrade |
||
1562 | */ |
||
1563 | $dataObject->newClassInstance('Controller'); |
||
1564 | } |
||
1565 | |||
1566 | public function testMultipleManyManyWithSameClass() |
||
1567 | { |
||
1568 | $team = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
1569 | $company2 = $this->objFromFixture(DataObjectTest\EquipmentCompany::class, 'equipmentcompany2'); |
||
1570 | $sponsors = $team->Sponsors(); |
||
1571 | $equipmentSuppliers = $team->EquipmentSuppliers(); |
||
1572 | |||
1573 | // Check that DataObject::many_many() works as expected |
||
1574 | $manyManyComponent = DataObject::getSchema()->manyManyComponent(DataObjectTest\Team::class, 'Sponsors'); |
||
1575 | $this->assertEquals(ManyManyList::class, $manyManyComponent['relationClass']); |
||
1576 | $this->assertEquals( |
||
1577 | DataObjectTest\Team::class, |
||
1578 | $manyManyComponent['parentClass'], |
||
1579 | 'DataObject::many_many() didn\'t find the correct base class' |
||
1580 | ); |
||
1581 | $this->assertEquals( |
||
1582 | DataObjectTest\EquipmentCompany::class, |
||
1583 | $manyManyComponent['childClass'], |
||
1584 | 'DataObject::many_many() didn\'t find the correct target class for the relation' |
||
1585 | ); |
||
1586 | $this->assertEquals( |
||
1587 | 'DataObjectTest_EquipmentCompany_SponsoredTeams', |
||
1588 | $manyManyComponent['join'], |
||
1589 | 'DataObject::many_many() didn\'t find the correct relation table' |
||
1590 | ); |
||
1591 | $this->assertEquals('DataObjectTest_TeamID', $manyManyComponent['parentField']); |
||
1592 | $this->assertEquals('DataObjectTest_EquipmentCompanyID', $manyManyComponent['childField']); |
||
1593 | |||
1594 | // Check that ManyManyList still works |
||
1595 | $this->assertEquals(2, $sponsors->count(), 'Rows are missing from relation'); |
||
1596 | $this->assertEquals(1, $equipmentSuppliers->count(), 'Rows are missing from relation'); |
||
1597 | |||
1598 | // Check everything works when no relation is present |
||
1599 | $teamWithoutSponsor = $this->objFromFixture(DataObjectTest\Team::class, 'team3'); |
||
1600 | $this->assertInstanceOf(ManyManyList::class, $teamWithoutSponsor->Sponsors()); |
||
1601 | $this->assertEquals(0, $teamWithoutSponsor->Sponsors()->count()); |
||
1602 | |||
1603 | // Test that belongs_many_many can be infered from with getNonReciprocalComponent |
||
1604 | $this->assertListEquals( |
||
1605 | [ |
||
1606 | ['Name' => 'Company corp'], |
||
1607 | ['Name' => 'Team co.'], |
||
1608 | ], |
||
1609 | $team->inferReciprocalComponent(DataObjectTest\EquipmentCompany::class, 'SponsoredTeams') |
||
1610 | ); |
||
1611 | |||
1612 | // Test that many_many can be infered from getNonReciprocalComponent |
||
1613 | $this->assertListEquals( |
||
1614 | [ |
||
1615 | ['Title' => 'Team 1'], |
||
1616 | ['Title' => 'Team 2'], |
||
1617 | ['Title' => 'Subteam 1'], |
||
1618 | ], |
||
1619 | $company2->inferReciprocalComponent(DataObjectTest\Team::class, 'Sponsors') |
||
1620 | ); |
||
1621 | |||
1622 | // Check many_many_extraFields still works |
||
1623 | $equipmentCompany = $this->objFromFixture(DataObjectTest\EquipmentCompany::class, 'equipmentcompany1'); |
||
1624 | $equipmentCompany->SponsoredTeams()->add($teamWithoutSponsor, array('SponsorFee' => 1000)); |
||
1625 | $sponsoredTeams = $equipmentCompany->SponsoredTeams(); |
||
1626 | $this->assertEquals( |
||
1627 | 1000, |
||
1628 | $sponsoredTeams->byID($teamWithoutSponsor->ID)->SponsorFee, |
||
1629 | 'Data from many_many_extraFields was not stored/extracted correctly' |
||
1630 | ); |
||
1631 | |||
1632 | // Check subclasses correctly inherit multiple many_manys |
||
1633 | $subTeam = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam1'); |
||
1634 | $this->assertEquals( |
||
1635 | 2, |
||
1636 | $subTeam->Sponsors()->count(), |
||
1637 | 'Child class did not inherit multiple many_manys' |
||
1638 | ); |
||
1639 | $this->assertEquals( |
||
1640 | 1, |
||
1641 | $subTeam->EquipmentSuppliers()->count(), |
||
1642 | 'Child class did not inherit multiple many_manys' |
||
1643 | ); |
||
1644 | // Team 2 has one EquipmentCompany sponsor and one SubEquipmentCompany |
||
1645 | $team2 = $this->objFromFixture(DataObjectTest\Team::class, 'team2'); |
||
1646 | $this->assertEquals( |
||
1647 | 2, |
||
1648 | $team2->Sponsors()->count(), |
||
1649 | 'Child class did not inherit multiple belongs_many_manys' |
||
1650 | ); |
||
1651 | |||
1652 | // Check many_many_extraFields also works from the belongs_many_many side |
||
1653 | $sponsors = $team2->Sponsors(); |
||
1654 | $sponsors->add($equipmentCompany, array('SponsorFee' => 750)); |
||
1655 | $this->assertEquals( |
||
1656 | 750, |
||
1657 | $sponsors->byID($equipmentCompany->ID)->SponsorFee, |
||
1658 | 'Data from many_many_extraFields was not stored/extracted correctly' |
||
1659 | ); |
||
1660 | |||
1661 | $subEquipmentCompany = $this->objFromFixture(DataObjectTest\SubEquipmentCompany::class, 'subequipmentcompany1'); |
||
1662 | $subTeam->Sponsors()->add($subEquipmentCompany, array('SponsorFee' => 1200)); |
||
1663 | $this->assertEquals( |
||
1664 | 1200, |
||
1665 | $subTeam->Sponsors()->byID($subEquipmentCompany->ID)->SponsorFee, |
||
1666 | 'Data from inherited many_many_extraFields was not stored/extracted correctly' |
||
1667 | ); |
||
1668 | } |
||
1669 | |||
1670 | public function testManyManyExtraFields() |
||
1671 | { |
||
1672 | $team = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
||
1673 | $schema = DataObject::getSchema(); |
||
1674 | |||
1675 | // Get all extra fields |
||
1676 | $teamExtraFields = $team->manyManyExtraFields(); |
||
1677 | $this->assertEquals( |
||
1678 | array( |
||
1679 | 'Players' => array('Position' => 'Varchar(100)') |
||
1680 | ), |
||
1681 | $teamExtraFields |
||
1682 | ); |
||
1683 | |||
1684 | // Ensure fields from parent classes are included |
||
1685 | $subTeam = singleton(DataObjectTest\SubTeam::class); |
||
1686 | $teamExtraFields = $subTeam->manyManyExtraFields(); |
||
1687 | $this->assertEquals( |
||
1688 | array( |
||
1689 | 'Players' => array('Position' => 'Varchar(100)'), |
||
1690 | 'FormerPlayers' => array('Position' => 'Varchar(100)') |
||
1691 | ), |
||
1692 | $teamExtraFields |
||
1693 | ); |
||
1694 | |||
1695 | // Extra fields are immediately available on the Team class (defined in $many_many_extraFields) |
||
1696 | $teamExtraFields = $schema->manyManyExtraFieldsForComponent(DataObjectTest\Team::class, 'Players'); |
||
1697 | $this->assertEquals( |
||
1698 | $teamExtraFields, |
||
1699 | array( |
||
1700 | 'Position' => 'Varchar(100)' |
||
1701 | ) |
||
1702 | ); |
||
1703 | |||
1704 | // We'll have to go through the relation to get the extra fields on Player |
||
1705 | $playerExtraFields = $schema->manyManyExtraFieldsForComponent(DataObjectTest\Player::class, 'Teams'); |
||
1706 | $this->assertEquals( |
||
1707 | $playerExtraFields, |
||
1708 | array( |
||
1709 | 'Position' => 'Varchar(100)' |
||
1710 | ) |
||
1711 | ); |
||
1712 | |||
1713 | // Iterate through a many-many relationship and confirm that extra fields are included |
||
1714 | $newTeam = new DataObjectTest\Team(); |
||
1715 | $newTeam->Title = "New team"; |
||
1716 | $newTeam->write(); |
||
1717 | $newTeamID = $newTeam->ID; |
||
1718 | |||
1719 | $newPlayer = new DataObjectTest\Player(); |
||
1720 | $newPlayer->FirstName = "Sam"; |
||
1721 | $newPlayer->Surname = "Minnee"; |
||
1722 | $newPlayer->write(); |
||
1723 | |||
1724 | // The idea of Sam as a prop is essentially humourous. |
||
1725 | $newTeam->Players()->add($newPlayer, array("Position" => "Prop")); |
||
1726 | |||
1727 | // Requery and uncache everything |
||
1728 | $newTeam->flushCache(); |
||
1729 | $newTeam = DataObject::get_by_id(DataObjectTest\Team::class, $newTeamID); |
||
1730 | |||
1731 | // Check that the Position many_many_extraField is extracted. |
||
1732 | $player = $newTeam->Players()->first(); |
||
1733 | $this->assertEquals('Sam', $player->FirstName); |
||
1734 | $this->assertEquals("Prop", $player->Position); |
||
1735 | |||
1736 | // Check that ordering a many-many relation by an aggregate column doesn't fail |
||
1737 | $player = $this->objFromFixture(DataObjectTest\Player::class, 'player2'); |
||
1738 | $player->Teams()->sort("count(DISTINCT \"DataObjectTest_Team_Players\".\"DataObjectTest_PlayerID\") DESC"); |
||
1739 | } |
||
1740 | |||
1741 | /** |
||
1742 | * Check that the queries generated for many-many relation queries can have unlimitedRowCount |
||
1743 | * called on them. |
||
1744 | */ |
||
1745 | public function testManyManyUnlimitedRowCount() |
||
1746 | { |
||
1747 | $player = $this->objFromFixture(DataObjectTest\Player::class, 'player2'); |
||
1748 | // TODO: What's going on here? |
||
1749 | $this->assertEquals(2, $player->Teams()->dataQuery()->query()->unlimitedRowCount()); |
||
1750 | } |
||
1751 | |||
1752 | /** |
||
1753 | * Tests that singular_name() generates sensible defaults. |
||
1754 | */ |
||
1755 | public function testSingularName() |
||
1756 | { |
||
1757 | $assertions = array( |
||
1758 | DataObjectTest\Player::class => 'Player', |
||
1759 | DataObjectTest\Team::class => 'Team', |
||
1760 | DataObjectTest\Fixture::class => 'Fixture', |
||
1761 | ); |
||
1762 | |||
1763 | foreach ($assertions as $class => $expectedSingularName) { |
||
1764 | $this->assertEquals( |
||
1765 | $expectedSingularName, |
||
1766 | singleton($class)->singular_name(), |
||
1767 | "Assert that the singular_name for '$class' is correct." |
||
1768 | ); |
||
1769 | } |
||
1770 | } |
||
1771 | |||
1772 | /** |
||
1773 | * Tests that plural_name() generates sensible defaults. |
||
1774 | */ |
||
1775 | public function testPluralName() |
||
1776 | { |
||
1777 | $assertions = array( |
||
1778 | DataObjectTest\Player::class => 'Players', |
||
1779 | DataObjectTest\Team::class => 'Teams', |
||
1780 | DataObjectTest\Fixture::class => 'Fixtures', |
||
1781 | DataObjectTest\Play::class => 'Plays', |
||
1782 | DataObjectTest\Bogey::class => 'Bogeys', |
||
1783 | DataObjectTest\Ploy::class => 'Ploys', |
||
1784 | ); |
||
1785 | i18n::set_locale('en_NZ'); |
||
1786 | foreach ($assertions as $class => $expectedPluralName) { |
||
1787 | $this->assertEquals( |
||
1788 | $expectedPluralName, |
||
1789 | DataObject::singleton($class)->plural_name(), |
||
1790 | "Assert that the plural_name for '$class' is correct." |
||
1791 | ); |
||
1792 | $this->assertEquals( |
||
1793 | $expectedPluralName, |
||
1794 | DataObject::singleton($class)->i18n_plural_name(), |
||
1795 | "Assert that the i18n_plural_name for '$class' is correct." |
||
1796 | ); |
||
1797 | } |
||
1798 | } |
||
1799 | |||
1800 | public function testHasDatabaseField() |
||
1801 | { |
||
1802 | $team = singleton(DataObjectTest\Team::class); |
||
1803 | $subteam = singleton(DataObjectTest\SubTeam::class); |
||
1804 | |||
1805 | $this->assertTrue( |
||
1806 | $team->hasDatabaseField('Title'), |
||
1807 | "hasOwnDatabaseField() works with \$db fields" |
||
1808 | ); |
||
1809 | $this->assertTrue( |
||
1810 | $team->hasDatabaseField('CaptainID'), |
||
1811 | "hasOwnDatabaseField() works with \$has_one fields" |
||
1812 | ); |
||
1813 | $this->assertFalse( |
||
1814 | $team->hasDatabaseField('NonExistentField'), |
||
1815 | "hasOwnDatabaseField() doesn't detect non-existend fields" |
||
1816 | ); |
||
1817 | $this->assertTrue( |
||
1818 | $team->hasDatabaseField('ExtendedDatabaseField'), |
||
1819 | "hasOwnDatabaseField() works with extended fields" |
||
1820 | ); |
||
1821 | $this->assertFalse( |
||
1822 | $team->hasDatabaseField('SubclassDatabaseField'), |
||
1823 | "hasOwnDatabaseField() doesn't pick up fields in subclasses on parent class" |
||
1824 | ); |
||
1825 | |||
1826 | $this->assertTrue( |
||
1827 | $subteam->hasDatabaseField('SubclassDatabaseField'), |
||
1828 | "hasOwnDatabaseField() picks up fields in subclasses" |
||
1829 | ); |
||
1830 | } |
||
1831 | |||
1832 | public function testFieldTypes() |
||
1833 | { |
||
1834 | $obj = new DataObjectTest\Fixture(); |
||
1835 | $obj->DateField = '1988-01-02'; |
||
1836 | $obj->DatetimeField = '1988-03-04 06:30'; |
||
1837 | $obj->write(); |
||
1838 | $obj->flushCache(); |
||
1839 | |||
1840 | $obj = DataObject::get_by_id(DataObjectTest\Fixture::class, $obj->ID); |
||
1841 | $this->assertEquals('1988-01-02', $obj->DateField); |
||
1842 | $this->assertEquals('1988-03-04 06:30:00', $obj->DatetimeField); |
||
1843 | } |
||
1844 | |||
1845 | public function testTwoSubclassesWithTheSameFieldNameWork() |
||
1846 | { |
||
1847 | // Create two objects of different subclasses, setting the values of fields that are |
||
1848 | // defined separately in each subclass |
||
1849 | $obj1 = new DataObjectTest\SubTeam(); |
||
1850 | $obj1->SubclassDatabaseField = "obj1"; |
||
1851 | $obj2 = new DataObjectTest\OtherSubclassWithSameField(); |
||
1852 | $obj2->SubclassDatabaseField = "obj2"; |
||
1853 | |||
1854 | // Write them to the database |
||
1855 | $obj1->write(); |
||
1856 | $obj2->write(); |
||
1857 | |||
1858 | // Check that the values of those fields are properly read from the database |
||
1859 | $values = DataObject::get( |
||
1860 | DataObjectTest\Team::class, |
||
1861 | "\"DataObjectTest_Team\".\"ID\" IN |
||
1862 | ($obj1->ID, $obj2->ID)" |
||
1863 | )->column("SubclassDatabaseField"); |
||
1864 | $this->assertEquals(array_intersect($values, array('obj1', 'obj2')), $values); |
||
1865 | } |
||
1866 | |||
1867 | public function testClassNameSetForNewObjects() |
||
1868 | { |
||
1869 | $d = new DataObjectTest\Player(); |
||
1870 | $this->assertEquals(DataObjectTest\Player::class, $d->ClassName); |
||
1871 | } |
||
1872 | |||
1873 | public function testHasValue() |
||
1874 | { |
||
1875 | $team = new DataObjectTest\Team(); |
||
1876 | $this->assertFalse($team->hasValue('Title', null, false)); |
||
1877 | $this->assertFalse($team->hasValue('DatabaseField', null, false)); |
||
1878 | |||
1879 | $team->Title = 'hasValue'; |
||
1880 | $this->assertTrue($team->hasValue('Title', null, false)); |
||
1881 | $this->assertFalse($team->hasValue('DatabaseField', null, false)); |
||
1882 | |||
1883 | $team->Title = '<p></p>'; |
||
1884 | $this->assertTrue( |
||
1885 | $team->hasValue('Title', null, false), |
||
1886 | 'Test that an empty paragraph is a value for non-HTML fields.' |
||
1887 | ); |
||
1888 | |||
1889 | $team->DatabaseField = 'hasValue'; |
||
1890 | $this->assertTrue($team->hasValue('Title', null, false)); |
||
1891 | $this->assertTrue($team->hasValue('DatabaseField', null, false)); |
||
1892 | } |
||
1893 | |||
1894 | public function testHasMany() |
||
1895 | { |
||
1896 | $company = new DataObjectTest\Company(); |
||
1897 | |||
1898 | $this->assertEquals( |
||
1899 | array( |
||
1900 | 'CurrentStaff' => DataObjectTest\Staff::class, |
||
1901 | 'PreviousStaff' => DataObjectTest\Staff::class |
||
1902 | ), |
||
1903 | $company->hasMany(), |
||
1904 | 'has_many strips field name data by default.' |
||
1905 | ); |
||
1906 | |||
1907 | $this->assertEquals( |
||
1908 | DataObjectTest\Staff::class, |
||
1909 | DataObject::getSchema()->hasManyComponent(DataObjectTest\Company::class, 'CurrentStaff'), |
||
1910 | 'has_many strips field name data by default on single relationships.' |
||
1911 | ); |
||
1912 | |||
1913 | $this->assertEquals( |
||
1914 | array( |
||
1915 | 'CurrentStaff' => DataObjectTest\Staff::class . '.CurrentCompany', |
||
1916 | 'PreviousStaff' => DataObjectTest\Staff::class . '.PreviousCompany' |
||
1917 | ), |
||
1918 | $company->hasMany(false), |
||
1919 | 'has_many returns field name data when $classOnly is false.' |
||
1920 | ); |
||
1921 | |||
1922 | $this->assertEquals( |
||
1923 | DataObjectTest\Staff::class . '.CurrentCompany', |
||
1924 | DataObject::getSchema()->hasManyComponent(DataObjectTest\Company::class, 'CurrentStaff', false), |
||
1925 | 'has_many returns field name data on single records when $classOnly is false.' |
||
1926 | ); |
||
1927 | } |
||
1928 | |||
1929 | public function testGetRemoteJoinField() |
||
1930 | { |
||
1931 | $schema = DataObject::getSchema(); |
||
1932 | |||
1933 | // Company schema |
||
1934 | $staffJoinField = $schema->getRemoteJoinField( |
||
1935 | DataObjectTest\Company::class, |
||
1936 | 'CurrentStaff', |
||
1937 | 'has_many', |
||
1938 | $polymorphic |
||
1939 | ); |
||
1940 | $this->assertEquals('CurrentCompanyID', $staffJoinField); |
||
1941 | $this->assertFalse($polymorphic, 'DataObjectTest_Company->CurrentStaff is not polymorphic'); |
||
1942 | $previousStaffJoinField = $schema->getRemoteJoinField( |
||
1943 | DataObjectTest\Company::class, |
||
1944 | 'PreviousStaff', |
||
1945 | 'has_many', |
||
1946 | $polymorphic |
||
1947 | ); |
||
1948 | $this->assertEquals('PreviousCompanyID', $previousStaffJoinField); |
||
1949 | $this->assertFalse($polymorphic, 'DataObjectTest_Company->PreviousStaff is not polymorphic'); |
||
1950 | |||
1951 | // CEO Schema |
||
1952 | $this->assertEquals( |
||
1953 | 'CEOID', |
||
1954 | $schema->getRemoteJoinField( |
||
1955 | DataObjectTest\CEO::class, |
||
1956 | 'Company', |
||
1957 | 'belongs_to', |
||
1958 | $polymorphic |
||
1959 | ) |
||
1960 | ); |
||
1961 | $this->assertFalse($polymorphic, 'DataObjectTest_CEO->Company is not polymorphic'); |
||
1962 | $this->assertEquals( |
||
1963 | 'PreviousCEOID', |
||
1964 | $schema->getRemoteJoinField( |
||
1965 | DataObjectTest\CEO::class, |
||
1966 | 'PreviousCompany', |
||
1967 | 'belongs_to', |
||
1968 | $polymorphic |
||
1969 | ) |
||
1970 | ); |
||
1971 | $this->assertFalse($polymorphic, 'DataObjectTest_CEO->PreviousCompany is not polymorphic'); |
||
1972 | |||
1973 | // Team schema |
||
1974 | $this->assertEquals( |
||
1975 | 'Favourite', |
||
1976 | $schema->getRemoteJoinField( |
||
1977 | DataObjectTest\Team::class, |
||
1978 | 'Fans', |
||
1979 | 'has_many', |
||
1980 | $polymorphic |
||
1981 | ) |
||
1982 | ); |
||
1983 | $this->assertTrue($polymorphic, 'DataObjectTest_Team->Fans is polymorphic'); |
||
1984 | $this->assertEquals( |
||
1985 | 'TeamID', |
||
1986 | $schema->getRemoteJoinField( |
||
1987 | DataObjectTest\Team::class, |
||
1988 | 'Comments', |
||
1989 | 'has_many', |
||
1990 | $polymorphic |
||
1991 | ) |
||
1992 | ); |
||
1993 | $this->assertFalse($polymorphic, 'DataObjectTest_Team->Comments is not polymorphic'); |
||
1994 | } |
||
1995 | |||
1996 | public function testBelongsTo() |
||
1997 | { |
||
1998 | $company = new DataObjectTest\Company(); |
||
1999 | $ceo = new DataObjectTest\CEO(); |
||
2000 | |||
2001 | $company->Name = 'New Company'; |
||
2002 | $company->write(); |
||
2003 | $ceo->write(); |
||
2004 | |||
2005 | // Test belongs_to assignment |
||
2006 | $company->CEOID = $ceo->ID; |
||
2007 | $company->write(); |
||
2008 | |||
2009 | $this->assertEquals($company->ID, $ceo->Company()->ID, 'belongs_to returns the right results.'); |
||
2010 | |||
2011 | // Test belongs_to can be infered via getNonReciprocalComponent |
||
2012 | // Note: Will be returned as has_many since the belongs_to is ignored. |
||
2013 | $this->assertListEquals( |
||
2014 | [['Name' => 'New Company']], |
||
2015 | $ceo->inferReciprocalComponent(DataObjectTest\Company::class, 'CEO') |
||
2016 | ); |
||
2017 | |||
2018 | // Test has_one to a belongs_to can be infered via getNonReciprocalComponent |
||
2019 | $this->assertEquals( |
||
2020 | $ceo->ID, |
||
2021 | $company->inferReciprocalComponent(DataObjectTest\CEO::class, 'Company')->ID |
||
2022 | ); |
||
2023 | |||
2024 | // Test automatic creation of class where no assigment exists |
||
2025 | $ceo = new DataObjectTest\CEO(); |
||
2026 | $ceo->write(); |
||
2027 | |||
2028 | $this->assertTrue( |
||
2029 | $ceo->Company() instanceof DataObjectTest\Company, |
||
2030 | 'DataObjects across belongs_to relations are automatically created.' |
||
2031 | ); |
||
2032 | $this->assertEquals($ceo->ID, $ceo->Company()->CEOID, 'Remote IDs are automatically set.'); |
||
2033 | |||
2034 | // Write object with components |
||
2035 | $ceo->Name = 'Edward Scissorhands'; |
||
2036 | $ceo->write(false, false, false, true); |
||
2037 | $this->assertTrue($ceo->Company()->isInDB(), 'write() writes belongs_to components to the database.'); |
||
2038 | |||
2039 | $newCEO = DataObject::get_by_id(DataObjectTest\CEO::class, $ceo->ID); |
||
2040 | $this->assertEquals( |
||
2041 | $ceo->Company()->ID, |
||
2042 | $newCEO->Company()->ID, |
||
2043 | 'belongs_to can be retrieved from the database.' |
||
2044 | ); |
||
2045 | } |
||
2046 | |||
2047 | public function testBelongsToPolymorphic() |
||
2048 | { |
||
2049 | $company = new DataObjectTest\Company(); |
||
2050 | $ceo = new DataObjectTest\CEO(); |
||
2051 | |||
2052 | $company->write(); |
||
2053 | $ceo->write(); |
||
2054 | |||
2055 | // Test belongs_to assignment |
||
2056 | $company->OwnerID = $ceo->ID; |
||
2057 | $company->OwnerClass = DataObjectTest\CEO::class; |
||
2058 | $company->write(); |
||
2059 | |||
2060 | $this->assertEquals($company->ID, $ceo->CompanyOwned()->ID, 'belongs_to returns the right results.'); |
||
2061 | $this->assertInstanceOf( |
||
2062 | DataObjectTest\Company::class, |
||
2063 | $ceo->CompanyOwned(), |
||
2064 | 'belongs_to returns the right results.' |
||
2065 | ); |
||
2066 | |||
2067 | // Test automatic creation of class where no assigment exists |
||
2068 | $ceo = new DataObjectTest\CEO(); |
||
2069 | $ceo->write(); |
||
2070 | |||
2071 | $this->assertTrue( |
||
2072 | $ceo->CompanyOwned() instanceof DataObjectTest\Company, |
||
2073 | 'DataObjects across polymorphic belongs_to relations are automatically created.' |
||
2074 | ); |
||
2075 | $this->assertEquals($ceo->ID, $ceo->CompanyOwned()->OwnerID, 'Remote IDs are automatically set.'); |
||
2076 | $this->assertInstanceOf($ceo->CompanyOwned()->OwnerClass, $ceo, 'Remote class is automatically set'); |
||
2077 | |||
2078 | // Write object with components |
||
2079 | $ceo->write(false, false, false, true); |
||
2080 | $this->assertTrue($ceo->CompanyOwned()->isInDB(), 'write() writes belongs_to components to the database.'); |
||
2081 | |||
2082 | $newCEO = DataObject::get_by_id(DataObjectTest\CEO::class, $ceo->ID); |
||
2083 | $this->assertEquals( |
||
2084 | $ceo->CompanyOwned()->ID, |
||
2085 | $newCEO->CompanyOwned()->ID, |
||
2086 | 'polymorphic belongs_to can be retrieved from the database.' |
||
2087 | ); |
||
2088 | } |
||
2089 | |||
2090 | /** |
||
2091 | * @expectedException LogicException |
||
2092 | */ |
||
2093 | public function testInvalidate() |
||
2094 | { |
||
2095 | $do = new DataObjectTest\Fixture(); |
||
2096 | $do->write(); |
||
2097 | |||
2098 | $do->delete(); |
||
2099 | |||
2100 | $do->delete(); // Prohibit invalid object manipulation |
||
2101 | $do->write(); |
||
2102 | $do->duplicate(); |
||
2103 | } |
||
2104 | |||
2105 | public function testToMap() |
||
2106 | { |
||
2107 | $obj = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam1'); |
||
2108 | |||
2109 | $map = $obj->toMap(); |
||
2110 | |||
2111 | $this->assertArrayHasKey('ID', $map, 'Contains base fields'); |
||
2112 | $this->assertArrayHasKey('Title', $map, 'Contains fields from parent class'); |
||
2113 | $this->assertArrayHasKey('SubclassDatabaseField', $map, 'Contains fields from concrete class'); |
||
2114 | |||
2115 | $this->assertEquals( |
||
2116 | $obj->ID, |
||
2117 | $map['ID'], |
||
2118 | 'Contains values from base fields' |
||
2119 | ); |
||
2120 | $this->assertEquals( |
||
2121 | $obj->Title, |
||
2122 | $map['Title'], |
||
2123 | 'Contains values from parent class fields' |
||
2124 | ); |
||
2125 | $this->assertEquals( |
||
2126 | $obj->SubclassDatabaseField, |
||
2127 | $map['SubclassDatabaseField'], |
||
2128 | 'Contains values from concrete class fields' |
||
2129 | ); |
||
2130 | |||
2131 | $newObj = new DataObjectTest\SubTeam(); |
||
2132 | $this->assertArrayHasKey('Title', $map, 'Contains null fields'); |
||
2133 | } |
||
2134 | |||
2135 | public function testIsEmpty() |
||
2136 | { |
||
2137 | $objEmpty = new DataObjectTest\Team(); |
||
2138 | $this->assertTrue($objEmpty->isEmpty(), 'New instance without populated defaults is empty'); |
||
2139 | |||
2140 | $objEmpty->Title = '0'; // |
||
2141 | $this->assertFalse($objEmpty->isEmpty(), 'Zero value in attribute considered non-empty'); |
||
2142 | } |
||
2143 | |||
2144 | public function testRelField() |
||
2145 | { |
||
2146 | $captain1 = $this->objFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
2147 | // Test traversal of a single has_one |
||
2148 | $this->assertEquals("Team 1", $captain1->relField('FavouriteTeam.Title')); |
||
2149 | // Test direct field access |
||
2150 | $this->assertEquals("Captain", $captain1->relField('FirstName')); |
||
2151 | |||
2152 | // Test empty link |
||
2153 | $captain2 = $this->objFromFixture(DataObjectTest\Player::class, 'captain2'); |
||
2154 | $this->assertEmpty($captain2->relField('FavouriteTeam.Title')); |
||
2155 | $this->assertNull($captain2->relField('FavouriteTeam.ReturnsNull')); |
||
2156 | $this->assertNull($captain2->relField('FavouriteTeam.ReturnsNull.Title')); |
||
2157 | |||
2158 | $player = $this->objFromFixture(DataObjectTest\Player::class, 'player2'); |
||
2159 | // Test that we can traverse more than once, and that arbitrary methods are okay |
||
2160 | $this->assertEquals("Team 1", $player->relField('Teams.First.Title')); |
||
2161 | |||
2162 | $newPlayer = new DataObjectTest\Player(); |
||
2163 | $this->assertNull($newPlayer->relField('Teams.First.Title')); |
||
2164 | |||
2165 | // Test that relField works on db field manipulations |
||
2166 | $comment = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment3'); |
||
2167 | $this->assertEquals("PHIL IS A UNIQUE GUY, AND COMMENTS ON TEAM2", $comment->relField('Comment.UpperCase')); |
||
2168 | |||
2169 | // relField throws exception on invalid properties |
||
2170 | $this->expectException(LogicException::class); |
||
2171 | $this->expectExceptionMessage("Not is not a relation/field on " . DataObjectTest\TeamComment::class); |
||
2172 | $comment->relField('Not.A.Field'); |
||
2173 | } |
||
2174 | |||
2175 | public function testRelObject() |
||
2176 | { |
||
2177 | $captain1 = $this->objFromFixture(DataObjectTest\Player::class, 'captain1'); |
||
2178 | |||
2179 | // Test traversal of a single has_one |
||
2180 | $this->assertInstanceOf(DBVarchar::class, $captain1->relObject('FavouriteTeam.Title')); |
||
2181 | $this->assertEquals("Team 1", $captain1->relObject('FavouriteTeam.Title')->getValue()); |
||
2182 | |||
2183 | // Test empty link |
||
2184 | $captain2 = $this->objFromFixture(DataObjectTest\Player::class, 'captain2'); |
||
2185 | $this->assertEmpty($captain2->relObject('FavouriteTeam.Title')->getValue()); |
||
2186 | $this->assertNull($captain2->relObject('FavouriteTeam.ReturnsNull.Title')); |
||
2187 | |||
2188 | // Test direct field access |
||
2189 | $this->assertInstanceOf(DBBoolean::class, $captain1->relObject('IsRetired')); |
||
2190 | $this->assertEquals(1, $captain1->relObject('IsRetired')->getValue()); |
||
2191 | |||
2192 | $player = $this->objFromFixture(DataObjectTest\Player::class, 'player2'); |
||
2193 | // Test that we can traverse more than once, and that arbitrary methods are okay |
||
2194 | $this->assertInstanceOf(DBVarchar::class, $player->relObject('Teams.First.Title')); |
||
2195 | $this->assertEquals("Team 1", $player->relObject('Teams.First.Title')->getValue()); |
||
2196 | |||
2197 | // relObject throws exception on invalid properties |
||
2198 | $this->expectException(LogicException::class); |
||
2199 | $this->expectExceptionMessage("Not is not a relation/field on " . DataObjectTest\Player::class); |
||
2200 | $player->relObject('Not.A.Field'); |
||
2201 | } |
||
2202 | |||
2203 | public function testLateStaticBindingStyle() |
||
2204 | { |
||
2205 | // Confirm that DataObjectTest_Player::get() operates as excepted |
||
2206 | $this->assertEquals(4, DataObjectTest\Player::get()->count()); |
||
2207 | $this->assertInstanceOf(DataObjectTest\Player::class, DataObjectTest\Player::get()->first()); |
||
2208 | |||
2209 | // You can't pass arguments to LSB syntax - use the DataList methods instead. |
||
2210 | $this->expectException(InvalidArgumentException::class); |
||
2211 | |||
2212 | DataObjectTest\Player::get(null, "\"ID\" = 1"); |
||
2213 | } |
||
2214 | |||
2215 | /** |
||
2216 | * @expectedException \InvalidArgumentException |
||
2217 | */ |
||
2218 | public function testBrokenLateStaticBindingStyle() |
||
2219 | { |
||
2220 | // If you call DataObject::get() you have to pass a first argument |
||
2221 | DataObject::get(); |
||
2222 | } |
||
2223 | |||
2224 | public function testBigIntField() |
||
2225 | { |
||
2226 | $staff = new DataObjectTest\Staff(); |
||
2227 | $staff->Salary = PHP_INT_MAX; |
||
2228 | $staff->write(); |
||
2229 | $this->assertEquals(PHP_INT_MAX, DataObjectTest\Staff::get()->byID($staff->ID)->Salary); |
||
2230 | } |
||
2231 | |||
2232 | public function testGetOneMissingValueReturnsNull() |
||
2233 | { |
||
2234 | |||
2235 | // Test that missing values return null |
||
2236 | $this->assertEquals(null, DataObject::get_one( |
||
2237 | DataObjectTest\TeamComment::class, |
||
2238 | ['"DataObjectTest_TeamComment"."Name"' => 'does not exists'] |
||
2239 | )); |
||
2240 | } |
||
2241 | |||
2242 | public function testSetFieldWithArrayOnScalarOnlyField() |
||
2243 | { |
||
2244 | $this->expectException(InvalidArgumentException::class); |
||
2245 | $do = Company::singleton(); |
||
2246 | $do->FoundationYear = '1984'; |
||
2247 | $do->FoundationYear = array('Amount' => 123, 'Currency' => 'CAD'); |
||
2248 | $this->assertEmpty($do->FoundationYear); |
||
2249 | } |
||
2250 | |||
2251 | public function testSetFieldWithArrayOnCompositeField() |
||
2252 | { |
||
2253 | $do = Company::singleton(); |
||
2254 | $do->SalaryCap = array('Amount' => 123456, 'Currency' => 'CAD'); |
||
2255 | $this->assertNotEmpty($do->SalaryCap); |
||
2256 | } |
||
2257 | |||
2258 | public function testWriteManipulationWithNonScalarValuesAllowed() |
||
2259 | { |
||
2260 | $do = DataObjectTest\MockDynamicAssignmentDataObject::create(); |
||
2261 | $do->write(); |
||
2262 | |||
2263 | $do->StaticScalarOnlyField = true; |
||
2264 | $do->DynamicScalarOnlyField = false; |
||
2265 | $do->DynamicField = true; |
||
2266 | |||
2267 | $do->write(); |
||
2268 | |||
2269 | $this->assertTrue($do->StaticScalarOnlyField); |
||
2270 | $this->assertFalse($do->DynamicScalarOnlyField); |
||
2271 | $this->assertTrue($do->DynamicField); |
||
2272 | } |
||
2273 | |||
2274 | public function testWriteManipulationWithNonScalarValuesDisallowed() |
||
2286 | } |
||
2287 | } |
||
2288 |
This check looks for accesses to local static members using the fully qualified name instead of
self::
.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBC
could just as well be replaced byself::TRIPLEDES_CBC
. Referencing local members withself::
assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.