-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[DX][Testing] Added a loginUser() method to test protected resources #35997
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
Conversation
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SecurityTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Core/Test/Authentication/ConcreteToken.php
Outdated
Show resolved
Hide resolved
bdffecd to
df54a4b
Compare
| } | ||
|
|
||
| if (!$user instanceof UserInterface) { | ||
| throw new \LogicException(sprintf('The first argument of "%s" must be instance of "%s", "%s" provided.', __METHOD__, UserInterface::class, \is_object($user) ? \get_class($user) : \gettype($user))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I need to use the get_debug_type() function here already, or if #35945 has to be merged first.
| /** | ||
| * @param UserInterface $user | ||
| */ | ||
| public function loginUser($user, string $firewallContext = 'main'): self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the typehint allowed me to have a bit nicer error message when Security is not installed.
|
build failures are unrelated (HttpClient is failing) |
| } | ||
|
|
||
| $client->request('GET', '/'.($firewallContext ?? 'main').'/user_profile'); | ||
| $this->assertEquals('Welcome '.$username.'!', $client->getResponse()->getContent()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no assertion on the roles, I'd suggest to add some, to make sure roles are passed to the user and token, WDYT?
|
|
||
| $client->request('GET', '/main/user_profile'); | ||
| $this->assertEquals('Welcome the-username!', $client->getResponse()->getContent()); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about adding some tests to check for users that are not in the provider?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has to be in the user provider (as Symfony reloads the user upon every requests and if the user changed (i.e. is no longer available), the user is logged out). Do you mean adding a test that the request is unsuccesfull?
|
Thank you @wouterj. |
This finishes #32850 original description:
A custom
ConcreteTokentest-object is created as suggested by @linaori, to not bind this token to any specific implementation (as other implementations aren't fully compatible with eachother).