Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
663135d5
Commit
663135d5
authored
Jul 27, 2014
by
Marco Pivetta
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #643 from JeroenDeDauw/TestUtill
Split the methods in TestUtil more and improve naming
parents
8dbf562d
a27733a0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
51 deletions
+70
-51
TestUtil.php
tests/Doctrine/Tests/TestUtil.php
+70
-51
No files found.
tests/Doctrine/Tests/TestUtil.php
View file @
663135d5
...
...
@@ -2,6 +2,7 @@
namespace
Doctrine\Tests
;
use
Doctrine\DBAL\Connection
;
use
Doctrine\DBAL\DriverManager
;
/**
...
...
@@ -32,61 +33,23 @@ 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
(
self
::
hasRequiredConnectionParams
())
{
$realDbParams
=
self
::
getConnectionParams
();
$tmpDbParams
=
self
::
getTmpConnectionParams
();
$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
();
$realConn
->
close
();
$tmpConn
->
getSchemaManager
()
->
dropAndCreateDatabase
(
$dbname
);
$tmpConn
->
close
();
}
else
{
$sm
=
$realConn
->
getSchemaManager
();
$conn
=
DriverManager
::
getConnection
(
self
::
getConnectionParams
());
$schema
=
$sm
->
createSchema
();
$stmts
=
$schema
->
toDropSql
(
$realConn
->
getDatabasePlatform
());
self
::
addDbEventSubscribers
(
$conn
);
foreach
(
$stmts
as
$stmt
)
{
$realConn
->
exec
(
$stmt
);
}
}
$conn
=
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
=
DriverManager
::
getConnection
(
$params
);
}
return
$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
getConnectionParams
()
{
if
(
self
::
hasRequiredConnectionParams
())
{
return
self
::
getSpecifiedConnectionParams
();
}
return
$conn
;
return
self
::
getFallbackConnectionParams
()
;
}
private
static
function
hasRequiredConnectionParams
()
...
...
@@ -108,7 +71,63 @@ class TestUtil
);
}
private
static
function
getTmpConnectionParams
()
private
static
function
getSpecifiedConnectionParams
()
{
$realDbParams
=
self
::
getParamsForMainConnection
();
$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
();
$realConn
->
close
();
$tmpConn
->
getSchemaManager
()
->
dropAndCreateDatabase
(
$dbname
);
$tmpConn
->
close
();
}
else
{
$sm
=
$realConn
->
getSchemaManager
();
$schema
=
$sm
->
createSchema
();
$stmts
=
$schema
->
toDropSql
(
$realConn
->
getDatabasePlatform
());
foreach
(
$stmts
as
$stmt
)
{
$realConn
->
exec
(
$stmt
);
}
}
return
$realDbParams
;
}
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
()
{
$connectionParams
=
array
(
'driver'
=>
$GLOBALS
[
'tmpdb_type'
],
...
...
@@ -134,7 +153,7 @@ class TestUtil
return
$connectionParams
;
}
private
static
function
get
ConnectionParams
()
private
static
function
get
ParamsForMainConnection
()
{
$connectionParams
=
array
(
'driver'
=>
$GLOBALS
[
'db_type'
],
...
...
@@ -157,10 +176,10 @@ class TestUtil
}
/**
* @return
\Doctrine\DBAL\
Connection
* @return Connection
*/
public
static
function
getTempConnection
()
{
return
DriverManager
::
getConnection
(
self
::
get
TmpConnectionParams
());
return
DriverManager
::
getConnection
(
self
::
get
ParamsForTemporaryConnection
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment