Skip to content

Commit

Permalink
refactored and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Wagner committed Feb 6, 2018
1 parent 8c787b2 commit 073f2de
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Manager/QuizAnswerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
namespace HeimrichHannot\QuizBundle\Manager;

use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\Model\Collection;
use Contao\System;
use Haste\Util\Url;
use HeimrichHannot\QuizBundle\Model\QuizAnswerModel;
use Model\Collection;

class QuizAnswerManager extends Manager
{
Expand All @@ -37,7 +37,7 @@ public function prepareAnswers(Collection $answersCollection)
$answers = [];

foreach ($answersCollection as $answer) {
$answers[] = $this->parseAnswer($answer, $answer->imgSize);
$answers[] = $this->parseAnswer($answer);
}

return $answers;
Expand All @@ -50,13 +50,13 @@ public function prepareAnswers(Collection $answersCollection)
*
* @return string
*/
public function parseAnswer(QuizAnswerModel $answerModel, $imgSize)
public function parseAnswer(QuizAnswerModel $answerModel)
{
/*
* @var \Twig_Environment
*/
$twig = System::getContainer()->get('twig');
$templateData['answer'] = System::getContainer()->get('huh.quiz.model.manager')->parseModel($answerModel, $answerModel->answer, QuizAnswerModel::getTable(), $answerModel->cssClass, $imgSize);
$templateData['answer'] = System::getContainer()->get('huh.quiz.model.manager')->parseModel($answerModel, $answerModel->answer, QuizAnswerModel::getTable(), $answerModel->cssClass, $answerModel->imgSize);
$templateData['href'] = $this->framework->getAdapter(Url::class)->addQueryString('answer='.$answerModel->id, $this->getUri());

return $twig->render('@HeimrichHannotContaoQuiz/quiz/quiz_answer_item.html.twig', $templateData);
Expand Down
12 changes: 6 additions & 6 deletions src/Manager/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public function addDataToJwtToken($token, $data, $key)
} catch (\Exception $e) {
$token = ['session' => System::getContainer()->get('session')->getId()];
$encode = JWT::encode($token, System::getContainer()->getParameter('secret'));
$url = System::getContainer()->get('contao.framework')->getAdapter(Url::class)->addQueryString('token='.$encode, System::getContainer()->get('request_stack')->getCurrentRequest()->getUri());
System::getContainer()->get('contao.framework')->getAdapter(Controller::class)->redirect($url);
$url = $this->framework->getAdapter(Url::class)->addQueryString('token='.$encode, System::getContainer()->get('request_stack')->getCurrentRequest()->getUri());
$this->framework->getAdapter(Controller::class)->redirect($url);
}

if (!isset($decoded->session) || $decoded->session !== System::getContainer()->get('session')->getId()) {
$token = ['session' => System::getContainer()->get('session')->getId()];
$encode = JWT::encode($token, System::getContainer()->getParameter('secret'));
$url = System::getContainer()->get('contao.framework')->getAdapter(Url::class)->addQueryString('token='.$encode, System::getContainer()->get('request_stack')->getCurrentRequest()->getUri());
System::getContainer()->get('contao.framework')->getAdapter(Controller::class)->redirect($url);
$url = $this->framework->getAdapter(Url::class)->addQueryString('token='.$encode, System::getContainer()->get('request_stack')->getCurrentRequest()->getUri());
$this->framework->getAdapter(Controller::class)->redirect($url);
}

$decoded->$key = $data;
Expand All @@ -73,8 +73,8 @@ public function getDataFromJwtToken($token)
} catch (\Exception $e) {
$token = ['session' => System::getContainer()->get('session')->getId()];
$encode = JWT::encode($token, System::getContainer()->getParameter('secret'));
$url = System::getContainer()->get('contao.framework')->getAdapter(Url::class)->addQueryString('token='.$encode, System::getContainer()->get('request_stack')->getCurrentRequest()->getUri());
System::getContainer()->get('contao.framework')->getAdapter(Controller::class)->redirect($url);
$url = $this->framework->getAdapter(Url::class)->addQueryString('token='.$encode, System::getContainer()->get('request_stack')->getCurrentRequest()->getUri());
$this->framework->getAdapter(Controller::class)->redirect($url);
}

return $decoded;
Expand Down
75 changes: 75 additions & 0 deletions tests/Manager/QuizAnswerManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@

