diff --git a/src/Joomla/Database/README.md b/src/Joomla/Database/README.md index a4e467681..cccc3c601 100644 --- a/src/Joomla/Database/README.md +++ b/src/Joomla/Database/README.md @@ -57,7 +57,7 @@ The `quote` method will escape a string and wrap it in quotes, however, the esca function search($title) { // Get the database driver from the factory, or by some other suitable means. - $db = JFactory::getDbo(); + $db = DatabaseDriver::getInstance($options); // Search for an exact match of the title, correctly sanitising the untrusted input. $sql1 = 'SELECT * FROM #__content WHERE title = ' . $db->quote($title); @@ -95,9 +95,9 @@ These shorthand versions are also available when using the `Database\DatabaseQue The `Database\DatabaseIterator` class allows iteration over database results ```php -$dbo = JFactory::getDbo(); -$iterator = $dbo->setQuery( - $dbo->getQuery(true)->select('*')->from('#__content') +$db = DatabaseDriver::getInstance($options); +$iterator = $db->setQuery( + $db->getQuery(true)->select('*')->from('#__content') )->getIterator(); foreach ($iterator as $row) diff --git a/src/Joomla/Database/Tests/DatabaseCase.php b/src/Joomla/Database/Tests/DatabaseCase.php deleted file mode 100644 index f34b69913..000000000 --- a/src/Joomla/Database/Tests/DatabaseCase.php +++ /dev/null @@ -1,156 +0,0 @@ - 'sqlite', - 'database' => ':memory:', - 'prefix' => 'jos_' - ); - - try - { - // Attempt to instantiate the driver. - self::$driver = \Joomla\Database\DatabaseDriver::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(__DIR__ . '/Stubs/ddl.sql')); - - // Set the PDO instance to the driver using reflection whizbangery. - TestHelper::setValue(self::$driver, 'connection', $pdo); - } - catch (\RuntimeException $e) - { - self::$driver = null; - } - - // If for some reason an exception object was returned set our database object to null. - if (self::$driver instanceof \Exception) - { - self::$driver = null; - } - } - - /** - * This method is called after the last test of this test class is run. - * - * @return void - * - * @since 1.0 - */ - public static function tearDownAfterClass() - { - self::$driver = null; - } - - /** - * Returns the default database connection for running the tests. - * - * @return \PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection - * - * @since 1.0 - */ - protected function getConnection() - { - if (!is_null(self::$driver)) - { - return $this->createDefaultDBConnection(self::$driver->getConnection(), ':memory:'); - } - else - { - return null; - } - } - - /** - * Gets the data set to be loaded into the database during setup. - * - * @return \PHPUnit_Extensions_Database_DataSet_XmlDataSet - * - * @since 1.0 - */ - protected function getDataSet() - { - return $this->createXMLDataSet(__DIR__ . '/Stubs/database.xml'); - } - - /** - * Returns the database operation executed in test setup. - * - * @return \PHPUnit_Extensions_Database_Operation_Composite - * - * @since 1.0 - */ - protected function getSetUpOperation() - { - // Required given the use of InnoDB contraints. - return new \PHPUnit_Extensions_Database_Operation_Composite( - array( - \PHPUnit_Extensions_Database_Operation_Factory::DELETE_ALL(), - \PHPUnit_Extensions_Database_Operation_Factory::INSERT() - ) - ); - } - - /** - * Returns the database operation executed in test cleanup. - * - * @return \PHPUnit_Extensions_Database_Operation_IDatabaseOperation - * - * @since 1.0 - */ - protected function getTearDownOperation() - { - // Required given the use of InnoDB contraints. - return \PHPUnit_Extensions_Database_Operation_Factory::DELETE_ALL(); - } - - /** - * Sets up the fixture. - * - * This method is called before a test is executed. - * - * @return void - * - * @since 1.0 - */ - protected function setUp() - { - if (empty(static::$driver)) - { - $this->markTestSkipped('There is no database driver.'); - } - - parent::setUp(); - } -} diff --git a/src/Joomla/Database/Tests/DatabaseFactoryTest.php b/src/Joomla/Database/Tests/DatabaseFactoryTest.php index aff9ecfdb..7529ab41f 100644 --- a/src/Joomla/Database/Tests/DatabaseFactoryTest.php +++ b/src/Joomla/Database/Tests/DatabaseFactoryTest.php @@ -8,13 +8,14 @@ use Joomla\Database\DatabaseFactory; use Joomla\Test\TestHelper; +use Joomla\Test\TestDatabase; /** * Test class for Joomla\Database\DatabaseFactory. * * @since 1.0 */ -class DatabaseFactoryTest extends DatabaseCase +class DatabaseFactoryTest extends TestDatabase { /** * Object being tested diff --git a/src/Joomla/Database/Tests/DatabaseMysqlCase.php b/src/Joomla/Database/Tests/DatabaseMysqlCase.php index e41a93d65..5139fc403 100644 --- a/src/Joomla/Database/Tests/DatabaseMysqlCase.php +++ b/src/Joomla/Database/Tests/DatabaseMysqlCase.php @@ -6,33 +6,22 @@ namespace Joomla\Database\Tests; -use Joomla\Factory; +use Joomla\Test\TestDatabase; +use Joomla\Database\DatabaseDriver; /** * Abstract test case class for MySQL database testing. * * @since 1.0 */ -abstract class DatabaseMysqlCase extends DatabaseCase +abstract class DatabaseMysqlCase extends TestDatabase { - /** - * @var \Joomla\Database\Mysql\MysqlDriver The active database driver being used for the tests. - * @since 1.0 - */ - protected static $driver; - /** * @var array The database driver options for the connection. * @since 1.0 */ private static $options = array('driver' => 'mysql'); - /** - * @var \Joomla\Database\Mysql\MysqlDriver The saved database driver to be restored after these tests. - * @since 1.0 - */ - private static $stash; - /** * This method is called before the first test of this test class is run. * @@ -88,7 +77,7 @@ public static function setUpBeforeClass() try { // Attempt to instantiate the driver. - self::$driver = \Joomla\Database\DatabaseDriver::getInstance(self::$options); + self::$driver = DatabaseDriver::getInstance(self::$options); } catch (\RuntimeException $e) { @@ -100,23 +89,18 @@ public static function setUpBeforeClass() { self::$driver = null; } - - // Setup the factory pointer for the driver and stash the old one. - self::$stash = Factory::$database; - Factory::$database = self::$driver; } /** - * This method is called after the last test of this test class is run. + * Gets the data set to be loaded into the database during setup * - * @return void + * @return \PHPUnit_Extensions_Database_DataSet_XmlDataSet * * @since 1.0 */ - public static function tearDownAfterClass() + protected function getDataSet() { - Factory::$database = self::$stash; - self::$driver = null; + return $this->createXMLDataSet(__DIR__ . '/Stubs/database.xml'); } /** diff --git a/src/Joomla/Database/Tests/DatabaseMysqliCase.php b/src/Joomla/Database/Tests/DatabaseMysqliCase.php index 3718c8f96..37d61483d 100644 --- a/src/Joomla/Database/Tests/DatabaseMysqliCase.php +++ b/src/Joomla/Database/Tests/DatabaseMysqliCase.php @@ -6,33 +6,22 @@ namespace Joomla\Database\Tests; -use Joomla\Factory; +use Joomla\Test\TestDatabase; +use Joomla\Database\DatabaseDriver; /** * Abstract test case class for MySQLi database testing. * * @since 1.0 */ -abstract class DatabaseMysqliCase extends DatabaseCase +abstract class DatabaseMysqliCase extends TestDatabase { - /** - * @var \Joomla\Database\Mysqli\MysqliDriver The active database driver being used for the tests. - * @since 1.0 - */ - protected static $driver; - /** * @var array The database driver options for the connection. * @since 1.0 */ private static $options = array('driver' => 'mysqli'); - /** - * @var \Joomla\Database\Mysqli\MysqliDriver The saved database driver to be restored after these tests. - * @since 1.0 - */ - private static $stash; - /** * This method is called before the first test of this test class is run. * @@ -88,7 +77,7 @@ public static function setUpBeforeClass() try { // Attempt to instantiate the driver. - self::$driver = \Joomla\Database\DatabaseDriver::getInstance(self::$options); + self::$driver = DatabaseDriver::getInstance(self::$options); } catch (\RuntimeException $e) { @@ -100,23 +89,18 @@ public static function setUpBeforeClass() { self::$driver = null; } - - // Setup the factory pointer for the driver and stash the old one. - self::$stash = Factory::$database; - Factory::$database = self::$driver; } /** - * This method is called after the last test of this test class is run. + * Gets the data set to be loaded into the database during setup * - * @return void + * @return \PHPUnit_Extensions_Database_DataSet_XmlDataSet * * @since 1.0 */ - public static function tearDownAfterClass() + protected function getDataSet() { - Factory::$database = self::$stash; - self::$driver = null; + return $this->createXMLDataSet(__DIR__ . '/Stubs/database.xml'); } /** diff --git a/src/Joomla/Database/Tests/DatabaseOracleCase.php b/src/Joomla/Database/Tests/DatabaseOracleCase.php index 01453b657..4a7a6b118 100644 --- a/src/Joomla/Database/Tests/DatabaseOracleCase.php +++ b/src/Joomla/Database/Tests/DatabaseOracleCase.php @@ -6,33 +6,22 @@ namespace Joomla\Database\Tests; -use Joomla\Factory; +use Joomla\Test\TestDatabase; +use Joomla\Database\DatabaseDriver; /** * Abstract test case class for Oracle database testing. * * @since 1.0 */ -abstract class DatabaseOracleCase extends DatabaseCase +abstract class DatabaseOracleCase extends TestDatabase { - /** - * @var \Joomla\Database\Oracle\OracleDriver The active database driver being used for the tests. - * @since 1.0 - */ - protected static $driver; - /** * @var array The database driver options for the connection. * @since 1.0 */ private static $options = array('driver' => 'oracle'); - /** - * @var \Joomla\Database\Oracle\OracleDriver The saved database driver to be restored after these tests. - * @since 1.0 - */ - private static $stash; - /** * This method is called before the first test of this test class is run. * @@ -101,7 +90,7 @@ public static function setUpBeforeClass() try { // Attempt to instantiate the driver. - self::$driver = \Joomla\Database\DatabaseDriver::getInstance(self::$options); + self::$driver = DatabaseDriver::getInstance(self::$options); } catch (\RuntimeException $e) { @@ -113,23 +102,18 @@ public static function setUpBeforeClass() { self::$driver = null; } - - // Setup the factory pointer for the driver and stash the old one. - self::$stash = Factory::$database; - Factory::$database = self::$driver; } /** - * This method is called after the last test of this test class is run. + * Gets the data set to be loaded into the database during setup * - * @return void + * @return \PHPUnit_Extensions_Database_DataSet_XmlDataSet * * @since 1.0 */ - public static function tearDownAfterClass() + protected function getDataSet() { - Factory::$database = self::$stash; - self::$driver = null; + return $this->createXMLDataSet(__DIR__ . '/Stubs/database.xml'); } /** diff --git a/src/Joomla/Database/Tests/DatabasePostgresqlCase.php b/src/Joomla/Database/Tests/DatabasePostgresqlCase.php index ca329b7bf..05ab1a405 100644 --- a/src/Joomla/Database/Tests/DatabasePostgresqlCase.php +++ b/src/Joomla/Database/Tests/DatabasePostgresqlCase.php @@ -6,33 +6,22 @@ namespace Joomla\Database\Tests; -use Joomla\Factory; +use Joomla\Test\TestDatabase; +use Joomla\Database\DatabaseDriver; /** * Abstract test case class for PostgreSQL database testing. * * @since 1.0 */ -abstract class DatabasePostgresqlCase extends DatabaseCase +abstract class DatabasePostgresqlCase extends TestDatabase { - /** - * @var \Joomla\Database\Postgresql\PostgresqlDriver The active database driver being used for the tests. - * @since 1.0 - */ - protected static $driver; - /** * @var array The database driver options for the connection. * @since 1.0 */ private static $options = array('driver' => 'postgresql'); - /** - * @var \Joomla\Database\Postgresql\PostgresqlDriver The saved database driver to be restored after these tests. - * @since 1.0 - */ - private static $stash; - /** * This method is called before the first test of this test class is run. * @@ -91,7 +80,7 @@ public static function setUpBeforeClass() try { // Attempt to instantiate the driver. - self::$driver = \Joomla\Database\DatabaseDriver::getInstance(self::$options); + self::$driver = DatabaseDriver::getInstance(self::$options); } catch (\RuntimeException $e) { @@ -103,23 +92,18 @@ public static function setUpBeforeClass() { self::$driver = null; } - - // Setup the factory pointer for the driver and stash the old one. - self::$stash = Factory::$database; - Factory::$database = self::$driver; } /** - * This method is called after the last test of this test class is run. + * Gets the data set to be loaded into the database during setup * - * @return void + * @return \PHPUnit_Extensions_Database_DataSet_XmlDataSet * * @since 1.0 */ - public static function tearDownAfterClass() + protected function getDataSet() { - Factory::$database = self::$stash; - self::$driver = null; + return $this->createXMLDataSet(__DIR__ . '/Stubs/database.xml'); } /** diff --git a/src/Joomla/Database/Tests/DatabaseSqlsrvCase.php b/src/Joomla/Database/Tests/DatabaseSqlsrvCase.php index 220e2bd51..f9fbe5b1f 100644 --- a/src/Joomla/Database/Tests/DatabaseSqlsrvCase.php +++ b/src/Joomla/Database/Tests/DatabaseSqlsrvCase.php @@ -6,33 +6,22 @@ namespace Joomla\Database\Tests; -use Joomla\Factory; +use Joomla\Test\TestDatabase; +use Joomla\Database\DatabaseDriver; /** * Abstract test case class for Microsoft SQL Server database testing. * * @since 1.0 */ -abstract class DatabaseSqlsrvCase extends DatabaseCase +abstract class DatabaseSqlsrvCase extends TestDatabase { - /** - * @var \Joomla\Database\Sqlsrv\SqlsrvDriver The active database driver being used for the tests. - * @since 1.0 - */ - protected static $driver; - /** * @var array The database driver options for the connection. * @since 1.0 */ private static $options = array('driver' => 'sqlsrv'); - /** - * @var \Joomla\Database\Sqlsrv\SqlsrvDriver The saved database driver to be restored after these tests. - * @since 1.0 - */ - private static $stash; - /** * This method is called before the first test of this test class is run. * @@ -88,7 +77,7 @@ public static function setUpBeforeClass() try { // Attempt to instantiate the driver. - self::$driver = \Joomla\Database\DatabaseDriver::getInstance(self::$options); + self::$driver = DatabaseDriver::getInstance(self::$options); } catch (\RuntimeException $e) { @@ -100,23 +89,18 @@ public static function setUpBeforeClass() { self::$driver = null; } - - // Setup the factory pointer for the driver and stash the old one. - self::$stash = Factory::$database; - Factory::$database = self::$driver; } /** - * This method is called after the last test of this test class is run. + * Gets the data set to be loaded into the database during setup * - * @return void + * @return \PHPUnit_Extensions_Database_DataSet_XmlDataSet * * @since 1.0 */ - public static function tearDownAfterClass() + protected function getDataSet() { - Factory::$database = self::$stash; - self::$driver = null; + return $this->createXMLDataSet(__DIR__ . '/Stubs/database.xml'); } /** diff --git a/src/Joomla/Database/Tests/DriverSqliteTest.php b/src/Joomla/Database/Tests/DriverSqliteTest.php index ba4d7bda8..1692b6b79 100644 --- a/src/Joomla/Database/Tests/DriverSqliteTest.php +++ b/src/Joomla/Database/Tests/DriverSqliteTest.php @@ -6,12 +6,14 @@ namespace Joomla\Database\Tests; +use Joomla\Test\TestDatabase; + /** * Test class for Joomla\Database\Sqlite\SqliteDriver. * * @since 1.0 */ -class DriverSqliteTest extends DatabaseCase +class DriverSqliteTest extends TestDatabase { /** * Data for the testEscape test. diff --git a/src/Joomla/Database/Tests/DriverTest.php b/src/Joomla/Database/Tests/DriverTest.php index 77c0422cd..0afed144b 100644 --- a/src/Joomla/Database/Tests/DriverTest.php +++ b/src/Joomla/Database/Tests/DriverTest.php @@ -7,6 +7,7 @@ namespace Joomla\Database\Tests; use Joomla\Test\TestHelper; +use Joomla\Test\TestDatabase; use Psr\Log; require_once __DIR__ . '/Stubs/nosqldriver.php'; @@ -17,7 +18,7 @@ * * @since 1.0 */ -class DriverTest extends DatabaseCase +class DriverTest extends TestDatabase { /** * @var \Joomla\Database\DatabaseDriver diff --git a/src/Joomla/Factory.php b/src/Joomla/Factory.php deleted file mode 100644 index d316da777..000000000 --- a/src/Joomla/Factory.php +++ /dev/null @@ -1,246 +0,0 @@ -loadObject($config); - } - - return $registry; - } - - /** - * Create a session object - * - * @param array $options An array containing session options - * - * @return Session object - * - * @since 1.0 - */ - protected static function createSession(array $options = array()) - { - // Get the editor configuration setting - $conf = self::getConfig(); - $handler = $conf->get('session_handler', 'none'); - - // Config time is in minutes - $options['expire'] = ($conf->get('lifetime')) ? $conf->get('lifetime') * 60 : 900; - - $session = Session::getInstance($handler, $options); - - if ($session->getState() == 'expired') - { - $session->restart(); - } - - return $session; - } - - /** - * Create an database object - * - * @return DatabaseDriver - * - * @see DatabaseDriver - * @since 1.0 - */ - protected static function createDbo() - { - $conf = self::getConfig(); - - $host = $conf->get('host'); - $user = $conf->get('user'); - $password = $conf->get('password'); - $database = $conf->get('db'); - $prefix = $conf->get('dbprefix'); - $driver = $conf->get('dbtype'); - $debug = $conf->get('debug'); - - $options = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix); - - try - { - $db = DatabaseDriver::getInstance($options); - } - catch (\RuntimeException $e) - { - if (!headers_sent()) - { - header('HTTP/1.1 500 Internal Server Error'); - } - - exit('Database Error: ' . $e->getMessage()); - } - - $db->setDebug($debug); - - return $db; - } -} diff --git a/src/Joomla/Test/Stubs/empty.xml b/src/Joomla/Test/Stubs/empty.xml new file mode 100644 index 000000000..8da43f167 --- /dev/null +++ b/src/Joomla/Test/Stubs/empty.xml @@ -0,0 +1,33 @@ + + + + id + title + start_date + description + + 1 + Testing + 1980-04-18 00:00:00 + one + + + 2 + Testing2 + 1980-04-18 00:00:00 + one + + + 3 + Testing3 + 1980-04-18 00:00:00 + three + + + 4 + Testing4 + 1980-04-18 00:00:00 + four + +
+
diff --git a/src/Joomla/Test/TestDatabase.php b/src/Joomla/Test/TestDatabase.php index 9f1fee0c7..791e4ad7a 100644 --- a/src/Joomla/Test/TestDatabase.php +++ b/src/Joomla/Test/TestDatabase.php @@ -9,7 +9,6 @@ namespace Joomla\Test; use Joomla\Database\DatabaseDriver; -use Joomla\Factory; use Joomla\Test\TestHelper; /** @@ -25,22 +24,6 @@ abstract class TestDatabase extends \PHPUnit_Extensions_Database_TestCase */ protected static $driver; - /** - * @var DatabaseDriver The saved database driver to be restored after these tests. - * @since 1.0 - */ - private static $_stash; - - /** - * @var array Various Factory static instances stashed away to be restored later. - * @since 1.0 - */ - private $_stashedFactoryState = array( - 'config' => null, - 'session' => null, - 'language' => null, - ); - /** * This method is called before the first test of this test class is run. * @@ -79,10 +62,6 @@ public static function setUpBeforeClass() { self::$driver = null; } - - // Setup the factory pointer for the driver and stash the old one. - self::$_stash = Factory::$database; - Factory::$database = self::$driver; } /** @@ -94,7 +73,6 @@ public static function setUpBeforeClass() */ public static function tearDownAfterClass() { - Factory::$database = self::$_stash; self::$driver = null; } @@ -179,7 +157,7 @@ protected function getConnection() */ protected function getDataSet() { - return $this->createXMLDataSet(JPATH_TESTS . '/suites/unit/stubs/empty.xml'); + return $this->createXMLDataSet(__DIR__ . '/Stubs/empty.xml'); } /** @@ -213,34 +191,6 @@ protected function getTearDownOperation() return \PHPUnit_Extensions_Database_Operation_Factory::DELETE_ALL(); } - /** - * Sets the Factory pointers - * - * @return void - * - * @since 1.0 - */ - protected function restoreFactoryState() - { - Factory::$config = $this->_stashedFactoryState['config']; - Factory::$session = $this->_stashedFactoryState['session']; - Factory::$language = $this->_stashedFactoryState['language']; - } - - /** - * Saves the Factory pointers - * - * @return void - * - * @since 1.0 - */ - protected function saveFactoryState() - { - $this->_stashedFactoryState['config'] = Factory::$config; - $this->_stashedFactoryState['session'] = Factory::$session; - $this->_stashedFactoryState['language'] = Factory::$language; - } - /** * Sets up the fixture. * diff --git a/tests/suites/unit/joomla/JFactoryTest.php b/tests/suites/unit/joomla/JFactoryTest.php deleted file mode 100644 index 8638a4cc3..000000000 --- a/tests/suites/unit/joomla/JFactoryTest.php +++ /dev/null @@ -1,94 +0,0 @@ -saveFactoryState(); - } - - /** - * Tears down the fixture. - * - * This method is called after a test is executed. - * - * @return void - * - * @since 11.3 - */ - public function tearDown() - { - $this->restoreFactoryState(); - - parent::tearDown(); - } - - /** - * Tests the JFactory::getConfig method. - * - * @return void - * - * @since 11.3 - * @covers JFactory::getConfig - * @covers JFactory::createConfig - */ - public function testGetConfig() - { - // Temporarily override the config cache in JFactory. - $temp = JFactory::$config; - JFactory::$config = null; - - $this->assertInstanceOf( - 'JRegistry', - JFactory::getConfig(JPATH_TESTS . '/config.php'), - 'Line: ' . __LINE__ - ); - - JFactory::$config = $temp; - } - - /** - * Tests the JFactory::getLangauge method. - * - * @return void - * - * @since 12.1 - * @covers JFactory::getLangauge - * @covers JFactory::createLanguage - * @todo Implement testGetLanguage(). - */ - public function testGetLanguage() - { - $this->assertInstanceOf( - 'JLanguage', - JFactory::getLanguage(), - 'Line: ' . __LINE__ - ); - - $this->markTestIncomplete( - 'This test has not been implemented completely yet.' - ); - } -}