Laravel 4模型事件不适用于PHPUnit [英] Laravel 4 Model Events don't work with PHPUnit

查看:69
本文介绍了Laravel 4模型事件不适用于PHPUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用creating Model Event在Laravel 4中构建了一个模型侧验证:

I build a model side validation in Laravel 4 with the creating Model Event :

class User extends Eloquent {

    public function isValid()
    {
        return Validator::make($this->toArray(), array('name' => 'required'))->passes();
    }

    public static function boot()
    {
        parent::boot();

        static::creating(function($user)
        {
            echo "Hello";
            if (!$user->isValid()) return false;
        });
    }
}

它运作良好,但PHPUnit出现问题.接下来的两个测试完全相同,只是第一次通过:

It works well but I have issues with PHPUnit. The two following tests are exactly the same but juste the first one pass :

class UserTest extends TestCase {

    public function testSaveUserWithoutName()
    {
        $count = User::all()->count();

        $user = new User;
        $saving = $user->save();

        assertFalse($saving);                       // pass
        assertEquals($count, User::all()->count()); // pass
    }

    public function testSaveUserWithoutNameBis()
    {
        $count = User::all()->count();

        $user = new User;
        $saving = $user->save();

        assertFalse($saving);                       // fail
        assertEquals($count, User::all()->count()); // fail, the user is created
    }
}

如果我尝试在同一测试中创建一个用户两次,那么它会起作用,但是就像绑定事件仅在我的测试类的第一个测试中出现一样.在第一次测试执行期间,echo "Hello";仅打印一次.

If I try to create a user twice in the same test, it works, but it's like if the binding event is present only in the first test of my test class. The echo "Hello"; is printed only one time, during the first test execution.

我简化了我的问题,但您可以看到问题:我无法在不同的单元测试中测试多个验证规则.从几个小时开始,我几乎尝试了所有事情,但是现在我快要跳出窗户了!有什么主意吗?

I simplify the case for my question but you can see the problem : I can't test several validation rules in different unit tests. I try almost everything since hours but I'm near to jump out the windows now ! Any idea ?

推荐答案

该问题已在Github中得到了很好的记录.请参阅上面的注释,对其进行进一步说明.

The issue is well documented in Github. See comments above that explains it further.

我修改了Github中的解决方案"之一,以在测试过程中自动重置所有模型事件.将以下内容添加到您的TestCase.php文件中.

I've modified one of the 'solutions' in Github to automatically reset all model events during the tests. Add the following to your TestCase.php file.

app/tests/TestCase.php

public function setUp()
{
    parent::setUp();
    $this->resetEvents();
}


private function resetEvents()
{
    // Get all models in the Model directory
    $pathToModels = '/app/models';   // <- Change this to your model directory
    $files = File::files($pathToModels);

    // Remove the directory name and the .php from the filename
    $files = str_replace($pathToModels.'/', '', $files);
    $files = str_replace('.php', '', $files);

    // Remove "BaseModel" as we dont want to boot that moodel
    if(($key = array_search('BaseModel', $files)) !== false) {
        unset($files[$key]);
    }

    // Reset each model event listeners.
    foreach ($files as $model) {

        // Flush any existing listeners.
        call_user_func(array($model, 'flushEventListeners'));

        // Reregister them.
        call_user_func(array($model, 'boot'));
    }
}

这篇关于Laravel 4模型事件不适用于PHPUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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