我正在使用PHPUnit进行测试。这是我的班级,以粗体显示失败的测试:
//Check that the database is clean
public function testNoUsers(){
$users = User::all();
$this->assertEquals(count($users), 0);
}
//Test that users can login and a find and search works.
public function testUserRegistrationAndSave(){
User::create(array(
'email' => 'thisisatest@gmail.com',
'password' => Hash::make('first_password')
));
$user = User::where('email', '=', 'thisisatest@gmail.com')->first();
$this->assertEquals('thisisatest@gmail.com', $user->email);
}
//Make sure that the email is a proper email address
public function testShouldNotSaveWithInvalidEmail(){
$invalidUser = new User(array(
'email' => 'thisisatest',
'password' => Hash::make('first_password')
));
$this->assertEquals($invalidUser->save(), false, 'Incorrect email address');
}
//Make sure that if the email is not filled out, it can't be saved.
public function testEmailIsFilled(){
$invalidUser = new User(array(
'password' => Hash::make('first_password')
));
$this->assertEquals($invalidUser->save(), false, 'Email not filled out!');
}
public function testUserActive(){
$invalidUser = new User(array(
'email' => 'test@test.com',
'password' => Hash::make('first_password')
));
$invalidUser->save();
//Has active = 1
$validUser = new User(array(
'email' => 'test@test.com',
'password' => Hash::make('first_password'),
'active' => 1
));
$validUser->save();
//User with no active can't login
$login = $invalidUser->login();
$this->assertEquals(false, $login, 'Inactive user logged in');
var_dump($validUser->active); //This outputs 1
//Reset password to plaintext
$validUser->password = "first_password";
$login = $validUser->login();
**$this->assertEquals(true, $login, 'Active user logged in');** //Error is here
}
}
这是我的登录方法。当我添加active =>时返回FALSE 1,但当我删除active =>时返回true 1部分:
使用Illuminate \ Auth \ UserInterface; 使用Illuminate \ Auth \ Reminders \ RemindableInterface;
class User extends Model实现UserInterface,RemindableInterface {
protected $fillable = array('email', 'password');
protected $softDelete = true;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
protected static $rules = array(
'email' => 'required|email|unique:users|min:4'
);
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return $this->remember_token;
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
$this->remember_token = $value;
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return 'remember_token';
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
**public function login(){
if(Auth::attempt(array('email' => $this->email, 'password' => $this->password, 'active' => 1), true)){
return true;
}else{
return false;
}
}**
}
我正在使用PHPUnit 4运行SQLite测试。迁移会将活动字段添加为默认值1,因此我不确定为什么这不起作用。
答案 0 :(得分:0)
你必须手动完成,但这很简单:
public function login()
{
$loggable = $user = User::where('email', $this->email) &&
Hash::check($this->password, $user->password) &&
$user->active;
if ($loggable)
{
Auth::login($user);
}
return $loggable;
}