Skip to content

Commit

Permalink
Add proper cleanup in test teardown
Browse files Browse the repository at this point in the history
Added a tearDown() function to the SqlQueryInterceptModuleTest, SqlQueryModuleTest, and SqlQueryTest classes. This method takes care of appropriately cleaning up the SQLite connection after each test, preventing potential issues related to connection leakage.
  • Loading branch information
koriym committed Jun 13, 2024
1 parent e70ecfb commit ce8e9a3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
15 changes: 13 additions & 2 deletions tests/SqlQueryInterceptModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

class SqlQueryInterceptModuleTest extends TestCase
{
/** @var ExtendedPdo */
private $pdo;

/** @var FakeRo */
private $fakeRo;

Expand All @@ -22,14 +25,14 @@ class SqlQueryInterceptModuleTest extends TestCase

protected function setUp(): void
{
$pdo = new ExtendedPdo('sqlite::memory:');
$pdo = $pdo = new ExtendedPdo('sqlite::memory:');

Check failure on line 28 in tests/SqlQueryInterceptModuleTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Duplicate assignment to variable $pdo.

Check failure on line 28 in tests/SqlQueryInterceptModuleTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Duplicate assignment to variable $pdo.
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
$pdo->query('CREATE TABLE IF NOT EXISTS todo (
id INTEGER,
title TEXT
)');
$pdo->perform('INSERT INTO todo (id, title) VALUES (:id, :title) ', ['id' => 1, 'title' => 'run']);
$module = new class ($pdo) extends AbstractModule {
$module = new class ($this->pdo) extends AbstractModule {
/** @var ExtendedPdo */
private $pdo;

Expand All @@ -45,11 +48,19 @@ protected function configure()
$this->bind(FakeBar::class);
}
};
$this->pdo = $pdo;
$injector = new Injector($module, __DIR__ . '/tmp');
$this->fakeRo = $injector->getInstance(FakeRo::class);
$this->fakeBar = $injector->getInstance(FakeBar::class);
}

protected function tearDown(): void
{
unset($this->pdo); // This effectively closes the SQLite connection

parent::tearDown();
}

public function testResourceObject200(): void
{
$response = $this->fakeRo->onGet('1');
Expand Down
14 changes: 13 additions & 1 deletion tests/SqlQueryModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@

class SqlQueryModuleTest extends TestCase
{
/** @var ExtendedPdo */
private $pdo;

/** @var AbstractModule */
private $module;

protected function setUp(): void
{
$pdo = new ExtendedPdo('sqlite::memory:');
Expand All @@ -30,7 +34,7 @@ protected function setUp(): void
title TEXT
)');
$this->module = new class ($pdo) extends AbstractModule {
$this->module = new class ($this->pdo) extends AbstractModule {
/** @var ExtendedPdo */
private $pdo;

Expand All @@ -45,6 +49,14 @@ protected function configure()
$this->install(new SqlQueryModule(__DIR__ . '/Fake/sql'));
}
};
$this->pdo = $pdo;
}

protected function tearDown(): void
{
unset($this->pdo); // This effectively closes the SQLite connection

parent::tearDown();
}

public function testProviderInject(): void
Expand Down
7 changes: 7 additions & 0 deletions tests/SqlQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ protected function setUp(): void
$this->pdo = $pdo;
}

protected function tearDown(): void
{
unset($this->pdo); // This effectively closes the SQLite connection

parent::tearDown();
}

public function testInvoke(): void
{
$sql = (string) file_get_contents(__DIR__ . '/Fake/sql/todo_item_by_id.sql');
Expand Down

0 comments on commit ce8e9a3

Please sign in to comment.