protected function getObjectsForAction(Request $request)
 {
     $doctrine = $this->getDoctrine()->getManager();
     $sessionRepo = $doctrine->getRepository('QuestionKeyBundle:VisitorSession');
     $sessionRanTreeRepo = $doctrine->getRepository('QuestionKeyBundle:VisitorSessionRanTreeVersion');
     $treeRepo = $doctrine->getRepository('QuestionKeyBundle:Tree');
     $treeVersionRepo = $doctrine->getRepository('QuestionKeyBundle:TreeVersion');
     $nodeRepo = $doctrine->getRepository('QuestionKeyBundle:Node');
     $nodeOptionRepo = $doctrine->getRepository('QuestionKeyBundle:NodeOption');
     $isIPInIPConfig = new IsIPInIPConfig($this->container->hasParameter('internal_ips') ? $this->container->getParameter('internal_ips') : '');
     // Session!
     if ($request->query->get("session_id")) {
         $this->session = $sessionRepo->findOneByPublicId($request->query->get("session_id"));
     }
     if (!$this->session) {
         $this->session = new VisitorSession();
         $this->session->setIsInternalIP($isIPInIPConfig->checkIP($request->getClientIp()));
         $doctrine->persist($this->session);
         $doctrine->flush();
         // TODO check for collisions of public ID
     }
     // Tree!
     if ($request->query->get("ran_tree_version_id")) {
         $this->sessionRanTreeVersion = $sessionRanTreeRepo->findOneByPublicId($request->query->get("ran_tree_version_id"));
         // include session to
     } else {
         if ($request->query->get("tree_id")) {
             $this->tree = $treeRepo->findOneByPublicId($request->query->get("tree_id"));
             if ($this->tree) {
                 if ($request->query->get("tree_version_id")) {
                     $this->treeVersion = $treeVersionRepo->findOneBy(array('publicId' => $request->query->get("tree_version_id"), 'tree' => $this->tree));
                     // TODO was this version ever published?
                 } else {
                     $this->treeVersion = $treeVersionRepo->findPublishedVersionForTree($this->tree);
                 }
             }
         }
     }
     if ($this->sessionRanTreeVersion && !$this->treeVersion) {
         $this->treeVersion = $this->sessionRanTreeVersion->getTreeVersion();
         $this->tree = $this->treeVersion->getTree();
     } else {
         if ($this->treeVersion && !$this->sessionRanTreeVersion) {
             $this->sessionRanTreeVersion = new VisitorSessionRanTreeVersion();
             $this->sessionRanTreeVersion->setTreeVersion($this->treeVersion);
             $this->sessionRanTreeVersion->setVisitorSession($this->session);
             $this->sessionRanTreeVersion->setIsInternalIP($isIPInIPConfig->checkIP($request->getClientIp()));
             $doctrine->persist($this->sessionRanTreeVersion);
             $doctrine->flush();
             // TODO check for collisions of public ID
         }
     }
     // Node!
     if ($this->treeVersion && $this->sessionRanTreeVersion && $request->query->get("node_id")) {
         $this->node = $request->query->get("node_id") ? $nodeRepo->findOneBy(array('treeVersion' => $this->treeVersion, 'publicId' => $request->query->get("node_id"))) : null;
         if ($this->node) {
             $this->nodeOption = $request->query->get("node_option_id") ? $nodeOptionRepo->findOneBy(array('treeVersion' => $this->treeVersion, 'publicId' => $request->query->get("node_option_id"))) : null;
             $sessionOnNode = new VisitorSessionOnNode();
             $sessionOnNode->setNode($this->node);
             $sessionOnNode->setNodeOption($this->nodeOption);
             $sessionOnNode->setSessionRanTreeVersion($this->sessionRanTreeVersion);
             $sessionOnNode->setGoneBackTo(filter_var($request->query->get("gone_back_to"), FILTER_VALIDATE_BOOLEAN));
             $doctrine->persist($sessionOnNode);
             $doctrine->flush();
         }
     }
     // Out!
     $out = array('session' => array('id' => $this->session->getPublicId()), 'session_ran_tree' => null);
     if ($this->sessionRanTreeVersion) {
         $out['session_ran_tree_version'] = array('id' => $this->sessionRanTreeVersion->getPublicId());
     }
     return $out;
 }
 function testNextNodeInExistingRun()
 {
     $user = new User();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("ouhosu");
     $this->em->persist($user);
     $tree = new Tree();
     $tree->setTitleAdmin('Tree');
     $tree->setOwner($user);
     $tree->setPublicId('tree');
     $this->em->persist($tree);
     $treeVersion = new TreeVersion();
     $treeVersion->setTree($tree);
     $treeVersion->setPublicId('version');
     $this->em->persist($treeVersion);
     $startNode = new Node();
     $startNode->setTreeVersion($treeVersion);
     $startNode->setPublicId('start');
     $this->em->persist($startNode);
     $endNode = new Node();
     $endNode->setTreeVersion($treeVersion);
     $endNode->setPublicId('end');
     $this->em->persist($endNode);
     $nodeOption1 = new NodeOption();
     $nodeOption1->setTreeVersion($treeVersion);
     $nodeOption1->setNode($startNode);
     $nodeOption1->setDestinationNode($endNode);
     $nodeOption1->setPublicId('no1');
     $this->em->persist($nodeOption1);
     $this->em->flush();
     $tvsn = new TreeVersionStartingNode();
     $tvsn->setNode($startNode);
     $tvsn->setTreeVersion($treeVersion);
     $this->em->persist($tvsn);
     $tp = new TreeVersionPublished();
     $tp->setTreeVersion($treeVersion);
     $tp->setPublishedBy($user);
     $tp->setPublishedAt(new \DateTime());
     $this->em->persist($tp);
     $session = new VisitorSession();
     $session->setPublicId('vspid');
     $this->em->persist($session);
     $visitorSessionRanTreeVersion = new VisitorSessionRanTreeVersion();
     $visitorSessionRanTreeVersion->setVisitorSession($session);
     $visitorSessionRanTreeVersion->setPublicId('vsrtvpid');
     $visitorSessionRanTreeVersion->setTreeVersion($treeVersion);
     $this->em->persist($visitorSessionRanTreeVersion);
     $visitorSessionOnNode = new VisitorSessionOnNode();
     $visitorSessionOnNode->setNode($startNode);
     $visitorSessionOnNode->setSessionRanTreeVersion($visitorSessionRanTreeVersion);
     $this->em->persist($visitorSessionOnNode);
     $this->em->flush();
     // Make Request
     $client = static::createClient();
     $crawler = $client->request('GET', '/api/v1/visitorsession/action.json?session_id=vspid&ran_tree_version_id=vsrtvpid&tree_id=tree&tree_version_id=version&node_id=end&node_option_id=no1');
     $response = $client->getResponse()->getContent();
     $responseData = json_decode($response);
     // Check Session
     $sessionLoaded = $this->em->getRepository('QuestionKeyBundle:VisitorSession')->findOneByPublicId($responseData->session->id);
     $this->assertNotNull($sessionLoaded);
     $this->assertEquals($responseData->session->id, $sessionLoaded->getPublicId());
     // test session we loaded is same as session we passed it get param
     $this->assertEquals($session->getId(), $sessionLoaded->getId());
     // Check Session Ran Tree
     $sessionRanTreeLoaded = $this->em->getRepository('QuestionKeyBundle:VisitorSessionRanTreeVersion')->findOneByPublicId($responseData->session_ran_tree_version->id);
     $this->assertNotNull($sessionRanTreeLoaded);
     $this->assertEquals($responseData->session_ran_tree_version->id, $sessionRanTreeLoaded->getPublicId());
     $this->assertEquals($sessionLoaded->getId(), $sessionRanTreeLoaded->getVisitorSession()->getId());
     $this->assertEquals($treeVersion->getId(), $sessionRanTreeLoaded->getTreeVersion()->getId());
     // test s-r-t we loaded is same as s-r-t we passed it get param
     $this->assertEquals($visitorSessionRanTreeVersion->getId(), $sessionRanTreeLoaded->getId());
     // Check Node Stored.
     $sessionOnNodesLoaded = $this->em->getRepository('QuestionKeyBundle:VisitorSessionOnNode')->findBy(array('sessionRanTreeVersion' => $sessionRanTreeLoaded));
     $this->assertEquals(2, count($sessionOnNodesLoaded));
     $sessionOnNodeLoaded = array_shift($sessionOnNodesLoaded);
     $this->assertEquals($startNode->getId(), $sessionOnNodeLoaded->getNode()->getId());
     $this->assertFalse($sessionOnNodeLoaded->getGoneBackTo());
     $this->assertNull($sessionOnNodeLoaded->getNodeOption());
     $sessionOnNodeLoaded = array_shift($sessionOnNodesLoaded);
     $this->assertEquals($endNode->getId(), $sessionOnNodeLoaded->getNode()->getId());
     $this->assertFalse($sessionOnNodeLoaded->getGoneBackTo());
     $this->assertNotNull($sessionOnNodeLoaded->getNodeOption());
     $this->assertEquals($nodeOption1->getId(), $sessionOnNodeLoaded->getNodeOption()->getId());
 }