-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PHPLIB-373 Use DatabaseTransactionsManager
in ManagesTransactions
trait
#3443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+291
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
<?php | ||
|
||
namespace MongoDB\Laravel\Tests\Ticket; | ||
|
||
use Closure; | ||
use Exception; | ||
use Illuminate\Contracts\Database\ConcurrencyErrorDetector; | ||
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit; | ||
use Illuminate\Database\Connection; | ||
use Illuminate\Database\Events\TransactionBeginning; | ||
use Illuminate\Database\Events\TransactionCommitted; | ||
use Illuminate\Database\Events\TransactionCommitting; | ||
use Illuminate\Database\Events\TransactionRolledBack; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Event; | ||
use MongoDB\Driver\Exception\RuntimeException; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
use Throwable; | ||
|
||
use function event; | ||
|
||
/** | ||
* @see https://github.com/mongodb/laravel-mongodb/issues/3328 | ||
* @see https://jira.mongodb.org/browse/PHPORM-373 | ||
*/ | ||
class GH3328Test extends TestCase | ||
{ | ||
public function testAfterCommitOnSuccessfulTransaction(): void | ||
{ | ||
$callback = static function (): void { | ||
event(new RegularEvent()); | ||
event(new AfterCommitEvent()); | ||
}; | ||
|
||
$assert = function (): void { | ||
Event::assertDispatchedTimes(BeforeTransactionEvent::class); | ||
Event::assertDispatchedTimes(RegularEvent::class); | ||
Event::assertDispatchedTimes(AfterCommitEvent::class); | ||
|
||
Event::assertDispatched(TransactionBeginning::class); | ||
Event::assertDispatched(TransactionCommitting::class); | ||
Event::assertDispatched(TransactionCommitted::class); | ||
}; | ||
|
||
$this->assertTransactionCallbackResult($callback, $assert); | ||
} | ||
|
||
public function testAfterCommitOnFailedTransaction(): void | ||
{ | ||
$this->app->bind(ConcurrencyErrorDetector::class, FakeConcurrencyErrorDetector::class); | ||
|
||
$callback = static function (): void { | ||
event(new RegularEvent()); | ||
event(new AfterCommitEvent()); | ||
|
||
// Transaction failed; after commit event should not be dispatched | ||
throw new FakeException(); | ||
}; | ||
|
||
$assert = function (): void { | ||
Event::assertDispatchedTimes(BeforeTransactionEvent::class, 3); | ||
Event::assertDispatchedTimes(RegularEvent::class, 3); | ||
|
||
Event::assertDispatchedTimes(TransactionBeginning::class, 3); | ||
Event::assertDispatched(TransactionRolledBack::class); | ||
Event::assertNotDispatched(TransactionCommitting::class); | ||
Event::assertNotDispatched(TransactionCommitted::class); | ||
}; | ||
|
||
$this->assertTransactionCallbackResult($callback, $assert, 3); | ||
} | ||
|
||
public function testAfterCommitOnSuccessfulManualTransaction(): void | ||
{ | ||
$callback = function (): void { | ||
event(new RegularEvent()); | ||
event(new AfterCommitEvent()); | ||
}; | ||
|
||
$assert = function (): void { | ||
Event::assertDispatchedTimes(BeforeTransactionEvent::class); | ||
Event::assertDispatchedTimes(RegularEvent::class); | ||
Event::assertDispatchedTimes(AfterCommitEvent::class); | ||
|
||
Event::assertDispatched(TransactionBeginning::class); | ||
Event::assertNotDispatched(TransactionRolledBack::class); | ||
Event::assertDispatched(TransactionCommitting::class); | ||
Event::assertDispatched(TransactionCommitted::class); | ||
}; | ||
|
||
$this->assertTransactionResult($callback, $assert); | ||
} | ||
|
||
public function testAfterCommitOnFailedManualTransaction(): void | ||
{ | ||
$callback = function (): void { | ||
event(new RegularEvent()); | ||
event(new AfterCommitEvent()); | ||
|
||
throw new FakeException(); | ||
}; | ||
|
||
$assert = function (): void { | ||
Event::assertDispatchedTimes(BeforeTransactionEvent::class); | ||
Event::assertDispatchedTimes(RegularEvent::class); | ||
Event::assertNotDispatched(AfterCommitEvent::class); | ||
|
||
Event::assertDispatched(TransactionBeginning::class); | ||
Event::assertDispatched(TransactionRolledBack::class); | ||
Event::assertNotDispatched(TransactionCommitting::class); | ||
Event::assertNotDispatched(TransactionCommitted::class); | ||
}; | ||
|
||
$this->assertTransactionResult($callback, $assert); | ||
} | ||
|
||
private function assertTransactionCallbackResult(Closure $callback, Closure $assert, ?int $attempts = 1): void | ||
{ | ||
$this->assertCallbackResultForConnection( | ||
DB::connection('sqlite'), | ||
$callback, | ||
$assert, | ||
$attempts, | ||
); | ||
|
||
$this->assertCallbackResultForConnection( | ||
DB::connection('mongodb'), | ||
$callback, | ||
$assert, | ||
$attempts, | ||
); | ||
} | ||
|
||
/** | ||
* Ensure equal transaction behavior between SQLite (handled by Laravel) and MongoDB | ||
*/ | ||
private function assertCallbackResultForConnection(Connection $connection, Closure $callback, Closure $assertions, int $attempts): void | ||
{ | ||
$fake = Event::fake(); | ||
$connection->setEventDispatcher($fake); | ||
$connection->beforeStartingTransaction(function () { | ||
event(new BeforeTransactionEvent()); | ||
}); | ||
|
||
try { | ||
$connection->transaction($callback, $attempts); | ||
} catch (Exception) { | ||
} | ||
|
||
$assertions(); | ||
} | ||
|
||
private function assertTransactionResult(Closure $callback, Closure $assert): void | ||
{ | ||
$this->assertManualResultForConnection( | ||
DB::connection('sqlite'), | ||
$callback, | ||
$assert, | ||
); | ||
|
||
$this->assertManualResultForConnection( | ||
DB::connection('mongodb'), | ||
$callback, | ||
$assert, | ||
); | ||
} | ||
|
||
/** | ||
* Ensure equal transaction behavior between SQLite (handled by Laravel) and MongoDB | ||
*/ | ||
private function assertManualResultForConnection(Connection $connection, Closure $callback, Closure $assert): void | ||
{ | ||
$fake = Event::fake(); | ||
$connection->setEventDispatcher($fake); | ||
|
||
$connection->beforeStartingTransaction(function () { | ||
event(new BeforeTransactionEvent()); | ||
}); | ||
|
||
$connection->beginTransaction(); | ||
|
||
try { | ||
$callback(); | ||
$connection->commit(); | ||
} catch (Exception) { | ||
$connection->rollBack(); | ||
} | ||
|
||
$assert(); | ||
} | ||
} | ||
|
||
class AfterCommitEvent implements ShouldDispatchAfterCommit | ||
{ | ||
use Dispatchable; | ||
} | ||
|
||
class BeforeTransactionEvent | ||
{ | ||
use Dispatchable; | ||
} | ||
class RegularEvent | ||
{ | ||
use Dispatchable; | ||
} | ||
class FakeException extends RuntimeException | ||
{ | ||
public function __construct() | ||
{ | ||
$this->errorLabels = ['TransientTransactionError']; | ||
} | ||
} | ||
|
||
class FakeConcurrencyErrorDetector implements ConcurrencyErrorDetector | ||
{ | ||
public function causedByConcurrencyError(Throwable $e): bool | ||
{ | ||
return true; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.