Skip to content

Commit

Permalink
Extra test-cases have been added
Browse files Browse the repository at this point in the history
  • Loading branch information
m.yakunin committed May 28, 2019
1 parent 3cf48bc commit 6077297
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor/
composer.phar
composer.lock
phpunit.xml
/.idea
2 changes: 1 addition & 1 deletion Controller/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function switchAction(Request $request)
$cookie = new Cookie(
$this->cookieOptions['name'],
$theme,
time() + $this->cookieOptions['lifetime'],
$request->server->get('REQUEST_TIME') + $this->cookieOptions['lifetime'],
$this->cookieOptions['path'],
$this->cookieOptions['domain'],
(bool) $this->cookieOptions['secure'],
Expand Down
71 changes: 67 additions & 4 deletions Tests/Controller/ThemeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@
use Liip\ThemeBundle\Tests\Common\Comparator\SymfonyResponse as SymfonyResponseComparator;
use PHPUnit\Framework\MockObject\Matcher\Invocation;
use SebastianBergmann\Comparator\Factory as ComparatorFactory;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ThemeControllerTest extends \PHPUnit\Framework\TestCase
{
const WRONG_THEME = 'wrong_theme';
const RIGHT_THEME = 'right_theme';
const REFERER = 'some_referer';
const DEFAULT_REDIRECT_URL = '/';
const COOKIE_NAME = 'some_cookie_name';
const COOKIE_LIFETIME = 112233;
const COOKIE_PATH = '/';
const COOKIE_DOMAIN = 'some_domain';
const IS_COOKIE_SECURE = false;
const IS_COOKIE_HTTP_ONLY = false;
const REQUEST_TIME = 123;

/**
* @var SymfonyResponseComparator
Expand Down Expand Up @@ -78,6 +87,20 @@ public function switchActionDataProvider()
);
}

/**
* @dataProvider switchActionDataProvider
* @param string|null $referer
* @param RedirectResponse $expectedResponse
*/
public function testSwitchActionWithCookieOptions($referer, RedirectResponse $expectedResponse)
{
$controller = $this->createThemeController(self::once(), $this->createCookieOptions());

$actualResponse = $controller->switchAction($this->createRequest($referer));

self::assertEquals($this->addCookie($expectedResponse), $actualResponse);
}

/**
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* @expectedExceptionMessage The theme "wrong_theme" does not exist
Expand Down Expand Up @@ -148,9 +171,37 @@ private function createRequest($referer)
array('Referer' => array($referer))
);

$request->server->add(
array('REQUEST_TIME' => self::REQUEST_TIME)
);

return $request;
}

/**
* @param string $url
* @return RedirectResponse
*/
private function createExpectedResponse($url)
{
return new RedirectResponse($url);
}

/**
* @return mixed[]
*/
private function createCookieOptions()
{
return array(
'name' => self::COOKIE_NAME,
'lifetime' => self::COOKIE_LIFETIME,
'path' => self::COOKIE_PATH,
'domain' => self::COOKIE_DOMAIN,
'secure' => self::IS_COOKIE_SECURE,
'http_only' => self::IS_COOKIE_HTTP_ONLY,
);
}

/**
* @return Request
*/
Expand All @@ -160,11 +211,23 @@ private function createRequestWithWrongTheme()
}

/**
* @param string $url
* @return RedirectResponse
* @param Response $response
* @return Response
*/
private function createExpectedResponse($url)
private function addCookie(Response $response)
{
return new RedirectResponse($url);
$response->headers->setCookie(
new Cookie(
self::COOKIE_NAME,
self::RIGHT_THEME,
self::REQUEST_TIME + self::COOKIE_LIFETIME,
self::COOKIE_PATH,
self::COOKIE_DOMAIN,
self::IS_COOKIE_SECURE,
self::IS_COOKIE_HTTP_ONLY
)
);

return $response;
}
}
15 changes: 11 additions & 4 deletions Tests/UseCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

namespace Liip\ThemeBundle\Tests\EventListener;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Liip\ThemeBundle\EventListener\ThemeRequestListener;
use Liip\ThemeBundle\Controller\ThemeController;
use Liip\ThemeBundle\ActiveTheme;
use Liip\ThemeBundle\Controller\ThemeController;
use Liip\ThemeBundle\EventListener\ThemeRequestListener;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* Bundle Functional tests.
Expand Down Expand Up @@ -98,7 +98,14 @@ protected function getMockRequest($theme, $cookieReturnValue = 'cookie', $userAg
->getMock();
$request->server->expects($this->any())
->method('get')
->will($this->returnValue($userAgent));
->will(
$this->returnValueMap(
array(
array('HTTP_USER_AGENT', null, $userAgent),
array('REQUEST_TIME', null, 123),
)
)
);

return $request;
}
Expand Down

0 comments on commit 6077297

Please sign in to comment.