Skip to content

Commit 044cb93

Browse files
committed
Add failing test
1 parent 0b96d9b commit 044cb93

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/Integration/Database/EloquentModelCustomEventsTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Integration\Database\EloquentModelCustomEventsTest;
44

5+
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Database\Schema\Blueprint;
78
use 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

5070
class 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

Comments
 (0)