繁体   English   中英

Behat/Symfony 3 - 如何检查用户是否登录

[英]Behat/Symfony 3 - How to check if a user is logged in

我正在使用 behat 和 Symfony 3,我想创建一个上下文来检查用户是否已登录,如下所示:

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"
        );
    }

这些是步骤。 第一个失败,运行我在上下文中定义的步骤。 第二个有效,该步骤转到用户个人资料页面并检查用户的电子邮件是否在该页面中,确实如此。

只有正确登录后才会发生这种情况。

所以问题似乎是用户上下文中的令牌存储,知道为什么会发生这种情况吗?

Scenario: Log in with username and password
    Given I am on "/login"
    When I fill in the following:
      | username | jsmith@domain.com |
      | password | 1234              |
    And I press "_submit"
    And I am on "/"
    Then I should be login as "jsmith@domain.com" with the role "ROLE_USER"
      The logged user is anon., expected jsmith@domain.com (InvalidArgumentException)

  Scenario: Log in with username and password
    Given I am on "/login"
    When I fill in the following:
      | username | jsmith@domain.com |
      | password | 1234              |
    And I press "_submit"
    And I am on "/profile"
    Then I should see "jsmith@domain.com"

这是 behat 配置

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: "~@todo && ~@cli" # CLI is excluded as it registers an error handler that mutes fatal errors

您正在通过“模拟浏览器页面”登录

Given I am on "/login"
  When I fill in the following:
    | username | jsmith@domain.com |
    | password | 1234              |
  And I press "_submit"

这是一个完全独立于您尝试检索登录用户的 behat 进程的进程。

你唯一能做的就是重新定义登录步骤,明确查询数据库,将这些用户保存在一种共享上下文中,然后检查它(但在这一点上,这个测试似乎毫无用处)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM