ulogger-server/.tests/lib/BaseDatabaseTestCase.php

270 lines
8.4 KiB
PHP
Raw Normal View History

2017-09-05 09:36:10 +02:00
<?php
2019-05-15 12:03:55 +02:00
if (!defined("ROOT_DIR")) { define("ROOT_DIR", __DIR__ . "/../.."); }
2020-02-20 17:08:47 +01:00
require_once(__DIR__ . "/../../helpers/config.php");
2019-05-15 12:03:55 +02:00
2017-09-05 09:36:10 +02:00
abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase {
2019-05-15 12:03:55 +02:00
/**
* @var PDO $pdo
*/
2020-02-17 18:51:27 +01:00
static private $pdo;
2019-05-15 12:03:55 +02:00
/**
* @var PHPUnit_Extensions_Database_DB_IDatabaseConnection $conn
*/
2020-02-17 18:51:27 +01:00
private $conn;
2019-01-24 19:07:41 +01:00
static private $driver = "mysql";
2017-09-05 09:36:10 +02:00
2020-02-20 17:08:47 +01:00
protected $mockConfig;
2017-09-05 09:36:10 +02:00
protected $testUser = "testUser";
protected $testUser2 = "testUser2";
protected $testAdminUser = "admin";
protected $testAdminPass = "admin";
protected $testPass = "testPass1234567890-;";
protected $testUserId = 1;
protected $testUserId2 = 2;
protected $testUserId3 = 3;
protected $testTrackId = 1;
protected $testTrackId2 = 2;
protected $testTrackName = "test track";
protected $testTrackComment = "test track comment";
protected $testTimestamp = 1502974402;
2020-02-17 18:51:27 +01:00
protected $testLat = 0.0;
2017-09-19 22:04:40 +02:00
protected $testLon = 10.604001083;
2017-09-05 09:36:10 +02:00
protected $testAltitude = 10.01;
protected $testSpeed = 10.01;
protected $testBearing = 10.01;
protected $testAccuracy = 10;
protected $testProvider = "gps";
protected $testComment = "test comment";
2019-07-12 21:50:21 +02:00
protected $testImage = "1234_1502974402_5d1a1960335cf.jpg";
2017-09-05 09:36:10 +02:00
2019-01-24 19:07:41 +01:00
// Fixes PostgreSQL: "cannot truncate a table referenced in a foreign key constraint"
protected function getSetUpOperation() {
return PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT(TRUE);
}
public function setUp() {
parent::setUp();
2020-02-20 17:08:47 +01:00
$this->mockConfig = new uConfig(false);
2019-01-24 19:07:41 +01:00
}
2017-09-05 09:36:10 +02:00
public static function setUpBeforeClass() {
if (file_exists(__DIR__ . '/../.env')) {
2019-02-28 11:03:41 +01:00
$dotenv = Dotenv\Dotenv::create(__DIR__ . '/..');
2017-09-05 09:36:10 +02:00
$dotenv->load();
2019-01-24 19:07:41 +01:00
$dotenv->required(['DB_DSN', 'DB_USER', 'DB_PASS']);
2017-09-05 09:36:10 +02:00
}
2019-01-24 19:07:41 +01:00
$db_dsn = getenv('DB_DSN');
2017-09-05 09:36:10 +02:00
$db_user = getenv('DB_USER');
$db_pass = getenv('DB_PASS');
// pdo connection
if (self::$pdo == null) {
2019-01-24 19:07:41 +01:00
self::$pdo = new PDO($db_dsn, $db_user, $db_pass);
self::$driver = self::$pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
2017-09-05 09:36:10 +02:00
}
}
public static function tearDownAfterClass() {
self::$pdo = null;
}
/**
* Set up database connection
* This will also override uDb class connection
*
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
public function getConnection() {
if ($this->conn === null) {
$this->conn = $this->createDefaultDBConnection(self::$pdo, getenv('DB_NAME'));
}
return $this->conn;
}
/**
* Create data set from xml fixture
*
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
protected function getDataSet() {
2019-01-28 23:03:03 +01:00
$this->resetAutoincrement();
return $this->createFlatXMLDataSet(__DIR__ . '/../fixtures/fixture_empty.xml');
2017-09-05 09:36:10 +02:00
}
2020-02-19 18:42:44 +01:00
protected function resetAutoincrement($users = 1, $tracks = 1, $positions = 1, $layers = 1) {
2020-02-17 18:51:27 +01:00
if (self::$driver === "pgsql") {
self::$pdo->exec("ALTER SEQUENCE users_id_seq RESTART WITH $users");
self::$pdo->exec("ALTER SEQUENCE tracks_id_seq RESTART WITH $tracks");
self::$pdo->exec("ALTER SEQUENCE positions_id_seq RESTART WITH $positions");
2020-02-19 18:42:44 +01:00
self::$pdo->exec("ALTER SEQUENCE ol_layers_id_seq RESTART WITH $layers");
2020-02-17 18:51:27 +01:00
} else if (self::$driver === "sqlite") {
2019-05-15 12:03:55 +02:00
$retry = 1;
do {
try {
2020-02-17 18:51:27 +01:00
self::$pdo->exec("DELETE FROM sqlite_sequence WHERE NAME = 'users'");
self::$pdo->exec("DELETE FROM sqlite_sequence WHERE NAME = 'tracks'");
self::$pdo->exec("DELETE FROM sqlite_sequence WHERE NAME = 'positions'");
2020-02-19 18:42:44 +01:00
self::$pdo->exec("DELETE FROM sqlite_sequence WHERE NAME = 'ol_layers'");
2019-05-15 12:03:55 +02:00
$retry = 0;
} catch (Exception $e) {
// sqlite raises error when db schema changes in another connection.
if (strpos($e->getMessage(), 'database schema has changed') !== false) {
self::$pdo = null;
self::setUpBeforeClass();
}
}
} while ($retry--);
2019-01-24 19:07:41 +01:00
}
}
2017-09-05 09:36:10 +02:00
/**
* Insert to database from array
*
* @param string $table Table name
* @param array $rowsArr Array of rows
* @return int|null Last insert id if available, NULL otherwise
*/
private function pdoInsert($table, $rowsArr = []) {
$ret = NULL;
if (!empty($rowsArr)) {
$values = ':' . implode(', :', array_keys($rowsArr));
$columns = implode(', ', array_keys($rowsArr));
$query = "INSERT INTO $table ($columns) VALUES ($values)";
$stmt = self::$pdo->prepare($query);
if ($stmt !== false) {
$stmt->execute(array_combine(explode(', ', $values), array_values($rowsArr)));
}
$ret = self::$pdo->lastInsertId();
}
return $ret;
}
2017-09-19 22:04:40 +02:00
/**
* Execute raw insert query on database
*
* @param string $query Insert query
* @return int|null Last insert id if available, NULL otherwise
*/
private function pdoInsertRaw($query) {
$ret = NULL;
if (self::$pdo->exec($query) !== false) {
$ret = self::$pdo->lastInsertId();
}
return $ret;
}
2017-09-05 09:36:10 +02:00
/**
* Get single column from first row of query result
*
* @param string $query SQL query
* @param int $columnNumber Optional column number (default is first column)
* @return string|bool Column or false if no data
*/
protected function pdoGetColumn($query, $columnNumber = 0) {
$column = false;
$stmt = self::$pdo->query($query);
if ($stmt !== false) {
$column = $stmt->fetchColumn($columnNumber);
$stmt->closeCursor();
}
return $column;
}
/**
* Insert user data to database
* If parameters are omitted they default test values are used
*
* @param string $user User login
* @param string $pass User password
2020-02-17 18:51:27 +01:00
* @param bool $isAdmin User is admin
2017-09-05 09:36:10 +02:00
* @return int|bool User id or false on error
*/
2020-02-17 18:51:27 +01:00
protected function addTestUser($user = NULL, $pass = NULL, $isAdmin = false) {
2017-09-05 09:36:10 +02:00
if (is_null($user)) { $user = $this->testUser; }
if (is_null($pass)) { $pass = $this->testPass; }
2020-02-17 18:51:27 +01:00
$id = $this->pdoInsert('users', [ 'login' => $user, 'password' => $pass, 'admin' => (int) $isAdmin ]);
2020-01-07 17:21:40 +01:00
if ($id !== false) {
return (int) $id;
}
return false;
2017-09-05 09:36:10 +02:00
}
/**
* Insert track data to database.
* If parameters are omitted they default test values are used
*
* @param int $userId Optional track id
* @param string $trackName Optional track name
* @param string $comment Optional comment
* @return int|bool Track id or false on error
*/
protected function addTestTrack($userId = NULL, $trackName = NULL, $comment = NULL) {
if (is_null($userId)) { $userId = $this->testUserId; }
if (is_null($trackName)) { $trackName = $this->testTrackName; }
2017-09-19 22:04:40 +02:00
if (is_null($comment)) { $comment = $this->testTrackComment; }
2020-01-07 17:21:40 +01:00
$id = $this->pdoInsert('tracks', [ 'user_id' => $userId, 'name' => $trackName, 'comment' => $comment ]);
if ($id !== false) {
return (int) $id;
}
return false;
2017-09-05 09:36:10 +02:00
}
/**
* Insert position data to database
* If parameters are omitted they default test values are used
*
* @param int $userId
* @param int $trackId
* @param int $timeStamp
* @param double $latitude
* @param double $longitude
* @return int|bool Position id or false on error
*/
protected function addTestPosition($userId = NULL, $trackId = NULL, $timeStamp = NULL, $latitude = NULL, $longitude = NULL) {
if (is_null($userId)) { $userId = $this->testUserId; }
if (is_null($trackId)) { $trackId = $this->testTrackId; }
if (is_null($timeStamp)) { $timeStamp = $this->testTimestamp; }
if (is_null($latitude)) { $latitude = $this->testLat; }
if (is_null($longitude)) { $longitude = $this->testLon; }
2017-09-19 22:04:40 +02:00
$query = "INSERT INTO positions (user_id, track_id, time, latitude, longitude)
2019-01-24 19:07:41 +01:00
VALUES ('$userId', '$trackId', " . $this->from_unixtime($timeStamp) . ", '$latitude', '$longitude')";
2017-09-19 22:04:40 +02:00
return $this->pdoInsertRaw($query);
2017-09-05 09:36:10 +02:00
}
2019-01-24 19:07:41 +01:00
public function unix_timestamp($column) {
switch (self::$driver) {
default:
case "mysql":
return "UNIX_TIMESTAMP($column)";
break;
case "pgsql":
return "EXTRACT(EPOCH FROM $column)";
break;
case "sqlite":
return "STRFTIME('%s', $column)";
break;
}
}
public function from_unixtime($column) {
switch (self::$driver) {
default:
case "mysql":
return "FROM_UNIXTIME($column)";
break;
case "pgsql":
return "TO_TIMESTAMP($column)";
break;
case "sqlite":
2019-01-28 23:03:03 +01:00
return "DATETIME($column, 'unixepoch')";
2019-01-24 19:07:41 +01:00
break;
}
}
2017-09-05 09:36:10 +02:00
}
?>