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

Using @depends - Transaction for 2 methods #151

Open
tarach opened this issue Mar 24, 2021 · 10 comments
Open

Using @depends - Transaction for 2 methods #151

tarach opened this issue Mar 24, 2021 · 10 comments

Comments

@tarach
Copy link

tarach commented Mar 24, 2021

Hi, Great package btw.

Does this package allow to reuse the code using @Depends annotation?
For example.

I test resource POST /api/posts this test return an id of newly created post than I reuse it in test that checks the update endpoint.

    public function testShouldCreatePost(): string
    {
        // ...
    }

    /**
     * @depends testShouldCreatePost
     */
    public function testShouldUpdatePost(string $id): void
    {
        // ...
    }
@dmaicher
Copy link
Owner

No this is currently not supported. This bundle really aims to have all tests independent of each other when it comes to the database state.

I do see that this might be useful in some scenarios. If this would be possible to implement it would need to be configurable (by default inactive) though as it might break existing tests for other users of this bundle that have @depends annotations because of some non-database related state maybe.

I will keep this open as a feature request. Feel free to dig into it @tarach .

@AntonioCS
Copy link

@dmaicher here is a 👍 for this feature as I'm creating API tests.
I have a test where I create something and then a test where I patch that something I created (using @depends) but since the DB is "cleared" between tests the @depends is useless..
There should be something I can add to the code (maybe a flag or something) that would commit all that I did on the test.

@NicoHaase
Copy link

Why not simply call the other test function first? Why not refactor your tests to have a single method that inserts the data, and call this method from both test cases?

@AntonioCS
Copy link

@NicoHaase why not just use something that is available in phpunit called @depends?
There are many ways to achieve this, I just gave an example.

@NicoHaase
Copy link

NicoHaase commented Sep 22, 2021

@NicoHaase why not just use something that is available in phpunit called @depends?

Because that is not supported by this bundle, and I wanted to show you a way to circumvent this. But as this bundle is open source: if you need the combination of both features, feel free to open a pull request

@AntonioCS
Copy link

@NicoHaase I know it's not supported but I just wanted to show support for @tarach and his use case because I'm suffering from the same issue.

@BenMorel
Copy link

BenMorel commented Oct 4, 2021

Add additional +1 from me, it would be nice to be able to configure the bundle to keep the transaction open between two tests when one @depends on the other.

@tsetsee
Copy link

tsetsee commented Jan 27, 2022

+1

@chriskaya
Copy link

chriskaya commented Feb 15, 2022

Hi all

I'm currently facing the same issue, for which I'm simply disabling the feature for the class (see #182 )
A way to make it work would be using a "manual" flag, which would disable auto-begin and auto-rollback before and after test, what do you guys think?
I'm ok to create a PR if you're ok with this :)

public static function setUpBeforeClass(): void
{
    StaticDriver::setManualOperations(true);
    StaticDriver::beginTransaction();
}

public static function tearDownAfterClass(): void
{
    StaticDriver::rollBack();
    StaticDriver::setManualOperations(false);
}

This would maybe be also possible with annotations, but I'm not really familiar with them :p

EDIT: I've already created the PR: #203

@chriskaya
Copy link

chriskaya commented Feb 21, 2022

My PR having been rejected by @dmaicher for reasons I totally understand, I'm now using a custom phpunit extension built on top of doctrine-test-bundle.

If you need this, you may do the same: https://github.com/chriskaya/custom-dama-extension/blob/main/CustomDamaExtension.php

BTW, thx @dmaicher for your work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants