Skip to content

Commit b5f0573

Browse files
committed
removed class not responsible for code, corrections, update code coverage
1 parent b7bf0c5 commit b5f0573

File tree

12 files changed

+146
-113
lines changed

12 files changed

+146
-113
lines changed

lib/Config.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ public static function initialize(string $driver = '', array $arguments = null)
101101

102102
private function setupMysqli($args)
103103
{
104-
if ( ! \function_exists ('mysqli_connect') )
104+
if ( ! \function_exists('mysqli_connect') )
105105
throw new Exception('<b>Fatal Error:</b> ez_mysql requires mySQLi Lib to be compiled and or linked in to the PHP engine');
106106

107-
if (\count($args)>=3) {
107+
if (\count($args) >= 3) {
108108
$this->setUser($args[0]);
109109
$this->setPassword($args[1]);
110110
$this->setName($args[2]);
@@ -118,9 +118,9 @@ private function setupMysqli($args)
118118

119119
private function setupPdo($args)
120120
{
121-
if ( ! \class_exists ('PDO') )
121+
if ( ! \class_exists('PDO') )
122122
throw new Exception('<b>Fatal Error:</b> ez_pdo requires PDO Lib to be compiled and or linked in to the PHP engine');
123-
if (\count($args)>=3) {
123+
if (\count($args) >= 3) {
124124
$this->setDsn($args[0]);
125125
$this->setUser($args[1]);
126126
$this->setPassword($args[2]);
@@ -132,10 +132,10 @@ private function setupPdo($args)
132132

133133
private function setupSqlsrv($args)
134134
{
135-
if ( ! \function_exists ('sqlsrv_connect') )
135+
if ( ! \function_exists('sqlsrv_connect') )
136136
throw new Exception('<b>Fatal Error:</b> ez_sqlsrv requires the php_sqlsrv.dll or php_pdo_sqlsrv.dll to be installed. Also enable MS-SQL extension in PHP.ini file ');
137137

138-
if (\count($args)>=3) {
138+
if (\count($args) >= 3) {
139139
$this->setUser($args[0]);
140140
$this->setPassword($args[1]);
141141
$this->setName($args[2]);
@@ -147,10 +147,10 @@ private function setupSqlsrv($args)
147147

148148
private function setupPgsql($args)
149149
{
150-
if ( ! \function_exists ('pg_connect') )
150+
if ( ! \function_exists('pg_connect') )
151151
throw new Exception('<b>Fatal Error:</b> ez_pgsql requires PostgreSQL Lib to be compiled and or linked in to the PHP engine');
152152

153-
if (count($args)>=3) {
153+
if (count($args) >= 3) {
154154
$this->setUser($args[0]);
155155
$this->setPassword($args[1]);
156156
$this->setName($args[2]);
@@ -161,10 +161,10 @@ private function setupPgsql($args)
161161
}
162162

163163
private function setupSqlite3($args) {
164-
if ( ! \class_exists ('SQLite3') )
164+
if ( ! \class_exists('SQLite3') )
165165
throw new Exception('<b>Fatal Error:</b> ez_sqlite3 requires SQLite3 Lib to be compiled and or linked in to the PHP engine');
166166

167-
if (\count($args)==2) {
167+
if (\count($args) == 2) {
168168
$this->setPath($args[0]);
169169
$this->setName($args[1]);
170170
} else

lib/Constants.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// Error messages
1414
\define('MISSING_CONFIGURATION', '<b>Fatal Error:</b> Missing configuration details to connect to database');
1515
\define('CONFIGURATION_REQUIRES', '<b>Fatal Error:</b> This configuration requires ezsqlModel (ezsqlModel.php) to be included/loaded before it can be used');
16+
\define('FAILED_CONNECTION', 'Failed to make connection to database');
17+
1618
// ezQuery prepare placeholder/positional tag
1719
\define('_TAG', '__ez__');
1820
// Use to set get_result output as json

lib/Database/ez_mysqli.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ class ez_mysqli extends ezsqlModel implements DatabaseInterface
1616
*/
1717
private $ezsql_mysql_str = array
1818
(
19-
1 => 'Require $user and $password to connect to a database server',
20-
2 => 'Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running?',
21-
3 => 'Require $name to select a database',
22-
4 => 'mySQL database connection is not active',
23-
5 => 'Unexpected error while trying to select database'
19+
1 => 'Require $name to select a database',
20+
2 => 'mySQL database connection is not active',
21+
3 => 'Unexpected error while trying to select database'
2422
);
2523

2624
private static $isSecure = false;
@@ -126,15 +124,10 @@ public function connect(
126124
$port = empty($port) ? $this->database->getPort() : $port;
127125
$charset = empty($charset) ? $this->database->getCharset() : $charset;
128126

129-
// Must have a user and a password
130-
if ( empty($user ) ) {
131-
$this->register_error($this->ezsql_mysql_str[1] . ' in ' . __FILE__ . ' on line ' . __LINE__);
132-
$this->show_errors ? \trigger_error($this->ezsql_mysql_str[1], \E_USER_WARNING) : null;
133-
} else if ( ! $this->dbh = \mysqli_connect($host, $user, $password, $this->database->getName(), (int) $port)
134-
) {
135-
// Try to establish the server database handle
136-
$this->register_error($this->ezsql_mysql_str[2] . ' in ' . __FILE__ . ' on line ' . __LINE__);
137-
$this->show_errors ? \trigger_error($this->ezsql_mysql_str[2], \E_USER_WARNING) : null;
127+
// Try to establish the server database handle
128+
if ( ! $this->dbh = \mysqli_connect($host, $user, $password, $this->database->getName(), (int) $port) ) {
129+
$this->register_error(\FAILED_CONNECTION . ' in ' . __FILE__ . ' on line ' . __LINE__);
130+
$this->show_errors ? \trigger_error(\FAILED_CONNECTION, \E_USER_WARNING) : null;
138131
} else {
139132
\mysqli_set_charset($this->dbh, $charset);
140133
$this->_connected = true;
@@ -156,17 +149,17 @@ public function select($name = '', $charset = '')
156149
$name = empty($name) ? $this->database->getName() : $name;
157150
if ( ! $name ) {
158151
// Must have a database name
159-
$this->register_error($this->ezsql_mysql_str[3] . ' in ' . __FILE__ . ' on line ' . __LINE__);
160-
$this->show_errors ? \trigger_error($this->ezsql_mysql_str[3], \E_USER_WARNING) : null;
152+
$this->register_error($this->ezsql_mysql_str[1] . ' in ' . __FILE__ . ' on line ' . __LINE__);
153+
$this->show_errors ? \trigger_error($this->ezsql_mysql_str[1], \E_USER_WARNING) : null;
161154
} elseif ( ! $this->dbh ) {
162155
// Must have an active database connection
163-
$this->register_error($this->ezsql_mysql_str[4] . ' in ' . __FILE__ . ' on line ' . __LINE__);
164-
$this->show_errors ? \trigger_error($this->ezsql_mysql_str[4], \E_USER_WARNING) : null;
156+
$this->register_error($this->ezsql_mysql_str[2] . ' in ' . __FILE__ . ' on line ' . __LINE__);
157+
$this->show_errors ? \trigger_error($this->ezsql_mysql_str[2], \E_USER_WARNING) : null;
165158
} elseif ( !\mysqli_select_db($this->dbh, $name) ) {
166159
// Try to connect to the database
167160
// Try to get error supplied by mysql if not use our own
168161
if ( !$str = \mysqli_error($this->dbh)) {
169-
$str = $this->ezsql_mysql_str[5];
162+
$str = $this->ezsql_mysql_str[3];
170163
}
171164

172165
$this->register_error($str . ' in ' .__FILE__ . ' on line ' . __LINE__);

lib/Database/ez_pgsql.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@
1010

1111
class ez_pgsql extends ezsqlModel implements DatabaseInterface
1212
{
13-
/**
14-
* ezSQL error strings - PostgreSQL
15-
*/
16-
private $_ezsql_postgresql_str = array
17-
(
18-
1 => 'Require $user and $password to connect to a database server',
19-
2 => 'Error establishing PostgreSQL database connection. Correct user/password? Correct hostname? Database server running?',
20-
3 => 'Require $dbname to select a database',
21-
4 => 'mySQL database connection is not active',
22-
5 => 'Unexpected error while trying to select database',
23-
);
24-
2513
private static $isSecure = false;
2614
private static $secure = null;
2715

@@ -116,14 +104,10 @@ public function connect(
116104

117105
$connect_string = "host=".$host." port=".$port." dbname=".$name." user=".$user." password=".$password;
118106

119-
if (!$user) {
120-
// Must have a user and a password
121-
$this->register_error($this->_ezsql_postgresql_str[1] . ' in ' . __FILE__ . ' on line ' . __LINE__);
122-
$this->show_errors ? \trigger_error($this->_ezsql_postgresql_str[1], \E_USER_WARNING) : null;
123-
} else if (!$this->dbh = \pg_connect($connect_string, true)) {
124-
// Try to establish the server database handle
125-
$this->register_error($this->_ezsql_postgresql_str[2] . ' in ' . __FILE__ . ' on line ' . __LINE__);
126-
$this->show_errors ? \trigger_error($this->_ezsql_postgresql_str[2], \E_USER_WARNING) : null;
107+
// Try to establish the server database handle
108+
if (!$this->dbh = \pg_connect($connect_string, true)) {
109+
$this->register_error(\FAILED_CONNECTION . ' in ' . __FILE__ . ' on line ' . __LINE__);
110+
$this->show_errors ? \trigger_error(\FAILED_CONNECTION, \E_USER_WARNING) : null;
127111
} else {
128112
$this->_connected = true;
129113
}

lib/Database/ez_sqlite3.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010

1111
class ez_sqlite3 extends ezsqlModel implements DatabaseInterface
1212
{
13-
/**
14-
* ezSQL error strings - SQLite
15-
*/
16-
private $ezsql_sqlite3_str = array
17-
(
18-
1 => 'Require $path and $name to open an SQLite database',
19-
2 => 'Failed to make connection to database',
20-
);
21-
2213
/**
2314
* Database connection handle
2415
* @var resource
@@ -73,27 +64,21 @@ public function settings()
7364
*/
7465
public function connect($path = '', $name = '')
7566
{
76-
$return_val = false;
7767
$this->_connected = false;
7868

7969
$path = empty($path) ? $this->database->getPath() : $path;
80-
$setPassword = empty($name) ? $this->database->getName() : $name;
81-
82-
// Must have a user and a password
83-
if (!$path || !$name) {
84-
$this->register_error($this->ezsql_sqlite3_str[1] . ' in ' . __FILE__ . ' on line ' . __LINE__);
85-
$this->show_errors ? \trigger_error($this->ezsql_sqlite3_str[1], \E_USER_WARNING) : null;
86-
// Try to establish the server database handle
87-
} elseif (!$this->dbh = @new \SQLite3($path . $name)) {
88-
$this->register_error($this->ezsql_sqlite3_str[2]);
89-
$this->show_errors ? \trigger_error($this->ezsql_sqlite3_str[2], \E_USER_WARNING) : null;
70+
$name = empty($name) ? $this->database->getName() : $name;
71+
72+
// Try to establish the server database handle
73+
if (!$this->dbh = @new \SQLite3($path . $name)) {
74+
//$this->register_error(\FAILED_CONNECTION);
75+
//$this->show_errors ? \trigger_error(\FAILED_CONNECTION, \E_USER_WARNING) : null;
9076
} else {
91-
$return_val = true;
9277
$this->conn_queries = 0;
9378
$this->_connected = true;
9479
}
9580

96-
return $return_val;
81+
return $this->_connected;
9782
}
9883

9984
/**

lib/Database/ez_sqlsrv.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@
1010

1111
class ez_sqlsrv extends ezsqlModel implements DatabaseInterface
1212
{
13-
/**
14-
* ezSQL error strings - sqlsrv
15-
*/
16-
private $ezsql_sqlsrv_str = array
17-
(
18-
1 => 'Require $user and $password to connect to a database server',
19-
2 => 'Error establishing sqlsrv database connection. Correct user/password? Correct hostname? Database server running?',
20-
3 => 'Require $name to select a database',
21-
4 => 'SQL Server database connection is not active',
22-
5 => 'Unexpected error while trying to select database',
23-
);
24-
2513
/**
2614
* ezSQL non duplicating data type id's; converting type ids to str
2715
*/
@@ -95,7 +83,6 @@ public function quick_connect($user = '', $password = '', $name = '', $host = 'l
9583
*/
9684
public function connect($user = '', $password = '', $name = '', $host = 'localhost')
9785
{
98-
$return_val = false;
9986
$this->_connected = false;
10087

10188
$user = empty($user) ? $this->database->getUser() : $user;
@@ -122,16 +109,16 @@ public function connect($user = '', $password = '', $name = '', $host = 'localho
122109
);
123110
}
124111

112+
// Try to establish the server database handle
125113
if (($this->dbh = @\sqlsrv_connect($host, $connectionOptions)) === false) {
126-
$this->register_error($this->ezsql_sqlsrv_str[2] . ' in ' . __FILE__ . ' on line ' . __LINE__);
127-
$this->show_errors ? \trigger_error($this->ezsql_sqlsrv_str[2], \E_USER_WARNING) : null;
114+
$this->register_error(\FAILED_CONNECTION . ' in ' . __FILE__ . ' on line ' . __LINE__);
115+
$this->show_errors ? \trigger_error(\FAILED_CONNECTION, \E_USER_WARNING) : null;
128116
} else {
129-
$return_val = true;
130117
$this->_connected = true;
131118
$this->conn_queries = 0;
132119
}
133120

134-
return $return_val;
121+
return $this->_connected;
135122
}
136123

137124
/**

tests/EZTestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ abstract class EZTestCase extends \PHPUnit\Framework\TestCase
4242

4343
protected $errors;
4444

45-
function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
45+
public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
4646
{
4747
$this->errors[] = compact("errno", "errstr", "errfile", "errline", "errcontext");
4848
}
4949

50-
function assertError($errstr, $errno)
50+
public function assertError($errstr, $errno)
5151
{
5252
foreach ($this->errors as $error) {
5353
if ($error["errstr"] === $errstr && $error["errno"] === $errno) {
@@ -67,7 +67,7 @@ function assertError($errstr, $errno)
6767
*
6868
* @return mixed Method return.
6969
*/
70-
function invokeMethod(&$object, $methodName, array $parameters = array())
70+
public function invokeMethod(&$object, $methodName, array $parameters = array())
7171
{
7272
$reflection = new \ReflectionClass(get_class($object));
7373
$method = $reflection->getMethod($methodName);

tests/mysqli/mysqliTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ezsql\Tests\mysqli;
44

55
use ezsql\Database;
6+
use ezsql\Config;
67
use ezsql\Database\ez_mysqli;
78
use ezsql\Tests\EZTestCase;
89

@@ -226,13 +227,18 @@ public function testGetCharset()
226227

227228
/**
228229
* @covers ezsql\Database\ez_mysqli::disconnect
230+
* @covers ezsql\Database\ez_mysqli::reset
231+
* @covers ezsql\Database\ez_mysqli::handle
229232
*/
230233
public function testDisconnect()
231234
{
232235
$this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD);
233236
$this->object->select(self::TEST_DB_NAME);
237+
$this->assertNotNull($this->object->handle());
234238
$this->object->disconnect();
235239
$this->assertFalse($this->object->isConnected());
240+
$this->object->reset();
241+
$this->assertNull($this->object->handle());
236242
} // testDisconnect
237243

238244
/**
@@ -520,9 +526,17 @@ public function testQuery_prepared() {
520526
/**
521527
* @covers ezsql\Database\ez_mysqli::__construct
522528
*/
523-
public function test__Construct() {
529+
public function test__construct_Error() {
524530
$this->expectException(\Exception::class);
525531
$this->expectExceptionMessageRegExp('/[Missing configuration details]/');
526-
$this->assertNull(new ez_mysqli());
532+
$this->assertNull(new ez_mysqli());
533+
}
534+
535+
/**
536+
* @covers ezsql\Database\ez_mysqli::__construct
537+
*/
538+
public function test__construct() {
539+
$settings = new Config('mysqli', [self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME]);
540+
$this->assertNotNull(new ez_mysqli($settings));
527541
}
528542
} // ez_mysqliTest

tests/pdo/pdo_mysqlTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ezsql\Tests\pdo;
44

55
use ezsql\Database;
6+
use ezsql\Config;
67
use ezsql\Database\ez_pdo;
78
use ezsql\Tests\EZTestCase;
89

@@ -242,14 +243,18 @@ public function testSelecting()
242243

243244
/**
244245
* @covers ezsql\Database\ez_pdo::disconnect
246+
* @covers ezsql\Database\ez_pdo::reset
247+
* @covers ezsql\Database\ez_pdo::handle
245248
*/
246249
public function testMySQLDisconnect() {
247250
$this->assertTrue($this->object->connect('mysql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD));
248-
251+
$this->assertTrue($this->object->isConnected());
252+
$this->assertNotNull($this->object->handle());
249253
$this->object->disconnect();
250-
251254
$this->assertFalse($this->object->isConnected());
252-
}
255+
$this->object->reset();
256+
$this->assertNull($this->object->handle());
257+
} // testDisconnect
253258

254259
/**
255260
* @covers ezsql\Database\ez_pdo::connect
@@ -265,8 +270,18 @@ public function testMySQLConnectWithOptions() {
265270
/**
266271
* @covers ezsql\Database\ez_pdo::__construct
267272
*/
268-
public function test__Construct() {
273+
public function test__Construct_Error() {
269274
$this->expectExceptionMessageRegExp('/[Missing configuration details]/');
270275
$this->assertNull(new ez_pdo('bad'));
271-
}
276+
}
277+
278+
/**
279+
* @covers ezsql\Database\ez_pdo::__construct
280+
*/
281+
public function test__construct() {
282+
283+
$dsn = 'mysql:host='.self::TEST_DB_HOST.';dbname='. self::TEST_DB_NAME.';port=3306';
284+
$settings = Config::initialize('pdo', [$dsn, self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
285+
$this->assertNotNull(new ez_pdo($settings));
286+
}
272287
}

0 commit comments

Comments
 (0)