doctrine-test-bundle
doctrine-test-bundle copied to clipboard
Using @depends - Transaction for 2 methods
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
{
// ...
}
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 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.
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 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 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 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.
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.
+1
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
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 :)