2017-03-03 61 views
0

我使用貝哈特和Symfony的3記錄,我想創建上下文來檢查,如果用戶已登錄,就像這樣:貝哈特/ 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 | [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]" 

這是貝哈特配置

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 
+0

是涉及Web與WebDriver,或硒或類似的東西的UI?還是他們沒有任何「瀏覽器」設施運行? – DonCallisto

+0

@DonCallisto沒有任何瀏覽器工具,我添加了behat配置的問題 – petekaner

回答

0

您是通過「模擬瀏覽器頁面」

Given I am on "/login" 
    When I fill in the following: 
    | username | [email protected] | 
    | password | 1234    | 
    And I press "_submit" 

這是一個過程完全獨立貝哈特過程,其中你的日誌試圖檢索登錄的用戶。

只有你可以做的事情是重新定義登錄步驟,明確地查詢數據庫,保存在一個shared context這些用戶,並檢查後(但在這一點上,這個測試,似乎是很沒用)

相關問題