Preventing user login method running every single time after first login

风流意气都作罢 提交于 2019-12-11 20:34:44

问题


I'm trying to find a way to create a session/cookie to handle user login so that the method below doesn't run query against the database every singe time. It is being called in nearly all the scenarios and slows the test suite a lot.

Important note: There are 2 different users login: user and admin so there may be three different sessions.

When I login as "user"
When I login as "admin"

class FeatureContext extends MinkContext implements KernelAwareInterface
{
    /**
     * @When /^I login as "([^"]*)"$/
     *
     * @param $type User role type.
     */
    public function iLoginAs($type)
    {
        $userData['user']  = array('username' => 'you', 'password' => '111');
        $userData['admin'] = array('username' => 'mee', 'password' => '222');

        $this->visit('/login');
        $this->fillField('username', $userData[$type]['username']);
        $this->fillField('password', $userData[$type]['password']);
        $this->pressButton('_submit');
    }
}

回答1:


The biggest problem here comes from the fact that you send an actual request to the server, which gets processed by both Behat and your app. Instead you can take that logic into your context and do it like this in Behat 3:

Given I am logged in as "…"

class MinkContext extends \Behat\MinkExtension\Context\MinkContext
{

    /**
     * @Given /(?:|I am )logged in as "(?P<type>\d+)"/
     */ 
    public login($type)
    {
        // Here goes the same logic as per your login action: load the user details, create 
        // session, etc. In theory you can store the signed in user model or session id in a
        // static property and don't load it every time the step is invoked.
    }

    /**
     * @beforeStep
     *
     * @param BeforeStepScope $scope
     */
    public function synchroniseClientSession(BeforeStepScope $scope)
    {

        // Setup session id and Xdebug cookies to synchronise / enable both.

        $driver = $this->getSession()->getDriver();

        // Cookies must be set per a specific domain + Chrome might start with a weird url.

        if ($driver instanceof Selenium2Driver && $driver->getCurrentUrl() === 'data:,') {
            $driver->visit($this->getMinkParameter('base_url'));
        }

        $driver->setCookie(session_name(), session_id());
        $driver->setCookie('XDEBUG_SESSION', 'PHPSTORM'); // Can also do that to enable debugging…
    }
} 

It's all fairly simple if you understand how the sessions work and passed between the server and the client. You basically do it "manually" in your context, but the idea is exactly the same with what happens in the controller / action.

You'll need to ensure no session is open before you start the new one in the login, there you might run into some nasty problems, but hopefully not.



来源:https://stackoverflow.com/questions/26425623/preventing-user-login-method-running-every-single-time-after-first-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!