Move to another parent CategoryItem
public moveTo ( |
||
$item | item object | |
$parent | new parent item object | |
return |
public function moveItem(CategoryHandler $handler, $categoryId) { $id = Input::get('id'); $parentId = Input::get('parentId'); $ordering = Input::get('ordering'); if ($id === null || !($item = $handler->getItem($id))) { throw new InvalidArgumentHttpException(); } $parent = empty($parentId) === false ? $handler->getItem($parentId) : null; DB::beginTransaction(); try { $handler->moveTo($item, $parent); $handler->setOrder($item, $ordering); } catch (Exception $e) { DB::rollBack(); throw $e; } DB::commit(); }
public function testMoveTo() { list($repo, $itemRepo) = $this->getMocks(); $instance = new CategoryHandler($repo, $itemRepo); $mockItemEntity = m::mock('Xpressengine\\Category\\CategoryItemEntity'); $mockParent = m::mock('Xpressengine\\Category\\CategoryItemEntity'); $mockParent->id = 1; $mockOldParent = m::mock('Xpressengine\\Category\\CategoryItemEntity'); $mockOldParent->id = 2; $itemRepo->shouldReceive('fetchAsc')->once()->with($mockItemEntity, 1)->andReturn([$mockOldParent]); $itemRepo->shouldReceive('unlinkHierarchy')->once()->with($mockItemEntity, $mockOldParent)->andReturnNull(); $itemRepo->shouldReceive('linkHierarchy')->once()->with($mockItemEntity, $mockParent)->andReturnNull(); $instance->moveTo($mockItemEntity, $mockParent); }
public function testMoveToThorwsExceptionWhenGivenParentIsSelf() { $instance = new CategoryHandler(); $mockItem = m::mock('Xpressengine\\Category\\Models\\CategoryItem'); $mockItem->shouldReceive('getKey')->andReturn(1); $mockParent = m::mock('Xpressengine\\Category\\Models\\CategoryItem'); $mockParent->shouldReceive('getKey')->andReturn(1); $mockItem->shouldReceive('getParent')->andReturnNull(); try { $instance->moveTo($mockItem, $mockParent); $this->assertTrue(false); } catch (\Exception $e) { $this->assertInstanceOf('Xpressengine\\Category\\Exceptions\\UnableMoveToSelfException', $e); } }