From e7f5089ea45cb92a2542bfc582ff490b6af217d7 Mon Sep 17 00:00:00 2001 From: beberlei Date: Sun, 31 Jan 2010 21:51:15 +0000 Subject: [PATCH] [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. --- lib/Doctrine/DBAL/Connection.php | 5 ++ .../DBAL/Event/ConnectionEventArgs.php | 79 +++++++++++++++++++ lib/Doctrine/DBAL/Events.php | 3 +- tests/Doctrine/Tests/DBAL/ConnectionTest.php | 26 +++++- tests/Doctrine/Tests/TestUtil.php | 11 ++- 5 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 lib/Doctrine/DBAL/Event/ConnectionEventArgs.php diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 4962664ff..e1f3151f5 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -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; } diff --git a/lib/Doctrine/DBAL/Event/ConnectionEventArgs.php b/lib/Doctrine/DBAL/Event/ConnectionEventArgs.php new file mode 100644 index 000000000..ce80ecd4d --- /dev/null +++ b/lib/Doctrine/DBAL/Event/ConnectionEventArgs.php @@ -0,0 +1,79 @@ +. +*/ + +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 + */ +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(); + } +} diff --git a/lib/Doctrine/DBAL/Events.php b/lib/Doctrine/DBAL/Events.php index 9e2ef005b..e5f8e8ca3 100644 --- a/lib/Doctrine/DBAL/Events.php +++ b/lib/Doctrine/DBAL/Events.php @@ -37,6 +37,7 @@ final class Events const postExec = 'postExec'; const preExecute = 'preExecute'; const postExecute = 'postExecute'; - + + const postConnect = 'postConnect'; } diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index 0daac0eeb..6390bf374 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -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(); } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/TestUtil.php b/tests/Doctrine/Tests/TestUtil.php index 4b9ac8dfd..83e61f92d 100644 --- a/tests/Doctrine/Tests/TestUtil.php +++ b/tests/Doctrine/Tests/TestUtil.php @@ -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(