-
-
Notifications
You must be signed in to change notification settings - Fork 454
Close connections and clear managers on shutdown should be *optional* in test mode #407
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
Comments
Got exactly the same trouble. Using the same trick of Alexandre Salome to isolate my PHPUnit - FrameworkBundle\Client tests. How to get a good new way to isolate our tests ? Maybe have a way to tell the connection that if it close, it should rollback the transaction, and not commit it |
May be workaround to avoid new code: # app/AppKernel.php
/**
* Обходим принудительное закрытие коннекта
*
* @see https://github.com/doctrine/DoctrineBundle/issues/407
*
* {@inheritdoc}
*
* @api
*/
public function shutdown()
{
if (false === $this->booted) {
return;
}
$this->booted = false;
foreach ($this->getBundles() as $bundle) {
/**
* Вмешиваемся только в один класс и только в тестовой среде окружения
*/
if ($bundle instanceof Doctrine\Bundle\DoctrineBundle\DoctrineBundle && ('test' === $this->getEnvironment())) {
/**
* Используем замыкание для доступа к приватной переменной DoctrineBundle->autoloader
* @see http://habrahabr.ru/post/186718/
*/
$doctrineShutdown = Closure::bind(
function () {
if (null !== $this->autoloader) {
spl_autoload_unregister($this->autoloader);
$this->autoloader = null;
}
},
$bundle,
'Doctrine\Bundle\DoctrineBundle\DoctrineBundle'
);
$doctrineShutdown();
} else {
$bundle->shutdown();
}
$bundle->setContainer(null);
}
$this->container = null;
} |
This workaround works for me, thanks a lot @dvc |
I have the same problem. We run each test inside a transaction, which is rolled back on teardown. This keeps the test db in a clean state without the need to rebuild it for each test. When using symfony's test client to simulate http requests, after each request the kernel is shutdown which now means all connections are closed. It would be nice if there was a way to avoid that. |
I'm stuck on DoctrineBundle 1.3 because of this issue. It breaks a lot of my tests where assertions on entities are made after a request. |
I worked around the issue by decorating the connection in tests and noop On Thu, 25 Feb 2016, 9:28 PM Christian Raue notifications@github.com
|
Yeah to get around that I created a <?php
namespace AppBundle\Framework;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Exception;
// Decorates a connection to disable $connection->close()
//
// Every test we run is isolated within a DB transaction.
//
// However Doctrine closes all connections on kernel shutdown.
//
// This is a problem because in some tests the kernel is booted
// and shutdown multiple times (e.g. when using the WebTest client)
// which means Doctrine is closing the active transaction
// established in TestDatabaseConnectionFactory
//
// To work around the issue we stop the connection from being closed
//
// Also see https://github.com/doctrine/DoctrineBundle/issues/407
class TestDatabaseConnection extends MasterSlaveConnection
{
private $isTestTransactionCreated = false;
public function connect($connectionName = null)
{
$result = parent::connect($connectionName);
if ($result && $this->isConnectedToMaster() && !$this->isTestTransactionCreated) {
$this->setNestTransactionsWithSavepoints(true);
$this->beginTransaction();
$this->isTestTransactionCreated = true;
}
return $result;
}
public function rollbackTestTransactionAndClose()
{
if ($this->isTestTransactionCreated) {
$this->rollback();
if ($this->isTransactionActive()) {
throw new Exception("Didn't expect transaction to be active");
}
}
parent::close();
}
public function close()
{
// no-op
}
} |
The issue is quite old but I think this bundle will simplify all your transactional test logic quite a bit 😉 |
Closing as per #638 (comment). |
Related to: #366
Almost all our functional tests has been broken by #366 ! :(
Our project has very complex user-account-roles-subscriptions relations an hierarchy. And very complex bussiness objects inheritance and relations. It is a real pain to emulate realistic test environment... So, the decision for us:
See http://alexandre-salome.fr/blog/Symfony2-Isolation-Of-Tests for details
This recipe doesn`t work after #366.
Can you make this good functional configurable?
The text was updated successfully, but these errors were encountered: