Skip to content

Commit

Permalink
Merge branch 'ignaciogc-feature/sorted-hierarchy-tree' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepat committed Jul 3, 2014
2 parents f2c0fcd + f606e67 commit 6fbae57
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/Baum/Extensions/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class Collection extends BaseCollection {
public function toHierarchy() {
$dict = $this->getDictionary();

// Enforce sorting by $orderColumn setting in Baum\Node instance
uasort($dict, function($a, $b){
return ($a->getOrder() >= $b->getOrder()) ? 1 : -1;
});

return new BaseCollection($this->hierarchical($dict));
}

Expand Down
8 changes: 8 additions & 0 deletions tests/models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class OrderedCategory extends Category {

}

class OrderedScopedCategory extends Category {

protected $scoped = array('company_id');

protected $orderColumn = 'name';

}

class SoftCategory extends Category {

use SoftDeletingTrait;
Expand Down
31 changes: 31 additions & 0 deletions tests/seeders/CategorySeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,34 @@ public function run() {
}

}

class OrderedScopedCategorySeeder {

public function run() {
DB::table('categories')->delete();

OrderedScopedCategory::unguard();

OrderedScopedCategory::create(array('id' => 1 , 'company_id' => 1, 'name' => 'Root 1' , 'lft' => 1 , 'rgt' => 10 , 'depth' => 0));
OrderedScopedCategory::create(array('id' => 2 , 'company_id' => 1, 'name' => 'Child 3' , 'lft' => 8 , 'rgt' => 9 , 'depth' => 1, 'parent_id' => 1));
OrderedScopedCategory::create(array('id' => 3 , 'company_id' => 1, 'name' => 'Child 2' , 'lft' => 4 , 'rgt' => 7 , 'depth' => 1, 'parent_id' => 1));
OrderedScopedCategory::create(array('id' => 4 , 'company_id' => 1, 'name' => 'Child 2.1', 'lft' => 5 , 'rgt' => 6 , 'depth' => 2, 'parent_id' => 3));
OrderedScopedCategory::create(array('id' => 5 , 'company_id' => 1, 'name' => 'Child 1' , 'lft' => 2 , 'rgt' => 3 , 'depth' => 1, 'parent_id' => 1));
OrderedScopedCategory::create(array('id' => 6 , 'company_id' => 2, 'name' => 'Root 2' , 'lft' => 1 , 'rgt' => 10 , 'depth' => 0));
OrderedScopedCategory::create(array('id' => 7 , 'company_id' => 2, 'name' => 'Child 4' , 'lft' => 2 , 'rgt' => 3 , 'depth' => 1, 'parent_id' => 6));
OrderedScopedCategory::create(array('id' => 8 , 'company_id' => 2, 'name' => 'Child 5' , 'lft' => 4 , 'rgt' => 7 , 'depth' => 1, 'parent_id' => 6));
OrderedScopedCategory::create(array('id' => 9 , 'company_id' => 2, 'name' => 'Child 5.1', 'lft' => 5 , 'rgt' => 6 , 'depth' => 2, 'parent_id' => 8));
OrderedScopedCategory::create(array('id' => 10, 'company_id' => 2, 'name' => 'Child 6' , 'lft' => 8 , 'rgt' => 9 , 'depth' => 1, 'parent_id' => 6));

OrderedScopedCategory::reguard();

if ( DB::connection()->getDriverName() === 'pgsql' ) {
$tablePrefix = DB::connection()->getTablePrefix();

$sequenceName = $tablePrefix . 'categories_id_seq';

DB::connection()->statement('ALTER SEQUENCE ' . $sequenceName . ' RESTART WITH 11');
}
}

}
18 changes: 9 additions & 9 deletions tests/suite/Category/CategoryHierarchyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,19 +588,19 @@ public function testToHierarchyNestsCorrectly() {

// Perform assertions
$wholeTree = hmap(Category::all()->toHierarchy()->toArray());
$this->assertEquals($expectedWholeTree, $wholeTree);
$this->assertArraysAreEqual($expectedWholeTree, $wholeTree);

$subtreeA = hmap($this->categories('A')->getDescendantsAndSelf()->toHierarchy()->toArray());
$this->assertEquals($expectedSubtreeA, $subtreeA);
$this->assertArraysAreEqual($expectedSubtreeA, $subtreeA);

$subtreeB = hmap($this->categories('B')->getDescendantsAndSelf()->toHierarchy()->toArray());
$this->assertEquals($expectedSubtreeB, $subtreeB);
$this->assertArraysAreEqual($expectedSubtreeB, $subtreeB);

$subtreeC = hmap($this->categories('C')->getDescendants()->toHierarchy()->toArray());
$this->assertEquals($expectedSubtreeC, $subtreeC);
$this->assertArraysAreEqual($expectedSubtreeC, $subtreeC);

$subtreeD = hmap($this->categories('D')->getDescendantsAndSelf()->toHierarchy()->toArray());
$this->assertEquals($expectedSubtreeD, $subtreeD);
$this->assertArraysAreEqual($expectedSubtreeD, $subtreeD);

$this->assertTrue($this->categories('D')->getDescendants()->toHierarchy()->isEmpty());
}
Expand All @@ -622,7 +622,7 @@ public function testToHierarchyNestsCorrectlyNotSequential() {
);

$parent->reload();
$this->assertEquals($expected, hmap($parent->getDescendantsAndSelf()->toHierarchy()->toArray()));
$this->assertArraysAreEqual($expected, hmap($parent->getDescendantsAndSelf()->toHierarchy()->toArray()));
}

