Skip to content
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

[11.x] Skip the number of connections transacting while testing to run callbacks #53377

Merged

Conversation

tonysm
Copy link
Contributor

@tonysm tonysm commented Nov 1, 2024

Changed

  • Skip the number of connections transacting on tests to run callbacks correctly

The testing DatabaseTransactionsManager was hardcoding the number of
connections to skip as 1. In a multi-db context, it must skip the same
number of connections transacting defined on the tests.

related PRs: #48523
resolves #53334

…acks

The testing DatabaseTransactionsManager was hardcoding the number of
connections to skip as 1. In a multi-db context, it must skip the same
number of connections transacting defined on the tests.
@tonysm
Copy link
Contributor Author

tonysm commented Nov 1, 2024

@crynobone could you take a look at the changes here as well, pls? The mentioned discussion has some examples:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    protected array $connectionsToTransact = ['sqlite', 'second'];

    use RefreshDatabase;

    public function test_with_nested_transaction_on_default_connection(): void
    {
        $executed = false;

        // Works as expected...
        DB::transaction(function () use (&$executed) {
            DB::afterCommit(function () use (&$executed) {
                $executed = true;
            });
        });

        $this->assertTrue($executed); // SUCCEEDS!
    }

    public function test_without_nested_transaction_on_default_connection(): void
    {
        $executed = false;

        // Should execute right away, since there are no application-level 
        // transactions (only the two from the tests), but it doesn't...
        DB::afterCommit(function () use (&$executed) {
            $executed = true;
        });
        
        $this->assertTrue($executed); // FAILS!
    }
}

@tonysm tonysm marked this pull request as draft November 4, 2024 20:58
@tonysm tonysm marked this pull request as ready for review November 5, 2024 05:36
@tonysm tonysm requested a review from crynobone November 5, 2024 05:36
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
@crynobone
Copy link
Member

@tonysm I added EloquentTransactionWithAfterCommitUsingRefreshDatabaseOnMultipleConnectionsTest

Wondering if there any good tests we can add to cover using multiple database connection.

crynobone and others added 3 commits November 5, 2024 14:51
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
@tonysm
Copy link
Contributor Author

tonysm commented Nov 5, 2024

@crynobone I've added some tests, can't think of any other useful examples

@taylorotwell taylorotwell merged commit 4cd413b into laravel:11.x Nov 11, 2024
31 checks passed
@tonysm tonysm deleted the tm/multi-db-transactions-callbacks-on-tests branch November 12, 2024 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants