doctrine-test-bundle icon indicating copy to clipboard operation
doctrine-test-bundle copied to clipboard

Using @depends - Transaction for 2 methods

Open tarach opened this issue 3 years ago • 13 comments

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
    {
        // ...
    }

tarach avatar Mar 24 '21 03:03 tarach

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 .

dmaicher avatar Mar 24 '21 09:03 dmaicher

@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.

AntonioCS avatar Sep 22 '21 10:09 AntonioCS

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?

NicoHaase avatar Sep 22 '21 11:09 NicoHaase

@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.

AntonioCS avatar Sep 22 '21 11:09 AntonioCS

@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

NicoHaase avatar Sep 22 '21 12:09 NicoHaase

@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.

AntonioCS avatar Sep 22 '21 13:09 AntonioCS

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.

BenMorel avatar Oct 04 '21 14:10 BenMorel

+1

tsetsee avatar Jan 27 '22 07:01 tsetsee

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: https://github.com/dmaicher/doctrine-test-bundle/pull/203

chriskaya avatar Feb 15 '22 11:02 chriskaya

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 :)

chriskaya avatar Feb 21 '22 10:02 chriskaya