Getting tests to configure themselves
This commit is contained in:
parent
61a8c239fa
commit
ab83ff61f1
7 changed files with 201 additions and 105 deletions
|
|
@ -1,5 +1,8 @@
|
|||
<phpunit>
|
||||
<php>
|
||||
<ini name="display_errors" value="true"/>
|
||||
<ini name="display_errors" value="true"/>
|
||||
</php>
|
||||
<testsuite name="All Tests">
|
||||
<directory suffix='.php'>./tests/</directory>
|
||||
</testsuite>
|
||||
</phpunit>
|
||||
|
|
|
|||
|
|
@ -7,6 +7,43 @@ class MysqlTest extends PHP_CRUD_API_Test
|
|||
public static function setUpBeforeClass()
|
||||
{
|
||||
self::setConfig('MySQL');
|
||||
parent::setUpBeforeClass();
|
||||
self::seedDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds the database for this connection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function seedDatabase()
|
||||
{
|
||||
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');
|
||||
|
||||
$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");
|
||||
}
|
||||
|
||||
mysqli_close($link);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,45 @@ class PostgresqlTest extends PHP_CRUD_API_Test
|
|||
public static function setUpBeforeClass()
|
||||
{
|
||||
static::setConfig('PostgreSQL');
|
||||
parent::setUpBeforeClass();
|
||||
self::seedDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds the database for this connection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function seedDatabase()
|
||||
{
|
||||
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());
|
||||
}
|
||||
|
||||
$queries = preg_split('/;\s*\n/', file_get_contents($fixture));
|
||||
array_pop($queries);
|
||||
|
||||
foreach ($queries as $i=>$query) {
|
||||
if (!pg_query($db, $query.';')) {
|
||||
$i++;
|
||||
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
|
||||
}
|
||||
}
|
||||
|
||||
pg_close($db);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,50 @@ class SqlServerTest extends PHP_CRUD_API_Test
|
|||
public static function setUpBeforeClass()
|
||||
{
|
||||
self::setConfig('SQLServer');
|
||||
parent::setUpBeforeClass();
|
||||
|
||||
if (static::$config['database']=='{{test_database}}') {
|
||||
die("Configure database in 'config.php' before running tests.\n");
|
||||
}
|
||||
|
||||
self::seedDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds the database for this connection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function seedDatabase()
|
||||
{
|
||||
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 = [
|
||||
'UID' => static::$config['username'],
|
||||
'PWD' => static::$config['password'],
|
||||
'Database' => static::$config['database'],
|
||||
'CharacterSet' => static::$config['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));
|
||||
array_pop($queries);
|
||||
|
||||
foreach ($queries as $i=>$query) {
|
||||
if (!sqlsrv_query($conn, $query)) {
|
||||
$i++;
|
||||
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( sqlsrv_errors(), true)."\n");
|
||||
}
|
||||
}
|
||||
|
||||
sqlsrv_close($conn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,40 @@ class SqliteTest extends PHP_CRUD_API_Test
|
|||
public static function setUpBeforeClass()
|
||||
{
|
||||
self::setConfig('SQLite');
|
||||
parent::setUpBeforeClass();
|
||||
self::seedDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds the database for this connection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function seedDatabase()
|
||||
{
|
||||
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");
|
||||
}
|
||||
|
||||
$queries = preg_split('/;\s*\n/', file_get_contents($fixture));
|
||||
array_pop($queries);
|
||||
|
||||
foreach ($queries as $i=>$query) {
|
||||
if (!$db->query($query.';')) {
|
||||
$i++;
|
||||
die("Loading '$fixture' failed on statemement #$i with error:\n".$db->lastErrorCode().': '.$db->lastErrorMsg()."\n");
|
||||
}
|
||||
}
|
||||
|
||||
$db->close();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,40 @@
|
|||
|
||||
class PHP_CRUD_API_Config
|
||||
{
|
||||
public static $dbengine='MySQL'; // 'MySQL', 'SQLServer', 'PostgreSQL' or 'SQLite'
|
||||
public static $hostname='{{test_hostname}}'; // 'localhost' for MySQL, '(Local)' for SQLServer, empty for SQLite
|
||||
public static $username='{{test_username}}'; // May be empty on SQLServer or SQLite
|
||||
public static $password='{{test_password}}'; // May be empty on SQLServer or SQLite
|
||||
public static $database='{{test_database}}'; // NB: Use an empty database, data will be LOST!
|
||||
/**
|
||||
* Configure one or more database connections as associative arrays.
|
||||
* Tests will be run against any database connection specified here.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $config = [
|
||||
[
|
||||
'dbengine' => 'MySQL', // 'MySQL', 'SQLServer', 'PostgreSQL' or 'SQLite'
|
||||
'hostname' => '{{test_hostname}}', // 'localhost' for MySQL, '(Local)' for SQLServer, empty for SQLite
|
||||
'username' => '{{test_username}}', // May be empty on SQLServer or SQLite
|
||||
'password' => '{{test_password}}', // May be empty on SQLServer or SQLite
|
||||
'database' => '{{test_database}}', // NB: Use an empty database, data will be LOST!
|
||||
],
|
||||
[
|
||||
'dbengine' => 'PostgreSQL',
|
||||
'hostname' => '',
|
||||
'username' => '',
|
||||
'password' => '',
|
||||
'database' => '',
|
||||
],
|
||||
[
|
||||
'dbengine' => 'SQLite',
|
||||
'hostname' => '',
|
||||
'username' => '',
|
||||
'password' => '',
|
||||
'database' => '',
|
||||
],
|
||||
[
|
||||
'dbengine' => 'SQLServer',
|
||||
'hostname' => '',
|
||||
'username' => '',
|
||||
'password' => '',
|
||||
'database' => '',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -119,100 +119,9 @@ abstract class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
|
|||
{
|
||||
public static $config;
|
||||
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
if (static::$config['database']=='{{test_database}}') {
|
||||
die("Configure database in 'config.php' before running tests.\n");
|
||||
}
|
||||
public abstract function seedDatabase();
|
||||
|
||||
$dbengine = static::$config['dbengine'];
|
||||
$hostname = static::$config['hostname'];
|
||||
$username = static::$config['username'];
|
||||
$password = static::$config['password'];
|
||||
$database = static::$config['database'];
|
||||
|
||||
$fixture = __DIR__.'/data/blog_'.strtolower($dbengine).'.sql';
|
||||
|
||||
if ($dbengine == 'MySQL') {
|
||||
|
||||
$link = mysqli_connect($hostname, $username, $password, $database);
|
||||
if (mysqli_connect_errno()) {
|
||||
die("Connect failed: ".mysqli_connect_error()."\n");
|
||||
}
|
||||
mysqli_set_charset($link,'utf8');
|
||||
|
||||
$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");
|
||||
}
|
||||
|
||||
mysqli_close($link);
|
||||
|
||||
} elseif ($dbengine == 'SQLServer') {
|
||||
|
||||
$connectionInfo = array();
|
||||
$connectionInfo['UID']=$username;
|
||||
$connectionInfo['PWD']=$password;
|
||||
$connectionInfo['Database']=$database;
|
||||
$connectionInfo['CharacterSet']='UTF-8';
|
||||
$conn = sqlsrv_connect( $hostname, $connectionInfo);
|
||||
if (!$conn) {
|
||||
die("Connect failed: ".print_r( sqlsrv_errors(), true));
|
||||
}
|
||||
$queries = preg_split('/\n\s*GO\s*\n/', file_get_contents($fixture));
|
||||
array_pop($queries);
|
||||
foreach ($queries as $i=>$query) {
|
||||
if (!sqlsrv_query($conn, $query)) {
|
||||
$i++;
|
||||
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( sqlsrv_errors(), true)."\n");
|
||||
}
|
||||
}
|
||||
sqlsrv_close($conn);
|
||||
|
||||
} elseif ($dbengine == 'PostgreSQL') {
|
||||
|
||||
$e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); };
|
||||
$hostname = $e($hostname);
|
||||
$database = $e($database);
|
||||
$username = $e($username);
|
||||
$password = $e($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());
|
||||
}
|
||||
$queries = preg_split('/;\s*\n/', file_get_contents($fixture));
|
||||
array_pop($queries);
|
||||
foreach ($queries as $i=>$query) {
|
||||
if (!pg_query($db, $query.';')) {
|
||||
$i++;
|
||||
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
|
||||
}
|
||||
}
|
||||
pg_close($db);
|
||||
|
||||
} elseif ($dbengine == 'SQLite') {
|
||||
|
||||
$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));
|
||||
array_pop($queries);
|
||||
foreach ($queries as $i=>$query) {
|
||||
if (!$db->query($query.';')) {
|
||||
$i++;
|
||||
die("Loading '$fixture' failed on statemement #$i with error:\n".$db->lastErrorCode().': '.$db->lastErrorMsg()."\n");
|
||||
}
|
||||
}
|
||||
$db->close();
|
||||
}
|
||||
}
|
||||
|
||||
public function setConfig($dbengine = '')
|
||||
protected function setConfig($dbengine = '')
|
||||
{
|
||||
foreach (PHP_CRUD_API_Config::$config as $database) {
|
||||
if ($database['dbengine'] == $dbengine) {
|
||||
|
|
@ -220,11 +129,10 @@ abstract class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
|
|||
return true;
|
||||
}
|
||||
}
|
||||
self::markTestSkipped();
|
||||
self::markTestSkipped("Configuration for {$dbengine} database not found.");
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @group only */
|
||||
public function testListPosts()
|
||||
{
|
||||
$test = new API($this, static::$config);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue