Skip to content

Commit

Permalink
Refactor JSchemaChange* tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker authored and wilsonge committed Mar 29, 2016
1 parent 0750ba7 commit 7cb7116
Show file tree
Hide file tree
Showing 23 changed files with 1,051 additions and 223 deletions.
19 changes: 13 additions & 6 deletions libraries/cms/schema/changeset.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ class JSchemaChangeset
* Folder where SQL update files will be found
*
* @var string
* @since 2.5
*/
protected $folder = null;

/**
* The singleton instance of this object
*
* @var JSchemaChangeset
* @since 3.5.1
*/
protected static $instance;

/**
* Constructor: builds array of $changeItems by processing the .sql files in a folder.
* The folder for the Joomla core updates is `administrator/components/com_admin/sql/updates/<database>`.
Expand Down Expand Up @@ -116,16 +125,14 @@ public function __construct($db, $folder = null)
*
* @since 2.5
*/
public static function getInstance($db, $folder)
public static function getInstance($db, $folder = null)
{
static $instance;

if (!is_object($instance))
if (!is_object(static::$instance))
{
$instance = new JSchemaChangeset($db, $folder);
static::$instance = new JSchemaChangeset($db, $folder);
}

return $instance;
return static::$instance;
}

/**
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/core/case/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,29 @@ public static function setUpBeforeClass()
try
{
// Attempt to instantiate the driver.
self::$driver = JDatabaseDriver::getInstance($options);
static::$driver = JDatabaseDriver::getInstance($options);

// Create a new PDO instance for an SQLite memory database and load the test schema into it.
$pdo = new PDO('sqlite::memory:');
$pdo->exec(file_get_contents(JPATH_TESTS . '/schema/ddl.sql'));

// Set the PDO instance to the driver using reflection whizbangery.
TestReflection::setValue(self::$driver, 'connection', $pdo);
TestReflection::setValue(static::$driver, 'connection', $pdo);
}
catch (RuntimeException $e)
{
self::$driver = null;
static::$driver = null;
}

// If for some reason an exception object was returned set our database object to null.
if (self::$driver instanceof Exception)
if (static::$driver instanceof Exception)
{
self::$driver = null;
static::$driver = null;
}

// Setup the factory pointer for the driver and stash the old one.
self::$_stash = JFactory::$database;
JFactory::$database = self::$driver;
JFactory::$database = static::$driver;
}

/**
Expand All @@ -124,7 +124,7 @@ public static function setUpBeforeClass()
public static function tearDownAfterClass()
{
JFactory::$database = self::$_stash;
self::$driver = null;
static::$driver = null;
}

/**
Expand Down Expand Up @@ -359,9 +359,9 @@ class_exists('JApplicationWeb');
*/
protected function getConnection()
{
if (!is_null(self::$driver))
if (!is_null(static::$driver))
{
return $this->createDefaultDBConnection(self::$driver->getConnection(), ':memory:');
return $this->createDefaultDBConnection(static::$driver->getConnection(), ':memory:');
}
else
{
Expand Down
24 changes: 11 additions & 13 deletions tests/unit/core/case/database/mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,17 @@ public static function setUpBeforeClass()
{
if (PHP_MAJOR_VERSION >= 7)
{
self::markTestSkipped('ext/mysql is unsupported on PHP 7.');
static::markTestSkipped('ext/mysql is unsupported on PHP 7.');
}

// First let's look to see if we have a DSN defined or in the environment variables.
if (defined('JTEST_DATABASE_MYSQL_DSN') || getenv('JTEST_DATABASE_MYSQL_DSN'))
if (!defined('JTEST_DATABASE_MYSQL_DSN') && !getenv('JTEST_DATABASE_MYSQL_DSN'))
{
$dsn = defined('JTEST_DATABASE_MYSQL_DSN') ? JTEST_DATABASE_MYSQL_DSN : getenv('JTEST_DATABASE_MYSQL_DSN');
}
else
{
return;
static::markTestSkipped('The MySQL driver is not configured.');
}

$dsn = defined('JTEST_DATABASE_MYSQL_DSN') ? JTEST_DATABASE_MYSQL_DSN : getenv('JTEST_DATABASE_MYSQL_DSN');

// First let's trim the mysql: part off the front of the DSN if it exists.
if (strpos($dsn, 'mysql:') === 0)
{
Expand Down Expand Up @@ -92,22 +90,22 @@ public static function setUpBeforeClass()
try
{
// Attempt to instantiate the driver.
self::$driver = JDatabaseDriver::getInstance(self::$_options);
static::$driver = JDatabaseDriver::getInstance(self::$_options);
}
catch (RuntimeException $e)
{
self::$driver = null;
static::$driver = null;
}

// If for some reason an exception object was returned set our database object to null.
if (self::$driver instanceof Exception)
if (static::$driver instanceof Exception)
{
self::$driver = null;
static::$driver = null;
}

// Setup the factory pointer for the driver and stash the old one.
self::$_stash = JFactory::$database;
JFactory::$database = self::$driver;
JFactory::$database = static::$driver;
}

/**
Expand All @@ -120,7 +118,7 @@ public static function setUpBeforeClass()
public static function tearDownAfterClass()
{
JFactory::$database = self::$_stash;
self::$driver = null;
static::$driver = null;
}

/**
Expand Down
22 changes: 10 additions & 12 deletions tests/unit/core/case/database/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ abstract class TestCaseDatabaseMysqli extends TestCaseDatabase
public static function setUpBeforeClass()
{
// First let's look to see if we have a DSN defined or in the environment variables.
if (defined('JTEST_DATABASE_MYSQLI_DSN') || getenv('JTEST_DATABASE_MYSQLI_DSN'))
if (!defined('JTEST_DATABASE_MYSQLI_DSN') && !getenv('JTEST_DATABASE_MYSQLI_DSN'))
{
$dsn = defined('JTEST_DATABASE_MYSQLI_DSN') ? JTEST_DATABASE_MYSQLI_DSN : getenv('JTEST_DATABASE_MYSQLI_DSN');
}
else
{
return;
static::markTestSkipped('The MySQLi driver is not configured.');
}

$dsn = defined('JTEST_DATABASE_MYSQLI_DSN') ? JTEST_DATABASE_MYSQLI_DSN : getenv('JTEST_DATABASE_MYSQLI_DSN');

// First let's trim the mysql: part off the front of the DSN if it exists.
if (strpos($dsn, 'mysql:') === 0)
{
Expand Down Expand Up @@ -87,22 +85,22 @@ public static function setUpBeforeClass()
try
{
// Attempt to instantiate the driver.
self::$driver = JDatabaseDriver::getInstance(self::$_options);
static::$driver = JDatabaseDriver::getInstance(self::$_options);
}
catch (RuntimeException $e)
{
self::$driver = null;
static::$driver = null;
}

// If for some reason an exception object was returned set our database object to null.
if (self::$driver instanceof Exception)
if (static::$driver instanceof Exception)
{
self::$driver = null;
static::$driver = null;
}

// Setup the factory pointer for the driver and stash the old one.
self::$_stash = JFactory::$database;
JFactory::$database = self::$driver;
JFactory::$database = static::$driver;
}

/**
Expand All @@ -115,7 +113,7 @@ public static function setUpBeforeClass()
public static function tearDownAfterClass()
{
JFactory::$database = self::$_stash;
self::$driver = null;
static::$driver = null;
}

/**
Expand Down
22 changes: 10 additions & 12 deletions tests/unit/core/case/database/pdomysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ abstract class TestCaseDatabasePdomysql extends TestCaseDatabase
public static function setUpBeforeClass()
{
// First let's look to see if we have a DSN defined or in the environment variables.
if (defined('JTEST_DATABASE_PDO_MYSQL_DSN') || getenv('JTEST_DATABASE_PDO_MYSQL_DSN'))
if (!defined('JTEST_DATABASE_PDO_MYSQL_DSN') && !getenv('JTEST_DATABASE_PDO_MYSQL_DSN'))
{
$dsn = defined('JTEST_DATABASE_PDO_MYSQL_DSN') ? JTEST_DATABASE_PDO_MYSQL_DSN : getenv('JTEST_DATABASE_PDO_MYSQL_DSN');
}
else
{
return;
static::markTestSkipped('The PDO MySQL driver is not configured.');
}

$dsn = defined('JTEST_DATABASE_PDO_MYSQL_DSN') ? JTEST_DATABASE_PDO_MYSQL_DSN : getenv('JTEST_DATABASE_PDO_MYSQL_DSN');

// First let's trim the mysql: part off the front of the DSN if it exists.
if (strpos($dsn, 'mysql:') === 0)
{
Expand Down Expand Up @@ -87,22 +85,22 @@ public static function setUpBeforeClass()
try
{
// Attempt to instantiate the driver.
self::$driver = JDatabaseDriver::getInstance(self::$_options);
static::$driver = JDatabaseDriver::getInstance(self::$_options);
}
catch (RuntimeException $e)
{
self::$driver = null;
static::$driver = null;
}

// If for some reason an exception object was returned set our database object to null.
if (self::$driver instanceof Exception)
if (static::$driver instanceof Exception)
{
self::$driver = null;
static::$driver = null;
}

// Setup the factory pointer for the driver and stash the old one.
self::$_stash = JFactory::$database;
JFactory::$database = self::$driver;
JFactory::$database = static::$driver;
}

/**
Expand All @@ -115,7 +113,7 @@ public static function setUpBeforeClass()
public static function tearDownAfterClass()
{
JFactory::$database = self::$_stash;
self::$driver = null;
static::$driver = null;
}

/**
Expand Down
22 changes: 10 additions & 12 deletions tests/unit/core/case/database/postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ abstract class TestCaseDatabasePostgresql extends TestCaseDatabase
public static function setUpBeforeClass()
{
// First let's look to see if we have a DSN defined or in the environment variables.
if (defined('JTEST_DATABASE_POSTGRESQL_DSN') || getenv('JTEST_DATABASE_POSTGRESQL_DSN'))
if (!defined('JTEST_DATABASE_POSTGRESQL_DSN') && !getenv('JTEST_DATABASE_POSTGRESQL_DSN'))
{
$dsn = defined('JTEST_DATABASE_POSTGRESQL_DSN') ? JTEST_DATABASE_POSTGRESQL_DSN : getenv('JTEST_DATABASE_POSTGRESQL_DSN');
}
else
{
return;
static::markTestSkipped('The PostgreSQL driver is not configured.');
}

$dsn = defined('JTEST_DATABASE_POSTGRESQL_DSN') ? JTEST_DATABASE_POSTGRESQL_DSN : getenv('JTEST_DATABASE_POSTGRESQL_DSN');

// First let's trim the pgsql: part off the front of the DSN if it exists.
if (strpos($dsn, 'pgsql:') === 0)
{
Expand Down Expand Up @@ -90,22 +88,22 @@ public static function setUpBeforeClass()
try
{
// Attempt to instantiate the driver.
self::$driver = JDatabaseDriver::getInstance(self::$_options);
static::$driver = JDatabaseDriver::getInstance(self::$_options);
}
catch (RuntimeException $e)
{
self::$driver = null;
static::$driver = null;
}

// If for some reason an exception object was returned set our database object to null.
if (self::$driver instanceof Exception)
if (static::$driver instanceof Exception)
{
self::$driver = null;
static::$driver = null;
}

// Setup the factory pointer for the driver and stash the old one.
self::$_stash = JFactory::$database;
JFactory::$database = self::$driver;
JFactory::$database = static::$driver;
}

/**
Expand All @@ -118,7 +116,7 @@ public static function setUpBeforeClass()
public static function tearDownAfterClass()
{
JFactory::$database = self::$_stash;
self::$driver = null;
static::$driver = null;
}

/**
Expand Down
22 changes: 10 additions & 12 deletions tests/unit/core/case/database/sqlsrv.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ abstract class TestCaseDatabaseSqlsrv extends TestCaseDatabase
public static function setUpBeforeClass()
{
// First let's look to see if we have a DSN defined or in the environment variables.
if (defined('JTEST_DATABASE_SQLSRV_DSN') || getenv('JTEST_DATABASE_SQLSRV_DSN'))
if (!defined('JTEST_DATABASE_SQLSRV_DSN') && !getenv('JTEST_DATABASE_SQLSRV_DSN'))
{
$dsn = defined('JTEST_DATABASE_SQLSRV_DSN') ? JTEST_DATABASE_SQLSRV_DSN : getenv('JTEST_DATABASE_SQLSRV_DSN');
}
else
{
return;
static::markTestSkipped('The SQL Server driver is not configured.');
}

$dsn = defined('JTEST_DATABASE_SQLSRV_DSN') ? JTEST_DATABASE_SQLSRV_DSN : getenv('JTEST_DATABASE_SQLSRV_DSN');

// First let's trim the sqlsrv: part off the front of the DSN if it exists.
if (strpos($dsn, 'sqlsrv:') === 0)
{
Expand Down Expand Up @@ -87,22 +85,22 @@ public static function setUpBeforeClass()
try
{
// Attempt to instantiate the driver.
self::$driver = JDatabaseDriver::getInstance(self::$options);
static::$driver = JDatabaseDriver::getInstance(self::$options);
}
catch (RuntimeException $e)
{
self::$driver = null;
static::$driver = null;
}

// If for some reason an exception object was returned set our database object to null.
if (self::$driver instanceof Exception)
if (static::$driver instanceof Exception)
{
self::$driver = null;
static::$driver = null;
}

// Setup the factory pointer for the driver and stash the old one.
self::$stash = JFactory::$database;
JFactory::$database = self::$driver;
JFactory::$database = static::$driver;
}

/**
Expand All @@ -115,7 +113,7 @@ public static function setUpBeforeClass()
public static function tearDownAfterClass()
{
JFactory::$database = self::$stash;
self::$driver = null;
static::$driver = null;
}

/**
Expand Down
Loading

0 comments on commit 7cb7116

Please sign in to comment.