EloquentModelCustomEventsTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\EloquentModelCustomEventsTest;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Event;
  6. use Illuminate\Support\Facades\Schema;
  7. use Illuminate\Tests\Integration\Database\DatabaseTestCase;
  8. class EloquentModelCustomEventsTest extends DatabaseTestCase
  9. {
  10. protected function setUp(): void
  11. {
  12. parent::setUp();
  13. Event::listen(CustomEvent::class, function () {
  14. $_SERVER['fired_event'] = true;
  15. });
  16. }
  17. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  18. {
  19. Schema::create('test_model1', function (Blueprint $table) {
  20. $table->increments('id');
  21. });
  22. }
  23. public function testFlushListenersClearsCustomEvents()
  24. {
  25. $_SERVER['fired_event'] = false;
  26. TestModel1::flushEventListeners();
  27. TestModel1::create();
  28. $this->assertFalse($_SERVER['fired_event']);
  29. }
  30. public function testCustomEventListenersAreFired()
  31. {
  32. $_SERVER['fired_event'] = false;
  33. TestModel1::create();
  34. $this->assertTrue($_SERVER['fired_event']);
  35. }
  36. }
  37. class TestModel1 extends Model
  38. {
  39. public $dispatchesEvents = ['created' => CustomEvent::class];
  40. public $table = 'test_model1';
  41. public $timestamps = false;
  42. protected $guarded = [];
  43. }
  44. class CustomEvent
  45. {
  46. //
  47. }