Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Silvia Ulenberg committed Jun 13, 2018
1 parent 2bfbe41 commit e961202
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
27 changes: 17 additions & 10 deletions Controller/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
namespace Liip\ThemeBundle\Controller;

use Liip\ThemeBundle\ActiveTheme;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;

/**
* Theme controller.
Expand All @@ -42,24 +42,31 @@ class ThemeController
protected $cookieOptions;

/**
* @var ContainerInterface
* @var RouterInterface
*/
protected $container;
protected $router;

/**
* @var string
*/
protected $defaultRoute;

/**
* Theme controller construct.
*
* @param ActiveTheme $activeTheme active theme instance
* @param array $themes Available themes
* @param array|null $cookieOptions The options of the cookie we look for the theme to set
* @param ContainerInterface $container
* @param ActiveTheme $activeTheme active theme instance
* @param array $themes Available themes
* @param array|null $cookieOptions The options of the cookie we look for the theme to set
* @param RouterInterface $router
* @param string $defaultRoute
*/
public function __construct(ActiveTheme $activeTheme, array $themes, array $cookieOptions = null, ContainerInterface $container)
public function __construct(ActiveTheme $activeTheme, array $themes, array $cookieOptions = null, RouterInterface $router, $defaultRoute = '/')
{
$this->activeTheme = $activeTheme;
$this->themes = $themes;
$this->cookieOptions = $cookieOptions;
$this->container = $container;
$this->router = $router;
$this->defaultRoute = $defaultRoute;
}

/**
Expand All @@ -81,7 +88,7 @@ public function switchAction(Request $request)

$this->activeTheme->setName($theme);

$url = $request->headers->get('Referer', $this->container->get('router')->generate($this->container->getParameter('liip_theme.redirect_fallback')));
$url = $request->headers->get('Referer', $this->router->generate($this->defaultRoute));
$response = new RedirectResponse($url);

if (!empty($this->cookieOptions)) {
Expand Down
3 changes: 2 additions & 1 deletion Resources/config/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<argument type="service" id="liip_theme.active_theme" />
<argument>%liip_theme.themes%</argument>
<argument>%liip_theme.cookie%</argument>
<argument type="service" id="service_container"/>
<argument type="service" id="router"/>
<argument>%liip_theme.redirect_fallback%</argument>
</service>
</services>
</container>
13 changes: 12 additions & 1 deletion Tests/UseCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Liip\ThemeBundle\EventListener\ThemeRequestListener;
use Liip\ThemeBundle\Controller\ThemeController;
use Liip\ThemeBundle\ActiveTheme;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Router;

/**
* Bundle Functional tests.
Expand Down Expand Up @@ -133,9 +135,13 @@ public function testThemeAction($config, $assertion, $hasAlreadyACookie = true)
$request = $this->getMockRequest('cookie');
}

$router = $this->getMockBuilder(Router::class)
->disableOriginalConstructor()
->getMock();

$controller = false;
if ($config['load_controllers']) {
$controller = new ThemeController($activeTheme, $config['themes'], $config['cookie']);
$controller = new ThemeController($activeTheme, $config['themes'], $config['cookie'], $router, $config['redirect_fallback']);
}

$listener = new ThemeRequestListener($activeTheme, $config['cookie'], $device);
Expand Down Expand Up @@ -178,6 +184,7 @@ public function dataProvider()
// all-in Controller wins over Cookie and Autodetection
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'homepage',
'active_theme' => 'default',
'autodetect_theme' => true,
'load_controllers' => true,
Expand All @@ -194,6 +201,7 @@ public function dataProvider()
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'homepage',
'active_theme' => 'default',
'autodetect_theme' => true,
'load_controllers' => true,
Expand All @@ -210,6 +218,7 @@ public function dataProvider()
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'homepage',
'active_theme' => 'default',
'autodetect_theme' => true,
'load_controllers' => false,
Expand All @@ -226,6 +235,7 @@ public function dataProvider()
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'homepage',
'active_theme' => 'default',
'autodetect_theme' => false,
'load_controllers' => true,
Expand All @@ -242,6 +252,7 @@ public function dataProvider()
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'homepage',
'active_theme' => 'default',
'autodetect_theme' => false,
'load_controllers' => true,
Expand Down

0 comments on commit e961202

Please sign in to comment.