Skip to content

RefreshDatabase + double wrapping in DB::transaction() breaks $afterCommit functionality #48451

Closed
@ryanldy

Description

@ryanldy

Laravel Version

10.23.1

PHP Version

8.2.10

Database Driver & Version

8.0.33

Description

In tests, model observer created does not run if all condition applies:

  1. Test uses mysql DB connection.
  2. Test uses RefreshDatabase trait.
  3. Using createOrFirst() or firstOrCreate() method and the method is inside DB::transaction().

This issue started to appear in laravel/framework 10.21. I think it was introduced in #48144

cc: @tonysm / @mpyw

Steps To Reproduce

  1. Clone TestDBTransaction sample project.
  2. Make sure to update mysql database connection in your .env file.
  3. Run vendor/bin/phpunit. Test should pass but it fails.
  4. Try to comment the lines under Failing Test (line 27 and 30) section and uncomment a line from the Passing Tests (line 35 or 38) in NoteObserverTest. Test now passes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions