Skip to content

Commit 7376e97

Browse files
committed
Fix code structure about always close connections after each test
1 parent b62c332 commit 7376e97

File tree

9 files changed

+126
-84
lines changed

9 files changed

+126
-84
lines changed

tests/BaseTestCase.php

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @since 1.0
3131
* @version $Revision$
3232
*/
33-
class Doctrine_Base_TestCase extends Doctrine_UnitTestCase
33+
class Doctrine_Base_TestCase extends Doctrine_UnitTestCase
3434
{
3535
public function testAggressiveModelLoading()
3636
{
@@ -94,36 +94,20 @@ public function testModelLoadingCacheInformation()
9494

9595
public function testGetConnectionByTableNameForTableWithOneModel()
9696
{
97-
$conn = null;
98-
$thrownException = null;
97+
$connectionBefore = Doctrine_Core::getConnectionByTableName('account');
9998

100-
try {
101-
$connectionBefore = Doctrine_Core::getConnectionByTableName('account');
99+
$this->openAdditionalConnection('sqlite::memory:', 'test_memory');
102100

103-
$conn = Doctrine_Manager::connection('sqlite::memory:', 'test_memory');
104-
Doctrine_Manager::getInstance()->bindComponent('Account', 'test_memory');
101+
Doctrine_Manager::getInstance()->bindComponent('Account', 'test_memory');
105102

106-
$connectionAfter = Doctrine_Core::getConnectionByTableName('account');
103+
$connectionAfter = Doctrine_Core::getConnectionByTableName('account');
107104

108-
$this->assertEqual($connectionAfter->getName(), 'test_memory');
105+
$this->assertEqual($connectionAfter->getName(), 'test_memory');
109106

110-
Doctrine_Manager::getInstance()->bindComponent('Account', $connectionBefore->getName());
107+
Doctrine_Manager::getInstance()->bindComponent('Account', $connectionBefore->getName());
111108

112-
$connectionAfter = Doctrine_Core::getConnectionByTableName('account');
109+
$connectionAfter = Doctrine_Core::getConnectionByTableName('account');
113110

114-
$this->assertEqual($connectionBefore->getName(), $connectionAfter->getName());
115-
} catch (Throwable $e) {
116-
$thrownException = $e;
117-
} catch (Exception $e) {
118-
$thrownException = $e;
119-
}
120-
121-
if (null !== $conn) {
122-
Doctrine_Manager::getInstance()->closeConnection($conn);
123-
}
124-
125-
if (null !== $thrownException) {
126-
throw $thrownException;
127-
}
111+
$this->assertEqual($connectionBefore->getName(), $connectionAfter->getName());
128112
}
129-
}
113+
}

tests/CliTestCase.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @since 1.0
3131
* @version $Revision$
3232
*/
33-
class Doctrine_Cli_TestCase extends Doctrine_UnitTestCase
33+
class Doctrine_Cli_TestCase extends UnitTestCase
3434
{
3535
/**
3636
* @ignore
@@ -63,10 +63,6 @@ protected function getFixturesPath()
6363
return $this->fixturesPath;
6464
}
6565

66-
public function setUp() {}
67-
68-
public function tearDown() {}
69-
7066
public function testTheNameOfTheTaskBaseClassNameIsStoredInAClassConstant()
7167
{
7268
$this->assertFalse(is_null(constant('Doctrine_Cli::TASK_BASE_CLASS')));
@@ -446,4 +442,4 @@ protected function _run(array $args)
446442
class Doctrine_Cli_TestCase_TestTask01 extends Doctrine_Task
447443
{
448444
public function execute() {}
449-
}
445+
}

tests/DoctrineTest/Doctrine_UnitTestCase.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ class Doctrine_UnitTestCase extends UnitTestCase
5757

5858
protected $init = false;
5959

60+
/**
61+
* @var Doctrine_Connection
62+
*/
63+
private $additionalConnections = array();
64+
65+
public function setUp()
66+
{
67+
parent::setUp();
68+
69+
if ( ! $this->init) {
70+
$this->init();
71+
}
72+
if (isset($this->objTable)) {
73+
$this->objTable->clear();
74+
}
75+
76+
$this->init = true;
77+
}
78+
79+
public function tearDown()
80+
{
81+
$this->closeAdditionalConnections();
82+
83+
parent::tearDown();
84+
}
85+
6086
public function getName()
6187
{
6288
return $this->_name;
@@ -275,18 +301,20 @@ public function getDeclaration($type)
275301
{
276302
return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true));
277303
}
278-
public function setUp()
304+
305+
protected function openAdditionalConnection($adapter = null, $name = null)
279306
{
280-
if ( ! $this->init) {
281-
$this->init();
282-
}
283-
if (isset($this->objTable)) {
284-
$this->objTable->clear();
285-
}
307+
$connection = $this->manager->openConnection($adapter, $name);
286308

287-
$this->init = true;
309+
$this->additionalConnections[] = $connection;
310+
311+
return $connection;
288312
}
289313

