8000 Improve test suite by deeky666 · Pull Request #1289 · doctrine/orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve test suite #1289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ env:
before_script:
- if [[ $TRAVIS_PHP_VERSION = '5.6' && $DB = 'sqlite' ]]; then PHPUNIT_FLAGS="--coverage-clover ./build/logs/clover.xml"; else PHPUNIT_FLAGS=""; fi
- if [[ $TRAVIS_PHP_VERSION != '5.6' && $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != 'hhvm-nightly' ]]; then phpenv config-rm xdebug.ini; fi
- if [ $DB = 'pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS doctrine_tests;' -U postgres; fi
- if [ $DB = 'pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS doctrine_tests_tmp;' -U postgres; fi
- if [ $DB = 'pgsql' ]; then psql -c 'create database doctrine_tests;' -U postgres; fi
- if [ $DB = 'pgsql' ]; then psql -c 'create database doctrine_tests_tmp;' -U postgres; fi
- if [ $DB = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS doctrine_tests_tmp;create database IF NOT EXISTS doctrine_tests;'; fi
- composer self-update
- composer install --prefer-source --dev

script:
script:
- ENABLE_SECOND_LEVEL_CACHE=0 ./vendor/bin/phpunit -v -c tests/travis/$DB.travis.xml $PHPUNIT_FLAGS
- ENABLE_SECOND_LEVEL_CACHE=1 ./vendor/bin/phpunit -v -c tests/travis/$DB.travis.xml --exclude-group performance,non-cacheable,locking_functional

Expand Down
192 changes: 136 additions & 56 deletions tests/Doctrine/Tests/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

namespace Doctrine\Tests;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;

/**
* TestUtil is a class with static utility methods used during tests.
*
* @author robo
*/
class TestUtil
{
/**
* @var boolean Whether the database schema is initialized.
*/
private static $initialized = false;

/**
* Gets a <b>real</b> database connection using the following parameters
* of the $GLOBALS array:
Expand All @@ -17,6 +25,8 @@ class TestUtil
* 'db_username' : The username to use for connecting.
* 'db_password' : The password to use for connecting.
* 'db_host' : The hostname of the database to connect to.
* 'db_server' : The server name of the database to connect to
* (optional, some vendors allow multiple server instances with different names on the same host).
* 'db_name' : The name of the database to connect to.
* 'db_port' : The port of the database to connect to.
*
Expand All @@ -28,92 +38,162 @@ class TestUtil
* 1) Each invocation of this method returns a NEW database connection.
* 2) The database is dropped and recreated to ensure it's clean.
*
* @return \Doctrine\DBAL\Connection The database connection instance.
* @return Connection The database connection instance.
*/
public static function getConnection()
{
if (isset($GLOBALS['db_type'], $GLOBALS['db_username'], $GLOBALS['db_password'],
$GLOBALS['db_host'], $GLOBALS['db_name'], $GLOBALS['db_port']) &&
isset($GLOBALS['tmpdb_type'], $GLOBALS['tmpdb_username'], $GLOBALS['tmpdb_password'],
$GLOBALS['tmpdb_host'], $GLOBALS['tmpdb_name'], $GLOBALS['tmpdb_port'])) {
$realDbParams = array(
'driver' => $GLOBALS['db_type'],
'user' => $GLOBALS['db_username'],
'password' => $GLOBALS['db_password'],
'host' => $GLOBALS['db_host'],
'dbname' => $GLOBALS['db_name'],
'port' => $GLOBALS['db_port']
);
$tmpDbParams = array(
'driver' => $GLOBALS['tmpdb_type'],
'user' => $GLOBALS['tmpdb_username'],
'password' => $GLOBALS['tmpdb_password'],
'host' => $GLOBALS['tmpdb_host'],
'dbname' => $GLOBALS['tmpdb_name'],
'port' => $GLOBALS['tmpdb_port']
);

$realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);

$platform = $realConn->getDatabasePlatform();
$conn = DriverManager::getConnection(self::getConnectionParams());

self::addDbEventSubscribers($conn);

return $conn;
}

/**
* @return Connection
*/
public static function getTempConnection()
{
return DriverManager::getConnection(self::getParamsForTemporaryConnection());
}

private static function getConnectionParams()
{
if (self::hasRequiredConnectionParams()) {
return self::getSpecifiedConnectionParams();
}

return self::getFallbackConnectionParams();
}

private static function hasRequiredConnectionParams()
{
return isset(
$GLOBALS['db_type'],
$GLOBALS['db_username'],
$GLOBALS['db_password'],
$GLOBALS['db_host'],
$GLOBALS['db_name'],
$GLOBALS['db_port']
)
&& isset(
$GLOBALS['tmpdb_type'],
$GLOBALS['tmpdb_username'],
$GLOBALS['tmpdb_password'],
$GLOBALS['tmpdb_host'],
$GLOBALS['tmpdb_port']
);
}

private static function getSpecifiedConnectionParams()
{
$realDbParams = self::getParamsForMainConnection();

if (! self::$initialized) {
$tmpDbParams = self::getParamsForTemporaryConnection();

$realConn = DriverManager::getConnection($realDbParams);

// Connect to tmpdb in order to drop and create the real test db.
$tmpConn = DriverManager::getConnection($tmpDbParams);

$platform = $tmpConn->getDatabasePlatform();

if ($platform->supportsCreateDropDatabase()) {
$dbname = $realConn->getDatabase();
// Connect to tmpdb in order to drop and create the real test db.
$tmpConn = \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
$realConn->close();

$tmpConn->getSchemaManager()->dropDatabase($dbname);
$tmpConn->getSchemaManager()->createDatabase($dbname);
$tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname);

$tmpConn->close();
} else {
$sm = $realConn->getSchemaManager();

/* @var $schema Schema */
$schema = $sm->createSchema();
$stmts = $schema->toDropSql($realConn->getDatabasePlatform());

foreach ($stmts AS $stmt) {
try {
$realConn->exec($stmt);
} catch(\Exception $e) {
// TODO: Now is this a real good idea?
}
foreach ($stmts as $stmt) {
$realConn->exec($stmt);
}
}

$conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, null);
} else {
$params = array(
'driver' => 'pdo_sqlite',
'memory' => true
);
if (isset($GLOBALS['db_path'])) {
$params['path'] = $GLOBALS['db_path'];
unlink($GLOBALS['db_path']);
}
$conn = \Doctrine\DBAL\DriverManager::getConnection($params);
self::$initialized = true;
}

return $conn;
return $realDbParams;
}

/**
* @return \Doctrine\DBAL\Connection
*/
public static function getTempConnection()
private static function getFallbackConnectionParams()
{
$params = array(
'driver' => 'pdo_sqlite',
'memory' => true
);

if (isset($GLOBALS['db_path'])) {
$params['path'] = $GLOBALS['db_path'];
unlink($GLOBALS['db_path']);
}

return $params;
}

private static function addDbEventSubscribers(Connection $conn)
{
if (isset($GLOBALS['db_event_subscribers'])) {
$evm = $conn->getEventManager();
foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) {
$subscriberInstance = new $subscriberClass();
$evm->addEventSubscriber($subscriberInstance);
}
}
}

private static function getParamsForTemporaryConnection()
{
$tmpDbParams = array(
$connectionParams = array(
'driver' => $GLOBALS['tmpdb_type'],
'user' => $GLOBALS['tmpdb_username'],
'password' => $GLOBALS['tmpdb_password'],
'host' => $GLOBALS['tmpdb_host'],
'dbname' => $GLOBALS['tmpdb_name'],
'dbname' => null,
'port' => $GLOBALS['tmpdb_port']
);

// Connect to tmpdb in order to drop and create the real test db.
return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
if (isset($GLOBALS['tmpdb_name'])) {
$connectionParams['dbname'] = $GLOBALS['tmpdb_name'];
}

if (isset($GLOBALS['tmpdb_server'])) {
$connectionParams['server'] = $GLOBALS['tmpdb_server'];
}

if (isset($GLOBALS['tmpdb_unix_socket'])) {
$connectionParams['unix_socket'] = $GLOBALS['tmpdb_unix_socket'];
}

return $connectionParams;
}

private static function getParamsForMainConnection()
{
$connectionParams = array(
'driver' => $GLOBALS['db_type'],
'user' => $GLOBALS['db_username'],
'password' => $GLOBALS['db_password'],
'host' => $GLOBALS['db_host'],
'dbname' => $GLOBALS['db_name'],
'port' => $GLOBALS['db_port']
);

if (isset($GLOBALS['db_server'])) {
$connectionParams['server'] = $GLOBALS['db_server'];
}

if (isset($GLOBALS['db_unix_socket'])) {
$connectionParams['unix_socket'] = $GLOBALS['db_unix_socket'];
}

return $connectionParams;
}
}
1 change: 0 additions & 1 deletion tests/travis/mysql.travis.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<var name="tmpdb_host" value="localhost" />
<var name="tmpdb_username" value="travis" />
<var name="tmpdb_password" value="" />
<var name="tmpdb_name" value="doctrine_tests_tmp" />
<var name="tmpdb_port" value="3306"/>
</php>

Expand Down
1 change: 0 additions & 1 deletion tests/travis/pgsql.travis.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<var name="tmpdb_host" value="localhost" />
<var name="tmpdb_username" value="postgres" />
<var name="tmpdb_password" value="" />
<var name="tmpdb_name" value="doctrine_tests_tmp" />
<var name="tmpdb_port" value="5432"/>
</php>

Expand Down
0