Skip to content

Commit

Permalink
added TokenManagerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Wagner committed Jan 30, 2018
1 parent 3bf223a commit c0bd329
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 52 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"heimrichhannot/contao-utils-bundle": "dev-master"
},
"require-dev": {
"contao/test-case": "^1.1",
"contao/manager-plugin": "^2.0",
"friendsofphp/php-cs-fixer": "^2.2",
"phpunit/phpunit": "^6.0",
Expand Down
5 changes: 3 additions & 2 deletions src/Choice/TemplateChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@

namespace HeimrichHannot\QuizBundle\Choice;

use Contao\System;
use HeimrichHannot\UtilsBundle\Choice\AbstractChoice;

class TemplateChoice extends AbstractChoice
{
/**
* @return array
*/
protected function collect()
public function collect()
{
$choices = [];

$config = \System::getContainer()->getParameter('huh.quiz');
$config = System::getContainer()->getParameter('huh.quiz');

if (!isset($config['quiz']['templates'])) {
return $choices;
Expand Down
50 changes: 0 additions & 50 deletions tests/Frontend/InsertTagsTest.php

This file was deleted.

124 changes: 124 additions & 0 deletions tests/TokenManager/TokenManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\QuizBundle\Test\Choice;

use Contao\Config;
use Contao\ManagerBundle\HttpKernel\ContaoKernel;
use Contao\ManagerPlugin\PluginLoader;
use Contao\Model;
use Contao\System;
use Contao\TestCase\ContaoTestCase;
use Doctrine\DBAL\Connection;
use Firebase\JWT\JWT;
use HeimrichHannot\QuizBundle\Manager\TokenManager;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;

class TokenManagerTest extends ContaoTestCase
{
/**
* {@inheritdoc}
*/
public function setUp(): void
{
parent::setUp();

if (!defined('TL_ROOT')) {
\define('TL_ROOT', $this->getFixturesDir());
}

$router = $this->createRouterMock();
$requestStack = $this->createRequestStackMock();
$framework = $this->mockContaoFramework($this->createMockAdapater());

$database = $this->createMock(Connection::class);
$container = $this->mockContainer();
$container->set('kernel', $this->createMock(ContaoKernel::class));
$container->setParameter('secret', Config::class);
$container->set('request_stack', $requestStack);
$container->set('router', $router);
$container->set('contao.framework', $framework);
$container->set('database_connection', $database);
System::setContainer($container);
}

/**
* @return array
*/
public function testAddDataToJwtToken()
{
$tokenManager = new TokenManager();
$encode = JWT::encode(['id' => 12], System::getContainer()->getParameter('secret'));
$token = $tokenManager->getDataFromJwtToken($encode);

$this->assertSame(12, $token->id);
}

public function createRouterMock()
{
$router = $this->createMock(RouterInterface::class);
$router->method('generate')->with('contao_backend', $this->anything())->will($this->returnCallback(function ($route, $params = []) {
$url = '/contao';
if (!empty($params)) {
$count = 0;
foreach ($params as $key => $value) {
$url .= (0 === $count ? '?' : '&');
$url .= $key.'='.$value;
++$count;
}
}

return $url;
}));

return $router;
}

public function createRequestStackMock()
{
$requestStack = new RequestStack();
$request = new \Symfony\Component\HttpFoundation\Request();
$request->attributes->set('_contao_referer_id', 'foobar');
$requestStack->push($request);

return $requestStack;
}

public function createMockAdapater()
{
$modelAdapter = $this->mockAdapter(['__construct']);

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

/**
* @return string
*/
protected function getFixturesDir(): string
{
return __DIR__.DIRECTORY_SEPARATOR.'Fixtures';
}

/**
* Mocks the plugin loader.
*
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $expects
* @param array $plugins
*
* @return PluginLoader|\PHPUnit_Framework_MockObject_MockObject
*/
private function mockPluginLoader(\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $expects, array $plugins = [])
{
$pluginLoader = $this->createMock(PluginLoader::class);

$pluginLoader->expects($expects)->method('getInstancesOf')->with(PluginLoader::EXTENSION_PLUGINS)->willReturn($plugins);

return $pluginLoader;
}
}

0 comments on commit c0bd329

Please sign in to comment.