290-
public function tearDown() {
314+
private function closeAdditionalConnections()
315+
{
316+
foreach ($this->additionalConnections as $connection) {
317+
$this->manager->closeConnection($connection);
318+
}
291319
}
292320
}

tests/DoctrineTest/UnitTestCase.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ class UnitTestCase
1111

1212
protected static $_lastRunsPassesAndFails = array('passes' => array(), 'fails' => array());
1313

14+
public function setUp()
15+
{
16+
}
17+
18+
public function tearDown()
19+
{
20+
}
21+
1422
public function init()
1523
{
1624
$tmpFileName = $this->getPassesAndFailsCachePath();
@@ -149,15 +157,11 @@ public function _fail($message = "")
149157
self::$_passesAndFails['fails'][$class] = $class;
150158
}
151159

152-
public function run(DoctrineTest_Reporter $reporter = null, $filter = null)
160+
public function run(DoctrineTest_Reporter $reporter = null, $filter = null)
153161
{
154162
foreach (get_class_methods($this) as $method) {
155-
if (substr($method, 0, 4) === 'test') {
156-
$this->setUp();
157-
158-
$this->$method();
159-
160-
$this->tearDown();
163+
if ($this->isTestMethod($method)) {
164+
$this->runTest($method);
161165
}
162166
}
163167
}
@@ -249,4 +253,49 @@ public function getNumFixedFails()
249253
{
250254
return count($this->getFixedFails());
251255
}
252-
}
256+
257+
private function runTest($method)
258+
{
259+
$this->setUp();
260+
261+
$this->doRunTestAndTearDown($method);
262+
}
263+
264+
private function doRunTestAndTearDown($method)
265+
{
266+
$test = $this;
267+
268+
$this->tryFinally(
269+
function () use ($test, $method) {
270+
$test->$method();
271+
},
272+
function () use ($test) {
273+
$test->tearDown();
274+
}
275+
);
276+
}
277+
278+
private function isTestMethod($method)
279+
{
280+
return 'test' === substr($method, 0, 4);
281+
}
282+
283+
private function tryFinally(Closure $try, Closure $finally)
284+
{
285+
$thrownException = null;
286+
287+
try {
288+
$try();
289+
} catch (Throwable $e) {
290+
$thrownException = $e;
291+
} catch (Exception $e) { // for PHP v5.x
292+
$thrownException = $e;
293+
}
294+
295+
$finally();
296+
297+
if (null !== $thrownException) {
298+
throw $thrownException;
299+
}
300+
}
301+
}

tests/ExtensionTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function testBehaviorExtension()
5959
public function tearDown()
6060
{
6161
spl_autoload_unregister(array('Doctrine_Core', 'extensionsAutoload'));
62+
63+
parent::tearDown();
6264
}
6365
}
6466

