Skip to content

Commit

Permalink
Add Viewpoint::removeActionsWithSceneId for easier writing of scenes …
Browse files Browse the repository at this point in the history
…without default nav items
  • Loading branch information
austenmc committed Dec 31, 2016
1 parent 340d6d1 commit e041db8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Models/Viewpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,22 @@ public function findActionById(string $id)
}
return null;
}

/**
* Removes any actions that correspond to a given scene ID, if present.
* @param int $id
*/
public function removeActionsWithSceneId(int $id)
{
foreach ($this->getActionGroups() as $group) {
$actions = $group->getActions();
foreach ($actions as $key => $a) {
if ($a->getDestinationSceneId() == $id) {
unset($actions[$key]);
}
}
$actions = array_values($actions);
$group->setActions($actions);
}
}
}
50 changes: 50 additions & 0 deletions tests/Models/ViewpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,54 @@ public function testAttachments()
$this->assertEquals('baz', $output->getAttachments()[0]->getFoo());
$this->assertEquals('fiz', $output->getAttachments()[1]->getFoo());
}

public function testRemoveActionsWithSceneId()
{
$em = $this->getEntityManager();

$a1 = new Action(1);
$a2 = new Action(2);
$a3 = new Action(3);

$ag1 = new ActionGroup('id1', 'title1', 42);
$ag1->setActions([
$a1,
$a2,
$a3
]);
$ag2 = new ActionGroup('id2', 'title2', 101);
$ag2->setActions([
new Action(4)
]);

$actionGroups = [
$ag1,
$ag2
];

$input = $em->getRepository(Viewpoint::class)->find(2);
$input->setActionGroups($actionGroups);
$input->save($em);

$em->clear();

$output = $em->getRepository(Viewpoint::class)->find(2);

// Not finding the scene ID should change nothing.
$output->removeActionsWithSceneId(1000);
$this->assertEquals($actionGroups, $output->getActionGroups());

$ag1_output = new ActionGroup('id1', 'title1', 42);
$ag1_output->setActions([
$a1,
$a3
]);

$actionGroupsWithout2 = [
$ag1_output,
$ag2
];
$output->removeActionsWithSceneId(2);
$this->assertEquals($actionGroupsWithout2, $output->getActionGroups());
}
}

0 comments on commit e041db8

Please sign in to comment.