Merge branch 'master' into master

This commit is contained in:
Maurits van der Schee 2017-04-15 16:53:33 +02:00 committed by GitHub
commit 052934796a
9 changed files with 364 additions and 151 deletions

View file

@ -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

View file

@ -1,6 +1,6 @@
<?php
class PHP_CRUD_API_Config
class Config
{
/**
* Configure one or more database connections as associative arrays.

View file

@ -1,6 +1,6 @@
<?php
class PHP_CRUD_API_Config
class Config
{
/**
* Configure one or more database connections as associative arrays.

View file

@ -1,17 +1,74 @@
<?php
require_once(__DIR__ . '/tests.php');
require_once(__DIR__ . '/Tests.php');
class MysqlTest extends PHP_CRUD_API_Test
class MysqlTest extends Tests
{
const MYSQL_56 = 50600;
const MYSQL_57 = 50700;
public static $mysql_version;
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%10000)/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;
}
if ($version>=50700) {
$capabilities |= self::JSON;
}
return $capabilities;
}
/**
@ -19,39 +76,27 @@ 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_mysql.sql';
$contents = file_get_contents($fixture);
if (!($capabilities & self::GIS)) {
$contents = preg_replace('/(POINT|POLYGON) NOT NULL/i','text NOT NULL',$contents);
$contents = preg_replace('/ST_GeomFromText/i','concat',$contents);
}
$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");
if (!($capabilities & self::JSON)) {
$contents = preg_replace('/JSON NOT NULL/i','text NOT NULL',$contents);
}
// Note: For some reason this version is formatted:
// $mysql_version = main_version * 10000 + minor_version * 100 + sub_version
static::$mysql_version = mysqli_get_server_version($link);
$seed_file = self::getSeedFile();
mysqli_set_charset($link,'utf8');
$i=0;
if (mysqli_multi_query($link, file_get_contents($seed_file))) {
do { $i++; mysqli_next_result($link); } while (mysqli_more_results($link));
}
if (mysqli_errno($link)) {
die("Loading '$seed_file' failed on statemement #$i with error:\n".mysqli_error($link)."\n");
if (mysqli_multi_query($db, $contents)) {
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");
}
}
/**

View file

@ -1,16 +1,74 @@
<?php
require_once(__DIR__ . '/tests.php');
require_once(__DIR__ . '/Tests.php');
class PostgresqlTest extends PHP_CRUD_API_Test
class PostgresqlTest extends Tests
{
public static $gis_installed;
public static $pg_server_version;
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;
$major = 9;
$minor = 4;
$version = pg_version();
$v = explode('.',$version['server']);
if ($v[0]>$major || ($v[0]==$major && $v[1]>=$minor)) {
$capabilities |= self::JSON;
}
$extensions = pg_fetch_all(pg_query($db, "SELECT * FROM pg_extension;"));
foreach ($extensions as $extension) {
if ($extension['extname'] === 'postgis') {
$capabilities |= self::GIS;
}
}
return $capabilities;
}
/**
@ -18,33 +76,20 @@ 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_postgresql.sql';
$contents = file_get_contents($fixture);
if (!($capabilities & self::GIS)) {
$contents = preg_replace('/(geometry) NOT NULL/i','text NOT NULL',$contents);
$contents = preg_replace('/ST_GeomFromText/i','concat',$contents);
}
if (!($capabilities & self::JSON)) {
$contents = preg_replace('/JSONB? NOT NULL/i','text NOT NULL',$contents);
}
$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());
}
static::$pg_server_version = pg_version()['server'];
$gisInstalled = self::isGisInstalled(
pg_fetch_all(
pg_query($db, "SELECT * FROM pg_extension;")
)
);
$seed_file = self::getSeedFile();
$queries = preg_split('/;\s*\n/', file_get_contents($seed_file));
$queries = preg_split('/;\s*\n/', $contents);
array_pop($queries);
foreach ($queries as $i=>$query) {
@ -53,8 +98,6 @@ class PostgresqlTest extends PHP_CRUD_API_Test
die("Loading '$seed_file' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
}
}
pg_close($db);
}
/**

View file

@ -1,56 +1,90 @@
<?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'], $connectionInfo);
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 = 11;
$minor = 0;
$build = 3000;
$version = sqlsrv_server_info($db);
$v = explode('.',$version['SQLServerVersion']);
if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor) || ($v[0]==$major && $v[1]==$minor && $v[2]<$build)) {
die("Detected SQL Server $v[0].$v[1].$v[2], but only $major.$minor.$build and up are supported\n");
}
}
/**
* Gets the capabilities of the Database
*
* @return int Capabilites
*/
public function getCapabilities($db)
{
$capabilities = 0;
$capabilities |= self::GIS;
return $capabilities;
}
/**
* 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_sqlserver.sql';
$contents = file_get_contents($fixture);
$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));
}
$queries = preg_split('/\n\s*GO\s*\n/', file_get_contents($fixture));
$queries = preg_split('/\n\s*GO\s*\n/', $contents);
array_pop($queries);
foreach ($queries as $i=>$query) {
if (!sqlsrv_query($conn, $query)) {
if (!sqlsrv_query($db, $query)) {
$i++;
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( sqlsrv_errors(), true)."\n");
}
}
sqlsrv_close($conn);
}
}

View file

@ -1,13 +1,60 @@
<?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,23 +62,19 @@ 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");
$fixture = __DIR__.'/data/blog_sqlite.sql';
$contents = file_get_contents($fixture);
if (!($capabilities & self::GIS)) {
$contents = preg_replace('/GEOMETRY NOT NULL/i','text NOT NULL',$contents);
}
if (!($capabilities & self::JSON)) {
$contents = preg_replace('/JSON NOT NULL/i','text NOT NULL',$contents);
}
$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");
}
$queries = preg_split('/;\s*\n/', file_get_contents($fixture));
$queries = preg_split('/;\s*\n/', $contents);
array_pop($queries);
foreach ($queries as $i=>$query) {
@ -40,7 +83,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
View 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);
}

View file

@ -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);
@ -584,7 +568,7 @@ abstract class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
public function testSpatialFilterWithin()
{
if (static::$config['dbengine']!='SQLite') {
if (static::$capabilities & self::GIS) {
$test = new API($this, static::$config);
$test->get('/users?columns=id,username&filter=location,swi,POINT(30 20)');
$test->expect('{"users":{"columns":["id","username"],"records":[[1,"user1"]]}}');
@ -699,32 +683,52 @@ abstract class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
{
$test = new API($this, static::$config);
$test->get('/products?columns=id,properties&transform=1');
$test->expect('{"products":[{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":null}}]}');
if (static::$capabilities & self::JSON) {
$test->expect('{"products":[{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":null}}]}');
} else {
$test->expect('{"products":[{"id":1,"properties":"{\"depth\":false,\"model\":\"TRX-120\",\"width\":100,\"height\":null}"}]}');
}
}
public function testReadProductProperties()
{
$test = new API($this, static::$config);
$test->get('/products/1?columns=id,properties');
$test->expect('{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":null}}');
if (static::$capabilities & self::JSON) {
$test->expect('{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":null}}');
} else {
$test->expect('{"id":1,"properties":"{\"depth\":false,\"model\":\"TRX-120\",\"width\":100,\"height\":null}"}');
}
}
public function testWriteProductProperties()
{
$test = new API($this, static::$config);
$test->put('/products/1','{"properties":{"depth":false,"model":"TRX-120","width":100,"height":123}}');
if (static::$capabilities & self::JSON) {
$test->put('/products/1','{"properties":{"depth":false,"model":"TRX-120","width":100,"height":123}}');
} else {
$test->put('/products/1','{"properties":"{\"depth\":false,\"model\":\"TRX-120\",\"width\":100,\"height\":123}"}');
}
$test->expect('1');
$test->get('/products/1?columns=id,properties');
$test->expect('{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":123}}');
if (static::$capabilities & self::JSON) {
$test->expect('{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":123}}');
} else {
$test->expect('{"id":1,"properties":"{\"depth\":false,\"model\":\"TRX-120\",\"width\":100,\"height\":123}"}');
}
}
public function testAddProducts()
{
$test = new API($this, static::$config);
$test->post('/products','{"name":"Laptop","price":"1299.99","properties":{}}');
if (static::$capabilities & self::JSON) {
$test->post('/products','{"name":"Laptop","price":"1299.99","properties":{}}');
} else {
$test->post('/products','{"name":"Laptop","price":"1299.99","properties":"{}"}');
}
$test->expect('2');
$test->get('/products/2');
$test->expect('{"id":2,"name":"Laptop","price":"1299.99","properties":{},"created_at":"2013-12-11 10:09:08","deleted_at":null}');
$test->get('/products/2?columns=id,created_at,deleted_at');
$test->expect('{"id":2,"created_at":"2013-12-11 10:09:08","deleted_at":null}');
}
public function testSoftDeleteProducts()