public function testSerialization() { $s = new SurveyState(time() - 500); $serialzedValue = serialize($s); /** * @vars $state SurveyState */ $state = unserialize($serialzedValue); $this->assertTrue($state->isState(SurveyExecutionState::Unknown())); }
/** * @param int $timeout_min * @return bool */ public function isExpired($timeout_min) { if (!is_long($this->startTime) || !$this->isState(SurveyExecutionState::InProgress())) { return true; } $diff = time() - $this->startTime; if ($diff > $timeout_min) { return true; } return false; }
function testInvalidAnswer() { $manager = $this->newSurveyService(); $max = 5; $q = "Rate us 1 to {$max}"; $thankYou = "Thank you for filling out the survey. Visit us at farooqmasjid.org"; $s = new Survey(); $s->setSurveyName("my name is john"); $s->setDescription("a survey"); $s->setThankYouMessage($thankYou); $sq = new SurveyQuestion(); $sq->setQuestion($q); $sq->setType(QuestionType::StarRating()); $s->addQuestion($sq); $this->surveyManager->createSurvey($s); SurveyEntityManager::testClear(); $this->request[SurveyRequestService::BODY_KEY] = $s->getSurveyName(); $serviced = $manager->service(); $res = $manager->getResponse(); $this->assertTrue($serviced); $this->assertNotNull($res); $this->assertTrue(count($res->getContent()) > 0); $this->assertTrue(strstr($res->getContent(), $q) !== false); $this->request[SurveyRequestService::BODY_KEY] = "15"; $serviced = $manager->service(); $res = $manager->getResponse(); $this->assertTrue($serviced); $this->assertNotNull($res); $this->assertTrue(count($res->getContent()) > 0); $this->assertTrue(strstr($res->getContent(), "Sorry, we couldn't understand your response. Your answer must be between 1 and 5") !== false); $this->request[SurveyRequestService::BODY_KEY] = "0"; $serviced = $manager->service(); $res = $manager->getResponse(); $this->assertTrue($serviced); $this->assertNotNull($res); $this->assertTrue(count($res->getContent()) > 0); $this->assertTrue(strstr($res->getContent(), "Sorry, we couldn't understand your response") !== false); //$session = $this->session; for ($i = 1; $i <= $max; $i++) { //reset back to first question every time. $this->getSurveyState()->setQuestionIndex(0); $this->getSurveyState()->setSurveyExecutionState(SurveyExecutionState::InProgress()); $this->request[SurveyRequestService::BODY_KEY] = $i; $serviced = $manager->service(); $res = $manager->getResponse(); $this->assertTrue($serviced, "Testing on i = {$i}"); $this->assertNotNull($res); $this->assertTrue(count($res->getContent()) > 0); $this->assertTrue(strstr($res->getContent(), $thankYou) !== false); } }
/** * Public entry point to service * @return boolean */ public function service() { if (!$this->shouldService()) { $this->response("Sorry, could not find a survey by that name."); return false; } $qindex = $this->surveyState->getQuestionIndex(); //No qindex exists, or we are at the first question. if (is_null($qindex)) { $qindex = 0; $this->surveyState->setQuestionIndex($qindex); //return first question $question = $this->surveyState->getQuestion(); $this->response($question); return true; } $question = $this->surveyState->getQuestionObj(); $validator = $this->answerValidatorProvider->getValidator($question->getType()); $answer = $this->getRequestBody(); if ($validator->isValid($answer)) { //move to next question $qindex++; //persist our question index state $this->surveyState->setQuestionIndex($qindex); if (!$this->surveyState->hasId()) { $from = session_id(); if (array_key_exists('From', $this->request)) { $from = $this->request['From']; } $this->surveyState->setId($from); } $answer = $validator->normalize($answer); $this->persistAnswer($this->surveyState->getId(), $question, $answer); } else { $helperText = $validator->getHelperText(); $response = "Sorry, we couldn't understand your response. {$helperText}"; $this->response($response); return true; } $next_question = $this->surveyState->getQuestion($qindex); if (is_null($next_question)) { //no more questions. Send thank you message. $this->surveyState->setSurveyExecutionState(SurveyExecutionState::Completed()); $next_question = $this->surveyState->getSurvey()->getThankYouMessage(); } $this->response($next_question); return true; }