9
9
use Illuminate \Support \Facades \Event ;
10
10
use Illuminate \Support \Facades \Pipeline ;
11
11
use Orchestra \Testbench \TestCase ;
12
+ use PHPUnit \Framework \Attributes \DataProvider ;
12
13
13
14
class PipelineTransactionTest extends TestCase
14
15
{
15
16
public function testPipelineTransaction ()
16
17
{
17
18
Event::fake ();
18
19
19
- $ result = Pipeline::withinTransactions ()
20
+ $ result = Pipeline::withinTransaction ()
20
21
->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
+ ])
24
26
->thenReturn ();
25
27
26
28
$ 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
+ });
29
62
}
30
63
31
64
public function testExceptionThrownRollsBackTransaction ()
@@ -34,7 +67,7 @@ public function testExceptionThrownRollsBackTransaction()
34
67
35
68
$ finallyRan = false ;
36
69
try {
37
- Pipeline::withinTransactions ()
70
+ Pipeline::withinTransaction ()
38
71
->send ('some string ' )
39
72
->through ([
40
73
function ($ value , $ next ) {
@@ -54,3 +87,8 @@ function ($value, $next) {
54
87
Event::assertDispatched (TransactionRolledBack::class);
55
88
}
56
89
}
90
+
91
+ enum EnumForPipelineTransactionTest: string
92
+ {
93
+ case DEFAULT = 'testing ' ;
94
+ }
0 commit comments