Skip to content

Commit c237b22

Browse files
authored
[12.x] Pipeline::withinTransaction() accepts connection (#56550)
* pipeline transaction accepts connection * tests * Update PipelineTransactionTest.php * fix up
1 parent ae7a3da commit c237b22

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

src/Illuminate/Pipeline/Pipeline.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Pipeline implements PipelineContract
5151
/**
5252
* Indicates whether to wrap the pipeline in a database transaction.
5353
*
54-
* @var bool
54+
* @var string|null|\UnitEnum|false
5555
*/
5656
protected $withinTransaction = false;
5757

@@ -130,8 +130,8 @@ public function then(Closure $destination)
130130
);
131131

132132
try {
133-
return $this->withinTransaction
134-
? $this->getContainer()->make('db')->transaction(fn () => $pipeline($this->passable))
133+
return $this->withinTransaction !== false
134+
? $this->getContainer()->make('db')->connection($this->withinTransaction)->transaction(fn () => $pipeline($this->passable))
135135
: $pipeline($this->passable);
136136
} finally {
137137
if ($this->finally) {
@@ -257,10 +257,10 @@ protected function pipes()
257257
/**
258258
* Execute each pipeline step within a database transaction.
259259
*
260-
* @param bool $withinTransaction
260+
* @param string|null|\UnitEnum|false $withinTransaction
261261
* @return $this
262262
*/
263-
public function withinTransaction(bool $withinTransaction = true)
263+
public function withinTransaction($withinTransaction = null)
264264
{
265265
$this->withinTransaction = $withinTransaction;
266266

tests/Pipeline/PipelineTransactionTest.php

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,56 @@
99
use Illuminate\Support\Facades\Event;
1010
use Illuminate\Support\Facades\Pipeline;
1111
use Orchestra\Testbench\TestCase;
12+
use PHPUnit\Framework\Attributes\DataProvider;
1213

1314
class PipelineTransactionTest extends TestCase
1415
{
1516
public function testPipelineTransaction()
1617
{
1718
Event::fake();
1819

19-
$result = Pipeline::withinTransactions()
20+
$result = Pipeline::withinTransaction()
2021
->send('some string')
21-
->through([function ($value, $next) {
22-
return $next($value);
23-
}])
22+
->through([
23+
fn ($value, $next) => $next($value),
24+
fn ($value, $next) => $next($value),
25+
])
2426
->thenReturn();
2527

2628
$this->assertEquals('some string', $result);
27-
Event::assertDispatched(TransactionBeginning::class);
28-
Event::assertDispatched(TransactionCommitted::class);
29+
Event::assertDispatchedTimes(TransactionBeginning::class, 1);
30+
Event::assertDispatchedTimes(TransactionCommitted::class, 1);
31+
}
32+
33+
public static function transactionConnectionDataProvider(): array
34+
{
35+
return [
36+
'unit enum' => [EnumForPipelineTransactionTest::DEFAULT, 'testing'],
37+
'string' => ['testing', 'testing'],
38+
'null' => [null, 'testing2'],
39+
];
40+
}
41+
42+
#[DataProvider('transactionConnectionDataProvider')]
43+
public function testConnection($connection, $connectionName)
44+
{
45+
Event::fake();
46+
config(['database.connections.testing2' => config('database.connections.testing')]);
47+
config(['database.default' => 'testing2']);
48+
49+
$result = Pipeline::withinTransaction($connection)
50+
->send('some string')
51+
->through([
52+
function ($value, $next) {
53+
return $next($value);
54+
}
55+
])
56+
->thenReturn();
57+
58+
$this->assertEquals('some string', $result);
59+
Event::dispatched(TransactionBeginning::class, function(TransactionBeginning $event) use ($connectionName) {
60+
return $event->connection === $connectionName;
61+
});
2962
}
3063

3164
public function testExceptionThrownRollsBackTransaction()
@@ -34,7 +67,7 @@ public function testExceptionThrownRollsBackTransaction()
3467

3568
$finallyRan = false;
3669
try {
37-
Pipeline::withinTransactions()
70+
Pipeline::withinTransaction()
3871
->send('some string')
3972
->through([
4073
function ($value, $next) {
@@ -54,3 +87,8 @@ function ($value, $next) {
5487
Event::assertDispatched(TransactionRolledBack::class);
5588
}
5689
}
90+
91+
enum EnumForPipelineTransactionTest: string
92+
{
93+
case DEFAULT = 'testing';
94+
}

0 commit comments

Comments
 (0)