From b4e09013e866d1a0f126fdeed6b9c93197a11fe9 Mon Sep 17 00:00:00 2001 From: Tom Klingenberg Date: Thu, 14 Jan 2016 16:16:01 +0100 Subject: [PATCH] Streamline database port with Magento Setup Model Installer throw exception when port is set within a parameter of it's own. the (optional) database port is to be set within the host parameter and not within the port parameter. --- .../Framework/DB/Adapter/Pdo/Mysql.php | 6 +++++ .../DB/Test/Unit/Adapter/Pdo/MysqlTest.php | 27 +++++++++++++++++++ .../Model/ResourceModel/Type/Db/Pdo/Mysql.php | 7 +++++ .../Test/Unit/Validator/DbValidatorTest.php | 3 ++- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php b/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php index d5a5aa20d2961..6aa735ff1c6c6 100644 --- a/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php +++ b/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php @@ -329,6 +329,12 @@ protected function _connect() throw new \Zend_Db_Adapter_Exception('No host configured to connect'); } + if (isset($this->_config['port'])) { + throw new \Zend_Db_Adapter_Exception('Port must be configured within host parameter'); + } + + unset($this->_config['port']); + if (strpos($this->_config['host'], '/') !== false) { $this->_config['unix_socket'] = $this->_config['host']; unset($this->_config['host']); diff --git a/lib/internal/Magento/Framework/DB/Test/Unit/Adapter/Pdo/MysqlTest.php b/lib/internal/Magento/Framework/DB/Test/Unit/Adapter/Pdo/MysqlTest.php index 0d52fca446f36..18c522cf67193 100644 --- a/lib/internal/Magento/Framework/DB/Test/Unit/Adapter/Pdo/MysqlTest.php +++ b/lib/internal/Magento/Framework/DB/Test/Unit/Adapter/Pdo/MysqlTest.php @@ -14,6 +14,8 @@ use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\DB\Select; use Magento\Framework\DB\Select\SelectRenderer; +use Magento\Framework\Model\ResourceModel\Type\Db\Pdo\Mysql; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; class MysqlTest extends \PHPUnit_Framework_TestCase { @@ -523,4 +525,29 @@ public function getIndexNameDataProvider() ['short_table_name', ['field1', 'field2'], '', 'SHORT_TABLE_NAME_FIELD1_FIELD2'], ]; } + + /** + * @test + */ + public function connectPortThrow() + { + $arguments = [ + 'config' => ['host' => 'localhost'], + ]; + $subject = (new ObjectManager($this))->getObject(Mysql::class, $arguments); + $this->assertInstanceOf(Mysql::class, $subject); + + $arguments = [ + 'config' => ['host' => 'localhost', 'port' => '33390'], + ]; + + try { + (new ObjectManager($this))->getObject(Mysql::class, $arguments); + $this->fail('an expected exception was not thrown'); + } catch (\InvalidArgumentException $e) { + $expected = 'MySQL adapter: Port must be configured within host (like \'localhost:33390\') ' . + 'parameter, not within port'; + $this->assertSame($expected, $e->getMessage()); + } + } } diff --git a/lib/internal/Magento/Framework/Model/ResourceModel/Type/Db/Pdo/Mysql.php b/lib/internal/Magento/Framework/Model/ResourceModel/Type/Db/Pdo/Mysql.php index 735b0b7f23b5d..2cf976b79688b 100644 --- a/lib/internal/Magento/Framework/Model/ResourceModel/Type/Db/Pdo/Mysql.php +++ b/lib/internal/Magento/Framework/Model/ResourceModel/Type/Db/Pdo/Mysql.php @@ -114,6 +114,13 @@ private function getValidConfig(array $config) } } + if (isset($config['port'])) { + throw new \InvalidArgumentException( + "MySQL adapter: Port must be configured within host (like '$config[host]:$config[port]') parameter, " . + "not within port" + ); + } + $config['active'] = !( $config['active'] === 'false' || $config['active'] === false diff --git a/setup/src/Magento/Setup/Test/Unit/Validator/DbValidatorTest.php b/setup/src/Magento/Setup/Test/Unit/Validator/DbValidatorTest.php index ba614172c0b7c..792ce47504017 100644 --- a/setup/src/Magento/Setup/Test/Unit/Validator/DbValidatorTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Validator/DbValidatorTest.php @@ -38,7 +38,7 @@ protected function setUp() public function testCheckDatabaseConnection() { $this->connection - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('fetchOne') ->with('SELECT version()') ->willReturn('5.6.0-0ubuntu0.12.04.1'); @@ -79,6 +79,7 @@ public function testCheckDatabaseConnection() ] ); $this->assertEquals(true, $this->dbValidator->checkDatabaseConnection('name', 'host', 'user', 'password')); + $this->assertEquals(true, $this->dbValidator->checkDatabaseConnection('name', 'host:3339', 'user', 'password')); } /**