-
Notifications
You must be signed in to change notification settings - Fork 62
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
Comments
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 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. |
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 |
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 |
+1 |
Hi all I'm currently facing the same issue, for which I'm simply disabling the feature for the class (see #182 )
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 |
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 :) |
Hello, What about a rollback at the before the test instead of after to check if current has dependencies. Here is an example of implementation : final class DatabaseExtension implements Extension
{
public static bool $transactionStarted = false;
public static function rollBack(): void
{
if (!self::$transactionStarted) {
return;
}
StaticDriver::rollBack();
self::$transactionStarted = false;
}
public static function begin(): void
{
if (self::$transactionStarted) {
return;
}
StaticDriver::beginTransaction();
self::$transactionStarted = true;
}
#[\Override]
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
$facade->registerSubscriber(new class() implements TestRunnerStartedSubscriber {
public function notify(TestRunnerStartedEvent $event): void
{
StaticDriver::setKeepStaticConnections(true);
}
});
$facade->registerSubscriber(new class() implements TestStartedSubscriber {
public function notify(TestStartedEvent $event): void
{
$test = $event->test();
if ($test->isTestMethod()) {
/** @var TestMethod $test */
if (0 === $test->metadata()->isDepends()->count()) {
DatabaseExtension::rollBack();
}
}
DatabaseExtension::begin();
}
});
$facade->registerSubscriber(new class() implements TestRunnerFinishedSubscriber {
public function notify(TestRunnerFinishedEvent $event): void
{
DatabaseExtension::rollBack();
StaticDriver::setKeepStaticConnections(false);
}
});
}
} Do you think it could be a good idea ? |
@pmontoya Have you tried you extension ? Would it not trigger multiple being if the test have a depends ? Also some test could be written like this:
The work around is easy by adding a "useless" depends on the secondTest but I may have 2 other suggestions. Having an attribute on the TestCase itself to tell that rollback should only happen on the end of the "test class". The real question here is does your implementation works ? I woud want to do something like this (I am assuming that want this feature will do something similar).
|
@mpoiriert I use this extension from 8 months without any problem as the transaction is rollbacked only if test haven't depends or at the end of test suite. |
@pmontoya Thanks, that was exactly what I was searching for! 🎉 |
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.
The text was updated successfully, but these errors were encountered: