flarum / flarum/framework

[2.x] ShouldHandleEventsAfterCommit/ShouldDispatchAfterCommit listeners should be executed after nested transaction is commited during tests

Open
#5,022 0 comments 0 reactions 0 assignees View on GitHub
type/bug
Dominant language
PHP
Stars
6.7k
Forks
883
Avg merge
15h 16m
Merged PRs (30d)
73

Description

### Current Behavior

The tests implemented in https://github.com/flarum/framework/pull/4833 are missing to test the behaviour when the application code creates a transaction in it self (results in a nested transaction).

The fix in https://github.com/flarum/framework/pull/4833 leads to that an event marked with ShouldDispatchAfterCommit is dispatched to early during tests.

### Steps to Reproduce

Extend the AfterCommitListenerTest test case with following code and resolve deps:

```
#[Test]
public function after_commit_event_is_dispatched_after_a_transaction(): void
{
$this->extend(
(new Extend\Event())->listen(AfterCommitDispatchableEvent::class, function () use (&$ran) {
$ran = true;
})
);

$this->connection()->beginTransaction();

$this->app()->getContainer()->make(Dispatcher::class)->dispatch(new AfterCommitDispatchableEvent());

$this->assertFalse(
$ran,
'A ShouldDispatchAfterCommit event should not be dispatched during a nested transaction in a test.'
);

$this->connection()->commit();

$this->assertTrue(
$ran,
'A ShouldDispatchAfterCommit event should be dispatched during an integration test.'
);
}

private function connection(): ConnectionInterface
{
return $this->app()->getContainer()->make(ConnectionInterface::class);
}
```

### Expected Behavior

test should run

### Screenshots

_No response_

### Environment

- Flarum version: 2.0.0-rc.8
- Website URL: http://example.com
- Webserver: [e.g. apache, nginx]
- Hosting environment: [e.g. shared, vps]
- PHP version: x.y.z
- Database: [e.g. MySQL 8.4.11, MariaDB 10.11.2, PostgreSQL 16.1, SQLite 3.45]
- Browser: [e.g. chrome 67, safari 11]

### Output of `php flarum info`

```
Output of "php flarum info", run this in terminal in your Flarum directory.
```

### Possible Solution

_No response_

### Additional Context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the AfterCommitListenerTest test case and run the existing after-commit listener tests after resolving dependencies. Exercise the nested transaction scenario using the Dispatcher and ConnectionInterface entry points shown in the issue; done means the event is not dispatched before the nested commit and is dispatched after it.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.