ludofleury
3/3/2013 - 5:01 AM

A Behat Context with a hook to kill the Mysql connections

A Behat Context with a hook to kill the Mysql connections

<?php

use Behat\Symfony2Extension\Context\KernelAwareInterface;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Behat\MinkExtension\Context\MinkContext;

class MysqlContext extends MinkContext implements KernelAwareInterface
{
    use KernelDictionary;
    
    /**
     * @AfterScenario
     */
    public function killMysqlConnections()
    {
        $connections = $this->kernel->getContainer()->get('doctrine')->getConnections();

        foreach ($connections as $connection) {
            $threads = $connection->query('SHOW FULL PROCESSLIST');

            while ($thread = $threads->fetch()) {
                if ($thread['db'] == $connection->getDatabase() && $thread['Command'] == 'Sleep') {
                    $connection->query(sprintf('KILL %d', $thread['Id']));
                }
            }
        }
    }
}