Skip to content

Add way to call hooks for tests running in coroutines#7151

Merged
limingxinleo merged 3 commits intohyperf:masterfrom
marcoskubis:patch-1
Nov 20, 2024
Merged

Add way to call hooks for tests running in coroutines#7151
limingxinleo merged 3 commits intohyperf:masterfrom
marcoskubis:patch-1

Conversation

@marcoskubis
Copy link
Copy Markdown
Contributor

I know that is not the best way to to this, but this is just a suggestion.

While trying to run tests inside database transactions the setUp and tearDown hook does not work becouse the test is runing in a coroutine implemented by the RunTestsInCoroutine trait.

Adding these hooks we can do this:

use Hyperf\DbConnection\Db;

trait DatabaseTransactions
{

    public function beforeTestInCoroutine(): void
    {
        Db::beginTransaction();
    }

    public function afterTestInCoroutine(): void
    {
        Db::rollBack();
    }
}

And add the trait for each file test:

class ExampleTest extends TestCase
{
    use DatabaseTransactions;

    public function test_example(): void
    {
        // perform some insert operation
    }
}

How about that? What do you all think?

@limingxinleo limingxinleo merged commit f53c193 into hyperf:master Nov 20, 2024
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.

2 participants