|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Laravel\Tests\Ticket; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Exception; |
| 7 | +use Illuminate\Contracts\Events\ShouldDispatchAfterCommit; |
| 8 | +use Illuminate\Database\Connection; |
| 9 | +use Illuminate\Database\Events\TransactionBeginning; |
| 10 | +use Illuminate\Database\Events\TransactionCommitted; |
| 11 | +use Illuminate\Database\Events\TransactionCommitting; |
| 12 | +use Illuminate\Database\Events\TransactionRolledBack; |
| 13 | +use Illuminate\Foundation\Events\Dispatchable; |
| 14 | +use Illuminate\Support\Facades\DB; |
| 15 | +use Illuminate\Support\Facades\Event; |
| 16 | +use MongoDB\Driver\Exception\RuntimeException; |
| 17 | +use MongoDB\Laravel\Tests\TestCase; |
| 18 | +use Throwable; |
| 19 | + |
| 20 | +use function event; |
| 21 | +use function interface_exists; |
| 22 | + |
| 23 | +/** |
| 24 | + * @see https://github.com/mongodb/laravel-mongodb/issues/3328 |
| 25 | + * @see https://jira.mongodb.org/browse/PHPORM-373 |
| 26 | + */ |
| 27 | +class GH3328Test extends TestCase |
| 28 | +{ |
| 29 | + public function testAfterCommitOnSuccessfulTransaction(): void |
| 30 | + { |
| 31 | + $callback = static function (): void { |
| 32 | + event(new RegularEvent()); |
| 33 | + event(new AfterCommitEvent()); |
| 34 | + }; |
| 35 | + |
| 36 | + $assert = function (): void { |
| 37 | + Event::assertDispatchedTimes(BeforeTransactionEvent::class); |
| 38 | + Event::assertDispatchedTimes(RegularEvent::class); |
| 39 | + Event::assertDispatchedTimes(AfterCommitEvent::class); |
| 40 | + |
| 41 | + Event::assertDispatched(TransactionBeginning::class); |
| 42 | + Event::assertDispatched(TransactionCommitting::class); |
| 43 | + Event::assertDispatched(TransactionCommitted::class); |
| 44 | + }; |
| 45 | + |
| 46 | + $this->assertTransactionCallbackResult($callback, $assert); |
| 47 | + } |
| 48 | + |
| 49 | + public function testAfterCommitOnFailedTransaction(): void |
| 50 | + { |
| 51 | + $callback = static function (): void { |
| 52 | + event(new RegularEvent()); |
| 53 | + event(new AfterCommitEvent()); |
| 54 | + |
| 55 | + // Transaction failed; after commit event should not be dispatched |
| 56 | + throw new Fake(); |
| 57 | + }; |
| 58 | + |
| 59 | + $assert = function (): void { |
| 60 | + Event::assertDispatchedTimes(BeforeTransactionEvent::class, 3); |
| 61 | + Event::assertDispatchedTimes(RegularEvent::class, 3); |
| 62 | + |
| 63 | + Event::assertDispatchedTimes(TransactionBeginning::class, 3); |
| 64 | + Event::assertDispatched(TransactionRolledBack::class); |
| 65 | + Event::assertNotDispatched(TransactionCommitting::class); |
| 66 | + Event::assertNotDispatched(TransactionCommitted::class); |
| 67 | + }; |
| 68 | + |
| 69 | + $this->assertCallbackResultForConnection( |
| 70 | + DB::connection('mongodb'), |
| 71 | + $callback, |
| 72 | + $assert, |
| 73 | + 3, |
| 74 | + ); |
| 75 | + |
| 76 | + if (! interface_exists('\Illuminate\Contracts\Database\ConcurrencyErrorDetector')) { |
| 77 | + // Earlier versions of Laravel use a trait instead of DI to detect concurrency errors |
| 78 | + // That would increase the scope of this comparison dramatically and is probably not worth it. |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // phpcs:ignore |
| 83 | + $this->app->bind(\Illuminate\Contracts\Database\ConcurrencyErrorDetector::class, FakeConcurrencyErrorDetector::class); |
| 84 | + |
| 85 | + $this->assertCallbackResultForConnection( |
| 86 | + DB::connection('sqlite'), |
| 87 | + $callback, |
| 88 | + $assert, |
| 89 | + 3, |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + public function testAfterCommitOnSuccessfulManualTransaction(): void |
| 94 | + { |
| 95 | + $callback = function (): void { |
| 96 | + event(new RegularEvent()); |
| 97 | + event(new AfterCommitEvent()); |
| 98 | + }; |
| 99 | + |
| 100 | + $assert = function (): void { |
| 101 | + Event::assertDispatchedTimes(BeforeTransactionEvent::class); |
| 102 | + Event::assertDispatchedTimes(RegularEvent::class); |
| 103 | + Event::assertDispatchedTimes(AfterCommitEvent::class); |
| 104 | + |
| 105 | + Event::assertDispatched(TransactionBeginning::class); |
| 106 | + Event::assertNotDispatched(TransactionRolledBack::class); |
| 107 | + Event::assertDispatched(TransactionCommitting::class); |
| 108 | + Event::assertDispatched(TransactionCommitted::class); |
| 109 | + }; |
| 110 | + |
| 111 | + $this->assertTransactionResult($callback, $assert); |
| 112 | + } |
| 113 | + |
| 114 | + public function testAfterCommitOnFailedManualTransaction(): void |
| 115 | + { |
| 116 | + $callback = function (): void { |
| 117 | + event(new RegularEvent()); |
| 118 | + event(new AfterCommitEvent()); |
| 119 | + |
| 120 | + throw new Fake(); |
| 121 | + }; |
| 122 | + |
| 123 | + $assert = function (): void { |
| 124 | + Event::assertDispatchedTimes(BeforeTransactionEvent::class); |
| 125 | + Event::assertDispatchedTimes(RegularEvent::class); |
| 126 | + Event::assertNotDispatched(AfterCommitEvent::class); |
| 127 | + |
| 128 | + Event::assertDispatched(TransactionBeginning::class); |
| 129 | + Event::assertDispatched(TransactionRolledBack::class); |
| 130 | + Event::assertNotDispatched(TransactionCommitting::class); |
| 131 | + Event::assertNotDispatched(TransactionCommitted::class); |
| 132 | + }; |
| 133 | + |
| 134 | + $this->assertTransactionResult($callback, $assert); |
| 135 | + } |
| 136 | + |
| 137 | + private function assertTransactionCallbackResult(Closure $callback, Closure $assert, ?int $attempts = 1): void |
| 138 | + { |
| 139 | + $this->assertCallbackResultForConnection( |
| 140 | + DB::connection('sqlite'), |
| 141 | + $callback, |
| 142 | + $assert, |
| 143 | + $attempts, |
| 144 | + ); |
| 145 | + |
| 146 | + $this->assertCallbackResultForConnection( |
| 147 | + DB::connection('mongodb'), |
| 148 | + $callback, |
| 149 | + $assert, |
| 150 | + $attempts, |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Ensure equal transaction behavior between SQLite (handled by Laravel) and MongoDB |
| 156 | + */ |
| 157 | + private function assertCallbackResultForConnection(Connection $connection, Closure $callback, Closure $assertions, int $attempts): void |
| 158 | + { |
| 159 | + $fake = Event::fake(); |
| 160 | + $connection->setEventDispatcher($fake); |
| 161 | + $connection->beforeStartingTransaction(function () { |
| 162 | + event(new BeforeTransactionEvent()); |
| 163 | + }); |
| 164 | + |
| 165 | + try { |
| 166 | + $connection->transaction($callback, $attempts); |
| 167 | + } catch (Exception) { |
| 168 | + } |
| 169 | + |
| 170 | + $assertions(); |
| 171 | + } |
| 172 | + |
| 173 | + private function assertTransactionResult(Closure $callback, Closure $assert): void |
| 174 | + { |
| 175 | + $this->assertManualResultForConnection( |
| 176 | + DB::connection('sqlite'), |
| 177 | + $callback, |
| 178 | + $assert, |
| 179 | + ); |
| 180 | + |
| 181 | + $this->assertManualResultForConnection( |
| 182 | + DB::connection('mongodb'), |
| 183 | + $callback, |
| 184 | + $assert, |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Ensure equal transaction behavior between SQLite (handled by Laravel) and MongoDB |
| 190 | + */ |
| 191 | + private function assertManualResultForConnection(Connection $connection, Closure $callback, Closure $assert): void |
| 192 | + { |
| 193 | + $fake = Event::fake(); |
| 194 | + $connection->setEventDispatcher($fake); |
| 195 | + |
| 196 | + $connection->beforeStartingTransaction(function () { |
| 197 | + event(new BeforeTransactionEvent()); |
| 198 | + }); |
| 199 | + |
| 200 | + $connection->beginTransaction(); |
| 201 | + |
| 202 | + try { |
| 203 | + $callback(); |
| 204 | + $connection->commit(); |
| 205 | + } catch (Exception) { |
| 206 | + $connection->rollBack(); |
| 207 | + } |
| 208 | + |
| 209 | + $assert(); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +class AfterCommitEvent implements ShouldDispatchAfterCommit |
| 214 | +{ |
| 215 | + use Dispatchable; |
| 216 | +} |
| 217 | + |
| 218 | +class BeforeTransactionEvent |
| 219 | +{ |
| 220 | + use Dispatchable; |
| 221 | +} |
| 222 | +class RegularEvent |
| 223 | +{ |
| 224 | + use Dispatchable; |
| 225 | +} |
| 226 | +class Fake extends RuntimeException |
| 227 | +{ |
| 228 | + public function __construct() |
| 229 | + { |
| 230 | + $this->errorLabels = ['TransientTransactionError']; |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +if (interface_exists('\Illuminate\Contracts\Database\ConcurrencyErrorDetector')) { |
| 235 | + // phpcs:ignore |
| 236 | + class FakeConcurrencyErrorDetector implements \Illuminate\Contracts\Database\ConcurrencyErrorDetector |
| 237 | + { |
| 238 | + public function causedByConcurrencyError(Throwable $e): bool |
| 239 | + { |
| 240 | + return true; |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments