Skip to content

Commit 16da841

Browse files
[11.x] Add support for middlewares & failed handler on broadcastable events (#54562)
* [11.x] Add support for broadcasting middlewares & failed handler via proxy call * CS fix * CS fix * CS fix * Update BroadcastEvent.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 571853b commit 16da841

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Illuminate/Broadcasting/BroadcastEvent.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Arr;
1010
use ReflectionClass;
1111
use ReflectionProperty;
12+
use Throwable;
1213

1314
class BroadcastEvent implements ShouldQueue
1415
{
@@ -170,6 +171,35 @@ protected function getConnectionPayload($payload, $connection)
170171
return $connectionPayload;
171172
}
172173

174+
/**
175+
* Get the middleware for the underlying event.
176+
*
177+
* @return array<int, object>
178+
*/
179+
public function middleware(): array
180+
{
181+
if (! method_exists($this->event, 'middleware')) {
182+
return [];
183+
}
184+
185+
return $this->event->middleware();
186+
}
187+
188+
/**
189+
* Handle a job failure.
190+
*
191+
* @param \Throwable $e
192+
* @return void
193+
*/
194+
public function failed(?Throwable $e = null): void
195+
{
196+
if (! method_exists($this->event, 'failed')) {
197+
return;
198+
}
199+
200+
$this->event->failed($e);
201+
}
202+
173203
/**
174204
* Get the display name for the queued job.
175205
*

tests/Broadcasting/BroadcastEventTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Illuminate\Tests\Broadcasting;
44

5+
use Exception;
56
use Illuminate\Broadcasting\BroadcastEvent;
67
use Illuminate\Broadcasting\InteractsWithBroadcasting;
78
use Illuminate\Contracts\Broadcasting\Broadcaster;
89
use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory;
910
use Mockery as m;
1011
use PHPUnit\Framework\TestCase;
12+
use Throwable;
1113

1214
class BroadcastEventTest extends TestCase
1315
{
@@ -86,6 +88,39 @@ public function testSpecificChannelsPerConnection()
8688

8789
(new BroadcastEvent($event))->handle($manager);
8890
}
91+
92+
public function testMiddlewareProxiesMiddlewareFromUnderlyingEvent()
93+
{
94+
$event = new class
95+
{
96+
public function middleware(): array
97+
{
98+
return ['foo', 'bar'];
99+
}
100+
};
101+
102+
$job = new BroadcastEvent($event);
103+
104+
$this->assertSame(['foo', 'bar'], $job->middleware());
105+
}
106+
107+
public function testMiddlewareProxiesFailedHandlerFromUnderlyingEvent()
108+
{
109+
$event = new class
110+
{
111+
public function failed(?Throwable $e = null): void
112+
{
113+
$e->validateCall();
114+
}
115+
};
116+
117+
$job = new BroadcastEvent($event);
118+
119+
$exception = m::mock(Exception::class);
120+
$exception->expects('validateCall');
121+
122+
$job->failed($exception);
123+
}
89124
}
90125

91126
class TestBroadcastEvent

0 commit comments

Comments
 (0)