From 2dfffe00d01e6ffd8adfc73c955a613ee843ba8f Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Mon, 25 Jun 2018 13:03:07 +0300 Subject: [PATCH] Database helper improvements --- .../Espo/Core/Utils/Database/Helper.php | 35 +++-- .../Espo/Core/Utils/Metadata/OrmMetadata.php | 11 +- .../Espo/Core/Utils/Database/HelperTest.php | 124 ++++++++++++++++++ 3 files changed, 157 insertions(+), 13 deletions(-) create mode 100644 tests/unit/Espo/Core/Utils/Database/HelperTest.php diff --git a/application/Espo/Core/Utils/Database/Helper.php b/application/Espo/Core/Utils/Database/Helper.php index 706ab4d873..1586a6c26f 100644 --- a/application/Espo/Core/Utils/Database/Helper.php +++ b/application/Espo/Core/Utils/Database/Helper.php @@ -43,7 +43,7 @@ class Helper 'pdo_mysql' => '\Espo\Core\Utils\Database\DBAL\Driver\PDOMySql\Driver', ); - public function __construct(\Espo\Core\Utils\Config $config) + public function __construct(\Espo\Core\Utils\Config $config = null) { $this->config = $config; } @@ -56,6 +56,10 @@ class Helper public function getDbalConnection() { if (!isset($this->connection)) { + if (!$this->getConfig()) { + return null; + } + $connectionParams = $this->getConfig()->get('database'); $connectionParams['driverClass'] = $this->drivers[ $connectionParams['driver'] ]; unset($connectionParams['driver']); @@ -74,10 +78,12 @@ class Helper * * @return int */ - public function getMaxIndexLength($tableName = null) + public function getMaxIndexLength($tableName = null, $default = 1000) { - $connection = $this->getDbalConnection(); $mysqlEngine = $this->getMysqlEngine($tableName); + if (!$mysqlEngine) { + return $default; + } switch ($mysqlEngine) { case 'InnoDB': @@ -98,14 +104,18 @@ class Helper return 1000; //MyISAM } - public function getTableMaxIndexLength($tableName) + public function getTableMaxIndexLength($tableName, $default = 1000) { - return $this->getMaxIndexLength($tableName); + return $this->getMaxIndexLength($tableName, $default); } protected function getMysqlVersion() { $connection = $this->getDbalConnection(); + if (!$connection) { + return null; + } + return $connection->fetchColumn("select version()"); } @@ -116,9 +126,12 @@ class Helper * * @return string */ - protected function getMysqlEngine($tableName = null) + protected function getMysqlEngine($tableName = null, $default = null) { $connection = $this->getDbalConnection(); + if (!$connection) { + return $default; + } $query = "SHOW TABLE STATUS WHERE Engine = 'MyISAM'"; if (!empty($tableName)) { @@ -141,10 +154,12 @@ class Helper * * @return boolean */ - public function isSupportsFulltext($tableName = null) + public function isSupportsFulltext($tableName = null, $default = false) { - $connection = $this->getDbalConnection(); $mysqlEngine = $this->getMysqlEngine($tableName); + if (!$mysqlEngine) { + return $default; + } switch ($mysqlEngine) { case 'InnoDB': @@ -161,8 +176,8 @@ class Helper return true; //MyISAM } - public function isTableSupportsFulltext($tableName) + public function isTableSupportsFulltext($tableName, $default = false) { - return $this->isSupportsFulltext($tableName); + return $this->isSupportsFulltext($tableName, $default); } } \ No newline at end of file diff --git a/application/Espo/Core/Utils/Metadata/OrmMetadata.php b/application/Espo/Core/Utils/Metadata/OrmMetadata.php index d44d187969..7046d21487 100644 --- a/application/Espo/Core/Utils/Metadata/OrmMetadata.php +++ b/application/Espo/Core/Utils/Metadata/OrmMetadata.php @@ -45,13 +45,18 @@ class OrmMetadata protected $useCache; - public function __construct(\Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\Config $config) + public function __construct(\Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager, $config) { $this->metadata = $metadata; $this->fileManager = $fileManager; - $this->config = $config; - $this->useCache = $this->config->get('useCache', false); + $this->useCache = false; + if ($config instanceof \Espo\Core\Utils\Config) { + $this->config = $config; + $this->useCache = $this->config->get('useCache', false); + } elseif (is_bool($config)) { + $this->useCache = $config; + } } protected function getConverter() diff --git a/tests/unit/Espo/Core/Utils/Database/HelperTest.php b/tests/unit/Espo/Core/Utils/Database/HelperTest.php new file mode 100644 index 0000000000..fbda781460 --- /dev/null +++ b/tests/unit/Espo/Core/Utils/Database/HelperTest.php @@ -0,0 +1,124 @@ +objects['config'] = $this->getMockBuilder('\\Espo\\Core\\Utils\\Config')->disableOriginalConstructor()->getMock(); + + $this->objects['config']->expects($this->any()) + ->method('get') + ->with($this->equalTo('database')) + ->will($this->returnValue([ + 'driver' => 'pdo_mysql', + 'dbname' => 'test', + 'user' => 'test_database', + 'password' => 'test_user', + 'host' => 'localhost', + 'port' => '', + 'charset' => 'utf8mb4' + ])); + } + + protected function tearDown() + { + $this->object = NULL; + } + + protected function initDatabaseHelper($config = null) + { + $this->object = new \Espo\Core\Utils\Database\Helper($config); + $this->reflection = new ReflectionHelper($this->object); + + return $this->object; + } + + public function testGetDbalConnection() + { + $this->initDatabaseHelper(null); + + $this->assertNull($this->object->getDbalConnection()); + } + + public function testGetDbalConnectionWithConfig() + { + $this->initDatabaseHelper($this->objects['config']); + + $this->assertInstanceOf('\\Doctrine\\DBAL\\Connection', $this->object->getDbalConnection()); + } + + public function testGetMaxIndexLength() + { + $this->initDatabaseHelper(null); + + $this->assertEquals(1000, $this->object->getMaxIndexLength()); + $this->assertEquals(1000, $this->object->getMaxIndexLength('table_name')); + $this->assertEquals(2000, $this->object->getMaxIndexLength('table_name', 2000)); + $this->assertEquals(1000, $this->object->getTableMaxIndexLength('table_name')); + $this->assertEquals(2000, $this->object->getTableMaxIndexLength('table_name', 2000)); + } + + public function testGetMysqlVersion() + { + $this->initDatabaseHelper(null); + + $this->assertNull($this->reflection->invokeMethod('getMysqlVersion')); + } + + public function testGetMysqlEngine() + { + $this->initDatabaseHelper(null); + + $this->assertNull($this->reflection->invokeMethod('getMysqlEngine')); + $this->assertEquals('InnoDB', $this->reflection->invokeMethod('getMysqlEngine', array(null, 'InnoDB'))); + } + + public function testIsSupportsFulltext() + { + $this->initDatabaseHelper(null); + + $this->assertFalse($this->object->isSupportsFulltext()); + $this->assertFalse($this->object->isSupportsFulltext('table_name')); + $this->assertTrue($this->object->isSupportsFulltext('table_name', true)); + $this->assertFalse($this->object->isTableSupportsFulltext('table_name')); + $this->assertTrue($this->object->isTableSupportsFulltext('table_name', true)); + } +}