8000 [make:auth] handle session deprecations in 5.3 by jrushlow · Pull Request #801 · symfony/maker-bundle · GitHub
[go: up one dir, main page]

Skip to content

[make:auth] handle session deprecations in 5.3 #801

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

Merged
merged 3 commits into from
Mar 2, 2021
Merged
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
9 changes: 5 additions & 4 deletions src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,12 @@ public function configureDependencies(DependencyBuilder $dependencies, InputInte
private function providerKeyTypeHint(): string
{
$reflectionMethod = new \ReflectionMethod(AbstractFormLoginAuthenticator::class, 'onAuthenticationSuccess');
$typeHint = (string) $reflectionMethod->getParameters()[2]->getType();
if ($typeHint) {
$typeHint .= ' ';
$type = $reflectionMethod->getParameters()[2]->getType();

if (!$type instanceof \ReflectionNamedType) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getType() returns \ReflectionNamedType | null since 7.1. As maker is not used in production, I opted to check instanceOf vs the more performant null === for better DX.

Sorry, something went wrong.

return '';
}

return $typeHint;
return sprintf('%s ', $type->getName());
}
}
4 changes: 2 additions & 2 deletions tests/GeneratorTest.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GeneratorTest extends TestCase
/**
* @dataProvider getClassNameDetailsTests
*/
public function testCreateClassNameDetails(string $name, string $prefix, string $suffix = '', string $expectedFullClassName, string $expectedRelativeClassName)
public function testCreateClassNameDetails(string $name, string $prefix, string $suffix, string $expectedFullClassName, string $expectedRelativeClassName): void
{
$fileManager = $this->createMock(FileManager::class);
$fileManager->expects($this->any())
Expand All @@ -38,7 +38,7 @@ public function testCreateClassNameDetails(string $name, string $prefix, string
$this->assertSame($expectedRelativeClassName, $classNameDetails->getRelativeName());
}

public function getClassNameDetailsTests()
public function getClassNameDetailsTests(): \Generator
{
yield 'simple_class' => [
'foo',
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Security\AppCustomAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpKernel\Kernel;

class SecurityControllerTest extends WebTestCase
{
Expand Down Expand Up @@ -60,7 +61,17 @@ public function testCommand()
$client->submit($form);

$this->assertStringContainsString('TODO: provide a valid redirect', $client->getResponse()->getContent());
$this->assertNotNull($token = $client->getContainer()->get('security.token_storage')->getToken());

$tokenStorage = static::$container->get('security.token_storage');

// Handle Session deprecations in Symfony 5.3+
if (Kernel::VERSION_ID >= 50300) {
$tokenStorage->disableUsageTracking();
}

$token = $tokenStorage->getToken();

self::assertNotNull($token);
$this->assertInstanceOf(User::class, $token->getUser());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Security\AppCustomAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class SecurityControllerTest extends WebTestCase
{
Expand Down Expand Up @@ -60,10 +62,23 @@ public function testCommand()
$client->submit($form);

$this->assertStringContainsString('TODO: provide a valid redirect', $client->getResponse()->getContent());
$this->assertNotNull($token = $client->getContainer()->get('security.token_storage')->getToken());
$this->assertInstanceOf(User::class, $token->getUser());
$this->assertInstanceOf(User::class, $this->getToken()->getUser());

$client->request('GET', '/logout');
$this->assertNull($client->getContainer()->get('security.token_storage')->getToken());
$this->assertNull($this->getToken());
}

/**
* Handle Session deprecations in Symfony 5.3+
*/
private function getToken(): ?TokenInterface
{
$tokenStorage = static::$container->get('security.token_storage');

if (Kernel::VERSION_ID >= 50300) {
$tokenStorage->disableUsageTracking();
}

return $tokenStorage->getToken();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Security\AppTestSecurity52LoginFormAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
Expand Down Expand Up @@ -68,7 +69,17 @@ public function testLoginFormAuthenticatorUsingSecurity51(): void
$client->submit($form);

self::assertStringContainsString('TODO: provide a valid redirect', $client->getResponse()->getContent());
self::assertNotNull($token = $client->getContainer()->get('security.token_storage')->getToken());

$tokenStorage = static::$container->get('security.token_storage');

// Handle Session deprecations in Symfony 5.3+
if (Kernel::VERSION_ID >= 50300) {
$tokenStorage->disableUsageTracking();
}

$token = $tokenStorage->getToken();

self::assertNotNull($token);
self::assertInstanceOf(User::class, $token->getUser());
}
}
0