Prepare for #194
This commit is contained in:
parent
94922d799c
commit
4d43f8f3d6
9 changed files with 297 additions and 129 deletions
|
|
@ -22,7 +22,7 @@ before_install:
|
|||
- psql -c 'create database testing;' -U postgres
|
||||
- psql -U postgres -c "create extension postgis"
|
||||
# - mysql -e 'CREATE DATABASE testing;'
|
||||
- cp tests/config.php.travis tests/config.php
|
||||
- cp tests/Config.php.travis tests/Config.php
|
||||
|
||||
script:
|
||||
- curl https://phar.phpunit.de/phpunit-4.8.phar -L -o phpunit.phar && chmod +x phpunit.phar
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
class PHP_CRUD_API_Config
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* Configure one or more database connections as associative arrays.
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
class PHP_CRUD_API_Config
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* Configure one or more database connections as associative arrays.
|
||||
|
|
@ -1,13 +1,71 @@
|
|||
<?php
|
||||
|
||||
require_once(__DIR__ . '/tests.php');
|
||||
require_once(__DIR__ . '/Tests.php');
|
||||
|
||||
class MysqlTest extends PHP_CRUD_API_Test
|
||||
class MysqlTest extends Tests
|
||||
{
|
||||
public static function setUpBeforeClass()
|
||||
/**
|
||||
* Connects to the Database
|
||||
*
|
||||
* @return object Database connection
|
||||
*/
|
||||
public function connect($config)
|
||||
{
|
||||
self::setConfig('MySQL');
|
||||
self::seedDatabase();
|
||||
$db = mysqli_connect(
|
||||
$config['hostname'],
|
||||
$config['username'],
|
||||
$config['password'],
|
||||
$config['database']
|
||||
);
|
||||
|
||||
if (mysqli_connect_errno()) {
|
||||
die("Connect failed: ".mysqli_connect_error()."\n");
|
||||
}
|
||||
|
||||
mysqli_set_charset($db,'utf8');
|
||||
|
||||
return $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the Database
|
||||
*
|
||||
* @return boolean Success
|
||||
*/
|
||||
public function disconnect($db)
|
||||
{
|
||||
return mysqli_close($db);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the version of the Database
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function checkVersion($db)
|
||||
{
|
||||
$major = 5;
|
||||
$minor = 5;
|
||||
$version = mysqli_get_server_version($db);
|
||||
$v = array(floor($version/10000),floor($version/100));
|
||||
if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
|
||||
die("Detected MySQL $v[0].$v[1], but only $major.$minor and up are supported\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the capabilities of the Database
|
||||
*
|
||||
* @return int Capabilites
|
||||
*/
|
||||
public function getCapabilities($db)
|
||||
{
|
||||
$capabilities = 0;
|
||||
$version = mysqli_get_server_version($db);
|
||||
if ($version>50600) {
|
||||
$capabilities |= self::GIS;
|
||||
}
|
||||
return $capabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -15,35 +73,17 @@ class MysqlTest extends PHP_CRUD_API_Test
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function seedDatabase()
|
||||
public function seedDatabase($db, $capabilities)
|
||||
{
|
||||
if (static::$config['database']=='{{test_database}}') {
|
||||
die("Configure database in 'config.php' before running tests.\n");
|
||||
}
|
||||
|
||||
$fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
|
||||
|
||||
$link = mysqli_connect(
|
||||
static::$config['hostname'],
|
||||
static::$config['username'],
|
||||
static::$config['password'],
|
||||
static::$config['database']
|
||||
);
|
||||
|
||||
if (mysqli_connect_errno()) {
|
||||
die("Connect failed: ".mysqli_connect_error()."\n");
|
||||
}
|
||||
|
||||
mysqli_set_charset($link,'utf8');
|
||||
$fixture = __DIR__.'/data/blog_mysql.sql';
|
||||
|
||||
$i=0;
|
||||
if (mysqli_multi_query($link, file_get_contents($fixture))) {
|
||||
do { $i++; mysqli_next_result($link); } while (mysqli_more_results($link));
|
||||
}
|
||||
if (mysqli_errno($link)) {
|
||||
die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($link)."\n");
|
||||
if (mysqli_multi_query($db, file_get_contents($fixture))) {
|
||||
do { $i++; mysqli_next_result($db); } while (mysqli_more_results($db));
|
||||
}
|
||||
|
||||
mysqli_close($link);
|
||||
if (mysqli_errno($db)) {
|
||||
die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($db)."\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,67 @@
|
|||
<?php
|
||||
|
||||
require_once(__DIR__ . '/tests.php');
|
||||
require_once(__DIR__ . '/Tests.php');
|
||||
|
||||
class PostgresqlTest extends PHP_CRUD_API_Test
|
||||
class PostgresqlTest extends Tests
|
||||
{
|
||||
public static function setUpBeforeClass()
|
||||
/**
|
||||
* Connects to the Database
|
||||
*
|
||||
* @return object Database connection
|
||||
*/
|
||||
public function connect($config)
|
||||
{
|
||||
static::setConfig('PostgreSQL');
|
||||
self::seedDatabase();
|
||||
$e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); };
|
||||
$hostname = $e($config['hostname']);
|
||||
$database = $e($config['database']);
|
||||
$username = $e($config['username']);
|
||||
$password = $e($config['password']);
|
||||
$connectionString = "host='$hostname' dbname='$database' user='$username' password='$password' options='--client_encoding=UTF8'";
|
||||
|
||||
return pg_connect($connectionString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the Database
|
||||
*
|
||||
* @return boolean Success
|
||||
*/
|
||||
public function disconnect($db)
|
||||
{
|
||||
return pg_close($db);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the version of the Database
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function checkVersion($db)
|
||||
{
|
||||
$major = 9;
|
||||
$minor = 1;
|
||||
$version = pg_version();
|
||||
$v = explode('.',$version['server']);
|
||||
if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
|
||||
die("Detected PostgreSQL $v[0].$v[1], but only $major.$minor and up are supported\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the capabilities of the Database
|
||||
*
|
||||
* @return int Capabilites
|
||||
*/
|
||||
public function getCapabilities($db)
|
||||
{
|
||||
$capabilities = 0;
|
||||
$extensions = pg_fetch_all(pg_query($db, "SELECT * FROM pg_extension;"));
|
||||
foreach ($extensions as $extension) {
|
||||
if ($extension['extname'] === 'postgis') {
|
||||
$capabilities |= self::GIS;
|
||||
}
|
||||
}
|
||||
return $capabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -15,27 +69,9 @@ class PostgresqlTest extends PHP_CRUD_API_Test
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function seedDatabase()
|
||||
public function seedDatabase($db,$capabilities)
|
||||
{
|
||||
if (static::$config['database']=='{{test_database}}') {
|
||||
die("Configure database in 'config.php' before running tests.\n");
|
||||
}
|
||||
|
||||
$fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
|
||||
|
||||
$e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); };
|
||||
$hostname = $e(static::$config['hostname']);
|
||||
$database = $e(static::$config['database']);
|
||||
$username = $e(static::$config['username']);
|
||||
$password = $e(static::$config['password']);
|
||||
$conn_string = "host='$hostname' dbname='$database' user='$username' password='$password' options='--client_encoding=UTF8'";
|
||||
|
||||
$db = pg_connect($conn_string);
|
||||
|
||||
if (!$db) {
|
||||
die("Connect failed: ". pg_last_error());
|
||||
}
|
||||
|
||||
$fixture = __DIR__.'/data/blog_postgresql.sql';
|
||||
$queries = preg_split('/;\s*\n/', file_get_contents($fixture));
|
||||
array_pop($queries);
|
||||
|
||||
|
|
@ -45,7 +81,5 @@ class PostgresqlTest extends PHP_CRUD_API_Test
|
|||
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
|
||||
}
|
||||
}
|
||||
|
||||
pg_close($db);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,45 +1,77 @@
|
|||
<?php
|
||||
|
||||
require_once(__DIR__ . '/tests.php');
|
||||
require_once(__DIR__ . '/Tests.php');
|
||||
|
||||
class SqlServerTest extends PHP_CRUD_API_Test
|
||||
class SqlServerTest extends Tests
|
||||
{
|
||||
public static function setUpBeforeClass()
|
||||
/**
|
||||
* Connects to the Database
|
||||
*
|
||||
* @return object Database connection
|
||||
*/
|
||||
public function connect($config)
|
||||
{
|
||||
self::setConfig('SQLServer');
|
||||
$connectionInfo = array(
|
||||
'UID' => $config['username'],
|
||||
'PWD' => $config['password'],
|
||||
'Database' => $config['database'],
|
||||
'CharacterSet' => 'UTF-8',
|
||||
);
|
||||
|
||||
if (static::$config['database']=='{{test_database}}') {
|
||||
die("Configure database in 'config.php' before running tests.\n");
|
||||
$db = sqlsrv_connect($config['hostname'], $dbectionInfo);
|
||||
|
||||
if (!$db) {
|
||||
die("Connect failed: ".print_r( sqlsrv_errors(), true));
|
||||
}
|
||||
|
||||
self::seedDatabase();
|
||||
return $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the Database
|
||||
*
|
||||
* @return boolean Success
|
||||
*/
|
||||
public function disconnect($db)
|
||||
{
|
||||
return sqlsrv_close($db);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the version of the Database
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function checkVersion($db)
|
||||
{
|
||||
$major = 5;
|
||||
$minor = 5;
|
||||
$version = sqlsrv_server_info($db);
|
||||
$v = explode('.',$version['SQLServerVersion']);
|
||||
if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
|
||||
die("Detected MySQL $v[0].$v[1], but only $major.$minor and up are supported\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the capabilities of the Database
|
||||
*
|
||||
* @return int Capabilites
|
||||
*/
|
||||
public function getCapabilities($db)
|
||||
{
|
||||
return self::GIS;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Seeds the database for this connection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function seedDatabase()
|
||||
public function seedDatabase($db, $capabilities)
|
||||
{
|
||||
if (static::$config['database']=='{{test_database}}') {
|
||||
die("Configure database in 'config.php' before running tests.\n");
|
||||
}
|
||||
|
||||
$fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
|
||||
|
||||
$connectionInfo = array(
|
||||
'UID' => static::$config['username'],
|
||||
'PWD' => static::$config['password'],
|
||||
'Database' => static::$config['database'],
|
||||
'CharacterSet' => 'UTF-8',
|
||||
);
|
||||
|
||||
$conn = sqlsrv_connect(static::$config['hostname'], $connectionInfo);
|
||||
|
||||
if (!$conn) {
|
||||
die("Connect failed: ".print_r( sqlsrv_errors(), true));
|
||||
}
|
||||
$fixture = __DIR__.'/data/blog_sqlserver.sql';
|
||||
|
||||
$queries = preg_split('/\n\s*GO\s*\n/', file_get_contents($fixture));
|
||||
array_pop($queries);
|
||||
|
|
@ -50,7 +82,5 @@ class SqlServerTest extends PHP_CRUD_API_Test
|
|||
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( sqlsrv_errors(), true)."\n");
|
||||
}
|
||||
}
|
||||
|
||||
sqlsrv_close($conn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,61 @@
|
|||
<?php
|
||||
|
||||
require_once(__DIR__ . '/tests.php');
|
||||
require_once(__DIR__ . '/Tests.php');
|
||||
|
||||
class SqliteTest extends PHP_CRUD_API_Test
|
||||
class SqliteTest extends Tests
|
||||
{
|
||||
public static function setUpBeforeClass()
|
||||
/**
|
||||
* Connects to the Database
|
||||
*
|
||||
* @return object Database connection
|
||||
*/
|
||||
public function connect($config)
|
||||
{
|
||||
self::setConfig('SQLite');
|
||||
self::seedDatabase();
|
||||
$db = new SQLite3($config['database']);
|
||||
|
||||
if (!$db) {
|
||||
die("Could not open '$database' SQLite database: ".SQLite3::lastErrorMsg().' ('.SQLite3::lastErrorCode().")\n");
|
||||
}
|
||||
|
||||
return $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the Database
|
||||
*
|
||||
* @return boolean Success
|
||||
*/
|
||||
public function disconnect($db)
|
||||
{
|
||||
return $db->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the version of the Database
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function checkVersion($db)
|
||||
{
|
||||
$major = 3;
|
||||
$minor = 0;
|
||||
$version = SQLite3::version();
|
||||
$v = explode('.',$version['versionString']);
|
||||
if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
|
||||
die("Detected SQLite $v[0].$v[1], but only $major.$minor and up are supported\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the capabilities of the Database
|
||||
*
|
||||
* @return int Capabilites
|
||||
*/
|
||||
public function getCapabilities($db)
|
||||
{
|
||||
$capabilities = 0;
|
||||
|
||||
return $capabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -15,21 +63,9 @@ class SqliteTest extends PHP_CRUD_API_Test
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function seedDatabase()
|
||||
public function seedDatabase($db, $capabilities)
|
||||
{
|
||||
if (static::$config['database']=='{{test_database}}') {
|
||||
die("Configure database in 'config.php' before running tests.\n");
|
||||
}
|
||||
|
||||
$database = static::$config['database'];
|
||||
|
||||
$fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
|
||||
|
||||
$db = new SQLite3($database);
|
||||
|
||||
if (!$db) {
|
||||
die("Could not open '$database' SQLite database: ".SQLite3::lastErrorMsg().' ('.SQLite3::lastErrorCode().")\n");
|
||||
}
|
||||
$fixture = __DIR__.'/data/blog_sqlite.sql';
|
||||
|
||||
$queries = preg_split('/;\s*\n/', file_get_contents($fixture));
|
||||
array_pop($queries);
|
||||
|
|
@ -40,7 +76,5 @@ class SqliteTest extends PHP_CRUD_API_Test
|
|||
die("Loading '$fixture' failed on statemement #$i with error:\n".$db->lastErrorCode().': '.$db->lastErrorMsg()."\n");
|
||||
}
|
||||
}
|
||||
|
||||
$db->close();
|
||||
}
|
||||
}
|
||||
}
|
||||
46
tests/TestBase.php
Normal file
46
tests/TestBase.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
require_once(__DIR__ . '/Config.php');
|
||||
|
||||
abstract class TestBase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function getConfig()
|
||||
{
|
||||
$dbengine = strtolower(substr(get_called_class(),0,-4));
|
||||
foreach (Config::$config as $database) {
|
||||
if (strtolower($database['dbengine']) == $dbengine) {
|
||||
if ($database['database']!='{{test_database}}') {
|
||||
return $database;
|
||||
}
|
||||
}
|
||||
}
|
||||
self::markTestSkipped("Configuration for '{$dbengine}' in 'Config.php' not found.");
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
$config = self::getConfig();
|
||||
$db = static::connect($config);
|
||||
static::checkVersion($db);
|
||||
$capabilities = static::getCapabilities($db);
|
||||
static::seedDatabase($db,$capabilities);
|
||||
static::disconnect($db);
|
||||
// set params for test
|
||||
static::$config = $config;
|
||||
static::$capabilities = $capabilities;
|
||||
}
|
||||
|
||||
public static $config;
|
||||
public static $capabilities;
|
||||
|
||||
const GIS = 1;
|
||||
const JSON = 2;
|
||||
|
||||
public abstract function checkVersion($db);
|
||||
|
||||
public abstract function getCapabilities($db);
|
||||
|
||||
public abstract function seedDatabase($db,$capabilities);
|
||||
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
require __DIR__.'/config.php';
|
||||
require __DIR__.'/../api.php';
|
||||
require_once(__DIR__ . '/TestBase.php');
|
||||
require_once(__DIR__ . '/../api.php');
|
||||
|
||||
class API
|
||||
{
|
||||
|
|
@ -113,24 +113,8 @@ class API
|
|||
}
|
||||
}
|
||||
|
||||
abstract class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
|
||||
abstract class Tests extends TestBase
|
||||
{
|
||||
public static $config;
|
||||
|
||||
public abstract function seedDatabase();
|
||||
|
||||
protected function setConfig($dbengine = '')
|
||||
{
|
||||
foreach (PHP_CRUD_API_Config::$config as $database) {
|
||||
if ($database['dbengine'] == $dbengine) {
|
||||
static::$config = $database;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
self::markTestSkipped("Configuration for {$dbengine} database not found.");
|
||||
return false;
|
||||
}
|
||||
|
||||
public function testListPosts()
|
||||
{
|
||||
$test = new API($this, static::$config);
|
||||
Loading…
Add table
Add a link
Reference in a new issue