Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix use RequestStack instead of Request #118

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions Cookie/CookieChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,33 @@
namespace ConnectHolland\CookieConsentBundle\Cookie;

use ConnectHolland\CookieConsentBundle\Enum\CookieNameEnum;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class CookieChecker
{
/**
* @var Request
* @var RequestStack
*/
private $request;
private $requestStack;

public function __construct(Request $request)
public function __construct(RequestStack $requestStack)
{
$this->request = $request;
$this->requestStack = $requestStack;
}

/**
* Check if cookie consent has already been saved.
*/
public function isCookieConsentSavedByUser(): bool
{
return $this->request->cookies->has(CookieNameEnum::COOKIE_CONSENT_NAME);
return $this->requestStack->getCurrentRequest()->cookies->has(CookieNameEnum::COOKIE_CONSENT_NAME);
}

/**
* Check if given cookie category is permitted by user.
*/
public function isCategoryAllowedByUser(string $category): bool
{
return $this->request->cookies->get(CookieNameEnum::getCookieCategoryName($category)) === 'true';
return $this->requestStack->getCurrentRequest()->cookies->get(CookieNameEnum::getCookieCategoryName($category)) === 'true';
}
}
7 changes: 6 additions & 1 deletion Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ services:
public: true

ConnectHolland\CookieConsentBundle\Cookie\CookieChecker:
arguments: ["@=service('request_stack').getCurrentRequest()"]
arguments:
$requestStack: '@request_stack'

ConnectHolland\CookieConsentBundle\Cookie\CookieLogger:
arguments:
$request: "@=service('request_stack').getCurrentRequest()"

ConnectHolland\CookieConsentBundle\Twig\CHCookieConsentTwigExtension:
arguments:
$cookieChecker: '@ConnectHolland\CookieConsentBundle\Cookie\CookieChecker'
17 changes: 15 additions & 2 deletions Tests/Cookie/CookieCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class CookieCheckerTest extends TestCase
{
Expand All @@ -22,15 +23,27 @@ class CookieCheckerTest extends TestCase
*/
private $request;

/**
* @var MockObject
*/
private $requestStack;

/**
* @var CookieChecker
*/
private $cookieChecker;

public function setUp(): void
{
$this->request = $this->createMock(Request::class);
$this->cookieChecker = new CookieChecker($this->request);
$this->requestStack = $this->createMock(RequestStack::class);
$this->request = $this->createMock(Request::class);

$this->requestStack
->expects($this->any())
->method('getCurrentRequest')
->willReturn($this->request);

$this->cookieChecker = new CookieChecker($this->requestStack);
}

/**
Expand Down
33 changes: 10 additions & 23 deletions Tests/Twig/CHCookieConsentTwigExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

namespace ConnectHolland\CookieConsentBundle\Tests\Twig;

use ConnectHolland\CookieConsentBundle\Cookie\CookieChecker;
use ConnectHolland\CookieConsentBundle\Twig\CHCookieConsentTwigExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Component\HttpFoundation\Request;

class CHCookieConsentTwigExtensionTest extends TestCase
{
Expand All @@ -21,9 +20,15 @@ class CHCookieConsentTwigExtensionTest extends TestCase
*/
private $chCookieConsentTwigExtension;

/**
* @var MockObject
*/
private $cookieChecker;

public function setUp(): void
{
$this->chCookieConsentTwigExtension = new CHCookieConsentTwigExtension();
$this->cookieChecker = $this->createMock(CookieChecker::class);
$this->chCookieConsentTwigExtension = new CHCookieConsentTwigExtension($this->cookieChecker);
}

public function testGetFunctions(): void
Expand All @@ -37,32 +42,14 @@ public function testGetFunctions(): void

public function testIsCookieConsentSavedByUser(): void
{
$request = new Request();

$appVariable = $this->createMock(AppVariable::class);
$appVariable
->expects($this->once())
->method('getRequest')
->wilLReturn($request);

$context = ['app' => $appVariable];
$result = $this->chCookieConsentTwigExtension->isCookieConsentSavedByUser($context);
$result = $this->chCookieConsentTwigExtension->isCookieConsentSavedByUser();

$this->assertSame($result, false);
}

public function testIsCategoryAllowedByUser(): void
{
$request = new Request();

$appVariable = $this->createMock(AppVariable::class);
$appVariable
->expects($this->once())
->method('getRequest')
->wilLReturn($request);

$context = ['app' => $appVariable];
$result = $this->chCookieConsentTwigExtension->isCategoryAllowedByUser($context, 'analytics');
$result = $this->chCookieConsentTwigExtension->isCategoryAllowedByUser('analytics');

$this->assertSame($result, false);
}
Expand Down
31 changes: 13 additions & 18 deletions Twig/CHCookieConsentTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace ConnectHolland\CookieConsentBundle\Twig;

use ConnectHolland\CookieConsentBundle\Cookie\CookieChecker;
use Symfony\Component\HttpFoundation\Request;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

Expand All @@ -26,42 +25,38 @@ public function getFunctions()
return [
new TwigFunction(
'chcookieconsent_isCookieConsentSavedByUser',
[$this, 'isCookieConsentSavedByUser'],
['needs_context' => true]
[$this, 'isCookieConsentSavedByUser']
),
new TwigFunction(
'chcookieconsent_isCategoryAllowedByUser',
[$this, 'isCategoryAllowedByUser'],
['needs_context' => true]
[$this, 'isCategoryAllowedByUser']
),
];
}

/**
* Checks if user has sent cookie consent form.
* @var CookieChecker
*/
public function isCookieConsentSavedByUser(array $context): bool
{
$cookieChecker = $this->getCookieChecker($context['app']->getRequest());
private $cookieChecker;

return $cookieChecker->isCookieConsentSavedByUser();
public function __construct(CookieChecker $cookieChecker)
{
$this->cookieChecker = $cookieChecker;
}

/**
* Checks if user has given permission for cookie category.
* Checks if user has sent cookie consent form.
*/
public function isCategoryAllowedByUser(array $context, string $category): bool
public function isCookieConsentSavedByUser(): bool
{
$cookieChecker = $this->getCookieChecker($context['app']->getRequest());

return $cookieChecker->isCategoryAllowedByUser($category);
return $this->cookieChecker->isCookieConsentSavedByUser();
}

/**
* Get instance of CookieChecker.
* Checks if user has given permission for cookie category.
*/
private function getCookieChecker(Request $request): CookieChecker
public function isCategoryAllowedByUser(string $category): bool
{
return new CookieChecker($request);
return $this->cookieChecker->isCategoryAllowedByUser($category);
}
}