你能在phpunit的一个测试函数中有两个模型实例吗?

时间:2013-10-31 11:43:30

标签: php laravel phpunit

我正在尝试测试无法插入重复的用户。为此,我创建了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;
}

2 个答案:

答案 0 :(得分:0)

检查验证AccountAlreadyExistsException

的方式

可能是您应该更改电子邮件

答案 1 :(得分:0)

您的命名空间可能就是问题所在。

您在\ Models中创建了类,但是使用Models(没有反斜杠)来表示异常。也许你的代码正在抛出\ Models \ UserAlreadyExsitsException,而不是Models \ UserAlreadyExistsException。

$user2 = new \Models\User();
...
$this->setExpectedException('Models\UserAlreadyExistsException');

也许这应该是'\ Models \ UserAlreadyExistsException'

$user2 = new \Models\User();
...
$this->setExpectedException('\Models\UserAlreadyExistsException');