EloquentModelWithoutEventsTest.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Schema;
  6. class EloquentModelWithoutEventsTest extends DatabaseTestCase
  7. {
  8. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  9. {
  10. Schema::create('auto_filled_models', function (Blueprint $table) {
  11. $table->increments('id');
  12. $table->text('project')->nullable();
  13. });
  14. }
  15. public function testWithoutEventsRegistersBootedListenersForLater()
  16. {
  17. $model = AutoFilledModel::withoutEvents(function () {
  18. return AutoFilledModel::create();
  19. });
  20. $this->assertNull($model->project);
  21. $model->save();
  22. $this->assertSame('Laravel', $model->project);
  23. }
  24. }
  25. class AutoFilledModel extends Model
  26. {
  27. public $table = 'auto_filled_models';
  28. public $timestamps = false;
  29. protected $guarded = [];
  30. public static function boot()
  31. {
  32. parent::boot();
  33. static::saving(function ($model) {
  34. $model->project = 'Laravel';
  35. });
  36. }
  37. }