tests/ManagerTestCase.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
* @since 1.0
3131
* @version $Revision$
3232
*/
33-
class Doctrine_Manager_TestCase extends Doctrine_UnitTestCase {
33+
class Doctrine_Manager_TestCase extends Doctrine_UnitTestCase
34+
{
3435
public function testGetInstance() {
3536
$this->assertTrue(Doctrine_Manager::getInstance() instanceOf Doctrine_Manager);
3637
}
@@ -164,31 +165,16 @@ public function testDropDatabases()
164165

165166
public function testConnectionInformationDecoded()
166167
{
167-
$conn = null;
168-
$thrownException = null;
169168
$dsn = 'mysql://' . urlencode('test/t') . ':' . urlencode('p@ssword') . '@localhost/' . urlencode('db/name');
170169

171-
try {
172-
$conn = Doctrine_Manager::connection($dsn);
173-
$options = $conn->getOptions();
170+
$conn = $this->openAdditionalConnection($dsn);
171+
$options = $conn->getOptions();
174172

175-
$this->assertEqual($options['username'], 'test/t');
176-
$this->assertEqual($options['password'], 'p@ssword');
177-
$this->assertEqual($options['dsn'], 'mysql:host=localhost;dbname=db/name');
178-
} catch (Throwable $e) {
179-
$thrownException = $e;
180-
} catch (Exception $e) {
181-
$thrownException = $e;
182-
}
183-
184-
if (null !== $conn) {
185-
Doctrine_Manager::getInstance()->closeConnection($conn);
186-
}
187-
188-
if (null !== $thrownException) {
189-
throw $thrownException;
190-
}
173+
$this->assertEqual($options['username'], 'test/t');
174+
$this->assertEqual($options['password'], 'p@ssword');
175+
$this->assertEqual($options['dsn'], 'mysql:host=localhost;dbname=db/name');
191176
}
177+
192178
public function prepareData() { }
193179
public function prepareTables() { }
194180

tests/Migration/BaseTestCase.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@
3030
* @since 1.0
3131
* @version $Revision$
3232
*/
33-
class Doctrine_Migration_Base_TestCase extends Doctrine_UnitTestCase
33+
class Doctrine_Migration_Base_TestCase extends UnitTestCase
3434
{
35-
public function setUp() {}
35+
public function tearDown()
36+
{
37+
Doctrine_Migration_Base::setDefaultTableOptions(array());
38+
39+
parent::tearDown();
40+
}
3641

3742
public function testIsAbstract()
3843
{
@@ -59,11 +64,6 @@ public function testGetdefaulttableoptionsReturnsTheOptionsSetWithSetdefaulttabl
5964
}
6065
}
6166

62-
public function tearDown()
63-
{
64-
Doctrine_Migration_Base::setDefaultTableOptions(array());
65-
}
66-
6767
public function testCreatetableMergesTheDefaultTableOptionsWithTheSpecifiedOptions()
6868
{
6969
$fixtures = array(

tests/TaskTestCase.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@
3333
* @since 1.0
3434
* @version $Revision$
3535
*/
36-
class Doctrine_Task_TestCase extends Doctrine_UnitTestCase
36+
class Doctrine_Task_TestCase extends UnitTestCase
3737
{
38-
public function setUp() {}
39-
40-
public function tearDown() {}
41-
4238
public function testDerivetasknameReturnsTheNameOfATaskFromItsClassName()
4339
{
4440
$this->assertEqual('migrate', Doctrine_Task::deriveTaskName('Doctrine_Task_Migrate'));
@@ -154,4 +150,4 @@ class Doctrine_Task_TestCase_TestTask003 extends Doctrine_Task
154150
public $taskName = 'better-task-name';
155151

156152
public function execute() {}
157-
}
153+
}

tests/Ticket/DC521TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,4 @@ public function tearDown()
140140
$this->driverName = null;
141141
parent::tearDown();
142142
}
143+
}

0 commit comments

Comments
 (0)