Ich verwende Behat und Symfony 3 und ich möchte einen Kontext zu schaffen, um zu überprüfen, ob ein Benutzer angemeldet hat, wie folgt aus:Behat/Symfony 3 - Wie kann man überprüfen, ob ein Benutzer angemeldet ist
public function __construct(
UserManagerInterface $userManager,
AuthorizationCheckerInterface $authorizationChecker,
TokenStorageInterface $tokenStorage
) {
$this->userManager = $userManager;
$this->authorizationChecker = $authorizationChecker;
$this->tokenStorage = $tokenStorage;
}
/**
* @param string $user
* @param string $role
*
* @Then /^I should be login as "([^"]*)" with the role "([^"]*)"$/
*/
public function iShouldBeLoggedInAsWithTheRole(string $user, string $role): void
{
$loggedUser = $this->tokenStorage->getToken()->getUsername();
var_dump($loggedUser);
Assert::same(
$loggedUser,
$user,
"The logged user is $loggedUser, expected $user"
);
Assert::true(
$this->authorizationChecker->isGranted($role),
"The user $user must have the role $role"
);
}
Und das sind die Schritte. Die erste schlägt fehl und führt den im Kontext definierten Schritt aus. Die zweite Funktion besteht darin, dass die Schritte zur Benutzerprofilseite gehen und überprüft, ob die E-Mail des Benutzers auf der Seite ist. Das kann nur passieren, wenn die Anmeldung korrekt durchgeführt wurde. Also scheint das Problem der Token-Speicher im Benutzerkontext zu sein, irgendeine Idee, warum dies passiert?
Scenario: Log in with username and password
Given I am on "/login"
When I fill in the following:
| username | [email protected] |
| password | 1234 |
And I press "_submit"
And I am on "/"
Then I should be login as "[email protected]" with the role "ROLE_USER"
The logged user is anon., expected [email protected] (InvalidArgumentException)
Scenario: Log in with username and password
Given I am on "/login"
When I fill in the following:
| username | [email protected] |
| password | 1234 |
And I press "_submit"
And I am on "/profile"
Then I should see "[email protected]"
Und das ist die Behat Konfiguration
default:
formatters:
pretty:
verbose: true
paths: false
snippets: false
extensions:
Behat\MinkExtension:
base_url: 'https://localhost/app_dev.php/'
sessions:
default:
symfony2: ~
Behat\Symfony2Extension:
kernel:
env: test
debug: true
FriendsOfBehat\CrossContainerExtension: ~
FriendsOfBehat\PerformanceExtension: ~
FriendsOfBehat\VariadicExtension: ~
gherkin:
filters:
tags: "[email protected] && [email protected]" # CLI is excluded as it registers an error handler that mutes fatal errors
sind diese Szenarien mit UI mit WebDriver oder Selen oder etwas ähnliches? Oder laufen sie ohne "Browser" -Funktion? – DonCallisto
@DonCallisto ohne browser-fähigkeit, habe ich die behat config der frage hinzugefügt – petekaner