我正在尝试测试不能插入重复的用户。为此,我创建了 2 个具有相同详细信息的用户对象,只是更改了帐户名称 - 因为这也是独一无二的。
但是,我的二传手似乎没有第二次设置公司名称。当我从模型中回显时,我尝试设置的属性仍然与我创建的前一个对象相同。我的测试失败,因为它抛出了我设置的帐户已存在异常
Failed asserting that exception of type "Models\AccountAlreadyExistsException" matches expected exception "Models\UserAlreadyExistsException".
public function testCantInsertDuplicateUser ()
{
$user = new \Models\User();
$user->first_name = 'John';
$user->surname = 'Smith';
$user->email = 'myemail@gmail.com';
$user->password = 'password';
$user->password_confirmation = 'password';
$user->setCompanyName('Star');
$user->setPackageId(2);
$this->assertTrue($user->signUp());
$user2 = new \Models\User();
$user2->first_name = 'John';
$user2->surname = 'Smith';
$user2->email = 'myemail@gmail.com';
$user2->password = 'password';
$user2->password_confirmation = 'password';
$user2->setCompanyName('cross');
$user2->setPackageId(2);
$this->setExpectedException('Models\UserAlreadyExistsException');
$user2->signUp();
}
//user model
public function setCompanyName ($company_name)
{
$this->company_name = $company_name;
}
private function insertAccount ()
{
$account = new \Models\Account;
$account->setCompanyName($this->company_name);
$account->setPackageId($this->package_id);
$this->account_message_bag = new \Illuminate\Support\MessageBag();
if (!$account->insert()) {
$this->account_message_bag = $account->getValidationErrors();
}
return $account;
}
private function insertUser ()
{
$save_user = $this->save(self::$rules, array(), array(), function ($model)
{
//this is all performed before save
$existing_email = User::where('email', "=", $this->email)->count();
if ($existing_email) {
//delete account that was created in previous step
//as the signup hasn't been successful
$this->account->delete();
throw new UserAlreadyExistsException();
}
//are there any account validation errors?
if (count($this->account_message_bag->getMessages()>0)) {
return false;
}
});
return $save_user;
}
public function signUp ()
{
$this->account = $this->insertAccount();
$this->account_id = $this->account->getId();
if (!$this->insertUser()) {
//delete the company created in previous step
$this->account->delete();
$user_message_bag = Ardent::errors();
//combine user and account validation eerrors
$message_array = array_merge($user_message_bag->getMessages(), $this->account_message_bag->getMessages());
$this->validation_errors = new \Illuminate\Support\MessageBag($message_array);
throw new GenericValidationException();
}
//TODO - does this return false on failure?
$sent = $this->sendConfirmEmail();
if (!$sent) {
throw new WelcomeEmailNotSent();
}
//sende confirm email
return true;
}
//Account model
public function insert ()
{
$result = $this->save(self::$rules, array(), array(), function()
{
$existing_company_name = \Models\Account::where('company_name', '=', $this->company_name)->count();
if ($existing_company_name) {
throw new AccountAlreadyExistsException();
}
});
if (!$result) {
$this->validation_errors = Ardent::errors();
return false;
}
return true;
}