Skip to content
This repository
Browse code

MINOR Fixed DataObjectTest to use ManyManyList when testing relation …

…manipulations
  • Loading branch information...
commit 8d7bfd4ffa0310707ff937d97d55608e1363b69d 1 parent 73c2491
authored September 27, 2011 slindqvist committed September 28, 2011

Showing 1 changed file with 49 additions and 43 deletions. Show diff stats Hide diff stats

  1. 92  tests/model/DataObjectTest.php
92  tests/model/DataObjectTest.php
@@ -24,20 +24,17 @@ function testDataIntegrityWhenTwoSubclassesHaveSameField() {
24 24
 		$obj = new DataObjectTest_SubTeam();
25 25
 		$obj->SubclassDatabaseField = "obj-SubTeam";
26 26
 		$obj->write();
27  
-		
  27
+
28 28
 		// Change the class
29 29
 		$obj->ClassName = 'OtherSubclassWithSameField';
30 30
 		$obj->write();
31 31
 		$obj->flushCache();
32  
-		
33  
-		
34  
-		
35  
-		
36  
-		// Re-fetch from the database and confirm that the data is sourced from 
  32
+
  33
+		// Re-fetch from the database and confirm that the data is sourced from
37 34
 		// OtherSubclassWithSameField.SubclassDatabaseField
38 35
 		$obj = DataObject::get_by_id('DataObjectTest_Team', $obj->ID);
39 36
 		$this->assertNull($obj->SubclassDatabaseField);
40  
-		
  37
+
41 38
 		// Confirm that save the object in the other direction.
42 39
 		$obj->SubclassDatabaseField = 'obj-Other';
43 40
 		$obj->write();
@@ -70,8 +67,8 @@ function testDelete() {
70 67
 		// Check that page does not exist after deleting
71 68
 		$obj = DataObject::get_by_id('DataObjectTest_Player', $objID);
72 69
 		$this->assertTrue(!$obj || !$obj->exists());
73  
-		
74  
-		
  70
+
  71
+
75 72
 		// Test deleting using DataObject::delete_by_id()
76 73
 		// Get the second page
77 74
 		$obj = $this->objFromFixture('DataObjectTest_Player', 'captain2');
@@ -84,7 +81,7 @@ function testDelete() {
84 81
 		$obj = DataObject::get_by_id('DataObjectTest_Player', $objID);
85 82
 		$this->assertTrue(!$obj || !$obj->exists());
86 83
 	}
87  
-	
  84
+
88 85
 	/**
89 86
 	 * Test methods that get DataObjects
90 87
 	 *   - DataObject::get()
@@ -103,14 +100,14 @@ function testGet() {
103 100
 		// Test getting all records of a DataObject
104 101
 		$comments = DataObject::get('DataObjectTest_TeamComment');
105 102
 		$this->assertEquals(3, $comments->Count());
106  
-		
  103
+
107 104
 		// Test WHERE clause
108 105
 		$comments = DataObject::get('DataObjectTest_TeamComment', "\"Name\"='Bob'");
109 106
 		$this->assertEquals(1, $comments->Count());
110 107
 		foreach($comments as $comment) {
111 108
 			$this->assertEquals('Bob', $comment->Name);
112 109
 		}
113  
-		
  110
+
114 111
 		// Test sorting
115 112
 		$comments = DataObject::get('DataObjectTest_TeamComment', '', "\"Name\" ASC");
116 113
 		$this->assertEquals(3, $comments->Count());
@@ -118,58 +115,58 @@ function testGet() {
118 115
 		$comments = DataObject::get('DataObjectTest_TeamComment', '', "\"Name\" DESC");
119 116
 		$this->assertEquals(3, $comments->Count());
120 117
 		$this->assertEquals('Phil', $comments->First()->Name);
121  
-		
  118
+
122 119
 		// Test join
123 120
 		$comments = DataObject::get(
124  
-			'DataObjectTest_TeamComment', 
125  
-			"\"DataObjectTest_Team\".\"Title\" = 'Team 1'", 
126  
-			"\"Name\" ASC", 
  121
+			'DataObjectTest_TeamComment',
  122
+			"\"DataObjectTest_Team\".\"Title\" = 'Team 1'",
  123
+			"\"Name\" ASC",
127 124
 			"INNER JOIN \"DataObjectTest_Team\" ON \"DataObjectTest_TeamComment\".\"TeamID\" = \"DataObjectTest_Team\".\"ID\""
128 125
 		);
129  
-			
  126
+
130 127
 		$this->assertEquals(2, $comments->Count());
131 128
 		$this->assertEquals('Bob', $comments->First()->Name);
132 129
 		$this->assertEquals('Joe', $comments->Last()->Name);
133  
-		
  130
+
134 131
 		// Test limit
135 132
 		$comments = DataObject::get('DataObjectTest_TeamComment', '', "\"Name\" ASC", '', '1,2');
136 133
 		$this->assertEquals(2, $comments->Count());
137 134
 		$this->assertEquals('Joe', $comments->First()->Name);
138 135
 		$this->assertEquals('Phil', $comments->Last()->Name);
139  
-		
  136
+
140 137
 		// Test get_by_id()
141 138
 		$captain1ID = $this->idFromFixture('DataObjectTest_Player', 'captain1');
142 139
 		$captain1 = DataObject::get_by_id('DataObjectTest_Player', $captain1ID);
143 140
 		$this->assertEquals('Captain', $captain1->FirstName);
144  
-		
  141
+
145 142
 		// Test get_one() without caching
146 143
 		$comment1 = DataObject::get_one('DataObjectTest_TeamComment', "\"Name\" = 'Joe'", false);
147 144
 		$comment1->Comment = "Something Else";
148  
-		
  145
+
149 146
 		$comment2 = DataObject::get_one('DataObjectTest_TeamComment', "\"Name\" = 'Joe'", false);
150 147
 		$this->assertNotEquals($comment1->Comment, $comment2->Comment);
151  
-		
  148
+
152 149
 		// Test get_one() with caching
153 150
 		$comment1 = DataObject::get_one('DataObjectTest_TeamComment', "\"Name\" = 'Bob'", true);
154 151
 		$comment1->Comment = "Something Else";
155  
-		
  152
+
156 153
 		$comment2 = DataObject::get_one('DataObjectTest_TeamComment', "\"Name\" = 'Bob'", true);
157 154
 		$this->assertEquals((string)$comment1->Comment, (string)$comment2->Comment);
158  
-		
  155
+
159 156
 		// Test get_one() with order by without caching
160 157
 		$comment = DataObject::get_one('DataObjectTest_TeamComment', '', false, "\"Name\" ASC");
161 158
 		$this->assertEquals('Bob', $comment->Name);
162  
-		
  159
+
163 160
 		$comment = DataObject::get_one('DataObjectTest_TeamComment', '', false, "\"Name\" DESC");
164 161
 		$this->assertEquals('Phil', $comment->Name);
165  
-		
  162
+
166 163
 		// Test get_one() with order by with caching
167 164
 		$comment = DataObject::get_one('DataObjectTest_TeamComment', '', true, '"Name" ASC');
168 165
 		$this->assertEquals('Bob', $comment->Name);
169 166
 		$comment = DataObject::get_one('DataObjectTest_TeamComment', '', true, '"Name" DESC');
170 167
 		$this->assertEquals('Phil', $comment->Name);
171 168
 	}
172  
-	
  169
+
173 170
 	function testGetSubclassFields() {
174 171
 		/* Test that fields / has_one relations from the parent table and the subclass tables are extracted */
175 172
 		$captain1 = $this->objFromFixture("DataObjectTest_Player", "captain1");
@@ -180,7 +177,7 @@ function testGetSubclassFields() {
180 177
 		// Subclass has_one relation
181 178
 		$this->assertEquals($this->idFromFixture('DataObjectTest_Team', 'team1'), $captain1->FavouriteTeamID);
182 179
 	}
183  
-	
  180
+
184 181
 	function testGetHasOneRelations() {
185 182
 		$captain1 = $this->objFromFixture("DataObjectTest_Player", "captain1");
186 183
 		/* There will be a field called (relname)ID that contains the ID of the object linked to via the has_one relation */
@@ -188,13 +185,13 @@ function testGetHasOneRelations() {
188 185
 		/* There will be a method called $obj->relname() that returns the object itself */
189 186
 		$this->assertEquals($this->idFromFixture('DataObjectTest_Team', 'team1'), $captain1->FavouriteTeam()->ID);
190 187
 	}
191  
-	
  188
+
192 189
 	function testLimitAndCount() {
193 190
 		$players = DataObject::get("DataObjectTest_Player");
194  
-		
  191
+
195 192
 		// There's 4 records in total
196 193
 		$this->assertEquals(4, $players->count());
197  
-		
  194
+
198 195
 		// Testing "## offset ##" syntax
199 196
 		$this->assertEquals(4, $players->limit("20 OFFSET 0")->count());
200 197
 		$this->assertEquals(0, $players->limit("20 OFFSET 20")->count());
@@ -211,7 +208,7 @@ function testLimitAndCount() {
211 208
 
212 209
 	/**
213 210
 	 * Test writing of database columns which don't correlate to a DBField,
214  
-	 * e.g. all relation fields on has_one/has_many like "ParentID". 
  211
+	 * e.g. all relation fields on has_one/has_many like "ParentID".
215 212
 	 *
216 213
 	 */
217 214
 	function testWritePropertyWithoutDBField() {
@@ -222,7 +219,7 @@ function testWritePropertyWithoutDBField() {
222 219
 		$savedObj = DataObject::get_by_id('DataObjectTest_Player', $obj->ID);
223 220
 		$this->assertTrue($savedObj->FavouriteTeamID == 99);
224 221
 	}
225  
-	
  222
+
226 223
 	/**
227 224
 	 * Test has many relationships
228 225
 	 *   - Test getComponents() gets the ComponentSet of the other side of the relation
@@ -230,15 +227,15 @@ function testWritePropertyWithoutDBField() {
230 227
 	 */
231 228
 	function testHasManyRelationships() {
232 229
 		$team = $this->objFromFixture('DataObjectTest_Team', 'team1');
233  
-		
  230
+
234 231
 		// Test getComponents() gets the ComponentSet of the other side of the relation
235 232
 		$this->assertTrue($team->Comments()->Count() == 2);
236  
-		
  233
+
237 234
 		// Test the IDs on the DataObjects are set correctly
238 235
 		foreach($team->Comments() as $comment) {
239 236
 			$this->assertEquals($team->ID, $comment->TeamID);
240 237
 		}
241  
-		
  238
+
242 239
 		// Test that we can add and remove items that already exist in the database
243 240
 		$newComment = new DataObjectTest_TeamComment();
244 241
 		$newComment->Name = "Automated commenter";
@@ -246,7 +243,7 @@ function testHasManyRelationships() {
246 243
 		$newComment->write();
247 244
 		$team->Comments()->add($newComment);
248 245
 		$this->assertEquals($team->ID, $newComment->TeamID);
249  
-		
  246
+
250 247
 		$comment1 = $this->fixture->objFromFixture('DataObjectTest_TeamComment', 'comment1');
251 248
 		$comment2 = $this->fixture->objFromFixture('DataObjectTest_TeamComment', 'comment2');
252 249
 		$team->Comments()->remove($comment2);
@@ -258,11 +255,11 @@ function testHasManyRelationships() {
258 255
 	function testHasOneRelationship() {
259 256
 		$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
260 257
 		$player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');
261  
-	   
  258
+
262 259
 		// Add a captain to team 1
263 260
 		$team1->setField('CaptainID', $player1->ID);
264 261
 		$team1->write();
265  
-		
  262
+
266 263
 		$this->assertEquals($player1->ID, $team1->Captain()->ID, 'The captain exists for team 1');
267 264
 		$this->assertEquals($player1->ID, $team1->getComponent('Captain')->ID, 'The captain exists through the component getter');
268 265
 
@@ -282,7 +279,10 @@ function testManyManyRelationships() {
282 279
 	   // Test adding single DataObject by reference
283 280
 	   $player1->Teams()->add($team1);
284 281
 	   $player1->flushCache();
285  
-	   $compareTeams = new ManyManyList($team1);
  282
+	   
  283
+	   $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
  284
+	   $compareTeams->forForeignID($player1->ID);
  285
+	   $compareTeams->byID($team1->ID);
286 286
 	   $this->assertEquals(
287 287
 	      $player1->Teams()->column('ID'),
288 288
 	      $compareTeams->column('ID'),
@@ -292,7 +292,9 @@ function testManyManyRelationships() {
292 292
 	   // test removing single DataObject by reference
293 293
 	   $player1->Teams()->remove($team1);
294 294
 	   $player1->flushCache();
295  
-	   $compareTeams = new ManyManyList();
  295
+	   $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
  296
+	   $compareTeams->forForeignID($player1->ID);
  297
+	   $compareTeams->byID($team1->ID);
296 298
 	   $this->assertEquals(
297 299
 	      $player1->Teams()->column('ID'),
298 300
 	      $compareTeams->column('ID'),
@@ -302,7 +304,9 @@ function testManyManyRelationships() {
302 304
 	   // test adding single DataObject by ID
303 305
 	   $player1->Teams()->add($team1->ID);
304 306
 	   $player1->flushCache();
305  
-	   $compareTeams = new ManyManyList($team1);
  307
+	   $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
  308
+	   $compareTeams->forForeignID($player1->ID);
  309
+	   $compareTeams->byID($team1->ID);
306 310
 	   $this->assertEquals(
307 311
 	      $player1->Teams()->column('ID'),
308 312
 	      $compareTeams->column('ID'),
@@ -312,7 +316,9 @@ function testManyManyRelationships() {
312 316
 	   // test removing single DataObject by ID
313 317
 	   $player1->Teams()->removeByID($team1->ID);
314 318
 	   $player1->flushCache();
315  
-	   $compareTeams = new ManyManyList();
  319
+	   $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
  320
+	   $compareTeams->forForeignID($player1->ID);
  321
+	   $compareTeams->byID($team1->ID);
316 322
 	   $this->assertEquals(
317 323
 	      $player1->Teams()->column('ID'),
318 324
 	      $compareTeams->column('ID'),

0 notes on commit 8d7bfd4

Please sign in to comment.
Something went wrong with that request. Please try again.