public function testToHierarchyNestsCorrectlyWithOrder() {
Expand All @@ -637,7 +637,7 @@ public function testToHierarchyNestsCorrectlyWithOrder() {
)
);

$this->assertEquals($expectedWhole, hmap(OrderedCategory::all()->toHierarchy()->toArray()));
$this->assertArraysAreEqual($expectedWhole, hmap(OrderedCategory::all()->toHierarchy()->toArray()));

$expectedSubtreeZ = array(
'Root Z' => array(
Expand All @@ -646,7 +646,7 @@ public function testToHierarchyNestsCorrectlyWithOrder() {
'Child G' => array( 'Child G.1' => null )
)
);
$this->assertEquals($expectedSubtreeZ, hmap($this->categories('Root Z', 'OrderedCategory')->getDescendantsAndSelf()->toHierarchy()->toArray()));
$this->assertArraysAreEqual($expectedSubtreeZ, hmap($this->categories('Root Z', 'OrderedCategory')->getDescendantsAndSelf()->toHierarchy()->toArray()));
}

public function testGetNestedList() {
Expand All @@ -662,7 +662,7 @@ public function testGetNestedList() {
6 => str_repeat($seperator, 0). 'Root 2',
);

$this->assertEquals($expected, $nestedList);
$this->assertArraysAreEqual($expected, $nestedList);
}

}
27 changes: 27 additions & 0 deletions tests/suite/Category/CategoryScopedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,33 @@ public function testFullSubtreeMovementsMultiple() {
$this->assertEquals($expected, $root2->getDescendants()->all());
}

public function testToHierarchyNestsCorrectlyWithScopedOrder() {
with(new OrderedScopedCategorySeeder)->run();

$expectedWhole1 = array(
'Root 1' => array(
'Child 1' => null,
'Child 2' => array (
'Child 2.1' => null
),
'Child 3' => null
)
);

$expectedWhole2 = array(
'Root 2' => array(
'Child 4' => null,
'Child 5' => array (
'Child 5.1' => null
),
'Child 6' => null
)
);

$this->assertArraysAreEqual($expectedWhole1, hmap(OrderedScopedCategory::where('company_id', 1)->get()->toHierarchy()->toArray()));
$this->assertArraysAreEqual($expectedWhole2, hmap(OrderedScopedCategory::where('company_id', 2)->get()->toHierarchy()->toArray()));
}

