From 310d98cffebfac78f2fee7d5ce50eaa37cacbc4c Mon Sep 17 00:00:00 2001 From: beberlei Date: Mon, 1 Feb 2010 18:56:06 +0000 Subject: [PATCH] [2.0] DDC-296 - Implement and document Mysql Session Init 'postConnect' Subscriber --- .../DBAL/Event/Listeners/MysqlSessionInit.php | 75 +++++++++++++++++++ tests/Doctrine/Tests/DBAL/AllTests.php | 1 + .../DBAL/Events/MysqlSessionInitTest.php | 33 ++++++++ 3 files changed, 109 insertions(+) create mode 100644 lib/Doctrine/DBAL/Event/Listeners/MysqlSessionInit.php create mode 100644 tests/Doctrine/Tests/DBAL/Events/MysqlSessionInitTest.php diff --git a/lib/Doctrine/DBAL/Event/Listeners/MysqlSessionInit.php b/lib/Doctrine/DBAL/Event/Listeners/MysqlSessionInit.php new file mode 100644 index 000000000..e38536469 --- /dev/null +++ b/lib/Doctrine/DBAL/Event/Listeners/MysqlSessionInit.php @@ -0,0 +1,75 @@ +. +*/ + +namespace Doctrine\DBAL\Event\Listeners; + +use Doctrine\DBAL\Event\ConnectionEventArgs; +use Doctrine\DBAL\Events; +use Doctrine\Common\EventSubscriber; + +/** + * MySQL Session Init Event Subscriber which allows to set the Client Encoding of the 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 MysqlSessionInit implements EventSubscriber +{ + /** + * @var string + */ + private $_charset; + + /** + * @var string + */ + private $_collation; + + /** + * Configure Charset and Collation options of MySQL Client for each Connection + * + * @param string $charset + * @param string $collation + */ + public function __construct($charset = 'utf8', $collation = false) + { + $this->_charset = $charset; + $this->_collation = $collation; + } + + /** + * @param ConnectionEventArgs $args + * @return void + */ + public function postConnect(ConnectionEventArgs $args) + { + $collation = ($this->_collation) ? " COLLATE '".$this->_collation : "'"; + $args->getConnection()->executeUpdate("SET NAMES '".$this->_charset . "'" . $collation); + } + + public function getSubscribedEvents() + { + return array(Events::postConnect); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/AllTests.php b/tests/Doctrine/Tests/DBAL/AllTests.php index 4f8fb5060..3ced01e85 100644 --- a/tests/Doctrine/Tests/DBAL/AllTests.php +++ b/tests/Doctrine/Tests/DBAL/AllTests.php @@ -59,6 +59,7 @@ class AllTests // Events and Listeners $suite->addTestSuite('Doctrine\Tests\DBAL\Events\OracleSessionInitTest'); + $suite->addTestSuite('Doctrine\Tests\DBAL\Events\MysqlSessionInitTest'); // All Functional DBAL tests $suite->addTest(Functional\AllTests::suite()); diff --git a/tests/Doctrine/Tests/DBAL/Events/MysqlSessionInitTest.php b/tests/Doctrine/Tests/DBAL/Events/MysqlSessionInitTest.php new file mode 100644 index 000000000..f64fbb870 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Events/MysqlSessionInitTest.php @@ -0,0 +1,33 @@ +getMock('Doctrine\DBAL\Connection', array(), array(), '', false); + $connectionMock->expects($this->once()) + ->method('executeUpdate') + ->with($this->equalTo("SET NAMES 'foo' COLLATE 'bar")); + + $eventArgs = new ConnectionEventArgs($connectionMock); + + + $listener = new MysqlSessionInit('foo', 'bar'); + $listener->postConnect($eventArgs); + } + + public function testGetSubscribedEvents() + { + $listener = new MysqlSessionInit(); + $this->assertEquals(array(Events::postConnect), $listener->getSubscribedEvents()); + } +} \ No newline at end of file