Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/Illuminate/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,14 @@ protected function createClassCallable($listener)
return $this->createQueuedHandlerCallable($class, $method);
}

return [$this->container->make($class), $method];
$listener = $this->container->make($class);

if ($listener->dispatchAfterCommit ?? null &&
$this->container->bound('db.transactions')) {
return $this->createCallbackForListenerRunningAfterCommits($listener, $method);
}

return [$listener, $method];
}

/**
Expand Down Expand Up @@ -483,6 +490,26 @@ protected function createQueuedHandlerCallable($class, $method)
};
}

/**
* Create a callable for dispatching a listener after database transactions.
*
* @param mixed $listener
* @param string $method
* @return \Closure
*/
protected function createCallbackForListenerRunningAfterCommits($listener, $method)
{
return function () use ($method, $listener) {
$payload = func_get_args();

$this->container->make('db.transactions')->addCallback(
function () use ($listener, $method, $payload) {
$listener->$method(...$payload);
}
);
};
}

/**
* Determine if the event handler wants to be queued.
*
Expand Down
77 changes: 77 additions & 0 deletions tests/Integration/Events/ListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Illuminate\Tests\Integration\Events;

use Illuminate\Support\Facades\Event;
use Mockery as m;
use Orchestra\Testbench\TestCase;

class ListenerTest extends TestCase
{
protected function tearDown(): void
{
ListenerTestListener::$ran = false;
ListenerTestListenerAfterCommit::$ran = false;

m::close();
}

public function testClassListenerRunsNormallyIfNoTransactions()
{
$this->app->singleton('db.transactions', function () {
$transactionManager = m::mock(DatabaseTransactionsManager::class);
$transactionManager->shouldNotReceive('addCallback')->once()->andReturn(null);

return $transactionManager;
});

Event::listen(ListenerTestEvent::class, ListenerTestListener::class);

Event::dispatch(new ListenerTestEvent);

$this->assertTrue(ListenerTestListener::$ran);
}

public function testClassListenerDoesntRunInsideTransaction()
{
$this->app->singleton('db.transactions', function () {
$transactionManager = m::mock(DatabaseTransactionsManager::class);
$transactionManager->shouldReceive('addCallback')->once()->andReturn(null);

return $transactionManager;
});

Event::listen(ListenerTestEvent::class, ListenerTestListenerAfterCommit::class);

Event::dispatch(new ListenerTestEvent);

$this->assertFalse(ListenerTestListenerAfterCommit::$ran);
}
}

class ListenerTestEvent
{
//
}

class ListenerTestListener
{
public static $ran = false;

public function handle()
{
static::$ran = true;
}
}

class ListenerTestListenerAfterCommit
{
public static $ran = false;

public $dispatchAfterCommit = true;

public function handle()
{
static::$ran = true;
}
}