Skip to content

Commit d7ac79f

Browse files
committed
fix: database use in bootstrap #98
1 parent 956c3bb commit d7ac79f

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/Codeception/Lib/Connector/Yii2/ConnectionWatcher.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ConnectionWatcher
1919
private readonly Closure $handler;
2020

2121
/**
22-
* @var list<Connection>
22+
* @var list<\WeakReference<Connection>>
2323
*/
2424
private array $connections = [];
2525

@@ -35,7 +35,7 @@ public function __construct()
3535
protected function connectionOpened(Connection $connection): void
3636
{
3737
$this->debug('Connection opened!');
38-
$this->connections[] = $connection;
38+
$this->connections[] = \WeakReference::create($connection);
3939
}
4040

4141
public function start(): void
@@ -54,9 +54,12 @@ public function closeAll(): void
5454
{
5555
$count = count($this->connections);
5656
$this->debug("closing all ($count) connections");
57-
foreach ($this->connections as $connection) {
58-
$connection->close();
57+
foreach ($this->connections as $ref) {
58+
if (null !== $connection = $ref->get()) {
59+
$connection->close();
60+
}
5961
}
62+
$this->connections = [];
6063
}
6164

6265
/**

src/Codeception/Module/Yii2.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Codeception\Lib\Framework;
1515
use Codeception\Lib\Interfaces\ActiveRecord;
1616
use Codeception\Lib\Interfaces\PartedModule;
17+
use Codeception\Lib\ModuleContainer;
1718
use Codeception\TestInterface;
1819
use Exception;
1920
use PHPUnit\Framework\Assert;
@@ -247,7 +248,7 @@ final class Yii2 extends Framework implements ActiveRecord, PartedModule
247248
/**
248249
* Helper to force database transaction
249250
*/
250-
private TransactionForcer $transactionForcer;
251+
private null|TransactionForcer $transactionForcer;
251252

252253
/**
253254
* @var array<mixed> The contents of upon initialization of this object.
@@ -269,6 +270,14 @@ private function getClient(): Yii2Connector
269270
return $this->client;
270271
}
271272

273+
public function __construct(ModuleContainer $moduleContainer, ?array $config = null)
274+
{
275+
parent::__construct($moduleContainer, $config);
276+
$this->connectionWatcher = new ConnectionWatcher();
277+
$this->connectionWatcher->start();
278+
}
279+
280+
272281
public function _initialize(): void
273282
{
274283
if ($this->config['transaction'] === null) {
@@ -393,9 +402,6 @@ public function _before(TestInterface $test): void
393402
$this->yiiLogger = new Yii2Connector\Logger();
394403
$this->getClient()->startApp($this->yiiLogger);
395404

396-
$this->connectionWatcher = new ConnectionWatcher();
397-
$this->connectionWatcher->start();
398-
399405
// load fixtures before db transaction
400406
if ($test instanceof \Codeception\Test\Cest) {
401407
$this->loadFixtures($test->getTestInstance());
@@ -447,9 +453,7 @@ public function _after(TestInterface $test): void
447453
$this->getClient()->resetApplication();
448454

449455
if (isset($this->connectionWatcher)) {
450-
$this->connectionWatcher->stop();
451456
$this->connectionWatcher->closeAll();
452-
unset($this->connectionWatcher);
453457
}
454458

455459
parent::_after($test);

0 commit comments

Comments
 (0)