<?php
namespace Tests\TestHubBundle\Service;
use TestHubBundle\Entity\Attempt;
use TestHubBundle\Entity\DecimalAnswer;
use TestHubBundle\Entity\QuestionWithDecimalAnswer;
use TestHubBundle\Entity\QuestionWithMultipleCorrectAnswers;
use TestHubBundle\Entity\QuestionWithSingleCorrectAnswer;
use TestHubBundle\Entity\QuestionWithTextAnswer;
use TestHubBundle\Entity\Test;
use TestHubBundle\Entity\TextAnswer;
use TestHubBundle\Entity\Variant;
use TestHubBundle\Entity\VariantAnswer;
use TestHubBundle\Service\Calculator;
class CalculatorTest extends \PHPUnit_Framework_TestCase
{
public function testCalculateMark()
{
$attempt = new Attempt();
$test = new Test();
$attempt->setTest($test);
/**
* First question (with correct answer)
*/
$question1 = new QuestionWithSingleCorrectAnswer();
$question1->setId(1);
$question1->setDescription("Сколько будет 5 - 2 (пять минус два) ?");
$question1->setPoints(10);
$question1->setTest($test);
$question1->setSequenceNumber(1);
$variant1 = new Variant();
$variant1->setId(1);
$variant1->setValue("7");
$variant1->setIsRight("no");
$variant2 = new Variant();
$variant2->setId(2);
$variant2->setValue("3");
$variant2->setIsRight("yes");
$variant3 = new Variant();
$variant3->setId(3);
$variant3->setValue("2");
$variant3->setIsRight("no");
$variant4 = new Variant();
$variant4->setId(4);
$variant4->setValue("ни один из вариантов");
$variant4->setIsRight("no");
$question1->setVariants([$variant1, $variant2, $variant3, $variant4]);
$answer1 = new VariantAnswer();
$answer1->setAttempt($attempt);
$answer1->setQuestion($question1);
$answer1->setVariant($variant2);
/**
* Second question (with correct answer)
*/
$question2 = new QuestionWithTextAnswer();
$question2->setId(2);
$question2->setDescription(
"Как называется этот значок \"+\"?" .
"Подсказка: 4 буквы, начинается с \"п\", заканчивается на \"люс\"."
);
$question2->setPoints(5);
$question2->setTest($test);
$question2->setSequenceNumber(2);
$question2->setAnswerText("плюс");
$answer2 = new TextAnswer();
$answer2->setAttempt($attempt);
$answer2->setQuestion($question2);
$answer2->setTextAnswer("плюс");
/**
* Third question (with wrong answer)
*/
$question3 = new QuestionWithDecimalAnswer();
$question3->setId(3);
$question3->setDescription(
"Сколько будет 2 + 2 (два плюс два)? Ответ дать числом."
);
$question3->setPoints(5);
$question3->setTest($test);
$question3->setSequenceNumber(3);
$question3->setAnswerDecimal("4");
$answer3 = new DecimalAnswer();
$answer3->setAttempt($attempt);
$answer3->setQuestion($question3);
$answer3->setDecimalAnswer("5.5");
/**
* Fourth question (without answer, i.e. skipped)
*/
$variant5 = new Variant();
$variant5->setId(5);
$variant5->setValue("число 5 положительное");
$variant5->setIsRight("yes");
$variant6 = new Variant();
$variant6->setId(6);
$variant6->setValue("число 5 больше нуля");
$variant6->setIsRight("yes");
$variant7 = new Variant();
$variant7->setId(7);
$variant7->setValue("число 5 меньше нуля");
$variant7->setIsRight("no");
$variant8 = new Variant();
$variant8->setId(8);
$variant8->setValue("число 5 четное");
$variant8->setIsRight("no");
$question4 = new QuestionWithMultipleCorrectAnswers();
$question4->setId(4);
$question4->setDescription("Выберите правильные утверждения");
$question4->setPoints(10);
$question4->setTest($test);
$question4->setSequenceNumber(4);
$question4->setVariants([$variant5, $variant6, $variant7, $variant8]);
/**
* Attach questions and answers
*/
$test->setQuestions([$question1, $question2, $question3, $question4]);
$attempt->setAnswers([$answer1, $answer2, $answer3]);
/**
* Finally test itself
*/
$calculator = new Calculator();
$mark = $calculator->calculateMark($attempt);
$this->assertInternalType('integer', $mark);
$this->assertEquals(15, $mark);
}
}