public function testCreate() { DB::statement("SET foreign_key_checks=0"); ClosureTable::truncate(); Entity::truncate(); DB::statement("SET foreign_key_checks=1"); $entity1 = new Entity(); $entity1->save(); $this->assertEquals(0, $entity1->position); $entity2 = new Entity(); $entity2->save(); $this->assertEquals(1, $entity2->position); }
public function testToTree() { $rootEntity = new Entity(); $rootEntity->save(); $childEntity = with(new Entity())->moveTo(0, $rootEntity); $grandEntity = with(new Entity())->moveTo(0, $childEntity); $childrenRelationIndex = $rootEntity->getChildrenRelationIndex(); $tree = with(new Collection([$rootEntity, $childEntity, $grandEntity]))->toTree(); $rootItem = $tree->get(0); $this->assertArrayHasKey($childrenRelationIndex, $rootItem->getRelations()); $children = $rootItem->getRelation($childrenRelationIndex); $this->assertCount(1, $children); $childItem = $children->get(0); $this->assertEquals($childEntity->getKey(), $childItem->getKey()); $this->assertArrayHasKey($childrenRelationIndex, $childItem->getRelations()); $grandItems = $childItem->getRelation($childrenRelationIndex); $this->assertCount(1, $grandItems); $grandItem = $grandItems->get(0); $this->assertEquals($grandEntity->getKey(), $grandItem->getKey()); $this->assertArrayNotHasKey($childrenRelationIndex, $grandItem->getRelations()); }