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