This commit is contained in:
Mario Vavti 2018-02-07 09:03:49 +01:00
commit 91dda86a74
13 changed files with 432 additions and 343 deletions

View file

@ -1,15 +1,20 @@
<?php
/**
* @file dba_driver.php
* @brief Some database related functions and database classes.
*
* This file contains the abstract database driver class dba_driver, the
* database class DBA and some functions for working with databases.
*/
/**
* @brief Database classs with database factory method.
*
* The factory will return a database driver which is an implementation of the
* abstract dba_driver class.
*/
class DBA {
/**
* @file dba_driver.php
* @brief some database related functions and abstract driver class.
*
* This file contains the abstract database driver class dba_driver and some
* functions for working with databases.
*/
static public $dba = null;
static public $dbtype = null;
static public $scheme = 'mysql';
@ -21,7 +26,6 @@ class DBA {
static public $tquot = "`";
/**
* @brief Returns the database driver object.
*
@ -34,14 +38,11 @@ class DBA {
* @param bool $install Defaults to false
* @return null|dba_driver A database driver object (dba_pdo) or null if no driver found.
*/
static public function dba_factory($server,$port,$user,$pass,$db,$dbtype,$install = false) {
self::$dba = null;
self::$dbtype = intval($dbtype);
if(self::$dbtype == DBTYPE_POSTGRES) {
if(!($port))
$port = 5432;
@ -50,7 +51,6 @@ class DBA {
self::$utc_now = "now() at time zone 'UTC'";
self::$tquot = '"';
self::$scheme = 'pgsql';
}
else {
@ -66,40 +66,27 @@ class DBA {
require_once('include/dba/dba_pdo.php');
self::$dba = new dba_pdo($server,self::$scheme,$port,$user,$pass,$db,$install);
if(is_object(self::$dba) && self::$dba->connected) {
if(strpbrk($server,':;')) {
$dsn = $server;
}
else {
$dsn = self::$scheme . ':host=' . $server . (intval($port) ? '' : ';port=' . $port);
}
$dsn .= ';dbname=' . $db;
self::$dba->pdo_set(array($dsn,$user,$pass));
}
define('NULL_DATE', self::$null_date);
define('ACTIVE_DBTYPE', self::$dbtype);
define('TQUOT', self::$tquot);
return self::$dba;
}
}
/**
* @brief abstract database driver class.
* @brief Abstract database driver class.
*
* This class gets extended by the real database driver classes, e.g. dba_mysql,
* dba_mysqli.
* This class gets extended by the real database driver class. We used to have
* dba_mysql, dba_mysqli or dba_postgres, but we moved to PDO and the only
* implemented driver is dba_pdo.
*/
abstract class dba_driver {
// legacy behavior
public $db;
protected $pdo = array();
public $debug = 0;
public $connected = false;
@ -111,6 +98,7 @@ abstract class dba_driver {
* This abstract function needs to be implemented in the real driver.
*
* @param string $server DB server name
* @param string $scheme DB scheme
* @param string $port DB port
* @param string $user DB username
* @param string $pass DB password
@ -166,6 +154,7 @@ abstract class dba_driver {
$platform_name = \Zotlabs\Lib\System::get_platform_name();
if(file_exists('install/' . $platform_name . '/' . \DBA::$install_script))
return 'install/' . $platform_name . '/' . \DBA::$install_script;
return 'install/' . \DBA::$install_script;
}
@ -173,7 +162,6 @@ abstract class dba_driver {
return \DBA::$tquot;
}
function utcnow() {
return \DBA::$utc_now;
}
@ -232,19 +220,12 @@ abstract class dba_driver {
return $str;
}
function pdo_set($x) {
$this->pdo = $x;
}
function pdo_get() {
return $this->pdo;
}
} // end abstract dba_driver class
//
// Procedural functions
//
function printable($s) {
$s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
@ -275,7 +256,7 @@ function dbg($state) {
* wrapping with intval().
*
* @param string $str A string to pass to a DB query
* @return Return an escaped string of the value to pass to a DB query.
* @return string Return an escaped string of the value to pass to a DB query.
*/
function dbesc($str) {
@ -298,6 +279,7 @@ function dbunescbin($str) {
function dbescdate($date) {
if(is_null_date($date))
return \DBA::$dba->escape(NULL_DATE);
return \DBA::$dba->escape($date);
}
@ -330,17 +312,17 @@ function db_use_index($str) {
*
* printf style arguments %s and %d are replaced with variable arguments, which
* should each be appropriately dbesc() or intval().
*
* SELECT queries return an array of results or false if SQL or DB error. Other
* queries return true if the command was successful or false if it wasn't.
*
* Example:
* $r = q("SELECT * FROM %s WHERE `uid` = %d",
* 'user', 1);
* @code{.php}$r = q("SELECT * FROM %s WHERE `uid` = %d",
* 'user', 1);@endcode
*
* @param string $sql The SQL query to execute
* @return bool|array
*/
function q($sql) {
$args = func_get_args();
@ -359,8 +341,8 @@ function q($sql) {
}
/*
* This will happen occasionally trying to store the
* session data after abnormal program termination
* This will happen occasionally trying to store the
* session data after abnormal program termination
*/
db_logger('dba: no database: ' . print_r($args,true),LOGGER_NORMAL,LOG_CRIT);
@ -389,8 +371,8 @@ function dbq($sql) {
// Caller is responsible for ensuring that any integer arguments to
// dbesc_array are actually integers and not malformed strings containing
// SQL injection vectors. All integer array elements should be specifically
// cast to int to avoid trouble.
// SQL injection vectors. All integer array elements should be specifically
// cast to int to avoid trouble.
function dbesc_array_cb(&$item, $key) {
if(is_string($item)) {
@ -423,7 +405,7 @@ function dbesc_array(&$arr) {
function db_getfunc($f) {
$lookup = array(
'rand'=>array(
DBTYPE_MYSQL=>'RAND()',
DBTYPE_MYSQL=>'RAND()',
DBTYPE_POSTGRES=>'RANDOM()'
),
'utc_timestamp'=>array(

View file

@ -1,67 +0,0 @@
<?php
require_once('include/dba/dba_driver.php');
class dba_mysql extends dba_driver {
function connect($server, $scheme, $port, $user,$pass,$db) {
$this->db = mysql_connect($server.":".$port,$user,$pass);
if($this->db && mysql_select_db($db,$this->db)) {
$this->connected = true;
}
if($this->connected) {
return true;
}
return false;
}
function q($sql) {
if((! $this->db) || (! $this->connected))
return false;
$this->error = '';
$result = @mysql_query($sql,$this->db);
if(mysql_errno($this->db))
$this->error = mysql_error($this->db);
if($result === false || $this->error) {
logger('dba_mysql: ' . printable($sql) . ' returned false.' . "\n" . $this->error);
if(file_exists('dbfail.out'))
file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND);
}
if(($result === true) || ($result === false))
return $result;
$r = array();
if(mysql_num_rows($result)) {
while($x = mysql_fetch_array($result,MYSQL_ASSOC))
$r[] = $x;
mysql_free_result($result);
if($this->debug)
logger('dba_mysql: ' . printable(print_r($r,true)));
}
return $r;
}
function escape($str) {
if($this->db && $this->connected) {
return @mysql_real_escape_string($str,$this->db);
}
}
function close() {
if($this->db)
mysql_close($this->db);
$this->connected = false;
}
function getdriver() {
return 'mysql';
}
}

View file

@ -1,86 +0,0 @@
<?php /** @file */
require_once('include/dba/dba_driver.php');
class dba_mysqli extends dba_driver {
function connect($server,$scheme,$port,$user,$pass,$db) {
if($port)
$this->db = new mysqli($server,$user,$pass,$db, $port);
else
$this->db = new mysqli($server,$user,$pass,$db);
if($this->db->connect_error) {
$this->connected = false;
$this->error = $this->db->connect_error;
if(file_exists('dbfail.out')) {
file_put_contents('dbfail.out', datetime_convert() . "\nConnect: " . $this->error . "\n", FILE_APPEND);
}
return false;
}
else {
$this->connected = true;
return true;
}
}
function q($sql) {
if((! $this->db) || (! $this->connected))
return false;
$this->error = '';
$result = $this->db->query($sql);
if($this->db->errno)
$this->error = $this->db->error;
if($this->error) {
db_logger('dba_mysqli: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR);
if(file_exists('dbfail.out')) {
file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . "\n" . $this->error . "\n", FILE_APPEND);
}
}
if(($result === true) || ($result === false)) {
if($this->debug) {
db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returns ' . (($result) ? 'true' : 'false'), LOGGER_NORMAL,(($result) ? LOG_INFO : LOG_ERR));
}
return $result;
}
if($this->debug) {
db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returned ' . $result->num_rows . ' results.', LOGGER_NORMAL, LOG_INFO);
}
$r = array();
if($result->num_rows) {
while($x = $result->fetch_array(MYSQLI_ASSOC))
$r[] = $x;
$result->free_result();
if($this->debug) {
db_logger('dba_mysqli: ' . printable(print_r($r,true)), LOGGER_NORMAL, LOG_INFO);
}
}
return $r;
}
function escape($str) {
if($this->db && $this->connected) {
return @$this->db->real_escape_string($str);
}
}
function close() {
if($this->db)
$this->db->close();
$this->connected = false;
}
function getdriver() {
return 'mysqli';
}
}

View file

@ -1,14 +1,21 @@
<?php /** @file */
<?php
require_once('include/dba/dba_driver.php');
require_once 'include/dba/dba_driver.php';
/**
* @brief PDO based database driver.
*
*/
class dba_pdo extends dba_driver {
public $driver_dbtype = null;
function connect($server,$scheme,$port,$user,$pass,$db) {
/**
* {@inheritDoc}
* @see dba_driver::connect()
*/
function connect($server, $scheme, $port, $user, $pass, $db) {
$this->driver_dbtype = $scheme;
if(strpbrk($server,':;')) {
@ -17,7 +24,7 @@ class dba_pdo extends dba_driver {
else {
$dsn = $this->driver_dbtype . ':host=' . $server . (intval($port) ? ';port=' . $port : '');
}
$dsn .= ';dbname=' . $db;
try {
@ -36,10 +43,19 @@ class dba_pdo extends dba_driver {
$this->q("SET standard_conforming_strings = 'off'; SET backslash_quote = 'on';");
$this->connected = true;
return true;
return true;
}
/**
* {@inheritDoc}
* @see dba_driver::q()
*
* @return bool|array|PDOStatement
* - \b false if not connected or PDOException occured on query
* - \b array with results on a SELECT query
* - \b PDOStatement on a non SELECT SQL query
*/
function q($sql) {
if((! $this->db) || (! $this->connected))
return false;
@ -50,14 +66,15 @@ class dba_pdo extends dba_driver {
}
}
$result = null;
$this->error = '';
$select = ((stripos($sql,'select') === 0) ? true : false);
$select = ((stripos($sql, 'select') === 0) ? true : false);
try {
$result = $this->db->query($sql, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
$this->error = $e->getMessage();
if($this->error) {
db_logger('dba_pdo: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR);
@ -82,11 +99,10 @@ class dba_pdo extends dba_driver {
}
if($this->debug) {
db_logger('dba_pdo: DEBUG: ' . printable($sql) . ' returned ' . count($r) . ' results.', LOGGER_NORMAL, LOG_INFO);
db_logger('dba_pdo: DEBUG: ' . printable($sql) . ' returned ' . count($r) . ' results.', LOGGER_NORMAL, LOG_INFO);
db_logger('dba_pdo: ' . printable(print_r($r,true)), LOGGER_NORMAL, LOG_INFO);
}
return (($this->error) ? false : $r);
}
@ -99,9 +115,10 @@ class dba_pdo extends dba_driver {
function close() {
if($this->db)
$this->db = null;
$this->connected = false;
}
function concat($fld,$sep) {
if($this->driver_dbtype === 'pgsql') {
return 'string_agg(' . $fld . ',\'' . $sep . '\')';
@ -140,7 +157,7 @@ class dba_pdo extends dba_driver {
return $this->escape($str);
}
}
function unescapebin($str) {
if($this->driver_dbtype === 'pgsql' && (! is_null($str))) {
$x = '';

View file

@ -1,117 +0,0 @@
<?php
require_once('include/dba/dba_driver.php');
class dba_postgres extends dba_driver {
const INSTALL_SCRIPT='install/schema_postgres.sql';
const NULL_DATE = '0001-01-01 00:00:00';
const UTC_NOW = "now() at time zone 'UTC'";
const TQUOT = '"';
function connect($server,$scheme,$port,$user,$pass,$db) {
if(!$port) $port = 5432;
$connstr = 'host=' . $server . ' port='.$port . ' user=' . $user . ' password=' . $pass . ' dbname='. $db;
$this->db = pg_connect($connstr);
if($this->db !== false) {
$this->connected = true;
} else {
$this->connected = false;
}
$this->q("SET standard_conforming_strings = 'off'; SET backslash_quote = 'on';"); // emulate mysql string escaping to prevent massive code-clobber
return $this->connected;
}
function q($sql) {
if((! $this->db) || (! $this->connected))
return false;
if(!strpos($sql, ';'))
$sql .= ';';
if(strpos($sql, '`')) // this is a hack. quoted identifiers should be replaced everywhere in the code with dbesc_identifier(), remove this once it is
$sql = str_replace('`', '"', $sql);
$this->error = '';
$result = @pg_query($this->db, $sql);
if(file_exists('db-allqueries.out')) {
$bt = debug_backtrace();
$trace = array();
foreach($bt as $frame) {
if(!empty($frame['file']) && @strstr($frame['file'], $_SERVER['DOCUMENT_ROOT']))
$frame['file'] = substr($frame['file'], strlen($_SERVER['DOCUMENT_ROOT'])+1);
$trace[] = $frame['file'] . ':' . $frame['function'] . '():' . $frame['line'] ;
}
$compact = join(', ', $trace);
file_put_contents('db-allqueries.out', datetime_convert() . ": " . $sql . ' is_resource: '.var_export(is_resource($result), true).', backtrace: '.$compact."\n\n", FILE_APPEND);
}
if($result === false)
$this->error = pg_last_error($this->db);
if($result === false || $this->error) {
//db_logger('dba_postgres: ' . printable($sql) . ' returned false.' . "\n" . $this->error);
if(file_exists('dbfail.out'))
file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND);
}
if(($result === true) || ($result === false))
return $result;
if(pg_result_status($result) == PGSQL_COMMAND_OK)
return true;
$r = array();
if(pg_num_rows($result)) {
while($x = pg_fetch_array($result, null, PGSQL_ASSOC))
$r[] = $x;
pg_free_result($result);
if($this->debug)
db_logger('dba_postgres: ' . printable(print_r($r,true)));
}
return $r;
}
function escape($str) {
if($this->db && $this->connected) {
$x = @pg_escape_string($this->db, $str);
return $x;
}
}
function escapebin($str) {
return pg_escape_bytea($str);
}
function unescapebin($str) {
return pg_unescape_bytea($str);
}
function close() {
if($this->db)
pg_close($this->db);
$this->connected = false;
}
function quote_interval($txt) {
return "'$txt'";
}
function escape_identifier($str) {
return pg_escape_identifier($this->db, $str);
}
function optimize_table($table) {
// perhaps do some equivalent thing here, vacuum, etc? I think this is the DBA's domain anyway. Applications should not need to muss with this.
// for now do nothing without a compelling reason. function overrides default legacy mysql.
}
function concat($fld, $sep) {
return 'string_agg(' . $fld . ',\'' . $sep . '\')';
}
function getdriver() {
return 'pgsql';
}
}

View file

@ -32,4 +32,15 @@
highLowerBound="70"/>
<log type="testdox-text" target="./results/testdox.txt"/>
</logging>
<php>
<!-- Default test database config, only used if no environment variables
with same names are set.
!!! Never run against a real database, it will truncate all tables -->
<env name="hz_db_server" value="127.0.0.1"/>
<env name="hz_db_scheme" value="pgsql"/>
<env name="hz_db_port" value="5432"/>
<env name="hz_db_user" value="travis_hz"/>
<env name="hz_db_pass" value="hubzilla"/>
<env name="hz_db_database" value="travis_hubzilla"/>
</php>
</phpunit>

View file

@ -35,4 +35,15 @@
highLowerBound="70"/>
<log type="testdox-text" target="./results/testdox.txt"/>
</logging>
<php>
<!-- Default test database config, only used if no environment variables
with same names are set.
!!! Never run against a real database, it will truncate all tables -->
<env name="hz_db_server" value="127.0.0.1"/>
<env name="hz_db_scheme" value="mysql"/>
<env name="hz_db_port" value="3306"/>
<env name="hz_db_user" value="travis_hz"/>
<env name="hz_db_pass" value="hubzilla"/>
<env name="hz_db_database" value="travis_hubzilla"/>
</php>
</phpunit>

View file

@ -25,7 +25,7 @@
# Exit if anything fails
set -e
echo "Preparing for MySQL ..."
echo "Preparing for MySQL/MariaDB ..."
if [[ "$MYSQL_VERSION" == "5.7" ]]; then
echo "Using MySQL 5.7 in Docker container, need to use TCP"
@ -41,13 +41,13 @@ mysql $PROTO -e "SHOW VARIABLES LIKE 'character_set%';"
mysql $PROTO -e "SELECT @@sql_mode;"
# Create Hubzilla database
mysql $PROTO -u root -e "CREATE DATABASE IF NOT EXISTS hubzilla;";
mysql $PROTO -u root -e "CREATE USER 'hubzilla'@'localhost' IDENTIFIED BY 'hubzilla';"
mysql $PROTO -u root -e "GRANT ALL ON hubzilla.* TO 'hubzilla'@'localhost';"
mysql $PROTO -u root -e "CREATE DATABASE IF NOT EXISTS travis_hubzilla;";
mysql $PROTO -u root -e "CREATE USER 'travis_hz'@'%' IDENTIFIED BY 'hubzilla';"
mysql $PROTO -u root -e "GRANT ALL ON travis_hubzilla.* TO 'travis_hz'@'%';"
# Import table structure
mysql $PROTO -u root hubzilla < ./install/schema_mysql.sql
mysql $PROTO -u root travis_hubzilla < ./install/schema_mysql.sql
# Show databases and tables
mysql $PROTO -u root -e "SHOW DATABASES;"
mysql $PROTO -u root -e "USE hubzilla; SHOW TABLES;"
mysql $PROTO -u root -e "USE travis_hubzilla; SHOW TABLES;"

View file

@ -33,12 +33,17 @@ psql --version
psql -U postgres -c "SELECT VERSION();"
# Create Hubzilla database
psql -U postgres -c "DROP DATABASE IF EXISTS hubzilla;"
psql -U postgres -c "CREATE DATABASE hubzilla;"
psql -U postgres -c "DROP DATABASE IF EXISTS travis_hubzilla;"
psql -U postgres -v ON_ERROR_STOP=1 <<-EOSQL
CREATE USER travis_hz WITH PASSWORD 'hubzilla';
CREATE DATABASE travis_hubzilla;
ALTER DATABASE travis_hubzilla OWNER TO travis_hz;
GRANT ALL PRIVILEGES ON DATABASE travis_hubzilla TO travis_hz;
EOSQL
# Import table structure
psql -U postgres -v ON_ERROR_STOP=1 hubzilla < ./install/schema_postgres.sql
psql -U travis_hz -v ON_ERROR_STOP=1 travis_hubzilla < ./install/schema_postgres.sql
# Show databases and tables
psql -U postgres -l
psql -U postgres -d hubzilla -c "\dt;"
psql -U postgres -d travis_hubzilla -c "\dt;"

View file

@ -0,0 +1,68 @@
<?php
/* Copyright (c) 2017 Hubzilla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Zotlabs\Tests\Unit;
use PHPUnit\DbUnit\TestCaseTrait;
use PHPUnit\Framework\TestCase;
/**
* @brief Base class for our Database Unit Tests.
*
* @warning Never run these tests against a production database, because all
* tables will get truncated and there is no way to recover without a backup.
*
* @author Klaus Weidenbach
*/
abstract class DatabaseTestCase extends TestCase {
use TestCaseTrait;
/**
* Only instantiate PDO once for test clean-up/fixture load.
*
* @var \PDO
*/
static private $pdo = null;
/**
* Only instantiate \PHPUnit\DbUnit\Database\Connection once per test.
*
* @var \PHPUnit\DbUnit\Database\Connection
*/
private $conn = null;
final public function getConnection() {
if ($this->conn === null) {
if (self::$pdo === null) {
$dsn = \getenv('hz_db_scheme') . ':host=' . \getenv('hz_db_server')
. ';port=' . \getenv('hz_db_port') . ';dbname=' . \getenv('hz_db_database');
self::$pdo = new \PDO($dsn, \getenv('hz_db_user'), \getenv('hz_db_pass'));
}
$this->conn = $this->createDefaultDBConnection(self::$pdo, \getenv('hz_db_database'));
}
return $this->conn;
}
}

View file

@ -0,0 +1,67 @@
<?php
/*
* Copyright (c) 2017 Hubzilla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Zotlabs\Tests\Unit\includes;
use Zotlabs\Tests\Unit\UnitTestCase;
// required because of process isolation and no autoloading
require_once 'include/dba/dba_driver.php';
/**
* @brief Unit Test case for include/dba/DBA.php file.
*
* This test needs process isolation because of static \DBA.
* @runTestsInSeparateProcesses
*/
class DBATest extends UnitTestCase {
public function testDbaFactoryMysql() {
$this->assertNull(\DBA::$dba);
$ret = \DBA::dba_factory('server', 'port', 'user', 'pass', 'db', '0');
$this->assertInstanceOf('dba_pdo', $ret);
$this->assertFalse($ret->connected);
$this->assertSame('mysql', \DBA::$scheme);
$this->assertSame('schema_mysql.sql', \DBA::$install_script);
$this->assertSame('0001-01-01 00:00:00', \DBA::$null_date);
$this->assertSame('UTC_TIMESTAMP()', \DBA::$utc_now);
$this->assertSame('`', \DBA::$tquot);
}
public function testDbaFactoryPostgresql() {
$this->assertNull(\DBA::$dba);
$ret = \DBA::dba_factory('server', 'port', 'user', 'pass', 'db', '1');
$this->assertInstanceOf('dba_pdo', $ret);
$this->assertFalse($ret->connected);
$this->assertSame('pgsql', \DBA::$scheme);
$this->assertSame('schema_postgres.sql', \DBA::$install_script);
$this->assertSame('0001-01-01 00:00:00', \DBA::$null_date);
$this->assertSame("now() at time zone 'UTC'", \DBA::$utc_now);
$this->assertSame('"', \DBA::$tquot);
}
}

View file

@ -0,0 +1,9 @@
account:
-
account_id: 42
account_email: "hubzilla@example.com"
account_language: "no"
-
account_id: 43
account_email: "hubzilla@example.org"
account_language: "de"

View file

@ -0,0 +1,189 @@
<?php
/*
* Copyright (c) 2017 Hubzilla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Zotlabs\Tests\Unit\includes;
use Zotlabs\Tests\Unit\DatabaseTestCase;
use PHPUnit\DbUnit\TestCaseTrait;
use PHPUnit\DbUnit\DataSet\YamlDataSet;
require_once 'include/dba/dba_pdo.php';
/**
* @brief Unit Test case for include/dba/dba_pdo.php file.
*
* Some very basic tests to see if our database layer can connect to a real
* database.
*/
class dba_pdoTest extends DatabaseTestCase {
use TestCaseTrait;
/**
* @var \dba_driver
*/
protected $dba;
/**
* Set initial state of the database before each test is executed.
* Load database fixtures.
*
* @return \PHPUnit\DbUnit\DataSet\IDataSet
*/
public function getDataSet() {
return new YamlDataSet(dirname(__FILE__) . '/_files/account.yml');
}
protected function setUp() {
// Will invoke getDataSet() to load fixtures into DB
parent::setUp();
$this->dba = new \dba_pdo(
\getenv('hz_db_server'),
\getenv('hz_db_scheme'),
\getenv('hz_db_port'),
\getenv('hz_db_user'),
\getenv('hz_db_pass'),
\getenv('hz_db_database')
);
}
protected function assertPreConditions() {
$this->assertSame('pdo', $this->dba->getdriver(), "Driver is expected to be 'pdo'.");
$this->assertInstanceOf('dba_driver', $this->dba);
$this->assertTrue($this->dba->connected, 'Pre condition failed, DB is not connected.');
$this->assertInstanceOf('PDO', $this->dba->db);
}
protected function tearDown() {
$this->dba = null;
}
/**
* @group mysql
*/
public function testQuoteintervalOnMysql() {
$this->assertSame('value', $this->dba->quote_interval('value'));
}
/**
* @group postgresql
*/
public function testQuoteintervalOnPostgresql() {
$this->assertSame("'value'", $this->dba->quote_interval('value'));
}
/**
* @group mysql
*/
public function testGenerateMysqlConcatSql() {
$this->assertSame('GROUP_CONCAT(DISTINCT field SEPARATOR \';\')', $this->dba->concat('field', ';'));
$this->assertSame('GROUP_CONCAT(DISTINCT field2 SEPARATOR \' \')', $this->dba->concat('field2', ' '));
}
/**
* @group postgresql
*/
public function testGeneratePostgresqlConcatSql() {
$this->assertSame('string_agg(field,\';\')', $this->dba->concat('field', ';'));
$this->assertSame('string_agg(field2,\' \')', $this->dba->concat('field2', ' '));
}
public function testConnectToSqlServer() {
// connect() is done in dba_pdo constructor which is called in setUp()
$this->assertTrue($this->dba->connected);
}
/**
* @depends testConnectToSqlServer
*/
public function testCloseSqlServerConnection() {
$this->dba->close();
$this->assertNull($this->dba->db);
$this->assertFalse($this->dba->connected);
}
/**
* @depends testConnectToSqlServer
*/
public function testSelectQueryShouldReturnArray() {
$ret = $this->dba->q('SELECT * FROM account');
$this->assertTrue(is_array($ret));
}
/**
* @depends testConnectToSqlServer
*/
public function testInsertQueryShouldReturnPdostatement() {
// Fixture account.yml adds two entries to account table
$this->assertEquals(2, $this->getConnection()->getRowCount('account'), 'Pre-Condition');
$ret = $this->dba->q('INSERT INTO account
(account_id, account_email, account_language)
VALUES (100, \'insert@example.com\', \'de\')
');
$this->assertInstanceOf('PDOStatement', $ret);
$this->assertEquals(3, $this->getConnection()->getRowCount('account'), 'Inserting failed');
}
public function testConnectToWrongSqlServer() {
$nodba = new \dba_pdo('wrongserver',
\getenv('hz_db_scheme'), \getenv('hz_db_port'),
\getenv('hz_db_user'), \getenv('hz_db_pass'),
\getenv('hz_db_database')
);
$this->assertSame('pdo', $nodba->getdriver());
$this->assertInstanceOf('dba_pdo', $nodba);
$this->assertFalse($nodba->connected);
$this->assertNull($nodba->db);
$this->assertFalse($nodba->q('SELECT * FROM account'));
}
/**
* @depends testConnectToSqlServer
*/
public function testSelectQueryToNonExistentTableShouldReturnFalse() {
$ret = $this->dba->q('SELECT * FROM non_existent_table');
$this->assertFalse($ret);
}
/**
* @depends testConnectToSqlServer
*/
public function testInsertQueryToNonExistentTableShouldReturnEmptyArray() {
$ret = $this->dba->q('INSERT INTO non_existent_table
(account_email, account_language)
VALUES (\'email@example.com\', \'en\')
');
$this->assertNotInstanceOf('PDOStatement', $ret);
$this->isEmpty($ret);
}
}