[2.0] DDC-294 - Added "postConnect" event in Doctrine\DBAL\Connection and refactored TestUtil to allow configuration of DBAL EventManager Subscribers by the use of PHPunit Xml configuration.
This commit is contained in:
parent
3ea1f8064a
commit
e7f5089ea4
@ -308,6 +308,11 @@ class Connection
|
||||
$this->_conn = $this->_driver->connect($this->_params, $user, $password, $driverOptions);
|
||||
$this->_isConnected = true;
|
||||
|
||||
if ($this->_eventManager->hasListeners(Events::postConnect)) {
|
||||
$eventArgs = new \Doctrine\DBAL\Event\ConnectionEventArgs($this);
|
||||
$this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
79
lib/Doctrine/DBAL/Event/ConnectionEventArgs.php
Normal file
79
lib/Doctrine/DBAL/Event/ConnectionEventArgs.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\DBAL\Event;
|
||||
|
||||
use Doctrine\Common\EventArgs,
|
||||
Doctrine\DBAL\Connection;
|
||||
|
||||
/**
|
||||
* Event Arguments used when a Driver connection is established inside Doctrine\DBAL\Connection.
|
||||
*
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @link www.doctrine-project.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
*/
|
||||
class ConnectionEventArgs extends EventArgs
|
||||
{
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
private $_connection = null;
|
||||
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->_connection = $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Doctrine\DBAL\Connection
|
||||
*/
|
||||
public function getConnection()
|
||||
{
|
||||
return $this->_connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Doctrine\DBAL\Driver
|
||||
*/
|
||||
public function getDriver()
|
||||
{
|
||||
return $this->_connection->getDriver();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Doctrine\DBAL\Platforms\AbstractPlatform
|
||||
*/
|
||||
public function getDatabasePlatform()
|
||||
{
|
||||
return $this->_connection->getDatabasePlatform();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Doctrine\DBAL\Schema\AbstractSchemaManager
|
||||
*/
|
||||
public function getSchemaManager()
|
||||
{
|
||||
return $this->_connection->getSchemaManager();
|
||||
}
|
||||
}
|
@ -37,6 +37,7 @@ final class Events
|
||||
const postExec = 'postExec';
|
||||
const preExecute = 'preExecute';
|
||||
const postExecute = 'postExecute';
|
||||
|
||||
|
||||
const postConnect = 'postConnect';
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,11 @@
|
||||
namespace Doctrine\Tests\DBAL;
|
||||
|
||||
require_once __DIR__ . '/../TestInit.php';
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\Common\EventManager;
|
||||
use Doctrine\DBAL\Configuration;
|
||||
use Doctrine\DBAL\Events;
|
||||
|
||||
class ConnectionTest extends \Doctrine\Tests\DbalTestCase
|
||||
{
|
||||
@ -40,11 +45,28 @@ class ConnectionTest extends \Doctrine\Tests\DbalTestCase
|
||||
|
||||
public function testGetDriver()
|
||||
{
|
||||
$this->assertEquals('Doctrine\DBAL\Driver\PDOMySql\Driver', get_class($this->_conn->getDriver()));
|
||||
$this->assertType('Doctrine\DBAL\Driver\PDOMySql\Driver', $this->_conn->getDriver());
|
||||
}
|
||||
|
||||
public function testGetEventManager()
|
||||
{
|
||||
$this->assertEquals('Doctrine\Common\EventManager', get_class($this->_conn->getEventManager()));
|
||||
$this->assertType('Doctrine\Common\EventManager', $this->_conn->getEventManager());
|
||||
}
|
||||
|
||||
public function testConnectDispatchEvent()
|
||||
{
|
||||
$listenerMock = $this->getMock('ConnectDispatchEventListener', array('postConnect'));
|
||||
$listenerMock->expects($this->once())->method('postConnect');
|
||||
|
||||
$eventManager = new EventManager();
|
||||
$eventManager->addEventListener(array(Events::postConnect), $listenerMock);
|
||||
|
||||
$driverMock = $this->getMock('Doctrine\DBAL\Driver');
|
||||
$driverMock->expects(($this->at(0)))
|
||||
->method('connect');
|
||||
$platform = new Mocks\MockPlatform();
|
||||
|
||||
$conn = new Connection(array('platform' => $platform), $driverMock, new Configuration(), $eventManager);
|
||||
$conn->connect();
|
||||
}
|
||||
}
|
@ -64,8 +64,17 @@ class TestUtil
|
||||
$tmpConn->getSchemaManager()->createDatabase($dbname);
|
||||
|
||||
$tmpConn->close();
|
||||
|
||||
$eventManager = null;
|
||||
if (isset($GLOBALS['db_event_subscribers'])) {
|
||||
$eventManager = new \Doctrine\Common\EventManager();
|
||||
foreach (explode(",", $GLOBALS['db_event_subscribers']) AS $subscriberClass) {
|
||||
$subscriberInstance = new $subscriberClass();
|
||||
$eventManager->addEventSubscriber($subscriberInstance);
|
||||
}
|
||||
}
|
||||
|
||||
$conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
|
||||
$conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, $eventManager);
|
||||
|
||||
} else {
|
||||
$params = array(
|
||||
|
Loading…
x
Reference in New Issue
Block a user