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

Conversation

jrushlow
Copy link
Collaborator
@jrushlow jrushlow commented Jan 30, 2021
  • fixed deprecated ReflectionType::__toString() (MakeAuthenticatorTest) Deprecated in PHP 7.1
  • removes unneeded optional param - triggers required param order deprecation in PHP8 (GeneratorTest)
  • handles Session deprecation's introduced in Symfony 5.3 in test suites

@weaverryan
Copy link
Member

I suspect that the failing tests are caused by - symfony/symfony#38616 - the timing is pretty much perfect. What we'll need to do is run that test locally (using the flags so we get the dev dependencies) and see if we can repeat. After we run the test (and it fails), you should be able to move into the tmp directory of the project and execute the test inside that project to see the failure again, because the actual failure is coming from when we run the test. It's coming from this line: https://github.com/symfony/maker-bundle/blob/main/tests/fixtures/MakeAuthenticatorLoginFormUserEntity/tests/SecurityControllerTest.php#L63

If we are able to reproduce all of this, then we could try to create a tiny, reproducer project that shows the (potential) bug and open an issue in symfony. I don't want to ping the author jderusse until we have something more easy for him to take a look at.

Thanks!

@jrushlow jrushlow force-pushed the fix/deprecated-get-type branch from a5303fb to 78674a9 Compare February 22, 2021 17:01
@jrushlow
Copy link
Collaborator Author
jrushlow commented Feb 23, 2021

Most of the session related test issues on the dev branches were fixed by symfony/recipes#905 referenced in symfony/symfony#40282. The remaining session based issues will be fixed by either

// login a user in WebTestCase

$usageTrackingTokenStorage = static::$container->get('security.token_storage');
$usageTrackingTokenStorage->disableUsageTracking();  // <- bypasses trying to get the session from the request stack
$token = $usageTrackingTokenStorage->getToken();
$this->assertNotNull($token); // passes

or by adding a event listener on the request with a callable to the test as done in https://github.com/symfony/symfony/pull/38616/files#diff-c1a4581fd182f7bd35bbb4f0297fd62d1df49fb0b2a4b4210707f3a8131766d1R53-R55

@jrushlow
Copy link
Collaborator Author
jrushlow commented Feb 24, 2021

dev CRUD failures may be related to symfony/recipes#892 which disables http_method_override by default.

With framework.http_method_override false, the hidden <input type="hidden" name="_method" value="DELETE"> is ignored by the router. A conditional is needed for make:crud in 5.3+ to enable http method overrides.

see #825

@jrushlow jrushlow changed the title [CI] fix failing PHP7.4 & 8.0 tests [make:auth] handle session deprecations in 5.3 Feb 24, 2021
@jrushlow jrushlow marked this pull request as ready for review February 24, 2021 08:40
@jrushlow jrushlow added the Status: Needs Review Needs to be reviewed label Feb 24, 2021
@jrushlow
Copy link
Collaborator Author

splitting up the test fixes into individual PR's - there are too many issues across different parts of maker to fix everything under one roof.


// Handle Session deprecations in Symfony 5.3+
if (Kernel::VERSION_ID >= 50300) {
$tokenStorage = static::$container->get('security.token_storage');
Copy link
Member

Choose a reason for hiding this comment

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

Why is the way that we fetch the service different in the two cases? This new way seems more correct.

And, would it be possible to use a method_exists() instead of the Symfony version?

$tokenStorage = static::$container->get('security.token_storage');
if (method_exists($tokenStorage, 'disableUsageTracking')) {
    $tokenStorage->disableUsageTracking();
}
$token = $tokenStorage->getToken();

Checking the kernel is sort of a "last resort". If we can rely on something hard in the code, that's even better.

@jrushlow jrushlow force-pushed the fix/deprecated-get-type branch from 73c30e4 to 4f96eb5 Compare February 25, 2021 15:20
$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.

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

if (class_exists(SessionNotFoundException::class)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And, would it be possible to use a method_exists() instead of the Symfony version?

Yes, kinda. The disableUsageTracking() method has always existed in the UsageTrackingTokenStorage::class since it was introduced in Symfony 4.4

For context, 8000 PR 38616 changed how the token is retrieved when calling UsageTrackingTokenStorage->getToken() if usage tracking is enabled (which the kernel enables).

Although I'm not sure we need to, but to support Symfony 4.0 - 4.3 we can't rely on disableUsageTracking() to exist. And for Symfony 4.4 - 5.2 I don't think we should disable usage tracking as thats not realistic from the users perspective although we could. E.g. class_exists(UsageTrackingTokenStorage)

The SessionNotFoundException::class was introduced in 38616 (Symfony 5.3) and was the only class introduced that didn't look like it would be removed anytime soon.

Copy link
Member

Choose a reason for hiding this comment

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

let's keep the kernel check then - thanks for the explantion

@jrushlow jrushlow force-pushed the fix/deprecated-get-type branch from d39be33 to cf0a27d Compare March 2, 2021 07:58
@weaverryan
Copy link
Member

Thanks Jesse!

@weaverryan weaverryan merged commit 24c7334 into symfony:main Mar 2, 2021
@jrushlow jrushlow deleted the fix/deprecated-get-type branch March 2, 2021 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0