1
|
|
|
<?php |
2
|
|
|
class NestedSetBehaviorTest extends CDbTestCase |
3
|
|
|
{ |
4
|
|
|
public $fixtures=array( |
5
|
|
|
'NestedSet', |
6
|
|
|
'NestedSetWithManyRoots', |
7
|
|
|
); |
8
|
|
|
|
9
|
|
|
public function testDescendants() |
10
|
|
|
{ |
11
|
|
|
// single root |
12
|
|
|
$nestedSet=NestedSet::model()->findByPk(1); |
13
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
14
|
|
|
$descendants=$nestedSet->descendants()->findAll(); |
15
|
|
|
$this->assertEquals(count($descendants),6); |
16
|
|
|
foreach($descendants as $descendant) |
17
|
|
|
$this->assertTrue($descendant instanceof NestedSet); |
18
|
|
|
$this->assertEquals($descendants[0]->primaryKey,2); |
19
|
|
|
$this->assertEquals($descendants[1]->primaryKey,3); |
20
|
|
|
$this->assertEquals($descendants[2]->primaryKey,4); |
21
|
|
|
$this->assertEquals($descendants[3]->primaryKey,5); |
22
|
|
|
$this->assertEquals($descendants[4]->primaryKey,6); |
23
|
|
|
$this->assertEquals($descendants[5]->primaryKey,7); |
24
|
|
|
|
25
|
|
|
// many roots |
26
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(1); |
27
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
28
|
|
|
$descendants=$nestedSet->descendants()->findAll(); |
29
|
|
|
$this->assertEquals(count($descendants),6); |
30
|
|
|
foreach($descendants as $descendant) |
31
|
|
|
$this->assertTrue($descendant instanceof NestedSetWithManyRoots); |
32
|
|
|
$this->assertEquals($descendants[0]->primaryKey,2); |
33
|
|
|
$this->assertEquals($descendants[1]->primaryKey,3); |
34
|
|
|
$this->assertEquals($descendants[2]->primaryKey,4); |
35
|
|
|
$this->assertEquals($descendants[3]->primaryKey,5); |
36
|
|
|
$this->assertEquals($descendants[4]->primaryKey,6); |
37
|
|
|
$this->assertEquals($descendants[5]->primaryKey,7); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testChildren() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
// single root |
43
|
|
|
$nestedSet=NestedSet::model()->findByPk(1); |
44
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
45
|
|
|
$children=$nestedSet->children()->findAll(); |
46
|
|
|
$this->assertEquals(count($children),2); |
47
|
|
|
foreach($children as $child) |
48
|
|
|
$this->assertTrue($child instanceof NestedSet); |
49
|
|
|
$this->assertEquals($children[0]->primaryKey,2); |
50
|
|
|
$this->assertEquals($children[1]->primaryKey,5); |
51
|
|
|
|
52
|
|
|
// many roots |
53
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(1); |
54
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
55
|
|
|
$children=$nestedSet->children()->findAll(); |
56
|
|
|
$this->assertEquals(count($children),2); |
57
|
|
|
foreach($children as $child) |
58
|
|
|
$this->assertTrue($child instanceof NestedSetWithManyRoots); |
59
|
|
|
$this->assertEquals($children[0]->primaryKey,2); |
60
|
|
|
$this->assertEquals($children[1]->primaryKey,5); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testAncestors() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
// single root |
66
|
|
|
$nestedSet=NestedSet::model()->findByPk(7); |
67
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
68
|
|
|
$ancestors=$nestedSet->ancestors()->findAll(); |
69
|
|
|
$this->assertEquals(count($ancestors),2); |
70
|
|
|
foreach($ancestors as $ancestor) |
71
|
|
|
$this->assertTrue($ancestor instanceof NestedSet); |
72
|
|
|
$this->assertEquals($ancestors[0]->primaryKey,1); |
73
|
|
|
$this->assertEquals($ancestors[1]->primaryKey,5); |
74
|
|
|
|
75
|
|
|
// many roots |
76
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(7); |
77
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
78
|
|
|
$ancestors=$nestedSet->ancestors()->findAll(); |
79
|
|
|
$this->assertEquals(count($ancestors),2); |
80
|
|
|
foreach($ancestors as $ancestor) |
81
|
|
|
$this->assertTrue($ancestor instanceof NestedSetWithManyRoots); |
82
|
|
|
$this->assertEquals($ancestors[0]->primaryKey,1); |
83
|
|
|
$this->assertEquals($ancestors[1]->primaryKey,5); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testRoots() |
87
|
|
|
{ |
88
|
|
|
// single root |
89
|
|
|
$roots=NestedSet::model()->roots()->findAll(); |
90
|
|
|
$this->assertEquals(count($roots),1); |
91
|
|
|
foreach($roots as $root) |
92
|
|
|
$this->assertTrue($root instanceof NestedSet); |
93
|
|
|
$this->assertEquals($roots[0]->primaryKey,1); |
94
|
|
|
|
95
|
|
|
// many roots |
96
|
|
|
$roots=NestedSetWithManyRoots::model()->roots()->findAll(); |
97
|
|
|
$this->assertEquals(count($roots),2); |
98
|
|
|
foreach($roots as $root) |
99
|
|
|
$this->assertTrue($root instanceof NestedSetWithManyRoots); |
100
|
|
|
$this->assertEquals($roots[0]->primaryKey,1); |
101
|
|
|
$this->assertEquals($roots[1]->primaryKey,8); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testParent() |
105
|
|
|
{ |
106
|
|
|
// single root |
107
|
|
|
$nestedSet=NestedSet::model()->findByPk(4); |
108
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
109
|
|
|
$parent=$nestedSet->parent()->find(); |
110
|
|
|
$this->assertTrue($parent instanceof NestedSet); |
111
|
|
|
$this->assertEquals($parent->primaryKey,2); |
112
|
|
|
|
113
|
|
|
// many roots |
114
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(4); |
115
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
116
|
|
|
$parent=$nestedSet->parent()->find(); |
117
|
|
|
$this->assertTrue($parent instanceof NestedSetWithManyRoots); |
118
|
|
|
$this->assertEquals($parent->primaryKey,2); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
public function testPrev() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
// single root |
124
|
|
|
$nestedSet=NestedSet::model()->findByPk(7); |
125
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
126
|
|
|
$sibling=$nestedSet->prev()->find(); |
127
|
|
|
$this->assertTrue($sibling instanceof NestedSet); |
128
|
|
|
$this->assertEquals($sibling->primaryKey,6); |
129
|
|
|
$sibling=$sibling->prev()->find(); |
130
|
|
|
$this->assertNull($sibling); |
131
|
|
|
|
132
|
|
|
// many roots |
133
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(7); |
134
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
135
|
|
|
$sibling=$nestedSet->prev()->find(); |
136
|
|
|
$this->assertTrue($sibling instanceof NestedSetWithManyRoots); |
137
|
|
|
$this->assertEquals($sibling->primaryKey,6); |
138
|
|
|
$sibling=$sibling->prev()->find(); |
139
|
|
|
$this->assertNull($sibling); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
View Code Duplication |
public function testNext() |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
// single root |
145
|
|
|
$nestedSet=NestedSet::model()->findByPk(6); |
146
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
147
|
|
|
$sibling=$nestedSet->next()->find(); |
148
|
|
|
$this->assertTrue($sibling instanceof NestedSet); |
149
|
|
|
$this->assertEquals($sibling->primaryKey,7); |
150
|
|
|
$sibling=$sibling->next()->find(); |
151
|
|
|
$this->assertNull($sibling); |
152
|
|
|
|
153
|
|
|
// many roots |
154
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(6); |
155
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
156
|
|
|
$sibling=$nestedSet->next()->find(); |
157
|
|
|
$this->assertTrue($sibling instanceof NestedSetWithManyRoots); |
158
|
|
|
$this->assertEquals($sibling->primaryKey,7); |
159
|
|
|
$sibling=$sibling->next()->find(); |
160
|
|
|
$this->assertNull($sibling); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @depends testDescendants |
165
|
|
|
*/ |
166
|
|
|
public function testIsDescendantOf() |
167
|
|
|
{ |
168
|
|
|
// single root |
169
|
|
|
$nestedSet=NestedSet::model()->findByPk(1); |
170
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
171
|
|
|
$descendants=$nestedSet->descendants()->findAll(); |
172
|
|
|
foreach($descendants as $descendant) |
173
|
|
|
$this->assertTrue($descendant->isDescendantOf($nestedSet)); |
174
|
|
|
$descendant=NestedSet::model()->findByPk(4); |
175
|
|
|
$this->assertTrue($descendant instanceof NestedSet); |
176
|
|
|
$this->assertFalse($nestedSet->isDescendantOf($descendant)); |
177
|
|
|
|
178
|
|
|
// many roots |
179
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(1); |
180
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
181
|
|
|
$descendants=$nestedSet->descendants()->findAll(); |
182
|
|
|
foreach($descendants as $descendant) |
183
|
|
|
$this->assertTrue($descendant->isDescendantOf($nestedSet)); |
184
|
|
|
$descendant=NestedSetWithManyRoots::model()->findByPk(4); |
185
|
|
|
$this->assertTrue($descendant instanceof NestedSetWithManyRoots); |
186
|
|
|
$this->assertFalse($nestedSet->isDescendantOf($descendant)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testIsRoot() |
190
|
|
|
{ |
191
|
|
|
// single root |
192
|
|
|
$roots=NestedSet::model()->roots()->findAll(); |
193
|
|
|
$this->assertEquals(count($roots),1); |
194
|
|
|
foreach($roots as $root) |
195
|
|
|
{ |
196
|
|
|
$this->assertTrue($root instanceof NestedSet); |
197
|
|
|
$this->assertTrue($root->isRoot()); |
198
|
|
|
} |
199
|
|
|
$notRoot=NestedSet::model()->findByPk(4); |
200
|
|
|
$this->assertTrue($notRoot instanceof NestedSet); |
201
|
|
|
$this->assertFalse($notRoot->isRoot()); |
202
|
|
|
|
203
|
|
|
// many roots |
204
|
|
|
$roots=NestedSetWithManyRoots::model()->roots()->findAll(); |
205
|
|
|
$this->assertEquals(count($roots),2); |
206
|
|
|
foreach($roots as $root) |
207
|
|
|
{ |
208
|
|
|
$this->assertTrue($root instanceof NestedSetWithManyRoots); |
209
|
|
|
$this->assertTrue($root->isRoot()); |
210
|
|
|
} |
211
|
|
|
$notRoot=NestedSetWithManyRoots::model()->findByPk(4); |
212
|
|
|
$this->assertTrue($notRoot instanceof NestedSetWithManyRoots); |
213
|
|
|
$this->assertFalse($notRoot->isRoot()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testIsLeaf() |
217
|
|
|
{ |
218
|
|
|
// single root |
219
|
|
|
$nestedSet=NestedSet::model()->findByPk(5); |
220
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
221
|
|
|
$this->assertFalse($nestedSet->isLeaf()); |
222
|
|
|
$descendants=$nestedSet->descendants()->findAll(); |
223
|
|
|
$this->assertEquals(count($descendants),2); |
224
|
|
|
foreach($descendants as $descendant) |
225
|
|
|
{ |
226
|
|
|
$this->assertTrue($descendant instanceof NestedSet); |
227
|
|
|
$this->assertTrue($descendant->isLeaf()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
// many roots |
231
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(5); |
232
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
233
|
|
|
$this->assertFalse($nestedSet->isLeaf()); |
234
|
|
|
$descendants=$nestedSet->descendants()->findAll(); |
235
|
|
|
$this->assertEquals(count($descendants),2); |
236
|
|
|
foreach($descendants as $descendant) |
237
|
|
|
{ |
238
|
|
|
$this->assertTrue($descendant instanceof NestedSetWithManyRoots); |
239
|
|
|
$this->assertTrue($descendant->isLeaf()); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testSaveNode() |
244
|
|
|
{ |
245
|
|
|
// single root |
246
|
|
|
|
247
|
|
|
// many roots |
248
|
|
|
$nestedSet=new NestedSetWithManyRoots; |
249
|
|
|
$this->assertFalse($nestedSet->saveNode()); |
250
|
|
|
$nestedSet->name='test'; |
251
|
|
|
$this->assertTrue($nestedSet->saveNode()); |
252
|
|
|
$this->assertEquals($nestedSet->root,$nestedSet->primaryKey); |
253
|
|
|
$this->assertEquals($nestedSet->lft,1); |
254
|
|
|
$this->assertEquals($nestedSet->rgt,2); |
255
|
|
|
$this->assertEquals($nestedSet->level,1); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testDeleteNode() |
259
|
|
|
{ |
260
|
|
|
// single root |
261
|
|
|
$array=NestedSet::model()->findAll(); |
262
|
|
|
$nestedSet=NestedSet::model()->findByPk(4); |
263
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
264
|
|
|
$this->assertTrue($nestedSet->deleteNode()); |
265
|
|
|
$this->assertTrue($this->checkTree()); |
266
|
|
|
$this->assertTrue($nestedSet->getIsDeletedRecord()); |
267
|
|
|
$this->assertTrue($this->checkArray($array)); |
268
|
|
|
$nestedSet=NestedSet::model()->findByPk(5); |
269
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
270
|
|
|
$this->assertTrue($nestedSet->deleteNode()); |
271
|
|
|
$this->assertTrue($this->checkTree()); |
272
|
|
|
$this->assertTrue($nestedSet->getIsDeletedRecord()); |
273
|
|
|
$this->assertTrue($this->checkArray($array)); |
274
|
|
View Code Duplication |
foreach($array as $item) |
|
|
|
|
275
|
|
|
{ |
276
|
|
|
if(in_array($item->primaryKey,array(4,5,6,7))) |
277
|
|
|
$this->assertTrue($item->getIsDeletedRecord()); |
278
|
|
|
else |
279
|
|
|
$this->assertFalse($item->getIsDeletedRecord()); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
// many roots |
283
|
|
|
$array=NestedSetWithManyRoots::model()->findAll(); |
284
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(4); |
285
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
286
|
|
|
$this->assertTrue($nestedSet->deleteNode()); |
287
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
288
|
|
|
$this->assertTrue($nestedSet->getIsDeletedRecord()); |
289
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
290
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(9); |
291
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
292
|
|
|
$this->assertTrue($nestedSet->deleteNode()); |
293
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
294
|
|
|
$this->assertTrue($nestedSet->getIsDeletedRecord()); |
295
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
296
|
|
View Code Duplication |
foreach($array as $item) |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
if(in_array($item->primaryKey,array(4,9,10,11))) |
299
|
|
|
$this->assertTrue($item->getIsDeletedRecord()); |
300
|
|
|
else |
301
|
|
|
$this->assertFalse($item->getIsDeletedRecord()); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
View Code Duplication |
public function testPrependTo() |
|
|
|
|
306
|
|
|
{ |
307
|
|
|
// single root |
308
|
|
|
$array=NestedSet::model()->findAll(); |
309
|
|
|
$target=NestedSet::model()->findByPk(5); |
310
|
|
|
$this->assertTrue($target instanceof NestedSet); |
311
|
|
|
$nestedSet1=new NestedSet; |
312
|
|
|
$this->assertFalse($nestedSet1->prependTo($target)); |
313
|
|
|
$nestedSet1->name='test'; |
314
|
|
|
$this->assertTrue($nestedSet1->prependTo($target)); |
315
|
|
|
$this->assertTrue($this->checkTree()); |
316
|
|
|
$array[]=$nestedSet1; |
317
|
|
|
$nestedSet2=new NestedSet; |
318
|
|
|
$nestedSet2->name='test'; |
319
|
|
|
$this->assertTrue($nestedSet2->prependTo($target)); |
320
|
|
|
$this->assertTrue($this->checkTree()); |
321
|
|
|
$array[]=$nestedSet2; |
322
|
|
|
$this->assertTrue($this->checkArray($array)); |
323
|
|
|
|
324
|
|
|
// many roots |
325
|
|
|
$array=NestedSetWithManyRoots::model()->findAll(); |
326
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(5); |
327
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
328
|
|
|
$nestedSet1=new NestedSetWithManyRoots; |
329
|
|
|
$this->assertFalse($nestedSet1->prependTo($target)); |
330
|
|
|
$nestedSet1->name='test'; |
331
|
|
|
$this->assertTrue($nestedSet1->prependTo($target)); |
332
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
333
|
|
|
$array[]=$nestedSet1; |
334
|
|
|
$nestedSet2=new NestedSetWithManyRoots; |
335
|
|
|
$nestedSet2->name='test'; |
336
|
|
|
$this->assertTrue($nestedSet2->prependTo($target)); |
337
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
338
|
|
|
$array[]=$nestedSet2; |
339
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
View Code Duplication |
public function testAppendTo() |
|
|
|
|
343
|
|
|
{ |
344
|
|
|
// single root |
345
|
|
|
$array=NestedSet::model()->findAll(); |
346
|
|
|
$target=NestedSet::model()->findByPk(2); |
347
|
|
|
$this->assertTrue($target instanceof NestedSet); |
348
|
|
|
$nestedSet1=new NestedSet; |
349
|
|
|
$this->assertFalse($nestedSet1->appendTo($target)); |
350
|
|
|
$nestedSet1->name='test'; |
351
|
|
|
$this->assertTrue($nestedSet1->appendTo($target)); |
352
|
|
|
$this->assertTrue($this->checkTree()); |
353
|
|
|
$array[]=$nestedSet1; |
354
|
|
|
$nestedSet2=new NestedSet; |
355
|
|
|
$nestedSet2->name='test'; |
356
|
|
|
$this->assertTrue($nestedSet2->appendTo($target)); |
357
|
|
|
$this->assertTrue($this->checkTree()); |
358
|
|
|
$array[]=$nestedSet2; |
359
|
|
|
$this->assertTrue($this->checkArray($array)); |
360
|
|
|
|
361
|
|
|
// many roots |
362
|
|
|
$array=NestedSetWithManyRoots::model()->findAll(); |
363
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(2); |
364
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
365
|
|
|
$nestedSet1=new NestedSetWithManyRoots; |
366
|
|
|
$this->assertFalse($nestedSet1->appendTo($target)); |
367
|
|
|
$nestedSet1->name='test'; |
368
|
|
|
$this->assertTrue($nestedSet1->appendTo($target)); |
369
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
370
|
|
|
$array[]=$nestedSet1; |
371
|
|
|
$nestedSet2=new NestedSetWithManyRoots; |
372
|
|
|
$nestedSet2->name='test'; |
373
|
|
|
$this->assertTrue($nestedSet2->appendTo($target)); |
374
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
375
|
|
|
$array[]=$nestedSet2; |
376
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
View Code Duplication |
public function testInsertBefore() |
|
|
|
|
380
|
|
|
{ |
381
|
|
|
// single root |
382
|
|
|
$array=NestedSet::model()->findAll(); |
383
|
|
|
$target=NestedSet::model()->findByPk(5); |
384
|
|
|
$this->assertTrue($target instanceof NestedSet); |
385
|
|
|
$nestedSet1=new NestedSet; |
386
|
|
|
$this->assertFalse($nestedSet1->insertBefore($target)); |
387
|
|
|
$nestedSet1->name='test'; |
388
|
|
|
$this->assertTrue($nestedSet1->insertBefore($target)); |
389
|
|
|
$this->assertTrue($this->checkTree()); |
390
|
|
|
$array[]=$nestedSet1; |
391
|
|
|
$nestedSet2=new NestedSet; |
392
|
|
|
$nestedSet2->name='test'; |
393
|
|
|
$this->assertTrue($nestedSet2->insertBefore($target)); |
394
|
|
|
$this->assertTrue($this->checkTree()); |
395
|
|
|
$array[]=$nestedSet2; |
396
|
|
|
$this->assertTrue($this->checkArray($array)); |
397
|
|
|
|
398
|
|
|
// many roots |
399
|
|
|
$array=NestedSetWithManyRoots::model()->findAll(); |
400
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(5); |
401
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
402
|
|
|
$nestedSet1=new NestedSetWithManyRoots; |
403
|
|
|
$this->assertFalse($nestedSet1->insertBefore($target)); |
404
|
|
|
$nestedSet1->name='test'; |
405
|
|
|
$this->assertTrue($nestedSet1->insertBefore($target)); |
406
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
407
|
|
|
$array[]=$nestedSet1; |
408
|
|
|
$nestedSet2=new NestedSetWithManyRoots; |
409
|
|
|
$nestedSet2->name='test'; |
410
|
|
|
$this->assertTrue($nestedSet2->insertBefore($target)); |
411
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
412
|
|
|
$array[]=$nestedSet2; |
413
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
View Code Duplication |
public function testInsertAfter() |
|
|
|
|
417
|
|
|
{ |
418
|
|
|
// single root |
419
|
|
|
$array=NestedSet::model()->findAll(); |
420
|
|
|
$target=NestedSet::model()->findByPk(2); |
421
|
|
|
$this->assertTrue($target instanceof NestedSet); |
422
|
|
|
$nestedSet1=new NestedSet; |
423
|
|
|
$this->assertFalse($nestedSet1->insertAfter($target)); |
424
|
|
|
$nestedSet1->name='test'; |
425
|
|
|
$this->assertTrue($nestedSet1->insertAfter($target)); |
426
|
|
|
$this->assertTrue($this->checkTree()); |
427
|
|
|
$array[]=$nestedSet1; |
428
|
|
|
$nestedSet2=new NestedSet; |
429
|
|
|
$nestedSet2->name='test'; |
430
|
|
|
$this->assertTrue($nestedSet2->insertAfter($target)); |
431
|
|
|
$this->assertTrue($this->checkTree()); |
432
|
|
|
$array[]=$nestedSet2; |
433
|
|
|
$this->assertTrue($this->checkArray($array)); |
434
|
|
|
|
435
|
|
|
// many roots |
436
|
|
|
$array=NestedSetWithManyRoots::model()->findAll(); |
437
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(2); |
438
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
439
|
|
|
$nestedSet1=new NestedSetWithManyRoots; |
440
|
|
|
$this->assertFalse($nestedSet1->insertAfter($target)); |
441
|
|
|
$nestedSet1->name='test'; |
442
|
|
|
$this->assertTrue($nestedSet1->insertAfter($target)); |
443
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
444
|
|
|
$array[]=$nestedSet1; |
445
|
|
|
$nestedSet2=new NestedSetWithManyRoots; |
446
|
|
|
$nestedSet2->name='test'; |
447
|
|
|
$this->assertTrue($nestedSet2->insertAfter($target)); |
448
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
449
|
|
|
$array[]=$nestedSet2; |
450
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
View Code Duplication |
public function testMoveBefore() |
|
|
|
|
454
|
|
|
{ |
455
|
|
|
// single root |
456
|
|
|
$array=NestedSet::model()->findAll(); |
457
|
|
|
|
458
|
|
|
$nestedSet=NestedSet::model()->findByPk(6); |
459
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
460
|
|
|
$target=NestedSet::model()->findByPk(2); |
461
|
|
|
$this->assertTrue($target instanceof NestedSet); |
462
|
|
|
$this->assertTrue($nestedSet->moveBefore($target)); |
463
|
|
|
$this->assertTrue($this->checkTree()); |
464
|
|
|
|
465
|
|
|
$this->assertTrue($this->checkArray($array)); |
466
|
|
|
|
467
|
|
|
$nestedSet=NestedSet::model()->findByPk(5); |
468
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
469
|
|
|
$this->assertTrue($nestedSet->moveBefore($target)); |
470
|
|
|
$this->assertTrue($this->checkTree()); |
471
|
|
|
|
472
|
|
|
$this->assertTrue($this->checkArray($array)); |
473
|
|
|
|
474
|
|
|
// many roots |
475
|
|
|
$array=NestedSetWithManyRoots::model()->findAll(); |
476
|
|
|
|
477
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(6); |
478
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
479
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(2); |
480
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
481
|
|
|
$this->assertTrue($nestedSet->moveBefore($target)); |
482
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
483
|
|
|
|
484
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
485
|
|
|
|
486
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(5); |
487
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
488
|
|
|
$this->assertTrue($nestedSet->moveBefore($target)); |
489
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
490
|
|
|
|
491
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
492
|
|
|
|
493
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(6); |
494
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
495
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(9); |
496
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
497
|
|
|
$this->assertTrue($nestedSet->moveBefore($target)); |
498
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
499
|
|
|
|
500
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
501
|
|
|
|
502
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(5); |
503
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
504
|
|
|
$this->assertTrue($nestedSet->moveBefore($target)); |
505
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
506
|
|
|
|
507
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
View Code Duplication |
public function testMoveAfter() |
|
|
|
|
511
|
|
|
{ |
512
|
|
|
// single root |
513
|
|
|
$array=NestedSet::model()->findAll(); |
514
|
|
|
|
515
|
|
|
$nestedSet=NestedSet::model()->findByPk(3); |
516
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
517
|
|
|
$target=NestedSet::model()->findByPk(5); |
518
|
|
|
$this->assertTrue($target instanceof NestedSet); |
519
|
|
|
$this->assertTrue($nestedSet->moveAfter($target)); |
520
|
|
|
$this->assertTrue($this->checkTree()); |
521
|
|
|
|
522
|
|
|
$this->assertTrue($this->checkArray($array)); |
523
|
|
|
|
524
|
|
|
$nestedSet=NestedSet::model()->findByPk(2); |
525
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
526
|
|
|
$this->assertTrue($nestedSet->moveAfter($target)); |
527
|
|
|
$this->assertTrue($this->checkTree()); |
528
|
|
|
|
529
|
|
|
$this->assertTrue($this->checkArray($array)); |
530
|
|
|
|
531
|
|
|
// many roots |
532
|
|
|
$array=NestedSetWithManyRoots::model()->findAll(); |
533
|
|
|
|
534
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(3); |
535
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
536
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(5); |
537
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
538
|
|
|
$this->assertTrue($nestedSet->moveAfter($target)); |
539
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
540
|
|
|
|
541
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
542
|
|
|
|
543
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(2); |
544
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
545
|
|
|
$this->assertTrue($nestedSet->moveAfter($target)); |
546
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
547
|
|
|
|
548
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
549
|
|
|
|
550
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(3); |
551
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
552
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(12); |
553
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
554
|
|
|
$this->assertTrue($nestedSet->moveAfter($target)); |
555
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
556
|
|
|
|
557
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
558
|
|
|
|
559
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(2); |
560
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
561
|
|
|
$this->assertTrue($nestedSet->moveAfter($target)); |
562
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
563
|
|
|
|
564
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
View Code Duplication |
public function testMoveAsFirst() |
|
|
|
|
568
|
|
|
{ |
569
|
|
|
// single root |
570
|
|
|
$array=NestedSet::model()->findAll(); |
571
|
|
|
|
572
|
|
|
$nestedSet=NestedSet::model()->findByPk(6); |
573
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
574
|
|
|
$target=NestedSet::model()->findByPk(2); |
575
|
|
|
$this->assertTrue($target instanceof NestedSet); |
576
|
|
|
$this->assertTrue($nestedSet->moveAsFirst($target)); |
577
|
|
|
$this->assertTrue($this->checkTree()); |
578
|
|
|
|
579
|
|
|
$this->assertTrue($this->checkArray($array)); |
580
|
|
|
|
581
|
|
|
$nestedSet=NestedSet::model()->findByPk(5); |
582
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
583
|
|
|
$this->assertTrue($nestedSet->moveAsFirst($target)); |
584
|
|
|
$this->assertTrue($this->checkTree()); |
585
|
|
|
|
586
|
|
|
$this->assertTrue($this->checkArray($array)); |
587
|
|
|
|
588
|
|
|
// many roots |
589
|
|
|
$array=NestedSetWithManyRoots::model()->findAll(); |
590
|
|
|
|
591
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(6); |
592
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
593
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(2); |
594
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
595
|
|
|
$this->assertTrue($nestedSet->moveAsFirst($target)); |
596
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
597
|
|
|
|
598
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
599
|
|
|
|
600
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(5); |
601
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
602
|
|
|
$this->assertTrue($nestedSet->moveAsFirst($target)); |
603
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
604
|
|
|
|
605
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
606
|
|
|
|
607
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(6); |
608
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
609
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(9); |
610
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
611
|
|
|
$this->assertTrue($nestedSet->moveAsFirst($target)); |
612
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
613
|
|
|
|
614
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
615
|
|
|
|
616
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(5); |
617
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
618
|
|
|
$this->assertTrue($nestedSet->moveAsFirst($target)); |
619
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
620
|
|
|
|
621
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
View Code Duplication |
public function testMoveAsLast() |
|
|
|
|
625
|
|
|
{ |
626
|
|
|
// single root |
627
|
|
|
$array=NestedSet::model()->findAll(); |
628
|
|
|
|
629
|
|
|
$nestedSet=NestedSet::model()->findByPk(3); |
630
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
631
|
|
|
$target=NestedSet::model()->findByPk(5); |
632
|
|
|
$this->assertTrue($target instanceof NestedSet); |
633
|
|
|
$this->assertTrue($nestedSet->moveAsLast($target)); |
634
|
|
|
$this->assertTrue($this->checkTree()); |
635
|
|
|
|
636
|
|
|
$this->assertTrue($this->checkArray($array)); |
637
|
|
|
|
638
|
|
|
$nestedSet=NestedSet::model()->findByPk(2); |
639
|
|
|
$this->assertTrue($nestedSet instanceof NestedSet); |
640
|
|
|
$this->assertTrue($nestedSet->moveAsLast($target)); |
641
|
|
|
$this->assertTrue($this->checkTree()); |
642
|
|
|
|
643
|
|
|
$this->assertTrue($this->checkArray($array)); |
644
|
|
|
|
645
|
|
|
// many roots |
646
|
|
|
$array=NestedSetWithManyRoots::model()->findAll(); |
647
|
|
|
|
648
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(3); |
649
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
650
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(5); |
651
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
652
|
|
|
$this->assertTrue($nestedSet->moveAsLast($target)); |
653
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
654
|
|
|
|
655
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
656
|
|
|
|
657
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(2); |
658
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
659
|
|
|
$this->assertTrue($nestedSet->moveAsLast($target)); |
660
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
661
|
|
|
|
662
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
663
|
|
|
|
664
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(3); |
665
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
666
|
|
|
$target=NestedSetWithManyRoots::model()->findByPk(12); |
667
|
|
|
$this->assertTrue($target instanceof NestedSetWithManyRoots); |
668
|
|
|
$this->assertTrue($nestedSet->moveAsLast($target)); |
669
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
670
|
|
|
|
671
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
672
|
|
|
|
673
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(2); |
674
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
675
|
|
|
$this->assertTrue($nestedSet->moveAsLast($target)); |
676
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
677
|
|
|
|
678
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
public function testMoveAsRoot() |
682
|
|
|
{ |
683
|
|
|
$array=NestedSetWithManyRoots::model()->findAll(); |
684
|
|
|
|
685
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(2); |
686
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
687
|
|
|
$this->assertTrue($nestedSet->moveAsRoot()); |
688
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
689
|
|
|
|
690
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
691
|
|
|
|
692
|
|
|
$nestedSet=NestedSetWithManyRoots::model()->findByPk(10); |
693
|
|
|
$this->assertTrue($nestedSet instanceof NestedSetWithManyRoots); |
694
|
|
|
$this->assertTrue($nestedSet->moveAsRoot()); |
695
|
|
|
$this->assertTrue($this->checkTreeWithManyRoots()); |
696
|
|
|
|
697
|
|
|
$this->assertTrue($this->checkArrayWithManyRoots($array)); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
private function checkTree() |
701
|
|
|
{ |
702
|
|
|
return $this->checkTree1() |
703
|
|
|
&& $this->checkTree2() |
704
|
|
|
&& $this->checkTree3() |
705
|
|
|
&& $this->checkTree4(); |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
private function checkTree1() |
709
|
|
|
{ |
710
|
|
|
return !Yii::app()->db->createCommand('SELECT COUNT(`id`) FROM `NestedSet` WHERE `lft`>=`rgt`;')->queryScalar(); |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
private function checkTree2() |
714
|
|
|
{ |
715
|
|
|
return !Yii::app()->db->createCommand('SELECT COUNT(`id`) FROM `NestedSet` WHERE NOT MOD(`rgt`-`lft`,2);')->queryScalar(); |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
private function checkTree3() |
719
|
|
|
{ |
720
|
|
|
return !Yii::app()->db->createCommand('SELECT COUNT(`id`) FROM `NestedSet` WHERE MOD(`lft`-`level`,2);')->queryScalar(); |
721
|
|
|
} |
722
|
|
|
|
723
|
|
View Code Duplication |
private function checkTree4() |
|
|
|
|
724
|
|
|
{ |
725
|
|
|
$row=Yii::app()->db->createCommand('SELECT MIN(`lft`),MAX(`rgt`),COUNT(`id`) FROM `NestedSet`;')->queryRow(false); |
726
|
|
|
|
727
|
|
|
if($row[0]!=1 || $row[1]!=$row[2]*2) |
728
|
|
|
return false; |
729
|
|
|
|
730
|
|
|
return true; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
private function checkArray($array) |
734
|
|
|
{ |
735
|
|
|
return $this->checkArray1($array) |
736
|
|
|
&& $this->checkArray2($array) |
737
|
|
|
&& $this->checkArray3($array) |
738
|
|
|
&& $this->checkArray4($array); |
739
|
|
|
} |
740
|
|
|
|
741
|
|
View Code Duplication |
private function checkArray1($array) |
|
|
|
|
742
|
|
|
{ |
743
|
|
|
foreach($array as $node) |
744
|
|
|
{ |
745
|
|
|
if(!$node->getIsDeletedRecord() && $node->lft>=$node->rgt) |
746
|
|
|
return false; |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
return true; |
750
|
|
|
} |
751
|
|
|
|
752
|
|
View Code Duplication |
private function checkArray2($array) |
|
|
|
|
753
|
|
|
{ |
754
|
|
|
foreach($array as $node) |
755
|
|
|
{ |
756
|
|
|
if(!$node->getIsDeletedRecord() && !(($node->rgt-$node->lft)%2)) |
757
|
|
|
return false; |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
return true; |
761
|
|
|
} |
762
|
|
|
|
763
|
|
View Code Duplication |
private function checkArray3($array) |
|
|
|
|
764
|
|
|
{ |
765
|
|
|
foreach($array as $node) |
766
|
|
|
{ |
767
|
|
|
if(!$node->getIsDeletedRecord() && ($node->lft-$node->level)%2) |
768
|
|
|
return false; |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
return true; |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
private function checkArray4($array) |
775
|
|
|
{ |
776
|
|
|
$count=0; |
777
|
|
|
|
778
|
|
|
foreach($array as $node) |
779
|
|
|
{ |
780
|
|
|
if($node->getIsDeletedRecord()) |
781
|
|
|
continue; |
782
|
|
|
else |
783
|
|
|
$count++; |
784
|
|
|
|
785
|
|
|
if(!isset($min) || $min>$node->lft) |
786
|
|
|
$min=$node->lft; |
787
|
|
|
|
788
|
|
|
if(!isset($max) || $max<$node->rgt) |
789
|
|
|
$max=$node->rgt; |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
if(!$count) |
793
|
|
|
return true; |
794
|
|
|
|
795
|
|
|
if($min!=1 || $max!=$count*2) |
|
|
|
|
796
|
|
|
return false; |
797
|
|
|
|
798
|
|
|
return true; |
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
private function checkTreeWithManyRoots() |
802
|
|
|
{ |
803
|
|
|
return $this->checkTreeWithManyRoots1() |
804
|
|
|
&& $this->checkTreeWithManyRoots2() |
805
|
|
|
&& $this->checkTreeWithManyRoots3() |
806
|
|
|
&& $this->checkTreeWithManyRoots4(); |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
private function checkTreeWithManyRoots1() |
810
|
|
|
{ |
811
|
|
|
return !Yii::app()->db->createCommand('SELECT COUNT(`id`) FROM `NestedSetWithManyRoots` WHERE `lft`>=`rgt` GROUP BY `root`;')->query()->getRowCount(); |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
private function checkTreeWithManyRoots2() |
815
|
|
|
{ |
816
|
|
|
return !Yii::app()->db->createCommand('SELECT COUNT(`id`) FROM `NestedSetWithManyRoots` WHERE NOT MOD(`rgt`-`lft`,2) GROUP BY `root`;')->query()->getRowCount(); |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
private function checkTreeWithManyRoots3() |
820
|
|
|
{ |
821
|
|
|
return !Yii::app()->db->createCommand('SELECT COUNT(`id`) FROM `NestedSetWithManyRoots` WHERE MOD(`lft`-`level`,2) GROUP BY `root`;')->query()->getRowCount(); |
822
|
|
|
} |
823
|
|
|
|
824
|
|
View Code Duplication |
private function checkTreeWithManyRoots4() |
|
|
|
|
825
|
|
|
{ |
826
|
|
|
$rows=Yii::app()->db->createCommand('SELECT MIN(`lft`),MAX(`rgt`),COUNT(`id`) FROM `NestedSetWithManyRoots` GROUP BY `root`;')->queryAll(false); |
827
|
|
|
|
828
|
|
|
foreach($rows as $row) |
829
|
|
|
{ |
830
|
|
|
if($row[0]!=1 || $row[1]!=$row[2]*2) |
831
|
|
|
return false; |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
return true; |
835
|
|
|
} |
836
|
|
|
|
837
|
|
|
private function checkArrayWithManyRoots($array) |
838
|
|
|
{ |
839
|
|
|
return $this->checkArrayWithManyRoots1($array) |
840
|
|
|
&& $this->checkArrayWithManyRoots2($array) |
841
|
|
|
&& $this->checkArrayWithManyRoots3($array) |
842
|
|
|
&& $this->checkArrayWithManyRoots4($array); |
843
|
|
|
} |
844
|
|
|
|
845
|
|
View Code Duplication |
private function checkArrayWithManyRoots1($array) |
|
|
|
|
846
|
|
|
{ |
847
|
|
|
foreach($array as $node) |
848
|
|
|
{ |
849
|
|
|
if(!$node->getIsDeletedRecord() && $node->lft>=$node->rgt) |
850
|
|
|
return false; |
851
|
|
|
} |
852
|
|
|
|
853
|
|
|
return true; |
854
|
|
|
} |
855
|
|
|
|
856
|
|
View Code Duplication |
private function checkArrayWithManyRoots2($array) |
|
|
|
|
857
|
|
|
{ |
858
|
|
|
foreach($array as $node) |
859
|
|
|
{ |
860
|
|
|
if(!$node->getIsDeletedRecord() && !(($node->rgt-$node->lft)%2)) |
861
|
|
|
return false; |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
return true; |
865
|
|
|
} |
866
|
|
|
|
867
|
|
View Code Duplication |
private function checkArrayWithManyRoots3($array) |
|
|
|
|
868
|
|
|
{ |
869
|
|
|
foreach($array as $node) |
870
|
|
|
{ |
871
|
|
|
if(!$node->getIsDeletedRecord() && ($node->lft-$node->level)%2) |
872
|
|
|
return false; |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
return true; |
876
|
|
|
} |
877
|
|
|
|
878
|
|
|
private function checkArrayWithManyRoots4($array) |
879
|
|
|
{ |
880
|
|
|
$min=array(); |
881
|
|
|
$max=array(); |
882
|
|
|
$count=array(); |
883
|
|
|
|
884
|
|
|
foreach($array as $n=>$node) |
885
|
|
|
{ |
886
|
|
|
if($node->getIsDeletedRecord()) |
887
|
|
|
continue; |
888
|
|
|
else if(isset($count[$node->root])) |
889
|
|
|
$count[$node->root]++; |
890
|
|
|
else |
891
|
|
|
$count[$node->root]=1; |
892
|
|
|
|
893
|
|
View Code Duplication |
if(!isset($min[$node->root]) || $min[$node->root]>$node->lft) |
|
|
|
|
894
|
|
|
$min[$node->root]=$node->lft; |
895
|
|
|
|
896
|
|
View Code Duplication |
if(!isset($max[$node->root]) || $max[$node->root]<$node->rgt) |
|
|
|
|
897
|
|
|
$max[$node->root]=$node->rgt; |
898
|
|
|
} |
899
|
|
|
|
900
|
|
|
foreach($count as $root=>$c) |
901
|
|
|
{ |
902
|
|
|
if($min[$root]!=1 || $max[$root]!=$c*2) |
903
|
|
|
return false; |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
return true; |
907
|
|
|
} |
908
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.