3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
- // @codingStandardsIgnoreFile
8
-
9
- /**
10
- * Test for an PDO MySQL adapter
11
- */
12
6
namespace Magento \Framework \DB \Adapter \Pdo ;
13
7
14
8
use Magento \Framework \App \ResourceConnection ;
15
- use Zend_Db_Statement_Exception ;
9
+ use Magento \ TestFramework \ Helper \ Bootstrap ;
16
10
17
11
class MysqlTest extends \PHPUnit_Framework_TestCase
18
12
{
19
13
/**
20
- * Database adapter instance
21
- *
22
- * @var \Magento\Framework\DB\Adapter\Pdo\Mysql
14
+ * @var ResourceConnection
23
15
*/
24
- protected $ _connection = null ;
16
+ private $ resourceConnection ;
25
17
26
- public function setUp ()
18
+ protected function setUp ()
27
19
{
28
20
set_error_handler (null );
21
+ $ this ->resourceConnection = Bootstrap::getObjectManager ()
22
+ ->get (ResourceConnection::class);
29
23
}
30
24
31
- public function tearDown ()
25
+ protected function tearDown ()
32
26
{
33
27
restore_error_handler ();
34
28
}
@@ -40,30 +34,20 @@ public function tearDown()
40
34
*/
41
35
public function testWaitTimeout ()
42
36
{
43
- if (!$ this ->_getConnection () instanceof \Magento \Framework \DB \Adapter \Pdo \Mysql) {
37
+ if (!$ this ->getDbAdapter () instanceof \Magento \Framework \DB \Adapter \Pdo \Mysql) {
44
38
$ this ->markTestSkipped ('This test is for \Magento\Framework\DB\Adapter\Pdo\Mysql ' );
45
39
}
46
40
try {
47
- $ defaultWaitTimeout = $ this ->_getWaitTimeout ();
48
41
$ minWaitTimeout = 1 ;
49
- $ this ->_setWaitTimeout ($ minWaitTimeout );
50
- $ this ->assertEquals ($ minWaitTimeout , $ this ->_getWaitTimeout (), 'Wait timeout was not changed ' );
42
+ $ this ->setWaitTimeout ($ minWaitTimeout );
43
+ $ this ->assertEquals ($ minWaitTimeout , $ this ->getWaitTimeout (), 'Wait timeout was not changed ' );
51
44
52
45
// Sleep for time greater than wait_timeout and try to perform query
53
46
sleep ($ minWaitTimeout + 1 );
54
- $ result = $ this ->_executeQuery ('SELECT 1 ' );
55
- $ this ->assertInstanceOf ('Magento\Framework\DB\Statement\Pdo\Mysql ' , $ result );
56
- // Restore wait_timeout
57
- $ this ->_setWaitTimeout ($ defaultWaitTimeout );
58
- $ this ->assertEquals (
59
- $ defaultWaitTimeout ,
60
- $ this ->_getWaitTimeout (),
61
- 'Default wait timeout was not restored '
62
- );
63
- } catch (\Exception $ e ) {
64
- // Reset connection on failure to restore global variables
65
- $ this ->_getConnection ()->closeConnection ();
66
- throw $ e ;
47
+ $ result = $ this ->executeQuery ('SELECT 1 ' );
48
+ $ this ->assertInstanceOf (\Magento \Framework \DB \Statement \Pdo \Mysql::class, $ result );
49
+ } finally {
50
+ $ this ->getDbAdapter ()->closeConnection ();
67
51
}
68
52
}
69
53
@@ -72,9 +56,9 @@ public function testWaitTimeout()
72
56
*
73
57
* @return int
74
58
*/
75
- protected function _getWaitTimeout ()
59
+ private function getWaitTimeout ()
76
60
{
77
- $ result = $ this ->_executeQuery ('SELECT @@session.wait_timeout ' );
61
+ $ result = $ this ->executeQuery ('SELECT @@session.wait_timeout ' );
78
62
return (int )$ result ->fetchColumn ();
79
63
}
80
64
@@ -83,54 +67,67 @@ protected function _getWaitTimeout()
83
67
*
84
68
* @param int $waitTimeout
85
69
*/
86
- protected function _setWaitTimeout ($ waitTimeout )
70
+ private function setWaitTimeout ($ waitTimeout )
87
71
{
88
- $ this ->_executeQuery ("SET @@session.wait_timeout = {$ waitTimeout }" );
72
+ $ this ->executeQuery ("SET @@session.wait_timeout = {$ waitTimeout }" );
89
73
}
90
74
91
75
/**
92
76
* Execute SQL query and return result statement instance
93
77
*
94
- * @param string $sql
95
- * @return \Zend_Db_Statement_Interface
96
- * @throws \Exception
78
+ * @param $sql
79
+ * @return void|\Zend_Db_Statement_Pdo
80
+ * @throws \Magento\Framework\Exception\LocalizedException
81
+ * @throws \Zend_Db_Adapter_Exception
97
82
*/
98
- protected function _executeQuery ($ sql )
83
+ private function executeQuery ($ sql )
99
84
{
100
- /**
101
- * Suppress PDO warnings to work around the bug
102
- * @link https://bugs.php.net/bug.php?id=63812
103
- */
104
- $ phpErrorReporting = error_reporting ();
105
- /** @var $pdoConnection \PDO */
106
- $ pdoConnection = $ this ->_getConnection ()->getConnection ();
107
- $ pdoWarningsEnabled = $ pdoConnection ->getAttribute (\PDO ::ATTR_ERRMODE ) & \PDO ::ERRMODE_WARNING ;
108
- if (!$ pdoWarningsEnabled ) {
109
- error_reporting ($ phpErrorReporting & ~E_WARNING );
110
- }
111
- try {
112
- $ result = $ this ->_getConnection ()->query ($ sql );
113
- error_reporting ($ phpErrorReporting );
114
- } catch (\Exception $ e ) {
115
- error_reporting ($ phpErrorReporting );
116
- throw $ e ;
117
- }
118
- return $ result ;
85
+ return $ this ->getDbAdapter ()->query ($ sql );
119
86
}
120
87
121
88
/**
122
89
* Retrieve database adapter instance
123
90
*
124
91
* @return \Magento\Framework\DB\Adapter\Pdo\Mysql
125
92
*/
126
- protected function _getConnection ()
93
+ private function getDbAdapter ()
127
94
{
128
- if (is_null ($ this ->_connection )) {
129
- /** @var $coreResource \Magento\Framework\App\ResourceConnection */
130
- $ coreResource = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
131
- ->get ('Magento\Framework\App\ResourceConnection ' );
132
- $ this ->_connection = $ coreResource ->getConnection ();
133
- }
134
- return $ this ->_connection ;
95
+ return $ this ->resourceConnection ->getConnection ();
96
+ }
97
+
98
+ public function testGetCreateTable ()
99
+ {
100
+ $ tableName = $ this ->resourceConnection ->getTableName ('core_config_data ' );
101
+ $ this ->assertEquals (
102
+ $ this ->getDbAdapter ()->getCreateTable ($ tableName ),
103
+ $ this ->getDbAdapter ()->getCreateTable ($ tableName )
104
+ );
105
+ }
106
+
107
+ public function testGetForeignKeys ()
108
+ {
109
+ $ tableName = $ this ->resourceConnection ->getTableName ('core_config_data ' );
110
+ $ this ->assertEquals (
111
+ $ this ->getDbAdapter ()->getForeignKeys ($ tableName ),
112
+ $ this ->getDbAdapter ()->getForeignKeys ($ tableName )
113
+ );
114
+ }
115
+
116
+ public function testGetIndexList ()
117
+ {
118
+ $ tableName = $ this ->resourceConnection ->getTableName ('core_config_data ' );
119
+ $ this ->assertEquals (
120
+ $ this ->getDbAdapter ()->getIndexList ($ tableName ),
121
+ $ this ->getDbAdapter ()->getIndexList ($ tableName )
122
+ );
123
+ }
124
+
125
+ public function testDescribeTable ()
126
+ {
127
+ $ tableName = $ this ->resourceConnection ->getTableName ('core_config_data ' );
128
+ $ this ->assertEquals (
129
+ $ this ->getDbAdapter ()->describeTable ($ tableName ),
130
+ $ this ->getDbAdapter ()->describeTable ($ tableName )
131
+ );
135
132
}
136
133
}
0 commit comments