防止用户登录方法在首次登录后每次运行 [英] Preventing user login method running every single time after first login

查看:100
本文介绍了防止用户登录方法在首次登录后每次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找到一种创建会话/cookie的方式来处理用户登录,以使下面的方法不会每次都对数据库运行查询.几乎在所有情况下都需要调用它,从而大大降低了测试套件的速度.

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.

重要提示:有2种不同的用户登录名:user和admin,因此可能会有3个不同的会话.

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

推荐答案

这里最大的问题来自您向服务器发送实际请求的事实,该请求同时由Behat和您的应用程序处理.相反,您可以将这种逻辑纳入您的上下文,并在Behat 3中像这样进行操作:

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.

login中启动新会话之前,您需要确保没有打开任何会话,在那里您可能会遇到一些令人讨厌的问题,但希望不会.

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.

这篇关于防止用户登录方法在首次登录后每次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