/**
* @expectedException Baum\MoveNotPossibleException
*/
Expand Down
19 changes: 10 additions & 9 deletions tests/suite/Cluster/ClusterHierarchyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,19 +592,19 @@ public function testToHierarchyNestsCorrectly() {

// Perform assertions
$wholeTree = hmap(Cluster::all()->toHierarchy()->toArray());
$this->assertEquals($expectedWholeTree, $wholeTree);
$this->assertArraysAreEqual($expectedWholeTree, $wholeTree);

$subtreeA = hmap($this->clusters('A')->getDescendantsAndSelf()->toHierarchy()->toArray());
$this->assertEquals($expectedSubtreeA, $subtreeA);
$this->assertArraysAreEqual($expectedSubtreeA, $subtreeA);

$subtreeB = hmap($this->clusters('B')->getDescendantsAndSelf()->toHierarchy()->toArray());
$this->assertEquals($expectedSubtreeB, $subtreeB);
$this->assertArraysAreEqual($expectedSubtreeB, $subtreeB);

$subtreeC = hmap($this->clusters('C')->getDescendants()->toHierarchy()->toArray());
$this->assertEquals($expectedSubtreeC, $subtreeC);
$this->assertArraysAreEqual($expectedSubtreeC, $subtreeC);

$subtreeD = hmap($this->clusters('D')->getDescendantsAndSelf()->toHierarchy()->toArray());
$this->assertEquals($expectedSubtreeD, $subtreeD);
$this->assertArraysAreEqual($expectedSubtreeD, $subtreeD);

$this->assertTrue($this->clusters('D')->getDescendants()->toHierarchy()->isEmpty());
}
Expand All @@ -626,7 +626,7 @@ public function testToHierarchyNestsCorrectlyNotSequential() {
);

$parent->reload();
$this->assertEquals($expected, hmap($parent->getDescendantsAndSelf()->toHierarchy()->toArray()));
$this->assertArraysAreEqual($expected, hmap($parent->getDescendantsAndSelf()->toHierarchy()->toArray()));
}

public function testToHierarchyNestsCorrectlyWithOrder() {
Expand All @@ -640,7 +640,7 @@ public function testToHierarchyNestsCorrectlyWithOrder() {
'Child G' => array( 'Child G.1' => null )
)
);
$this->assertEquals($expectedWhole, hmap(OrderedCluster::all()->toHierarchy()->toArray()));
$this->assertArraysAreEqual($expectedWhole, hmap(OrderedCluster::all()->toHierarchy()->toArray()));

$expectedSubtreeZ = array(
'Root Z' => array(
Expand All @@ -649,8 +649,9 @@ public function testToHierarchyNestsCorrectlyWithOrder() {
'Child G' => array( 'Child G.1' => null )
)
);
$this->assertEquals($expectedSubtreeZ, hmap($this->clusters('Root Z', 'OrderedCluster')->getDescendantsAndSelf()->toHierarchy()->toArray()));
$this->assertArraysAreEqual($expectedSubtreeZ, hmap($this->clusters('Root Z', 'OrderedCluster')->getDescendantsAndSelf()->toHierarchy()->toArray()));
}

public function testGetNestedList() {
$seperator = ' ';
$nestedList = Cluster::getNestedList('name', 'id', $seperator);
Expand All @@ -664,7 +665,7 @@ public function testGetNestedList() {
'3bb62314-9e1e-49c6-a5cb-17a9ab9b1b9a' => str_repeat($seperator, 0). 'Root 2',
);

$this->assertEquals($expected, $nestedList);
$this->assertArraysAreEqual($expected, $nestedList);
}

}

0 comments on commit 6fbae57

Please sign in to comment.