The issue you're encountering is likely due to the fact that the Event::create() method is being mocked, and the exception is not being thrown as expected. This means that the transaction is not being rolled back because the exception is not actually occurring within the transaction block.
To ensure that the exception is thrown and the transaction is rolled back, you need to make sure that the mock is correctly set up to throw the exception when Event::create() is called. Here's a step-by-step solution to address this:
-
Ensure the Mock is Correctly Set Up: Double-check that the mock is properly configured to throw an exception when
Event::create()is called. You might want to useshouldReceiveinstead ofexpectsto ensure the method is mocked correctly. -
Verify the Exception is Thrown: Make sure that the exception is actually being thrown during the test execution. You can add a temporary
dd()orLog::info()statement in the catch block to verify that the code is reaching there. -
Check the Test Setup: Ensure that the test is correctly set up to swap the
Eventclass with the mock. Theinstancemethod should be correctly replacing theEventclass in the container.
Here's an updated version of your test with some adjustments:
#[Test]
public function rolls_back_transaction_on_failure(): void
{
$user = User::factory()->create();
$this->instance(
Event::class,
Mockery::mock(Event::class, function (MockInterface $mock) {
$mock->shouldReceive('create')->andThrow(new \Exception('Test exception'));
})
);
Livewire::actingAs($user)
->test(Start::class)
->set('state.organization_name', 'Test Organization')
->set('state.event_name', 'Test Event')
->set('state.event_start_date', '2025-01-01 09:00:00')
->call('createOrganization')
->assertHasErrors(['createOrganization' => 'Test exception']);
// Assert nothing was created due to rollback
$this->assertDatabaseCount('organizations', 0);
$this->assertDatabaseCount('events', 0);
$this->assertNull($user->refresh()->current_organization);
$this->assertNull($user->refresh()->current_event);
}
-
Debugging: If the issue persists, add logging or debugging statements in your Livewire component to ensure that the exception is being caught and the rollback is being triggered.
-
Check the Livewire Component: Ensure that the
createOrganizationmethod in your Livewire component is correctly handling the exception and performing the rollback.
By following these steps, you should be able to confirm that the transaction is rolled back when an exception is thrown during the Event::create() call. If the problem persists, consider checking the Laravel documentation or forums for any known issues with mocking and transactions in your specific Laravel version.