namespace HeimrichHannot\QuizBundle\Test\Manager;

use Contao\ContentModel;
use Contao\ManagerBundle\HttpKernel\ContaoKernel;
use Contao\Model;
use Contao\System;
use Contao\TestCase\ContaoTestCase;
use Haste\Util\Url;
use HeimrichHannot\QuizBundle\Manager\ModelManager;
use HeimrichHannot\QuizBundle\Manager\QuizAnswerManager;
use HeimrichHannot\QuizBundle\Model\QuizAnswerModel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;

Expand All @@ -32,6 +38,27 @@ public function setUp(): void
$container = $this->mockContainer();
$container->set('kernel', $this->createMock(ContaoKernel::class));
$container->set('session', new Session(new MockArraySessionStorage()));

$framework = $this->mockContaoFramework($this->createMockAdapter());
$container->set('contao.framework', $framework);

$loader = new \Twig_Loader_Filesystem(__DIR__.'/../../src/Resources/views/');
$loader->addPath(__DIR__.'/../../src/Resources/views/', 'HeimrichHannotContaoQuiz');
$twig = new \Twig_Environment($loader, ['cache' => $this->getTempDir().'/var/cache/']);
$container->set('twig', $twig);

$contentModel = $this->mockClassWithProperties(ContentModel::class, ['id' => 1]);
$contentAdapter = $this->mockAdapter(['findPublishedByPidAndTable', 'countPublishedByPidAndTable']);
$contentAdapter->method('findPublishedByPidAndTable')->willReturn($contentModel);
$contentAdapter->method('countPublishedByPidAndTable')->willReturn(1);
$manager = new ModelManager($this->mockContaoFramework(array_merge($this->createMockAdapter(), [ContentModel::class => $contentAdapter])));
$container->set('huh.quiz.model.manager', $manager);

$request = new Request();
$requestStack = new RequestStack();
$requestStack->push($request);
$container->set('request_stack', $requestStack);

System::setContainer($container);
}

Expand Down Expand Up @@ -103,6 +130,54 @@ public function testFindOneBy()
$this->assertInstanceOf(QuizAnswerModel::class, $result);
}

public function testParseAnswer()
{
$answerModel = $this->mockClassWithProperties(QuizAnswerModel::class, ['cssClass' => 'css', 'answer' => 'answer', 'imgSize' => '', 'id' => 1]);
$manager = new QuizAnswerManager($this->mockContaoFramework($this->createMockAdapter()));
$template = $manager->parseAnswer($answerModel);

$html = '<div class="quiz-answer">
<a href="https://www.anwaltauskunft.dav.hhdev/app_dev.php/rechtsquiz/arbeitsrecht/8?answer=1">
<div class="css">
<div class="text">
answer
</div>
</div>
</a>
</div>';
$this->assertSame($html, $template);
}

public function testPrepareAnswers()
{
$answerModel = $this->mockClassWithProperties(QuizAnswerModel::class, ['cssClass' => 'css', 'answer' => 'answer', 'imgSize' => '', 'id' => 1]);
$collection = new Model\Collection([$answerModel], 'tl_quiz_answer');
$manager = new QuizAnswerManager($this->mockContaoFramework($this->createMockAdapter()));
$result = $manager->prepareAnswers($collection);
$html = '<div class="quiz-answer">
<a href="https://www.anwaltauskunft.dav.hhdev/app_dev.php/rechtsquiz/arbeitsrecht/8?answer=1">
<div class="css">
<div class="text">
answer
</div>
</div>
</a>
</div>';
$this->assertCount(1, $result);
$this->assertArrayHasKey('0', $result);
$this->assertSame($html, $result[0]);
}

public function createMockAdapter()
{
$modelAdapter = $this->mockAdapter(['__construct']);
$urlAdapter = $this->mockAdapter(['addQueryString', 'removeQueryString']);
$urlAdapter->method('addQueryString')->willReturn('https://www.anwaltauskunft.dav.hhdev/app_dev.php/rechtsquiz/arbeitsrecht/8?answer=1');
$urlAdapter->method('removeQueryString')->willReturn('https://www.anwaltauskunft.dav.hhdev/app_dev.php/rechtsquiz/arbeitsrecht/8');

return [Model::class => $modelAdapter, Url::class => $urlAdapter];
}

/**
* @return string
*/
Expand Down

0 comments on commit 073f2de

Please sign in to comment.