22
33namespace  Illuminate \Tests \Integration \Database \EloquentModelCustomEventsTest ;
44
5+ use  Illuminate \Database \Eloquent \Attributes \ObservedBy ;
56use  Illuminate \Database \Eloquent \Model ;
67use  Illuminate \Database \Schema \Blueprint ;
78use  Illuminate \Support \Facades \Event ;
@@ -24,6 +25,11 @@ protected function afterRefreshingDatabase()
2425        Schema::create ('test_model1 ' , function  (Blueprint   $ table ) {
2526            $ table ->increments ('id ' );
2627        });
28+ 
29+         Schema::create ('eloquent_model_stub_with_custom_event_from_traits ' , function  (Blueprint   $ table ) {
30+             $ table ->boolean ('custom_attribute ' );
31+             $ table ->boolean ('observer_attribute ' );
32+         });
2733    }
2834
2935    public  function  testFlushListenersClearsCustomEvents ()
@@ -45,6 +51,20 @@ public function testCustomEventListenersAreFired()
4551
4652        $ this  ->assertTrue ($ _SERVER  ['fired_event ' ]);
4753    }
54+ 
55+     public  function  testAddObservableEventFromTrait ()
56+     {
57+         $ model  = new  EloquentModelStubWithCustomEventFromTrait ();
58+ 
59+         $ this  ->assertNull ($ model ->custom_attribute );
60+         $ this  ->assertNull ($ model ->observer_attribute );
61+ 
62+         $ model ->completeCustomAction ();
63+ 
64+         $ this  ->assertTrue ($ model ->custom_attribute );
65+         $ this  ->assertTrue ($ model ->observer_attribute );
66+     }
67+ 
4868}
4969
5070class  TestModel1 extends  Model
@@ -59,3 +79,35 @@ class CustomEvent
5979{
6080    // 
6181}
82+ 
83+ trait  CustomEventTrait
84+ {
85+     public  function  completeCustomAction ()
86+     {
87+         $ this  ->custom_attribute  = true ;
88+ 
89+         $ this  ->fireModelEvent ('customEvent ' );
90+     }
91+ 
92+     public  function  initializeCustomEventTrait ()
93+     {
94+         $ this  ->addObservableEvents ([
95+             'customEvent ' ,
96+         ]);
97+     }
98+ }
99+ 
100+ class  CustomObserver
101+ {
102+     public  function  customEvent (EloquentModelStubWithCustomEventFromTrait   $ model )
103+     {
104+         $ model ->observer_attribute  = true ;
105+     }
106+ }
107+ 
108+ #[ObservedBy(CustomObserver::class)]
109+ class  EloquentModelStubWithCustomEventFromTrait extends  Model {
110+     use  CustomEventTrait;
111+ 
112+     public  $ timestamps  = false ;
113+ }
0 commit comments