简体   繁体   English

Laravel 5.1测试 - 从数据库获取行以完成测试

[英]Laravel 5.1 Testing - getting row from database to complete test

I am building a testing routine in Laravel 5.1 to run through the login process I have just created. 我正在Laravel 5.1中构建一个测试例程来运行我刚刚创建的登录过程。 I am wanting, as part of the testing, to test the password reset and change process. 作为测试的一部分,我希望测试密码重置和更改过程。

The issue is that the password reset process generates and row in a table with a timestamp and uuid. 问题是密码重置过程在带有时间戳和uuid的表中生成并排序。 The link remains valid for 1 hour for the reset to work. 该链接保持有效1小时,以使重置工作。

The flow is: 流程是:

1- Reset password by entering email address 1-输入电子邮件地址重置密码

2- System generates email with uuid link and sends to the user (currently it appears in the Laravel log). 2-系统使用uuid链接生成电子邮件并发送给用户(当前它出现在Laravel日志中)。

3- User clicks on link from email, and if within one hour since it was generated, the user is presented with a password change screen. 3-用户点击电子邮件中的链接,如果在生成后的一小时内,将向用户显示密码更改屏幕。 The link is also deleted from the table. 该链接也从表中删除。

So now for my test code: 所以现在我的测试代码:

public function testSendPasswordLink()
{
    $this->visit('/login')
        ->click('Forgot Your Password?')
        ->seePageIs('/forgot-password')
        ->type('test@test.com','email')
        ->press('Send Password Reset Link')
        ->seePageIs('/login')
        ->see('A password reset link was sent to the email address supplied.')
        ->seeInDatabase('password_resets', ['email' => 'test@test.com']);
}

I would like to: 我想要:

       ->getFromDatabase('password_resets', 'uuid')
       ->visit('/reset-password/'.$uuid)
       ->see(....

Is there a way of doing the above? 有没有办法做到这一点? I know how to see in the table but not how to get from the table in the test. 我知道如何see in the table但不知道如何get from the table测试中get from the tableget from the table

Alternatively is there a way to accomplish this via a different set of steps? 或者,有没有办法通过一组不同的步骤来实现这一目标?

Thanks! 谢谢!

Wow, insanity setting in... well, I am new at this so I will not execute myself as yet...! 哇,精神错乱......好吧,我是新人,所以我不会执行自己......! The answer was rather obvious (after sleeping on it!) 答案很明显(在睡觉之后!)

All I had to do is query the table and retrieve the token to perform the password reset test... below is the finished code: 我所要做的就是查询表并检索令牌以执行密码重置测试...下面是完成的代码:

  1. Add Eloquent to the test php file 将Eloquent添加到测试php文件中

    use Illuminate\\Database\\Eloquent\\Model; 使用Illuminate \\ Database \\ Eloquent \\ Model;

  2. Below is the complete function: 以下是完整的功能:

    public function testChangePassword() 公共函数testChangePassword()

     $this->visit('/login') ->click('Forgot Your Password?') ->seePageIs('/forgot-password') ->type('test@test.com','email') ->press('Send Password Reset Link') ->seePageIs('/login') ->see('A password reset link was sent to the email address supplied.') ->seeInDatabase('password_resets', ['email' => 'test@test.com']); $uuid = DB::table('password_resets') ->where('email', '=', 'test@test.com') ->value('token'); $this->visit('/reset-password/' . $uuid) ->type('bbbbbbbb','password') ->type('bbbbbbbb','password_confirmation') ->press('Change Password') ->see('Your password was reset.') ->seePageIs('/login') ->type('test@test.com','email') ->type('bbbbbbbb','password') ->press('Sign In') ->seePageIs('/welcome') ->click('Logout') ->seePageIs('/login'); // Change password back $this->visit('/login') ->click('Forgot Your Password?') ->seePageIs('/forgot-password') ->type('test@test.com','email') ->press('Send Password Reset Link') ->seePageIs('/login') ->see('A password reset link was sent to the email address supplied.') ->seeInDatabase('password_resets', ['email' => 'test@test.com']); $uuid = DB::table('password_resets') ->where('email', '=', 'test@test.com') ->value('token'); $this->visit('/reset-password/' . $uuid) ->type('abcd1234','password') ->type('abcd1234','password_confirmation') ->press('Change Password') ->see('Your password was reset.') ->seePageIs('/login') ->type('test@test.com','email') ->type('abcd1234','password') ->press('Sign In') ->seePageIs('/welcome') ->click('Logout') ->seePageIs('/login'); 

By the way, I cannot seem to format the code with the {} brackets, so apologies. 顺便说一句,我似乎无法使用{}括号格式化代码,所以道歉。

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

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