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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
6 | class DataObjectTest extends SapphireTest { |
||
7 | |||
8 | protected static $fixture_file = 'DataObjectTest.yml'; |
||
9 | |||
10 | protected $extraDataObjects = array( |
||
11 | 'DataObjectTest_Team', |
||
12 | 'DataObjectTest_Fixture', |
||
13 | 'DataObjectTest_SubTeam', |
||
14 | 'OtherSubclassWithSameField', |
||
15 | 'DataObjectTest_FieldlessTable', |
||
16 | 'DataObjectTest_FieldlessSubTable', |
||
17 | 'DataObjectTest_ValidatedObject', |
||
18 | 'DataObjectTest_Player', |
||
19 | 'DataObjectTest_TeamComment', |
||
20 | 'DataObjectTest_EquipmentCompany', |
||
21 | 'DataObjectTest_SubEquipmentCompany', |
||
22 | 'DataObjectTest\NamespacedClass', |
||
23 | 'DataObjectTest\RelationClass', |
||
24 | 'DataObjectTest_ExtendedTeamComment', |
||
25 | 'DataObjectTest_Company', |
||
26 | 'DataObjectTest_Staff', |
||
27 | 'DataObjectTest_CEO', |
||
28 | 'DataObjectTest_Fan', |
||
29 | 'DataObjectTest_Play', |
||
30 | 'DataObjectTest_Ploy', |
||
31 | 'DataObjectTest_Bogey', |
||
32 | 'ManyManyListTest_Product', |
||
33 | 'ManyManyListTest_Category', |
||
34 | ); |
||
35 | |||
36 | /** |
||
37 | * @dataProvider provideSingletons |
||
38 | */ |
||
39 | public function testSingleton($inst, $defaultValue, $altDefaultValue) |
||
40 | { |
||
41 | $inst = $inst(); |
||
42 | // Test that populateDefaults() isn't called on singletons |
||
43 | // which can lead to SQL errors during build, and endless loops |
||
44 | if ($defaultValue) { |
||
45 | $this->assertEquals($defaultValue, $inst->MyFieldWithDefault); |
||
46 | } else { |
||
47 | $this->assertEmpty($inst->MyFieldWithDefault); |
||
48 | } |
||
49 | |||
50 | if ($altDefaultValue) { |
||
51 | $this->assertEquals($altDefaultValue, $inst->MyFieldWithAltDefault); |
||
52 | } else { |
||
53 | $this->assertEmpty($inst->MyFieldWithAltDefault); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | public function provideSingletons() |
||
58 | { |
||
59 | // because PHPUnit evalutes test providers *before* setUp methods |
||
60 | // any extensions added in the setUp methods won't be available |
||
61 | // we must return closures to generate the arguments at run time |
||
62 | return array( |
||
63 | array(function () { |
||
64 | return DataObjectTest_Fixture::create(); |
||
65 | }, 'Default Value', 'Default Value'), |
||
66 | array(function () { |
||
67 | return new DataObjectTest_Fixture(); |
||
68 | }, 'Default Value', 'Default Value'), |
||
69 | array(function () { |
||
70 | return singleton('DataObjectTest_Fixture'); |
||
71 | }, null, null), |
||
72 | array(function () { |
||
73 | return DataObjectTest_Fixture::singleton(); |
||
74 | }, null, null), |
||
75 | array(function () { |
||
76 | return new DataObjectTest_Fixture(null, true); |
||
77 | }, null, null), |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | public function testDb() { |
||
82 | $obj = new DataObjectTest_TeamComment(); |
||
83 | $dbFields = $obj->db(); |
||
84 | |||
85 | // Assert fields are included |
||
86 | $this->assertArrayHasKey('Name', $dbFields); |
||
87 | |||
88 | // Assert the base fields are excluded |
||
89 | $this->assertArrayNotHasKey('Created', $dbFields); |
||
90 | $this->assertArrayNotHasKey('LastEdited', $dbFields); |
||
91 | $this->assertArrayNotHasKey('ClassName', $dbFields); |
||
92 | $this->assertArrayNotHasKey('ID', $dbFields); |
||
93 | |||
94 | // Assert that the correct field type is returned when passing a field |
||
95 | $this->assertEquals('Varchar', $obj->db('Name')); |
||
96 | $this->assertEquals('Text', $obj->db('Comment')); |
||
97 | |||
98 | $obj = new DataObjectTest_ExtendedTeamComment(); |
||
99 | $dbFields = $obj->db(); |
||
100 | |||
101 | // Assert overloaded fields have correct data type |
||
102 | $this->assertEquals('HTMLText', $obj->db('Comment')); |
||
103 | $this->assertEquals('HTMLText', $dbFields['Comment'], |
||
104 | 'Calls to DataObject::db without a field specified return correct data types'); |
||
105 | |||
106 | // assertEquals doesn't verify the order of array elements, so access keys manually to check order: |
||
107 | // expected: array('Name' => 'Varchar', 'Comment' => 'HTMLText') |
||
108 | reset($dbFields); |
||
109 | $this->assertEquals('Name', key($dbFields), 'DataObject::db returns fields in correct order'); |
||
110 | next($dbFields); |
||
111 | $this->assertEquals('Comment', key($dbFields), 'DataObject::db returns fields in correct order'); |
||
112 | } |
||
113 | |||
114 | public function testConstructAcceptsValues() { |
||
115 | // Values can be an array... |
||
116 | $player = new DataObjectTest_Player(array( |
||
117 | 'FirstName' => 'James', |
||
118 | 'Surname' => 'Smith' |
||
119 | )); |
||
120 | |||
121 | $this->assertEquals('James', $player->FirstName); |
||
122 | $this->assertEquals('Smith', $player->Surname); |
||
123 | |||
124 | // ... or a stdClass inst |
||
125 | $data = new stdClass(); |
||
126 | $data->FirstName = 'John'; |
||
127 | $data->Surname = 'Doe'; |
||
128 | $player = new DataObjectTest_Player($data); |
||
129 | |||
130 | $this->assertEquals('John', $player->FirstName); |
||
131 | $this->assertEquals('Doe', $player->Surname); |
||
132 | |||
133 | // IDs should be stored as integers, not strings |
||
134 | $player = new DataObjectTest_Player(array('ID' => '5')); |
||
135 | $this->assertSame(5, $player->ID); |
||
136 | } |
||
137 | |||
138 | public function testValidObjectsForBaseFields() { |
||
139 | $obj = new DataObjectTest_ValidatedObject(); |
||
140 | |||
141 | foreach (array('Created', 'LastEdited', 'ClassName', 'ID') as $field) { |
||
142 | $helper = $obj->dbObject($field); |
||
143 | $this->assertTrue( |
||
144 | ($helper instanceof DBField), |
||
145 | "for {$field} expected helper to be DBField, but was " . |
||
146 | (is_object($helper) ? get_class($helper) : "null") |
||
147 | ); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | public function testDataIntegrityWhenTwoSubclassesHaveSameField() { |
||
152 | // Save data into DataObjectTest_SubTeam.SubclassDatabaseField |
||
153 | $obj = new DataObjectTest_SubTeam(); |
||
154 | $obj->SubclassDatabaseField = "obj-SubTeam"; |
||
155 | $obj->write(); |
||
156 | |||
157 | // Change the class |
||
158 | $obj->ClassName = 'OtherSubclassWithSameField'; |
||
159 | $obj->write(); |
||
160 | $obj->flushCache(); |
||
161 | |||
162 | // Re-fetch from the database and confirm that the data is sourced from |
||
163 | // OtherSubclassWithSameField.SubclassDatabaseField |
||
164 | $obj = DataObject::get_by_id('DataObjectTest_Team', $obj->ID); |
||
165 | $this->assertNull($obj->SubclassDatabaseField); |
||
166 | |||
167 | // Confirm that save the object in the other direction. |
||
168 | $obj->SubclassDatabaseField = 'obj-Other'; |
||
169 | $obj->write(); |
||
170 | |||
171 | $obj->ClassName = 'DataObjectTest_SubTeam'; |
||
172 | $obj->write(); |
||
173 | $obj->flushCache(); |
||
174 | |||
175 | // If we restore the class, the old value has been lying dormant and will be available again. |
||
176 | // NOTE: This behaviour is volatile; we may change this in the future to clear fields that |
||
177 | // are no longer relevant when changing ClassName |
||
178 | $obj = DataObject::get_by_id('DataObjectTest_Team', $obj->ID); |
||
179 | $this->assertEquals('obj-SubTeam', $obj->SubclassDatabaseField); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Test deletion of DataObjects |
||
184 | * - Deleting using delete() on the DataObject |
||
185 | * - Deleting using DataObject::delete_by_id() |
||
186 | */ |
||
187 | public function testDelete() { |
||
188 | // Test deleting using delete() on the DataObject |
||
189 | // Get the first page |
||
190 | $obj = $this->objFromFixture('DataObjectTest_Player', 'captain1'); |
||
191 | $objID = $obj->ID; |
||
192 | // Check the page exists before deleting |
||
193 | $this->assertTrue(is_object($obj) && $obj->exists()); |
||
194 | // Delete the page |
||
195 | $obj->delete(); |
||
196 | // Check that page does not exist after deleting |
||
197 | $obj = DataObject::get_by_id('DataObjectTest_Player', $objID); |
||
198 | $this->assertTrue(!$obj || !$obj->exists()); |
||
199 | |||
200 | |||
201 | // Test deleting using DataObject::delete_by_id() |
||
202 | // Get the second page |
||
203 | $obj = $this->objFromFixture('DataObjectTest_Player', 'captain2'); |
||
204 | $objID = $obj->ID; |
||
205 | // Check the page exists before deleting |
||
206 | $this->assertTrue(is_object($obj) && $obj->exists()); |
||
207 | // Delete the page |
||
208 | DataObject::delete_by_id('DataObjectTest_Player', $obj->ID); |
||
209 | // Check that page does not exist after deleting |
||
210 | $obj = DataObject::get_by_id('DataObjectTest_Player', $objID); |
||
211 | $this->assertTrue(!$obj || !$obj->exists()); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Test methods that get DataObjects |
||
216 | * - DataObject::get() |
||
217 | * - All records of a DataObject |
||
218 | * - Filtering |
||
219 | * - Sorting |
||
220 | * - Joins |
||
221 | * - Limit |
||
222 | * - Container class |
||
223 | * - DataObject::get_by_id() |
||
224 | * - DataObject::get_one() |
||
225 | * - With and without caching |
||
226 | * - With and without ordering |
||
227 | */ |
||
228 | public function testGet() { |
||
229 | // Test getting all records of a DataObject |
||
230 | $comments = DataObject::get('DataObjectTest_TeamComment'); |
||
231 | $this->assertEquals(3, $comments->Count()); |
||
232 | |||
233 | // Test WHERE clause |
||
234 | $comments = DataObject::get('DataObjectTest_TeamComment', "\"Name\"='Bob'"); |
||
235 | $this->assertEquals(1, $comments->Count()); |
||
236 | foreach($comments as $comment) { |
||
237 | $this->assertEquals('Bob', $comment->Name); |
||
238 | } |
||
239 | |||
240 | // Test sorting |
||
241 | $comments = DataObject::get('DataObjectTest_TeamComment', '', "\"Name\" ASC"); |
||
242 | $this->assertEquals(3, $comments->Count()); |
||
243 | $this->assertEquals('Bob', $comments->First()->Name); |
||
244 | $comments = DataObject::get('DataObjectTest_TeamComment', '', "\"Name\" DESC"); |
||
245 | $this->assertEquals(3, $comments->Count()); |
||
246 | $this->assertEquals('Phil', $comments->First()->Name); |
||
247 | |||
248 | // Test limit |
||
249 | $comments = DataObject::get('DataObjectTest_TeamComment', '', "\"Name\" ASC", '', '1,2'); |
||
250 | $this->assertEquals(2, $comments->Count()); |
||
251 | $this->assertEquals('Joe', $comments->First()->Name); |
||
252 | $this->assertEquals('Phil', $comments->Last()->Name); |
||
253 | |||
254 | // Test get_by_id() |
||
255 | $captain1ID = $this->idFromFixture('DataObjectTest_Player', 'captain1'); |
||
256 | $captain1 = DataObject::get_by_id('DataObjectTest_Player', $captain1ID); |
||
257 | $this->assertEquals('Captain', $captain1->FirstName); |
||
258 | |||
259 | // Test get_one() without caching |
||
260 | $comment1 = DataObject::get_one('DataObjectTest_TeamComment', array( |
||
261 | '"DataObjectTest_TeamComment"."Name"' => 'Joe' |
||
262 | ), false); |
||
263 | $comment1->Comment = "Something Else"; |
||
264 | |||
265 | $comment2 = DataObject::get_one('DataObjectTest_TeamComment', array( |
||
266 | '"DataObjectTest_TeamComment"."Name"' => 'Joe' |
||
267 | ), false); |
||
268 | $this->assertNotEquals($comment1->Comment, $comment2->Comment); |
||
269 | |||
270 | // Test get_one() with caching |
||
271 | $comment1 = DataObject::get_one('DataObjectTest_TeamComment', array( |
||
272 | '"DataObjectTest_TeamComment"."Name"' => 'Bob' |
||
273 | ), true); |
||
274 | $comment1->Comment = "Something Else"; |
||
275 | |||
276 | $comment2 = DataObject::get_one('DataObjectTest_TeamComment', array( |
||
277 | '"DataObjectTest_TeamComment"."Name"' => 'Bob' |
||
278 | ), true); |
||
279 | $this->assertEquals((string)$comment1->Comment, (string)$comment2->Comment); |
||
280 | |||
281 | // Test get_one() with order by without caching |
||
282 | $comment = DataObject::get_one('DataObjectTest_TeamComment', '', false, "\"Name\" ASC"); |
||
283 | $this->assertEquals('Bob', $comment->Name); |
||
284 | |||
285 | $comment = DataObject::get_one('DataObjectTest_TeamComment', '', false, "\"Name\" DESC"); |
||
286 | $this->assertEquals('Phil', $comment->Name); |
||
287 | |||
288 | // Test get_one() with order by with caching |
||
289 | $comment = DataObject::get_one('DataObjectTest_TeamComment', '', true, '"Name" ASC'); |
||
290 | $this->assertEquals('Bob', $comment->Name); |
||
291 | $comment = DataObject::get_one('DataObjectTest_TeamComment', '', true, '"Name" DESC'); |
||
292 | $this->assertEquals('Phil', $comment->Name); |
||
293 | } |
||
294 | |||
295 | public function testGetCaseInsensitive() { |
||
296 | // Test get_one() with bad case on the classname |
||
297 | // Note: This will succeed only if the underlying DB server supports case-insensitive |
||
298 | // table names (e.g. such as MySQL, but not SQLite3) |
||
299 | if(!(DB::get_conn() instanceof MySQLDatabase)) { |
||
300 | $this->markTestSkipped('MySQL only'); |
||
301 | } |
||
302 | |||
303 | $subteam1 = DataObject::get_one('dataobjecttest_subteam', array( |
||
304 | '"DataObjectTest_Team"."Title"' => 'Subteam 1' |
||
305 | ), true); |
||
306 | $this->assertNotEmpty($subteam1); |
||
307 | $this->assertEquals($subteam1->Title, "Subteam 1"); |
||
308 | } |
||
309 | |||
310 | public function testGetSubclassFields() { |
||
311 | /* Test that fields / has_one relations from the parent table and the subclass tables are extracted */ |
||
312 | $captain1 = $this->objFromFixture("DataObjectTest_Player", "captain1"); |
||
313 | // Base field |
||
314 | $this->assertEquals('Captain', $captain1->FirstName); |
||
315 | // Subclass field |
||
316 | $this->assertEquals('007', $captain1->ShirtNumber); |
||
317 | // Subclass has_one relation |
||
318 | $this->assertEquals($this->idFromFixture('DataObjectTest_Team', 'team1'), $captain1->FavouriteTeamID); |
||
319 | } |
||
320 | |||
321 | public function testGetRelationClass() { |
||
322 | $obj = new DataObjectTest_Player(); |
||
323 | $this->assertEquals(singleton('DataObjectTest_Player')->getRelationClass('FavouriteTeam'), |
||
324 | 'DataObjectTest_Team', 'has_one is properly inspected'); |
||
325 | $this->assertEquals(singleton('DataObjectTest_Company')->getRelationClass('CurrentStaff'), |
||
326 | 'DataObjectTest_Staff', 'has_many is properly inspected'); |
||
327 | $this->assertEquals(singleton('DataObjectTest_Team')->getRelationClass('Players'), 'DataObjectTest_Player', |
||
328 | 'many_many is properly inspected'); |
||
329 | $this->assertEquals(singleton('DataObjectTest_Player')->getRelationClass('Teams'), 'DataObjectTest_Team', |
||
330 | 'belongs_many_many is properly inspected'); |
||
331 | $this->assertEquals(singleton('DataObjectTest_CEO')->getRelationClass('Company'), 'DataObjectTest_Company', |
||
332 | 'belongs_to is properly inspected'); |
||
333 | $this->assertEquals(singleton('DataObjectTest_Fan')->getRelationClass('Favourite'), 'DataObject', |
||
334 | 'polymorphic has_one is properly inspected'); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Test that has_one relations can be retrieved |
||
339 | */ |
||
340 | public function testGetHasOneRelations() { |
||
341 | $captain1 = $this->objFromFixture("DataObjectTest_Player", "captain1"); |
||
342 | $team1ID = $this->idFromFixture('DataObjectTest_Team', 'team1'); |
||
343 | |||
344 | // There will be a field called (relname)ID that contains the ID of the |
||
345 | // object linked to via the has_one relation |
||
346 | $this->assertEquals($team1ID, $captain1->FavouriteTeamID); |
||
347 | |||
348 | // There will be a method called $obj->relname() that returns the object itself |
||
349 | $this->assertEquals($team1ID, $captain1->FavouriteTeam()->ID); |
||
350 | |||
351 | // Check entity with polymorphic has-one |
||
352 | $fan1 = $this->objFromFixture("DataObjectTest_Fan", "fan1"); |
||
353 | $this->assertTrue((bool)$fan1->hasValue('Favourite')); |
||
354 | |||
355 | // There will be fields named (relname)ID and (relname)Class for polymorphic |
||
356 | // entities |
||
357 | $this->assertEquals($team1ID, $fan1->FavouriteID); |
||
358 | $this->assertEquals('DataObjectTest_Team', $fan1->FavouriteClass); |
||
359 | |||
360 | // There will be a method called $obj->relname() that returns the object itself |
||
361 | $favourite = $fan1->Favourite(); |
||
362 | $this->assertEquals($team1ID, $favourite->ID); |
||
363 | $this->assertInstanceOf('DataObjectTest_Team', $favourite); |
||
364 | |||
365 | // check behaviour of dbObject with polymorphic relations |
||
366 | $favouriteDBObject = $fan1->dbObject('Favourite'); |
||
367 | $favouriteValue = $favouriteDBObject->getValue(); |
||
368 | $this->assertInstanceOf('PolymorphicForeignKey', $favouriteDBObject); |
||
369 | $this->assertEquals($favourite->ID, $favouriteValue->ID); |
||
370 | $this->assertEquals($favourite->ClassName, $favouriteValue->ClassName); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Simple test to ensure that namespaced classes and polymorphic relations work together |
||
375 | */ |
||
376 | public function testPolymorphicNamespacedRelations() { |
||
377 | $parent = new \DataObjectTest\NamespacedClass(); |
||
378 | $parent->Name = 'New Parent'; |
||
379 | $parent->write(); |
||
380 | |||
381 | $child = new \DataObjectTest\RelationClass(); |
||
382 | $child->Title = 'New Child'; |
||
383 | $child->write(); |
||
384 | $parent->Relations()->add($child); |
||
385 | |||
386 | $this->assertEquals(1, $parent->Relations()->count()); |
||
387 | $this->assertEquals(array('New Child'), $parent->Relations()->column('Title')); |
||
388 | $this->assertEquals('New Parent', $child->Parent()->Name); |
||
389 | } |
||
390 | |||
391 | public function testLimitAndCount() { |
||
392 | $players = DataObject::get("DataObjectTest_Player"); |
||
393 | |||
394 | // There's 4 records in total |
||
395 | $this->assertEquals(4, $players->count()); |
||
396 | |||
397 | // Testing "##, ##" syntax |
||
398 | $this->assertEquals(4, $players->limit(20)->count()); |
||
399 | $this->assertEquals(4, $players->limit(20, 0)->count()); |
||
400 | $this->assertEquals(0, $players->limit(20, 20)->count()); |
||
401 | $this->assertEquals(2, $players->limit(2, 0)->count()); |
||
402 | $this->assertEquals(1, $players->limit(5, 3)->count()); |
||
403 | } |
||
404 | |||
405 | public function testWriteNoChangesDoesntUpdateLastEdited() { |
||
406 | // set mock now so we can be certain of LastEdited time for our test |
||
407 | SS_Datetime::set_mock_now('2017-01-01 00:00:00'); |
||
408 | $obj = new DataObjectTest_Player(); |
||
409 | $obj->FirstName = 'Test'; |
||
410 | $obj->Surname = 'Plater'; |
||
411 | $obj->Email = '[email protected]'; |
||
412 | $obj->write(); |
||
413 | $writtenObj = DataObjectTest_Player::get()->byID($obj->ID); |
||
414 | $this->assertEquals('2017-01-01 00:00:00', $writtenObj->LastEdited); |
||
415 | |||
416 | // set mock now so we get a new LastEdited if, for some reason, it's updated |
||
417 | SS_Datetime::set_mock_now('2017-02-01 00:00:00'); |
||
418 | $writtenObj->write(); |
||
419 | $this->assertEquals('2017-01-01 00:00:00', $writtenObj->LastEdited); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Test writing of database columns which don't correlate to a DBField, |
||
424 | * e.g. all relation fields on has_one/has_many like "ParentID". |
||
425 | * |
||
426 | */ |
||
427 | public function testWritePropertyWithoutDBField() { |
||
428 | $obj = $this->objFromFixture('DataObjectTest_Player', 'captain1'); |
||
429 | $obj->FavouriteTeamID = 99; |
||
430 | $obj->write(); |
||
431 | |||
432 | // reload the page from the database |
||
433 | $savedObj = DataObject::get_by_id('DataObjectTest_Player', $obj->ID); |
||
434 | $this->assertTrue($savedObj->FavouriteTeamID == 99); |
||
435 | |||
436 | // Test with porymorphic relation |
||
437 | $obj2 = $this->objFromFixture("DataObjectTest_Fan", "fan1"); |
||
438 | $obj2->FavouriteID = 99; |
||
439 | $obj2->FavouriteClass = 'DataObjectTest_Player'; |
||
440 | $obj2->write(); |
||
441 | |||
442 | $savedObj2 = DataObject::get_by_id('DataObjectTest_Fan', $obj2->ID); |
||
443 | $this->assertTrue($savedObj2->FavouriteID == 99); |
||
444 | $this->assertTrue($savedObj2->FavouriteClass == 'DataObjectTest_Player'); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Test has many relationships |
||
449 | * - Test getComponents() gets the ComponentSet of the other side of the relation |
||
450 | * - Test the IDs on the DataObjects are set correctly |
||
451 | */ |
||
452 | public function testHasManyRelationships() { |
||
453 | $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1'); |
||
454 | |||
455 | // Test getComponents() gets the ComponentSet of the other side of the relation |
||
456 | $this->assertTrue($team1->Comments()->Count() == 2); |
||
457 | |||
458 | // Test the IDs on the DataObjects are set correctly |
||
459 | foreach($team1->Comments() as $comment) { |
||
460 | $this->assertEquals($team1->ID, $comment->TeamID); |
||
461 | } |
||
462 | |||
463 | // Test that we can add and remove items that already exist in the database |
||
464 | $newComment = new DataObjectTest_TeamComment(); |
||
465 | $newComment->Name = "Automated commenter"; |
||
466 | $newComment->Comment = "This is a new comment"; |
||
467 | $newComment->write(); |
||
468 | $team1->Comments()->add($newComment); |
||
469 | $this->assertEquals($team1->ID, $newComment->TeamID); |
||
470 | |||
471 | $comment1 = $this->objFromFixture('DataObjectTest_TeamComment', 'comment1'); |
||
472 | $comment2 = $this->objFromFixture('DataObjectTest_TeamComment', 'comment2'); |
||
473 | $team1->Comments()->remove($comment2); |
||
474 | |||
475 | $team1CommentIDs = $team1->Comments()->sort('ID')->column('ID'); |
||
476 | $this->assertEquals(array($comment1->ID, $newComment->ID), $team1CommentIDs); |
||
477 | |||
478 | // Test that removing an item from a list doesn't remove it from the same |
||
479 | // relation belonging to a different object |
||
480 | $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1'); |
||
481 | $team2 = $this->objFromFixture('DataObjectTest_Team', 'team2'); |
||
482 | $team2->Comments()->remove($comment1); |
||
483 | $team1CommentIDs = $team1->Comments()->sort('ID')->column('ID'); |
||
484 | $this->assertEquals(array($comment1->ID, $newComment->ID), $team1CommentIDs); |
||
485 | } |
||
486 | |||
487 | |||
488 | /** |
||
489 | * Test has many relationships against polymorphic has_one fields |
||
490 | * - Test getComponents() gets the ComponentSet of the other side of the relation |
||
491 | * - Test the IDs on the DataObjects are set correctly |
||
492 | */ |
||
493 | public function testHasManyPolymorphicRelationships() { |
||
494 | $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1'); |
||
495 | |||
496 | // Test getComponents() gets the ComponentSet of the other side of the relation |
||
497 | $this->assertTrue($team1->Fans()->Count() == 2); |
||
498 | |||
499 | // Test the IDs/Classes on the DataObjects are set correctly |
||
500 | foreach($team1->Fans() as $fan) { |
||
501 | $this->assertEquals($team1->ID, $fan->FavouriteID, 'Fan has the correct FavouriteID'); |
||
502 | $this->assertEquals('DataObjectTest_Team', $fan->FavouriteClass, 'Fan has the correct FavouriteClass'); |
||
503 | } |
||
504 | |||
505 | // Test that we can add and remove items that already exist in the database |
||
506 | $newFan = new DataObjectTest_Fan(); |
||
507 | $newFan->Name = "New fan"; |
||
508 | $newFan->write(); |
||
509 | $team1->Fans()->add($newFan); |
||
510 | $this->assertEquals($team1->ID, $newFan->FavouriteID, 'Newly created fan has the correct FavouriteID'); |
||
511 | $this->assertEquals( |
||
512 | 'DataObjectTest_Team', |
||
513 | $newFan->FavouriteClass, |
||
514 | 'Newly created fan has the correct FavouriteClass' |
||
515 | ); |
||
516 | |||
517 | $fan1 = $this->objFromFixture('DataObjectTest_Fan', 'fan1'); |
||
518 | $fan3 = $this->objFromFixture('DataObjectTest_Fan', 'fan3'); |
||
519 | $team1->Fans()->remove($fan3); |
||
520 | |||
521 | $team1FanIDs = $team1->Fans()->sort('ID')->column('ID'); |
||
522 | $this->assertEquals(array($fan1->ID, $newFan->ID), $team1FanIDs); |
||
523 | |||
524 | // Test that removing an item from a list doesn't remove it from the same |
||
525 | // relation belonging to a different object |
||
526 | $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1'); |
||
527 | $player1 = $this->objFromFixture('DataObjectTest_Player', 'player1'); |
||
528 | $player1->Fans()->remove($fan1); |
||
529 | $team1FanIDs = $team1->Fans()->sort('ID')->column('ID'); |
||
530 | $this->assertEquals(array($fan1->ID, $newFan->ID), $team1FanIDs); |
||
531 | } |
||
532 | |||
533 | |||
534 | public function testHasOneRelationship() { |
||
535 | $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1'); |
||
536 | $player1 = $this->objFromFixture('DataObjectTest_Player', 'player1'); |
||
537 | $player2 = $this->objFromFixture('DataObjectTest_Player', 'player2'); |
||
538 | $fan1 = $this->objFromFixture('DataObjectTest_Fan', 'fan1'); |
||
539 | |||
540 | // Test relation probing |
||
541 | $this->assertFalse((bool)$team1->hasValue('Captain', null, false)); |
||
542 | $this->assertFalse((bool)$team1->hasValue('CaptainID', null, false)); |
||
543 | |||
544 | // Add a captain to team 1 |
||
545 | $team1->setField('CaptainID', $player1->ID); |
||
546 | $team1->write(); |
||
547 | |||
548 | $this->assertTrue((bool)$team1->hasValue('Captain', null, false)); |
||
549 | $this->assertTrue((bool)$team1->hasValue('CaptainID', null, false)); |
||
550 | |||
551 | $this->assertEquals($player1->ID, $team1->Captain()->ID, |
||
552 | 'The captain exists for team 1'); |
||
553 | $this->assertEquals($player1->ID, $team1->getComponent('Captain')->ID, |
||
554 | 'The captain exists through the component getter'); |
||
555 | |||
556 | $this->assertEquals($team1->Captain()->FirstName, 'Player 1', |
||
557 | 'Player 1 is the captain'); |
||
558 | $this->assertEquals($team1->getComponent('Captain')->FirstName, 'Player 1', |
||
559 | 'Player 1 is the captain'); |
||
560 | |||
561 | $team1->CaptainID = $player2->ID; |
||
562 | $team1->write(); |
||
563 | |||
564 | $this->assertEquals($player2->ID, $team1->Captain()->ID); |
||
565 | $this->assertEquals($player2->ID, $team1->getComponent('Captain')->ID); |
||
566 | $this->assertEquals('Player 2', $team1->Captain()->FirstName); |
||
567 | $this->assertEquals('Player 2', $team1->getComponent('Captain')->FirstName); |
||
568 | |||
569 | |||
570 | // Set the favourite team for fan1 |
||
571 | $fan1->setField('FavouriteID', $team1->ID); |
||
572 | $fan1->setField('FavouriteClass', $team1->class); |
||
573 | |||
574 | $this->assertEquals($team1->ID, $fan1->Favourite()->ID, 'The team is assigned to fan 1'); |
||
575 | $this->assertInstanceOf($team1->class, $fan1->Favourite(), 'The team is assigned to fan 1'); |
||
576 | $this->assertEquals($team1->ID, $fan1->getComponent('Favourite')->ID, |
||
577 | 'The team exists through the component getter' |
||
578 | ); |
||
579 | $this->assertInstanceOf($team1->class, $fan1->getComponent('Favourite'), |
||
580 | 'The team exists through the component getter' |
||
581 | ); |
||
582 | |||
583 | $this->assertEquals($fan1->Favourite()->Title, 'Team 1', |
||
584 | 'Team 1 is the favourite'); |
||
585 | $this->assertEquals($fan1->getComponent('Favourite')->Title, 'Team 1', |
||
586 | 'Team 1 is the favourite'); |
||
587 | } |
||
588 | |||
589 | /** |
||
590 | * @todo Extend type change tests (e.g. '0'==NULL) |
||
591 | */ |
||
592 | public function testChangedFields() { |
||
593 | $obj = $this->objFromFixture('DataObjectTest_Player', 'captain1'); |
||
594 | $obj->FirstName = 'Captain-changed'; |
||
595 | $obj->IsRetired = true; |
||
596 | |||
597 | $this->assertEquals( |
||
598 | $obj->getChangedFields(true, DataObject::CHANGE_STRICT), |
||
599 | array( |
||
600 | 'FirstName' => array( |
||
601 | 'before' => 'Captain', |
||
602 | 'after' => 'Captain-changed', |
||
603 | 'level' => DataObject::CHANGE_VALUE |
||
604 | ), |
||
605 | 'IsRetired' => array( |
||
606 | 'before' => 1, |
||
607 | 'after' => true, |
||
608 | 'level' => DataObject::CHANGE_STRICT |
||
609 | ) |
||
610 | ), |
||
611 | 'Changed fields are correctly detected with strict type changes (level=1)' |
||
612 | ); |
||
613 | |||
614 | $this->assertEquals( |
||
615 | $obj->getChangedFields(true, DataObject::CHANGE_VALUE), |
||
616 | array( |
||
617 | 'FirstName' => array( |
||
618 | 'before'=>'Captain', |
||
619 | 'after'=>'Captain-changed', |
||
620 | 'level' => DataObject::CHANGE_VALUE |
||
621 | ) |
||
622 | ), |
||
623 | 'Changed fields are correctly detected while ignoring type changes (level=2)' |
||
624 | ); |
||
625 | |||
626 | $newObj = new DataObjectTest_Player(); |
||
627 | $newObj->FirstName = "New Player"; |
||
628 | $this->assertEquals( |
||
629 | array( |
||
630 | 'FirstName' => array( |
||
631 | 'before' => null, |
||
632 | 'after' => 'New Player', |
||
633 | 'level' => DataObject::CHANGE_VALUE |
||
634 | ) |
||
635 | ), |
||
636 | $newObj->getChangedFields(true, DataObject::CHANGE_VALUE), |
||
637 | 'Initialised fields are correctly detected as full changes' |
||
638 | ); |
||
639 | } |
||
640 | |||
641 | public function testIsChanged() { |
||
642 | $obj = $this->objFromFixture('DataObjectTest_Player', 'captain1'); |
||
643 | $obj->NonDBField = 'bob'; |
||
644 | $obj->FirstName = 'Captain-changed'; |
||
645 | $obj->IsRetired = true; // type change only, database stores "1" |
||
646 | |||
647 | // Now that DB fields are changed, isChanged is true |
||
648 | $this->assertTrue($obj->isChanged('NonDBField')); |
||
649 | $this->assertFalse($obj->isChanged('NonField')); |
||
650 | $this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_STRICT)); |
||
651 | $this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_VALUE)); |
||
652 | $this->assertTrue($obj->isChanged('IsRetired', DataObject::CHANGE_STRICT)); |
||
653 | $this->assertFalse($obj->isChanged('IsRetired', DataObject::CHANGE_VALUE)); |
||
654 | $this->assertFalse($obj->isChanged('Email', 1), 'Doesnt change mark unchanged property'); |
||
655 | $this->assertFalse($obj->isChanged('Email', 2), 'Doesnt change mark unchanged property'); |
||
656 | |||
657 | $newObj = new DataObjectTest_Player(); |
||
658 | $newObj->FirstName = "New Player"; |
||
659 | $this->assertTrue($newObj->isChanged('FirstName', DataObject::CHANGE_STRICT)); |
||
660 | $this->assertTrue($newObj->isChanged('FirstName', DataObject::CHANGE_VALUE)); |
||
661 | $this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_STRICT)); |
||
662 | $this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_VALUE)); |
||
663 | |||
664 | $newObj->write(); |
||
665 | $this->assertFalse($newObj->ischanged()); |
||
666 | $this->assertFalse($newObj->isChanged('FirstName', DataObject::CHANGE_STRICT)); |
||
667 | $this->assertFalse($newObj->isChanged('FirstName', DataObject::CHANGE_VALUE)); |
||
668 | $this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_STRICT)); |
||
669 | $this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_VALUE)); |
||
670 | |||
671 | $obj = $this->objFromFixture('DataObjectTest_Player', 'captain1'); |
||
672 | $obj->FirstName = null; |
||
673 | $this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_STRICT)); |
||
674 | $this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_VALUE)); |
||
675 | |||
676 | /* Test when there's not field provided */ |
||
677 | $obj = $this->objFromFixture('DataObjectTest_Player', 'captain2'); |
||
678 | $this->assertFalse($obj->isChanged()); |
||
679 | $obj->NonDBField = 'new value'; |
||
680 | $this->assertFalse($obj->isChanged()); |
||
681 | $obj->FirstName = "New Player"; |
||
682 | $this->assertTrue($obj->isChanged()); |
||
683 | |||
684 | $obj->write(); |
||
685 | $this->assertFalse($obj->isChanged()); |
||
686 | } |
||
687 | |||
688 | public function testRandomSort() { |
||
689 | /* If we perform the same regularly sorted query twice, it should return the same results */ |
||
690 | $itemsA = DataObject::get("DataObjectTest_TeamComment", "", "ID"); |
||
691 | foreach($itemsA as $item) $keysA[] = $item->ID; |
||
692 | |||
693 | $itemsB = DataObject::get("DataObjectTest_TeamComment", "", "ID"); |
||
694 | foreach($itemsB as $item) $keysB[] = $item->ID; |
||
695 | |||
696 | /* Test when there's not field provided */ |
||
697 | $obj = $this->objFromFixture('DataObjectTest_Player', 'captain1'); |
||
698 | $obj->FirstName = "New Player"; |
||
699 | $this->assertTrue($obj->isChanged()); |
||
700 | |||
701 | $obj->write(); |
||
702 | $this->assertFalse($obj->isChanged()); |
||
703 | |||
704 | /* If we perform the same random query twice, it shouldn't return the same results */ |
||
705 | $itemsA = DataObject::get("DataObjectTest_TeamComment", "", DB::get_conn()->random()); |
||
706 | $itemsB = DataObject::get("DataObjectTest_TeamComment", "", DB::get_conn()->random()); |
||
707 | $itemsC = DataObject::get("DataObjectTest_TeamComment", "", DB::get_conn()->random()); |
||
708 | $itemsD = DataObject::get("DataObjectTest_TeamComment", "", DB::get_conn()->random()); |
||
709 | foreach($itemsA as $item) $keysA[] = $item->ID; |
||
710 | foreach($itemsB as $item) $keysB[] = $item->ID; |
||
711 | foreach($itemsC as $item) $keysC[] = $item->ID; |
||
712 | foreach($itemsD as $item) $keysD[] = $item->ID; |
||
713 | |||
714 | // These shouldn't all be the same (run it 4 times to minimise chance of an accidental collision) |
||
715 | // There's about a 1 in a billion chance of an accidental collision |
||
716 | $this->assertTrue($keysA != $keysB || $keysB != $keysC || $keysC != $keysD); |
||
717 | } |
||
718 | |||
719 | public function testWriteSavesToHasOneRelations() { |
||
720 | /* DataObject::write() should save to a has_one relationship if you set a field called (relname)ID */ |
||
721 | $team = new DataObjectTest_Team(); |
||
722 | $captainID = $this->idFromFixture('DataObjectTest_Player', 'player1'); |
||
723 | $team->CaptainID = $captainID; |
||
724 | $team->write(); |
||
725 | $this->assertEquals($captainID, |
||
726 | DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $team->ID")->value()); |
||
727 | |||
728 | /* After giving it a value, you should also be able to set it back to null */ |
||
729 | $team->CaptainID = ''; |
||
730 | $team->write(); |
||
731 | $this->assertEquals(0, |
||
732 | DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $team->ID")->value()); |
||
733 | |||
734 | /* You should also be able to save a blank to it when it's first created */ |
||
735 | $team = new DataObjectTest_Team(); |
||
736 | $team->CaptainID = ''; |
||
737 | $team->write(); |
||
738 | $this->assertEquals(0, |
||
739 | DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $team->ID")->value()); |
||
740 | |||
741 | /* Ditto for existing records without a value */ |
||
742 | $existingTeam = $this->objFromFixture('DataObjectTest_Team', 'team1'); |
||
743 | $existingTeam->CaptainID = ''; |
||
744 | $existingTeam->write(); |
||
745 | $this->assertEquals(0, |
||
746 | DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $existingTeam->ID")->value()); |
||
747 | } |
||
748 | |||
749 | public function testCanAccessHasOneObjectsAsMethods() { |
||
750 | /* If you have a has_one relation 'Captain' on $obj, and you set the $obj->CaptainID = (ID), then the |
||
751 | * object itself should be accessible as $obj->Captain() */ |
||
752 | $team = $this->objFromFixture('DataObjectTest_Team', 'team1'); |
||
753 | $captainID = $this->idFromFixture('DataObjectTest_Player', 'captain1'); |
||
754 | |||
755 | $team->CaptainID = $captainID; |
||
756 | $this->assertNotNull($team->Captain()); |
||
757 | $this->assertEquals($captainID, $team->Captain()->ID); |
||
758 | |||
759 | // Test for polymorphic has_one relations |
||
760 | $fan = $this->objFromFixture('DataObjectTest_Fan', 'fan1'); |
||
761 | $fan->FavouriteID = $team->ID; |
||
762 | $fan->FavouriteClass = $team->class; |
||
763 | $this->assertNotNull($fan->Favourite()); |
||
764 | $this->assertEquals($team->ID, $fan->Favourite()->ID); |
||
765 | $this->assertInstanceOf($team->class, $fan->Favourite()); |
||
766 | } |
||
767 | |||
768 | public function testFieldNamesThatMatchMethodNamesWork() { |
||
769 | /* Check that a field name that corresponds to a method on DataObject will still work */ |
||
770 | $obj = new DataObjectTest_Fixture(); |
||
771 | $obj->Data = "value1"; |
||
772 | $obj->DbObject = "value2"; |
||
773 | $obj->Duplicate = "value3"; |
||
774 | $obj->write(); |
||
775 | |||
776 | $this->assertNotNull($obj->ID); |
||
777 | $this->assertEquals('value1', |
||
778 | DB::query("SELECT \"Data\" FROM \"DataObjectTest_Fixture\" WHERE \"ID\" = $obj->ID")->value()); |
||
779 | $this->assertEquals('value2', |
||
780 | DB::query("SELECT \"DbObject\" FROM \"DataObjectTest_Fixture\" WHERE \"ID\" = $obj->ID")->value()); |
||
781 | $this->assertEquals('value3', |
||
782 | DB::query("SELECT \"Duplicate\" FROM \"DataObjectTest_Fixture\" WHERE \"ID\" = $obj->ID")->value()); |
||
783 | } |
||
784 | |||
785 | /** |
||
786 | * @todo Re-enable all test cases for field existence after behaviour has been fixed |
||
787 | */ |
||
788 | public function testFieldExistence() { |
||
789 | $teamInstance = $this->objFromFixture('DataObjectTest_Team', 'team1'); |
||
790 | $teamSingleton = singleton('DataObjectTest_Team'); |
||
791 | |||
792 | $subteamInstance = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1'); |
||
793 | $subteamSingleton = singleton('DataObjectTest_SubTeam'); |
||
794 | |||
795 | /* hasField() singleton checks */ |
||
796 | $this->assertTrue($teamSingleton->hasField('ID'), |
||
797 | 'hasField() finds built-in fields in singletons'); |
||
798 | $this->assertTrue($teamSingleton->hasField('Title'), |
||
799 | 'hasField() finds custom fields in singletons'); |
||
800 | |||
801 | /* hasField() instance checks */ |
||
802 | $this->assertFalse($teamInstance->hasField('NonExistingField'), |
||
803 | 'hasField() doesnt find non-existing fields in instances'); |
||
804 | $this->assertTrue($teamInstance->hasField('ID'), |
||
805 | 'hasField() finds built-in fields in instances'); |
||
806 | $this->assertTrue($teamInstance->hasField('Created'), |
||
807 | 'hasField() finds built-in fields in instances'); |
||
808 | $this->assertTrue($teamInstance->hasField('DatabaseField'), |
||
809 | 'hasField() finds custom fields in instances'); |
||
810 | //$this->assertFalse($teamInstance->hasField('SubclassDatabaseField'), |
||
811 | //'hasField() doesnt find subclass fields in parentclass instances'); |
||
812 | $this->assertTrue($teamInstance->hasField('DynamicField'), |
||
813 | 'hasField() finds dynamic getters in instances'); |
||
814 | $this->assertTrue($teamInstance->hasField('HasOneRelationshipID'), |
||
815 | 'hasField() finds foreign keys in instances'); |
||
816 | $this->assertTrue($teamInstance->hasField('ExtendedDatabaseField'), |
||
817 | 'hasField() finds extended fields in instances'); |
||
818 | $this->assertTrue($teamInstance->hasField('ExtendedHasOneRelationshipID'), |
||
819 | 'hasField() finds extended foreign keys in instances'); |
||
820 | //$this->assertTrue($teamInstance->hasField('ExtendedDynamicField'), |
||
821 | //'hasField() includes extended dynamic getters in instances'); |
||
822 | |||
823 | /* hasField() subclass checks */ |
||
824 | $this->assertTrue($subteamInstance->hasField('ID'), |
||
825 | 'hasField() finds built-in fields in subclass instances'); |
||
826 | $this->assertTrue($subteamInstance->hasField('Created'), |
||
827 | 'hasField() finds built-in fields in subclass instances'); |
||
828 | $this->assertTrue($subteamInstance->hasField('DatabaseField'), |
||
829 | 'hasField() finds custom fields in subclass instances'); |
||
830 | $this->assertTrue($subteamInstance->hasField('SubclassDatabaseField'), |
||
831 | 'hasField() finds custom fields in subclass instances'); |
||
832 | $this->assertTrue($subteamInstance->hasField('DynamicField'), |
||
833 | 'hasField() finds dynamic getters in subclass instances'); |
||
834 | $this->assertTrue($subteamInstance->hasField('HasOneRelationshipID'), |
||
835 | 'hasField() finds foreign keys in subclass instances'); |
||
836 | $this->assertTrue($subteamInstance->hasField('ExtendedDatabaseField'), |
||
837 | 'hasField() finds extended fields in subclass instances'); |
||
838 | $this->assertTrue($subteamInstance->hasField('ExtendedHasOneRelationshipID'), |
||
839 | 'hasField() finds extended foreign keys in subclass instances'); |
||
840 | |||
841 | /* hasDatabaseField() singleton checks */ |
||
842 | //$this->assertTrue($teamSingleton->hasDatabaseField('ID'), |
||
843 | //'hasDatabaseField() finds built-in fields in singletons'); |
||
844 | $this->assertTrue($teamSingleton->hasDatabaseField('Title'), |
||
845 | 'hasDatabaseField() finds custom fields in singletons'); |
||
846 | |||
847 | /* hasDatabaseField() instance checks */ |
||
848 | $this->assertFalse($teamInstance->hasDatabaseField('NonExistingField'), |
||
849 | 'hasDatabaseField() doesnt find non-existing fields in instances'); |
||
850 | //$this->assertTrue($teamInstance->hasDatabaseField('ID'), |
||
851 | //'hasDatabaseField() finds built-in fields in instances'); |
||
852 | $this->assertTrue($teamInstance->hasDatabaseField('Created'), |
||
853 | 'hasDatabaseField() finds built-in fields in instances'); |
||
854 | $this->assertTrue($teamInstance->hasDatabaseField('DatabaseField'), |
||
855 | 'hasDatabaseField() finds custom fields in instances'); |
||
856 | $this->assertFalse($teamInstance->hasDatabaseField('SubclassDatabaseField'), |
||
857 | 'hasDatabaseField() doesnt find subclass fields in parentclass instances'); |
||
858 | //$this->assertFalse($teamInstance->hasDatabaseField('DynamicField'), |
||
859 | //'hasDatabaseField() doesnt dynamic getters in instances'); |
||
860 | $this->assertTrue($teamInstance->hasDatabaseField('HasOneRelationshipID'), |
||
861 | 'hasDatabaseField() finds foreign keys in instances'); |
||
862 | $this->assertTrue($teamInstance->hasDatabaseField('ExtendedDatabaseField'), |
||
863 | 'hasDatabaseField() finds extended fields in instances'); |
||
864 | $this->assertTrue($teamInstance->hasDatabaseField('ExtendedHasOneRelationshipID'), |
||
865 | 'hasDatabaseField() finds extended foreign keys in instances'); |
||
866 | $this->assertFalse($teamInstance->hasDatabaseField('ExtendedDynamicField'), |
||
867 | 'hasDatabaseField() doesnt include extended dynamic getters in instances'); |
||
868 | |||
869 | /* hasDatabaseField() subclass checks */ |
||
870 | $this->assertTrue($subteamInstance->hasDatabaseField('DatabaseField'), |
||
871 | 'hasField() finds custom fields in subclass instances'); |
||
872 | $this->assertTrue($subteamInstance->hasDatabaseField('SubclassDatabaseField'), |
||
873 | 'hasField() finds custom fields in subclass instances'); |
||
874 | |||
875 | } |
||
876 | |||
877 | /** |
||
878 | * @todo Re-enable all test cases for field inheritance aggregation after behaviour has been fixed |
||
879 | */ |
||
880 | public function testFieldInheritance() { |
||
881 | $teamInstance = $this->objFromFixture('DataObjectTest_Team', 'team1'); |
||
882 | $subteamInstance = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1'); |
||
883 | |||
884 | $this->assertEquals( |
||
885 | array_keys($teamInstance->inheritedDatabaseFields()), |
||
886 | array( |
||
887 | //'ID', |
||
888 | //'ClassName', |
||
889 | //'Created', |
||
890 | //'LastEdited', |
||
891 | 'Title', |
||
892 | 'DatabaseField', |
||
893 | 'ExtendedDatabaseField', |
||
894 | 'CaptainID', |
||
895 | 'HasOneRelationshipID', |
||
896 | 'ExtendedHasOneRelationshipID' |
||
897 | ), |
||
898 | 'inheritedDatabaseFields() contains all fields defined on instance: base, extended and foreign keys' |
||
899 | ); |
||
900 | |||
901 | $this->assertEquals( |
||
902 | array_keys(DataObject::database_fields('DataObjectTest_Team', false)), |
||
903 | array( |
||
904 | //'ID', |
||
905 | 'ClassName', |
||
906 | 'LastEdited', |
||
907 | 'Created', |
||
908 | 'Title', |
||
909 | 'DatabaseField', |
||
910 | 'ExtendedDatabaseField', |
||
911 | 'CaptainID', |
||
912 | 'HasOneRelationshipID', |
||
913 | 'ExtendedHasOneRelationshipID' |
||
914 | ), |
||
915 | 'databaseFields() contains only fields defined on instance, including base, extended and foreign keys' |
||
916 | ); |
||
917 | |||
918 | $this->assertEquals( |
||
919 | array_keys($subteamInstance->inheritedDatabaseFields()), |
||
920 | array( |
||
921 | //'ID', |
||
922 | //'ClassName', |
||
923 | //'Created', |
||
924 | //'LastEdited', |
||
925 | 'SubclassDatabaseField', |
||
926 | 'ParentTeamID', |
||
927 | 'Title', |
||
928 | 'DatabaseField', |
||
929 | 'ExtendedDatabaseField', |
||
930 | 'CaptainID', |
||
931 | 'HasOneRelationshipID', |
||
932 | 'ExtendedHasOneRelationshipID', |
||
933 | ), |
||
934 | 'inheritedDatabaseFields() on subclass contains all fields, including base, extended and foreign keys' |
||
935 | ); |
||
936 | |||
937 | $this->assertEquals( |
||
938 | array_keys(DataObject::database_fields('DataObjectTest_SubTeam', false)), |
||
939 | array( |
||
940 | 'SubclassDatabaseField', |
||
941 | 'ParentTeamID', |
||
942 | ), |
||
943 | 'databaseFields() on subclass contains only fields defined on instance' |
||
944 | ); |
||
945 | } |
||
946 | |||
947 | public function testSearchableFields() { |
||
983 | |||
984 | public function testCastingHelper() { |
||
993 | |||
994 | public function testSummaryFieldsCustomLabels() { |
||
1010 | |||
1011 | public function testDataObjectUpdate() { |
||
1038 | |||
1039 | public function testDataObjectUpdateNew() { |
||
1061 | |||
1062 | public function testWritingInvalidDataObjectThrowsException() { |
||
1068 | |||
1069 | public function testWritingValidDataObjectDoesntThrowException() { |
||
1076 | |||
1077 | public function testSubclassCreation() { |
||
1084 | |||
1085 | public function testForceInsert() { |
||
1105 | |||
1106 | public function TestHasOwnTable() { |
||
1123 | |||
1124 | public function testMerge() { |
||
1168 | |||
1169 | public function testPopulateDefaults() { |
||
1183 | |||
1184 | protected function makeAccessible($object, $method) { |
||
1189 | |||
1190 | public function testValidateModelDefinitionsFailsWithArray() { |
||
1191 | |||
1192 | $object = new DataObjectTest_Team; |
||
1193 | $method = $this->makeAccessible($object, 'validateModelDefinitions'); |
||
1194 | |||
1195 | Config::inst()->update('DataObjectTest_Team', 'has_one', array('NotValid' => array('NoArraysAllowed'))); |
||
1196 | $this->setExpectedException('LogicException'); |
||
1197 | |||
1198 | $method->invoke($object); |
||
1199 | } |
||
1200 | |||
1201 | public function testValidateModelDefinitionsFailsWithIntKey() { |
||
1202 | $object = new DataObjectTest_Team; |
||
1203 | $method = $this->makeAccessible($object, 'validateModelDefinitions'); |
||
1204 | |||
1205 | Config::inst()->update('DataObjectTest_Team', 'has_many', array(12 => 'DataObjectTest_Player')); |
||
1206 | $this->setExpectedException('LogicException'); |
||
1207 | |||
1208 | $method->invoke($object); |
||
1209 | } |
||
1210 | |||
1211 | public function testValidateModelDefinitionsFailsWithIntValue() { |
||
1212 | $object = new DataObjectTest_Team; |
||
1213 | $method = $this->makeAccessible($object, 'validateModelDefinitions'); |
||
1214 | |||
1215 | Config::inst()->update('DataObjectTest_Team', 'many_many', array('Players' => 12)); |
||
1216 | $this->setExpectedException('LogicException'); |
||
1217 | |||
1218 | $method->invoke($object); |
||
1220 | |||
1221 | /** |
||
1222 | * many_many_extraFields is allowed to have an array value, so shouldn't throw an exception |
||
1223 | */ |
||
1224 | public function testValidateModelDefinitionsPassesWithExtraFields() { |
||
1238 | |||
1239 | public function testNewClassInstance() { |
||
1256 | |||
1257 | public function testMultipleManyManyWithSameClass() { |
||
1309 | |||
1310 | public function testManyManyExtraFields() { |
||
1367 | |||
1368 | /** |
||
1369 | * Check that the queries generated for many-many relation queries can have unlimitedRowCount |
||
1370 | * called on them. |
||
1371 | */ |
||
1372 | public function testManyManyUnlimitedRowCount() { |
||
1377 | |||
1378 | /** |
||
1379 | * Tests that singular_name() generates sensible defaults. |
||
1380 | */ |
||
1381 | public function testSingularName() { |
||
1396 | |||
1397 | /** |
||
1398 | * Tests that plural_name() generates sensible defaults. |
||
1399 | */ |
||
1400 | public function testPluralName() { |
||
1418 | |||
1419 | public function testHasDatabaseField() { |
||
1450 | |||
1451 | public function testFieldTypes() { |
||
1462 | |||
1463 | public function testTwoSubclassesWithTheSameFieldNameWork() { |
||
1480 | |||
1481 | public function testClassNameSetForNewObjects() { |
||
1485 | |||
1486 | public function testHasValue() { |
||
1512 | |||
1513 | public function testHasMany() { |
||
1546 | |||
1547 | public function testGetRemoteJoinField() { |
||
1571 | |||
1572 | public function testBelongsTo() { |
||
1605 | |||
1606 | public function testBelongsToPolymorphic() { |
||
1643 | |||
1644 | /** |
||
1645 | * @expectedException LogicException |
||
1646 | */ |
||
1647 | public function testInvalidate() { |
||
1657 | |||
1658 | public function testToMap() { |
||
1677 | |||
1678 | public function testIsEmpty() { |
||
1685 | |||
1686 | public function testRelField() { |
||
1704 | |||
1705 | public function testRelObject() { |
||
1721 | |||
1722 | public function testLateStaticBindingStyle() { |
||
1732 | |||
1733 | public function testBrokenLateStaticBindingStyle() { |
||
1739 | |||
1740 | public function testBigIntField() { |
||
1746 | |||
1747 | } |
||
1748 | |||
2026 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.