Laravel模型事件保存不会触发

Raf*_*del 3 php unit-testing laravel eloquent

我正在尝试模拟Ardent包正在做什么.这是在保存之前验证模型.

我创造了这个BaseModel(根据Laravel Testing decoded书).并添加了此代码:

class BaseModel extends Eloquent {
    protected static $rules = [];
    public $errors = [];

    public function validate(){
        $v = Validator::make($this->attributes, static::$rules);

        if($v->passes()) {
            return true;
        }

        $this->errors = $v->messages();

        return false;
    }

    public static function boot(){
        parent::boot();
        static::saving(function($model){
            if($model->validate() === true){
                foreach ($model->attributes as $key => $value) {
                    if(preg_match("/[a-zA-Z]+_confirmation/", $key)){
                        array_splice($model->attributes, array_search($key, array_keys($model->attributes)), 1);
                    }
                }
                echo "test"; //This is for debugging if this event is fired or not
                return true;
            } else {
                return false;
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,这是我的Post模型:

class Post extends BaseModel {
    public static $rules = array(
            'body' => 'required',
            'user_id' => 'required',
        );

}
Run Code Online (Sandbox Code Playgroud)

在这个测试中,我期待它失败.相反,它通过了!,$post->save()返回true!

class PostTest extends TestCase {

    public function testSavingPost(){
        $post = new Post();
        $this->assertFalse($post->save());
    }
}
Run Code Online (Sandbox Code Playgroud)

当我试图echosaving活动中发表声明时.它没有出现,所以我明白我的定义saving事件没有被调用.我不知道为什么.

awe*_*wei 7

看看这个讨论:https://github.com/laravel/framework/issues/1181

您可能需要在测试中重新注册您的活动.

class PostTest extends TestCase {

    public function setUp()
    {
        parent::setUp();

        // add this to remove all event listeners
        Post::flushEventListeners();
        // reboot the static to reattach listeners
        Post::boot();
    }

    public function testSavingPost(){
        $post = new Post();
        $this->assertFalse($post->save());
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,更好的是,您应该将引导函数中的事件注册功能提取到公共静态方法中:

  class Post extends Model {

       protected static boot()
       {
           parent::boot();
           static::registerEventListeners();
       }

       protected static registerEventListeners()
       {
           static::saving(...);
           static::creating(...);
           ...etc.
       }

  }
Run Code Online (Sandbox Code Playgroud)

然后调用Post :: flushEventListeners(); 邮政:: registerEventListeners(); 在setUp()测试方法中.


r15*_*h13 2

保存事件对我来说看起来不错。验证失败,因此$post->save()返回 false。您的测试通过了,因为您期望$post->save()结果为 false ( assertFalse),在本例中这是正确的。请尝试这些测试。

public function testSavingInvalidPost() {
    $post = new Post();
    $this->assertFalse($post->save());
}

public function testSavingValidPost() {
    $post = new Post();
    $post->body = 'Content';
    $post->user_id = 1;
    $this->assertTrue($post->save());
}
Run Code Online (Sandbox Code Playgroud)