Skip to content

Commit

Permalink
Extract session management in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Oct 5, 2022
1 parent 4292d52 commit 1b5e74d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 63 deletions.
12 changes: 0 additions & 12 deletions symfony.lock
Expand Up @@ -718,18 +718,6 @@
"symfony/intl": {
"version": "v4.1.3"
},
"symfony/mailer": {
"version": "6.1",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "4.3",
"ref": "97a61eabb351d7f6cb7702039bcfe07fe9d7e03c"
},
"files": [
"config/packages/mailer.yaml"
]
},
"symfony/messenger": {
"version": "4.3",
"recipe": {
Expand Down
17 changes: 1 addition & 16 deletions tests/Controller/AdminProductAjaxTest.php
Expand Up @@ -21,7 +21,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

final class AdminProductAjaxTest extends JsonApiTestCase
final class AdminProductAjaxTest extends SessionAwareAjaxTest
{
/**
* @test
Expand Down Expand Up @@ -81,19 +81,4 @@ private function authenticateAdminUser(): void
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}

protected function setUp(): void
{
parent::setUp();

$requestStack = self::$kernel->getContainer()->get('request_stack');
try {
$requestStack->getSession();
} catch (SessionNotFoundException) {
$session = self::$kernel->getContainer()->get('session_factory.public')->createSession();
$request = new Request();
$request->setSession($session);
$requestStack->push($request);
}
}
}
17 changes: 1 addition & 16 deletions tests/Controller/AdminProductVariantAjaxTest.php
Expand Up @@ -20,7 +20,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

final class AdminProductVariantAjaxTest extends JsonApiTestCase
final class AdminProductVariantAjaxTest extends SessionAwareAjaxTest
{
/** @test */
public function it_denies_access_to_product_variants_for_not_authenticated_user(): void
Expand Down Expand Up @@ -99,19 +99,4 @@ private function authenticateAdminUser(): void
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}

protected function setUp(): void
{
parent::setUp();

$requestStack = self::$kernel->getContainer()->get('request_stack');
try {
$requestStack->getSession();
} catch (SessionNotFoundException) {
$session = self::$kernel->getContainer()->get('session_factory.public')->createSession();
$request = new Request();
$request->setSession($session);
$requestStack->push($request);
}
}
}
20 changes: 1 addition & 19 deletions tests/Controller/AdminTaxonAjaxTest.php
Expand Up @@ -13,15 +13,12 @@

namespace Sylius\Tests\Controller;

use ApiTestCase\JsonApiTestCase;
use PHPUnit\Framework\Assert;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

final class AdminTaxonAjaxTest extends JsonApiTestCase
final class AdminTaxonAjaxTest extends SessionAwareAjaxTest
{
/** @test */
public function it_denies_access_to_taxons_for_not_authenticated_user(): void
Expand Down Expand Up @@ -106,19 +103,4 @@ private function authenticateAdminUser(): void
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}

protected function setUp(): void
{
parent::setUp();

$requestStack = self::$kernel->getContainer()->get('request_stack');
try {
$requestStack->getSession();
} catch (SessionNotFoundException) {
$session = self::$kernel->getContainer()->get('session_factory.public')->createSession();
$request = new Request();
$request->setSession($session);
$requestStack->push($request);
}
}
}
27 changes: 27 additions & 0 deletions tests/Controller/SessionAwareAjaxTest.php
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Sylius\Tests\Controller;

use ApiTestCase\JsonApiTestCase;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\Request;

abstract class SessionAwareAjaxTest extends JsonApiTestCase
{
protected function setUp(): void
{
parent::setUp();

$requestStack = self::$kernel->getContainer()->get('request_stack');
try {
$requestStack->getSession();
} catch (SessionNotFoundException) {
$session = self::$kernel->getContainer()->get('session_factory.public')->createSession();
$request = new Request();
$request->setSession($session);
$requestStack->push($request);
}
}
}

0 comments on commit 1b5e74d

Please sign in to comment.